mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 16:47:28 +01:00
First cut of a renderer.
This commit is contained in:
1 parent
5c934e5599
commit
aebbb2b830
3 files changed
+151
-23
No files matched your search
@@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
package parse
|
package parse
|
||||||
|
|
||||||
|
// TODO: write a formal grammar for the language.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,118 @@
|
|||||||
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
|
// in the LICENSE file.
|
||||||
|
|
||||||
|
package render
|
||||||
|
|
||||||
|
// TODO: render an *ast.Node instead of a []token.Token, as this will better
|
||||||
|
// allow automated refactoring tools, not just automated formatting. For now,
|
||||||
|
// rendering tokens is easier, especially with respect to tracking comments.
|
||||||
|
|
||||||
|
// TODO: handle comments.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/google/puffs/lang/token"
|
||||||
|
)
|
||||||
|
|
||||||
|
var newLine = []byte{'\n'}
|
||||||
|
|
||||||
|
func RenderFile(w io.Writer, src []token.Token, m *token.IDMap) (err error) {
|
||||||
|
if len(src) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const maxOpens = 0xFFFF
|
||||||
|
opens := make([]uint32, 0, 256)
|
||||||
|
indent := uint32(0)
|
||||||
|
buf := make([]byte, 0, 1024)
|
||||||
|
prevLine := src[0].Line - 1
|
||||||
|
prevLineEndedWithOpen := false
|
||||||
|
|
||||||
|
for len(src) > 0 {
|
||||||
|
// Find the tokens in this line.
|
||||||
|
line := src[0].Line
|
||||||
|
i := 1
|
||||||
|
for ; i < len(src) && src[i].Line == line; i++ {
|
||||||
|
if i == len(src) || src[i].Line != line {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lineTokens := src[:i]
|
||||||
|
src = src[i:]
|
||||||
|
|
||||||
|
// Strip any trailing semi-colons.
|
||||||
|
for len(lineTokens) > 0 && lineTokens[len(lineTokens)-1].ID == token.IDSemicolon {
|
||||||
|
lineTokens = lineTokens[:len(lineTokens)-1]
|
||||||
|
}
|
||||||
|
if len(lineTokens) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collapse one or more blank lines to just one. Collapse them to zero
|
||||||
|
// if the previous line ended with an open token (such as an open curly
|
||||||
|
// brace) or this line started with a close token.
|
||||||
|
if prevLine < line-1 && !prevLineEndedWithOpen && !lineTokens[0].IsClose() {
|
||||||
|
if _, err = w.Write(newLine); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render any leading indentation. If this line starts with a close
|
||||||
|
// token, indent it so that it aligns vertically with the line
|
||||||
|
// containing the matching open token.
|
||||||
|
buf = buf[:0]
|
||||||
|
if lineTokens[0].IsClose() {
|
||||||
|
if len(opens) > 0 {
|
||||||
|
indent = opens[len(opens)-1]
|
||||||
|
} else {
|
||||||
|
indent = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if indent != 0 {
|
||||||
|
const tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
|
||||||
|
n := int(indent)
|
||||||
|
for ; n > len(tabs); n -= len(tabs) {
|
||||||
|
buf = append(buf, tabs...)
|
||||||
|
}
|
||||||
|
buf = append(buf, tabs[:n]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render the lineTokens.
|
||||||
|
numOpens := len(opens)
|
||||||
|
prevIsTightRight := true
|
||||||
|
for _, t := range lineTokens {
|
||||||
|
if !t.IsTightLeft() && !prevIsTightRight {
|
||||||
|
buf = append(buf, ' ')
|
||||||
|
}
|
||||||
|
buf = append(buf, m.ByKey(t.Key())...)
|
||||||
|
|
||||||
|
if t.IsOpen() {
|
||||||
|
if len(opens) == maxOpens {
|
||||||
|
return errors.New("render: too many open tokens")
|
||||||
|
}
|
||||||
|
opens = append(opens, indent)
|
||||||
|
} else if t.IsClose() {
|
||||||
|
if len(opens) == 0 {
|
||||||
|
return errors.New("render: too many close tokens")
|
||||||
|
}
|
||||||
|
opens = opens[:len(opens)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
prevIsTightRight = t.IsTightRight()
|
||||||
|
}
|
||||||
|
if numOpens != len(opens) {
|
||||||
|
indent++
|
||||||
|
}
|
||||||
|
|
||||||
|
buf = append(buf, '\n')
|
||||||
|
if _, err = w.Write(buf); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
prevLine = line
|
||||||
|
prevLineEndedWithOpen = lineTokens[len(lineTokens)-1].IsOpen()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
+31
-23
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
package token
|
package token
|
||||||
|
|
||||||
// Flags are the low 8 bits of an ID. For example, they signify whether a token
|
// Flags are the low 12 bits of an ID. For example, they signify whether a
|
||||||
// is an operator, an identifier, etc.
|
// token is an operator, an identifier, etc.
|
||||||
//
|
//
|
||||||
// The flags are not exclusive. For example, the "+" token is both a unary and
|
// The flags are not exclusive. For example, the "+" token is both a unary and
|
||||||
// binary operator. It is also associative: (a + b) + c equals a + (b + c).
|
// binary operator. It is also associative: (a + b) + c equals a + (b + c).
|
||||||
@@ -14,23 +14,27 @@ package token
|
|||||||
type Flags uint32
|
type Flags uint32
|
||||||
|
|
||||||
const (
|
const (
|
||||||
FlagsOther = Flags(0x01)
|
FlagsOther = Flags(0x001)
|
||||||
FlagsUnaryOp = Flags(0x02)
|
FlagsUnaryOp = Flags(0x002)
|
||||||
FlagsBinaryOp = Flags(0x04)
|
FlagsBinaryOp = Flags(0x004)
|
||||||
FlagsAssociative = Flags(0x08)
|
FlagsAssociative = Flags(0x008)
|
||||||
FlagsAssign = Flags(0x10)
|
FlagsAssign = Flags(0x010)
|
||||||
FlagsLiteral = Flags(0x20)
|
FlagsLiteral = Flags(0x020)
|
||||||
FlagsIdent = Flags(0x40)
|
FlagsIdent = Flags(0x040)
|
||||||
FlagsImplicitSemicolon = Flags(0x80)
|
FlagsImplicitSemicolon = Flags(0x080)
|
||||||
|
FlagsOpen = Flags(0x100)
|
||||||
|
FlagsClose = Flags(0x200)
|
||||||
|
FlagsTightLeft = Flags(0x400)
|
||||||
|
FlagsTightRight = Flags(0x800)
|
||||||
)
|
)
|
||||||
|
|
||||||
// Key is the high 24 bits of an ID. It is the map key for an IDMap.
|
// Key is the high 20 bits of an ID. It is the map key for an IDMap.
|
||||||
type Key uint32
|
type Key uint32
|
||||||
|
|
||||||
const (
|
const (
|
||||||
idShift = 8
|
idShift = 12
|
||||||
idMask = 1<<8 - 1
|
idMask = 1<<12 - 1
|
||||||
maxKey = 1<<24 - 1
|
maxKey = 1<<20 - 1
|
||||||
)
|
)
|
||||||
|
|
||||||
// ID combines a Key and Flags.
|
// ID combines a Key and Flags.
|
||||||
@@ -55,6 +59,10 @@ func (t Token) IsAssign() bool { return Flags(t.ID)&FlagsAssign != 0
|
|||||||
func (t Token) IsLiteral() bool { return Flags(t.ID)&FlagsLiteral != 0 }
|
func (t Token) IsLiteral() bool { return Flags(t.ID)&FlagsLiteral != 0 }
|
||||||
func (t Token) IsIdent() bool { return Flags(t.ID)&FlagsIdent != 0 }
|
func (t Token) IsIdent() bool { return Flags(t.ID)&FlagsIdent != 0 }
|
||||||
func (t Token) IsImplicitSemicolon() bool { return Flags(t.ID)&FlagsImplicitSemicolon != 0 }
|
func (t Token) IsImplicitSemicolon() bool { return Flags(t.ID)&FlagsImplicitSemicolon != 0 }
|
||||||
|
func (t Token) IsOpen() bool { return Flags(t.ID)&FlagsOpen != 0 }
|
||||||
|
func (t Token) IsClose() bool { return Flags(t.ID)&FlagsClose != 0 }
|
||||||
|
func (t Token) IsTightLeft() bool { return Flags(t.ID)&FlagsTightLeft != 0 }
|
||||||
|
func (t Token) IsTightRight() bool { return Flags(t.ID)&FlagsTightRight != 0 }
|
||||||
|
|
||||||
// nBuiltInKeys is the number of built-in Keys. The packing is:
|
// nBuiltInKeys is the number of built-in Keys. The packing is:
|
||||||
// - Zero is invalid.
|
// - Zero is invalid.
|
||||||
@@ -73,16 +81,16 @@ const nBuiltInKeys = 256
|
|||||||
const (
|
const (
|
||||||
IDInvalid = ID(0)
|
IDInvalid = ID(0)
|
||||||
|
|
||||||
IDOpenParen = ID(0x10<<idShift | FlagsOther)
|
IDOpenParen = ID(0x10<<idShift | FlagsOpen | FlagsTightRight)
|
||||||
IDCloseParen = ID(0x11<<idShift | FlagsOther | FlagsImplicitSemicolon)
|
IDCloseParen = ID(0x11<<idShift | FlagsClose | FlagsTightLeft | FlagsImplicitSemicolon)
|
||||||
IDOpenBracket = ID(0x12<<idShift | FlagsOther)
|
IDOpenBracket = ID(0x12<<idShift | FlagsOpen | FlagsTightLeft | FlagsTightRight)
|
||||||
IDCloseBracket = ID(0x13<<idShift | FlagsOther | FlagsImplicitSemicolon)
|
IDCloseBracket = ID(0x13<<idShift | FlagsClose | FlagsTightLeft | FlagsImplicitSemicolon)
|
||||||
IDOpenCurly = ID(0x14<<idShift | FlagsOther)
|
IDOpenCurly = ID(0x14<<idShift | FlagsOpen)
|
||||||
IDCloseCurly = ID(0x15<<idShift | FlagsOther | FlagsImplicitSemicolon)
|
IDCloseCurly = ID(0x15<<idShift | FlagsClose | FlagsImplicitSemicolon)
|
||||||
|
|
||||||
IDDot = ID(0x20<<idShift | FlagsOther)
|
IDDot = ID(0x20<<idShift | FlagsOther | FlagsTightLeft | FlagsTightRight)
|
||||||
IDComma = ID(0x21<<idShift | FlagsOther)
|
IDComma = ID(0x21<<idShift | FlagsOther | FlagsTightLeft)
|
||||||
IDQuestion = ID(0x22<<idShift | FlagsOther)
|
IDQuestion = ID(0x22<<idShift | FlagsOther | FlagsUnaryOp)
|
||||||
IDColon = ID(0x23<<idShift | FlagsOther)
|
IDColon = ID(0x23<<idShift | FlagsOther)
|
||||||
IDSemicolon = ID(0x24<<idShift | FlagsOther)
|
IDSemicolon = ID(0x24<<idShift | FlagsOther)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user