Skip to content

Commit

Permalink
Fixes coding style and comments.
Browse files Browse the repository at this point in the history
Renames and unexports constants:

- `handlebars.DUMP_TPL`
- `lexer.ESCAPED_ESCAPED_OPEN_MUSTACHE`
- `lexer.ESCAPED_OPEN_MUSTACHE`
- `lexer.OPEN_MUSTACHE`
- `lexer.CLOSE_MUSTACHE`
- `lexer.CLOSE_STRIP_MUSTACHE`
- `lexer.CLOSE_UNESCAPED_STRIP_MUSTACHE`
- `lexer.DUMP_TOKEN_POS`
- `lexer.DUMP_ALL_TOKENS_VAL`
  • Loading branch information
aymerick committed May 1, 2016
1 parent ba90949 commit f75ebec
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 112 deletions.
42 changes: 30 additions & 12 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,46 @@ func (t NodeType) Type() NodeType {
}

const (
// program
// NodeProgram is the program node
NodeProgram NodeType = iota

// statements
// NodeMustache is the mustache statement node
NodeMustache

// NodeBlock is the block statement node
NodeBlock

// NodePartial is the partial statement node
NodePartial

// NodeContent is the content statement node
NodeContent

// NodeComment is the comment statement node
NodeComment

// expressions
// NodeExpression is the expression node
NodeExpression

// NodeSubExpression is the subexpression node
NodeSubExpression

// NodePath is the expression path node
NodePath

// literals
// NodeBoolean is the literal boolean node
NodeBoolean

// NodeNumber is the literal number node
NodeNumber

// NodeString is the literal string node
NodeString

// miscellaneous
// NodeHash is the hash node
NodeHash

// NodeHashPair is the hash pair node
NodeHashPair
)

Expand Down Expand Up @@ -548,7 +566,7 @@ func (node *PathExpression) Part(part string) {

switch part {
case "..":
node.Depth += 1
node.Depth++
node.Scoped = true
case ".", "this":
node.Scoped = true
Expand Down Expand Up @@ -637,9 +655,9 @@ func (node *BooleanLiteral) Accept(visitor Visitor) interface{} {
func (node *BooleanLiteral) Canonical() string {
if node.Value {
return "true"
} else {
return "false"
}

return "false"
}

//
Expand Down Expand Up @@ -691,9 +709,9 @@ func (node *NumberLiteral) Canonical() string {
func (node *NumberLiteral) Number() interface{} {
if node.IsInt {
return int(node.Value)
} else {
return node.Value
}

return node.Value
}

//
Expand All @@ -708,7 +726,7 @@ type Hash struct {
Pairs []*HashPair
}

// NewNumberLiteral instanciates a new hash node.
// NewHash instanciates a new hash node.
func NewHash(pos int, line int) *Hash {
return &Hash{
NodeType: NodeHash,
Expand All @@ -718,7 +736,7 @@ func NewHash(pos int, line int) *Hash {

// String returns a string representation of receiver that can be used for debugging.
func (node *Hash) String() string {
result := fmt.Sprintf("Hash{[", node.Loc.Pos)
result := fmt.Sprintf("Hash{[%d", node.Loc.Pos)

for i, p := range node.Pairs {
if i > 0 {
Expand Down
10 changes: 5 additions & 5 deletions handlebars/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
)

// cf. https://github.com/aymerick/go-fuzz-tests/raymond
const DUMP_TPL = false
const dumpTpl = false

var dump_tpl_nb = 0
var dumpTplNb = 0

type Test struct {
name string
Expand All @@ -32,12 +32,12 @@ func launchTests(t *testing.T, tests []Test) {
var err error
var tpl *raymond.Template

if DUMP_TPL {
filename := strconv.Itoa(dump_tpl_nb)
if dumpTpl {
filename := strconv.Itoa(dumpTplNb)
if err := ioutil.WriteFile(path.Join(".", "dump_tpl", filename), []byte(test.input), 0644); err != nil {
panic(err)
}
dump_tpl_nb += 1
dumpTplNb++
}

// parse template
Expand Down
1 change: 0 additions & 1 deletion handlebars/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ var basicTests = []Test{
nil, nil,
map[string]interface{}{"helper": func() string {
panic("fail")
return ""
}},
nil,
"test: ",
Expand Down
15 changes: 5 additions & 10 deletions handlebars/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,8 @@ var helpersTests = []Test{
if ok {
if p {
return "GOODBYE " + options.HashStr("cruel") + " " + options.HashStr("world")
} else {
return "NOT PRINTING"
}
return "NOT PRINTING"
}

return "THIS SHOULD NOT HAPPEN"
Expand All @@ -496,9 +495,8 @@ var helpersTests = []Test{
if ok {
if p {
return "GOODBYE " + options.HashStr("cruel") + " " + options.HashStr("world")
} else {
return "NOT PRINTING"
}
return "NOT PRINTING"
}

return "THIS SHOULD NOT HAPPEN"
Expand Down Expand Up @@ -535,9 +533,8 @@ var helpersTests = []Test{
if ok {
if p {
return "GOODBYE " + options.HashStr("cruel") + " " + options.Fn()
} else {
return "NOT PRINTING"
}
return "NOT PRINTING"
}

return "THIS SHOULD NOT HAPPEN"
Expand All @@ -554,9 +551,8 @@ var helpersTests = []Test{
if ok {
if p {
return "GOODBYE " + options.HashStr("cruel") + " " + options.Fn()
} else {
return "NOT PRINTING"
}
return "NOT PRINTING"
}

return "THIS SHOULD NOT HAPPEN"
Expand All @@ -580,9 +576,8 @@ var helpersTests = []Test{
if ok {
if p {
return "GOODBYE " + options.HashStr("cruel") + " " + options.Fn()
} else {
return "NOT PRINTING"
}
return "NOT PRINTING"
}

return "THIS SHOULD NOT HAPPEN"
Expand Down
4 changes: 2 additions & 2 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ func (options *Options) Hash() map[string]interface{} {
func (options *Options) Param(pos int) interface{} {
if len(options.params) > pos {
return options.params[pos]
} else {
return nil
}

return nil
}

// ParamStr returns string representation of parameter at given position.
Expand Down
25 changes: 12 additions & 13 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (

const (
// Mustaches detection
ESCAPED_ESCAPED_OPEN_MUSTACHE = "\\\\{{"
ESCAPED_OPEN_MUSTACHE = "\\{{"
OPEN_MUSTACHE = "{{"
CLOSE_MUSTACHE = "}}"
CLOSE_STRIP_MUSTACHE = "~}}"
CLOSE_UNESCAPED_STRIP_MUSTACHE = "}~}}"
escapedEscapedOpenMustache = "\\\\{{"
escapedOpenMustache = "\\{{"
openMustache = "{{"
closeMustache = "}}"
closeStripMustache = "~}}"
closeUnescapedStripMustache = "}~}}"
)

const eof = -1
Expand Down Expand Up @@ -255,9 +255,8 @@ func (l *Lexer) indexRegexp(r *regexp.Regexp) int {
loc := r.FindStringIndex(l.input[l.pos:])
if loc == nil {
return -1
} else {
return loc[0]
}
return loc[0]
}

// lexContent scans content (ie: not between mustaches)
Expand All @@ -274,7 +273,7 @@ func lexContent(l *Lexer) lexFunc {
} else {
return l.errorf("Unclosed raw block")
}
} else if l.isString(ESCAPED_ESCAPED_OPEN_MUSTACHE) {
} else if l.isString(escapedEscapedOpenMustache) {
// \\{{

// emit content with only one escaped escape
Expand All @@ -286,7 +285,7 @@ func lexContent(l *Lexer) lexFunc {
l.ignore()

next = lexContent
} else if l.isString(ESCAPED_OPEN_MUSTACHE) {
} else if l.isString(escapedOpenMustache) {
// \{{
next = lexEscapedOpenMustache
} else if str := l.findRegexp(rOpenCommentDash); str != "" {
Expand All @@ -299,7 +298,7 @@ func lexContent(l *Lexer) lexFunc {
l.closeComment = rCloseComment

next = lexComment
} else if l.isString(OPEN_MUSTACHE) {
} else if l.isString(openMustache) {
// {{
next = lexOpenMustache
}
Expand Down Expand Up @@ -408,7 +407,7 @@ func lexCloseMustache(l *Lexer) lexFunc {
// lexExpression scans inside mustaches
func lexExpression(l *Lexer) lexFunc {
// search close mustache delimiter
if l.isString(CLOSE_MUSTACHE) || l.isString(CLOSE_STRIP_MUSTACHE) || l.isString(CLOSE_UNESCAPED_STRIP_MUSTACHE) {
if l.isString(closeMustache) || l.isString(closeStripMustache) || l.isString(closeUnescapedStripMustache) {
return lexCloseMustache
}

Expand Down Expand Up @@ -515,7 +514,7 @@ func lexIgnorable(l *Lexer) lexFunc {
func lexString(l *Lexer) lexFunc {
// get string delimiter
delim := l.next()
var prev rune = 0
var prev rune

// ignore delimiter
l.ignore()
Expand Down
Loading

0 comments on commit f75ebec

Please sign in to comment.