diff --git a/boot.go b/boot.go index 65c4a34..b240857 100644 --- a/boot.go +++ b/boot.go @@ -197,7 +197,7 @@ func initBoot(definitions [][]string) (*Syntax, error) { } func bootSyntax() (*Syntax, error) { - b, err := initBoot(bootDefinitions) + b, err := initBoot(bootSyntaxDefs) if err != nil { return nil, err } diff --git a/boot_test.go b/boot_test.go index 0287ac1..5733efe 100644 --- a/boot_test.go +++ b/boot_test.go @@ -6,7 +6,7 @@ import ( ) func TestBoot(t *testing.T) { - b, err := initBoot(bootDefinitions) + b, err := initBoot(bootSyntaxDefs) if err != nil { t.Error(err) return diff --git a/bootsyntax.go b/bootsyntax.go index 76e41ba..44471d2 100644 --- a/bootsyntax.go +++ b/bootsyntax.go @@ -1,6 +1,6 @@ package parse -var bootDefinitions = [][]string{{ +var bootSyntaxDefs = [][]string{{ "chars", "space", "alias", " ", }, { "chars", "tab", "alias", "\\t", diff --git a/buzz.txt b/buzz.txt new file mode 100644 index 0000000..300475b --- /dev/null +++ b/buzz.txt @@ -0,0 +1,5 @@ +generator, in-process init or command line +syntax from file or in-memory +simple syntax with recursion +no lexer required +utf8, 8bit or custom tokens diff --git a/cache.go b/cache.go index 6ab6028..49d6dc6 100644 --- a/cache.go +++ b/cache.go @@ -1,5 +1,7 @@ package parse +// TODO: rename to store + type cacheItem struct { name string node *Node @@ -39,11 +41,11 @@ func (c *cache) get(offset int, name string) (*Node, bool, bool) { return nil, false, false } -func (c *cache) setOne(offset int, name string, n *Node) { -} - func (c *cache) set(offset int, name string, n *Node) { - if len(c.tokens) <= offset { + var tc *tokenCache + if len(c.tokens) > offset { + tc = c.tokens[offset] + } else { if cap(c.tokens) > offset { c.tokens = c.tokens[:offset+1] } else { @@ -52,10 +54,7 @@ func (c *cache) set(offset int, name string, n *Node) { c.tokens = append(c.tokens, nil) } } - } - tc := c.tokens[offset] - if tc == nil { tc = &tokenCache{} c.tokens[offset] = tc } diff --git a/char.go b/char.go index d2f8cf2..5bc0df8 100644 --- a/char.go +++ b/char.go @@ -89,7 +89,7 @@ func (p *charParser) parse(t Trace, c *context) { if tok, ok := c.token(); ok && p.match(tok) { // t.Out1("success", string(tok)) - n := newNode(p.name, p.commit, c.offset, c.offset+1) + n := newNode(p.name, c.offset, c.offset+1, p.commit) c.cache.set(c.offset, p.name, n) for _, includedBy := range p.includedBy { includedBy.cacheIncluded(c, n) diff --git a/choice.go b/choice.go index 7c7830a..3304037 100644 --- a/choice.go +++ b/choice.go @@ -79,13 +79,13 @@ func (p *choiceParser) setIncludedBy(includedBy parser, path []string) { } func (p *choiceParser) cacheIncluded(c *context, n *Node) { - if !c.excluded(n.from, p.name) { + if !c.excluded(n.From, p.name) { return } - nc := newNode(p.name, p.commit, n.from, n.to) + nc := newNode(p.name, n.From, n.To, p.commit) nc.append(n) - c.cache.set(nc.from, p.name, nc) + c.cache.set(nc.From, p.name, nc) for _, includedBy := range p.includedBy { includedBy.cacheIncluded(c, nc) @@ -116,7 +116,7 @@ func (p *choiceParser) parse(t Trace, c *context) { c.exclude(c.offset, p.name) defer c.include(c.offset, p.name) - node := newNode(p.name, p.commit, c.offset, c.offset) + node := newNode(p.name, c.offset, c.offset, p.commit) var match bool for { @@ -126,7 +126,7 @@ func (p *choiceParser) parse(t Trace, c *context) { for len(elements) > 0 { elements[0].parse(t, c) elements = elements[1:] - c.offset = node.from + c.offset = node.From if !c.match || match && c.node.tokenLength() <= node.tokenLength() { continue @@ -134,10 +134,10 @@ func (p *choiceParser) parse(t Trace, c *context) { match = true foundMatch = true - node = newNode(p.name, p.commit, c.offset, c.offset) + node = newNode(p.name, c.offset, c.offset, p.commit) node.append(c.node) - c.cache.set(node.from, p.name, node) + c.cache.set(node.From, p.name, node) for _, includedBy := range p.includedBy { includedBy.cacheIncluded(c, node) } @@ -155,6 +155,6 @@ func (p *choiceParser) parse(t Trace, c *context) { } // t.Out1("fail") - c.cache.set(node.from, p.name, nil) - c.fail(node.from) + c.cache.set(node.From, p.name, nil) + c.fail(node.From) } diff --git a/context.go b/context.go index 2121e9d..46b05cf 100644 --- a/context.go +++ b/context.go @@ -73,6 +73,10 @@ func (c *context) excluded(offset int, name string) bool { } func (c *context) exclude(offset int, name string) { + if c.excluded(offset, name) { + return + } + if len(c.isExcluded) <= offset { c.isExcluded = append(c.isExcluded, nil) if cap(c.isExcluded) > offset { @@ -96,6 +100,7 @@ func (c *context) include(offset int, name string) { for i := len(c.isExcluded[offset]) - 1; i >= 0; i-- { if c.isExcluded[offset][i] == name { c.isExcluded[offset] = append(c.isExcluded[offset][:i], c.isExcluded[offset][i+1:]...) + return } } } @@ -117,7 +122,7 @@ func (c *context) fromCache(name string) (bool, bool) { func (c *context) success(n *Node) { c.node = n - c.offset = n.to + c.offset = n.To c.match = true } @@ -127,7 +132,7 @@ func (c *context) fail(offset int) { } func (c *context) finalize() error { - if c.node.to < c.readOffset { + if c.node.To < c.readOffset { return ErrUnexpectedCharacter } @@ -142,11 +147,6 @@ func (c *context) finalize() error { } } - c.node.commit() - if c.node.commitType&Alias != 0 { - return nil - } - - c.node.applyTokens(c.tokens) + c.node.commit(c.tokens) return nil } diff --git a/keyval_test.go b/keyval_test.go index a948e4c..4198c30 100644 --- a/keyval_test.go +++ b/keyval_test.go @@ -13,13 +13,13 @@ func TestKeyVal(t *testing.T) { text: "a key", nodes: []*Node{{ Name: "key-val", - to: 5, + To: 5, Nodes: []*Node{{ Name: "key", - to: 5, + To: 5, Nodes: []*Node{{ Name: "symbol", - to: 5, + To: 5, }}, }}, }}, @@ -28,16 +28,16 @@ func TestKeyVal(t *testing.T) { text: " a key", nodes: []*Node{{ Name: "key-val", - from: 1, - to: 6, + From: 1, + To: 6, Nodes: []*Node{{ Name: "key", - from: 1, - to: 6, + From: 1, + To: 6, Nodes: []*Node{{ Name: "symbol", - from: 1, - to: 6, + From: 1, + To: 6, }}, }}, }}, @@ -50,16 +50,16 @@ func TestKeyVal(t *testing.T) { `, nodes: []*Node{{ Name: "key-val", - from: 20, - to: 25, + From: 20, + To: 25, Nodes: []*Node{{ Name: "key", - from: 20, - to: 25, + From: 20, + To: 25, Nodes: []*Node{{ Name: "symbol", - from: 20, - to: 25, + From: 20, + To: 25, }}, }}, }}, @@ -68,18 +68,18 @@ func TestKeyVal(t *testing.T) { text: "a key = a value", nodes: []*Node{{ Name: "key-val", - to: 15, + To: 15, Nodes: []*Node{{ Name: "key", - to: 5, + To: 5, Nodes: []*Node{{ Name: "symbol", - to: 5, + To: 5, }}, }, { Name: "value", - from: 8, - to: 15, + From: 8, + To: 15, }}, }}, }, { @@ -90,39 +90,39 @@ func TestKeyVal(t *testing.T) { `, nodes: []*Node{{ Name: "key-val", - from: 11, - to: 32, + From: 11, + To: 32, Nodes: []*Node{{ Name: "key", - from: 11, - to: 16, + From: 11, + To: 16, Nodes: []*Node{{ Name: "symbol", - from: 11, - to: 16, + From: 11, + To: 16, }}, }, { Name: "value", - from: 25, - to: 32, + From: 25, + To: 32, }}, }, { Name: "key-val", - from: 61, - to: 88, + From: 61, + To: 88, Nodes: []*Node{{ Name: "key", - from: 61, - to: 72, + From: 61, + To: 72, Nodes: []*Node{{ Name: "symbol", - from: 61, - to: 72, + From: 61, + To: 72, }}, }, { Name: "value", - from: 75, - to: 88, + From: 75, + To: 88, }}, }}, }, { @@ -130,11 +130,11 @@ func TestKeyVal(t *testing.T) { text: "= a value", nodes: []*Node{{ Name: "key-val", - to: 9, + To: 9, Nodes: []*Node{{ Name: "value", - from: 2, - to: 9, + From: 2, + To: 9, }}, }}, }, { @@ -145,25 +145,25 @@ func TestKeyVal(t *testing.T) { `, nodes: []*Node{{ Name: "key-val", - from: 4, - to: 34, + From: 4, + To: 34, Nodes: []*Node{{ Name: "comment", - from: 4, - to: 15, + From: 4, + To: 15, }, { Name: "key", - from: 19, - to: 24, + From: 19, + To: 24, Nodes: []*Node{{ Name: "symbol", - from: 19, - to: 24, + From: 19, + To: 24, }}, }, { Name: "value", - from: 27, - to: 34, + From: 27, + To: 34, }}, }}, }, { @@ -171,32 +171,32 @@ func TestKeyVal(t *testing.T) { text: "a key . with.multiple.symbols=a value", nodes: []*Node{{ Name: "key-val", - to: 37, + To: 37, Nodes: []*Node{{ Name: "key", - from: 0, - to: 29, + From: 0, + To: 29, Nodes: []*Node{{ Name: "symbol", - from: 0, - to: 5, + From: 0, + To: 5, }, { Name: "symbol", - from: 8, - to: 12, + From: 8, + To: 12, }, { Name: "symbol", - from: 13, - to: 21, + From: 13, + To: 21, }, { Name: "symbol", - from: 22, - to: 29, + From: 22, + To: 29, }}, }, { Name: "value", - from: 30, - to: 37, + From: 30, + To: 37, }}, }}, }, { @@ -207,20 +207,20 @@ func TestKeyVal(t *testing.T) { `, nodes: []*Node{{ Name: "group-key", - from: 4, - to: 38, + From: 4, + To: 38, Nodes: []*Node{{ Name: "comment", - from: 4, - to: 15, + From: 4, + To: 15, }, { Name: "symbol", - from: 20, - to: 31, + From: 20, + To: 31, }, { Name: "symbol", - from: 32, - to: 37, + From: 32, + To: 37, }}, }}, }, { diff --git a/mml_test.go b/mml_test.go index 7419d64..3435dd6 100644 --- a/mml_test.go +++ b/mml_test.go @@ -11,11 +11,11 @@ func TestMML(t *testing.T) { text: "// foo bar baz", nodes: []*Node{{ Name: "comment", - to: 14, + To: 14, Nodes: []*Node{{ Name: "line-comment-content", - from: 2, - to: 14, + From: 2, + To: 14, }}, }}, }, { @@ -23,15 +23,15 @@ func TestMML(t *testing.T) { text: "// foo bar\n// baz qux", nodes: []*Node{{ Name: "comment", - to: 21, + To: 21, Nodes: []*Node{{ Name: "line-comment-content", - from: 2, - to: 10, + From: 2, + To: 10, }, { Name: "line-comment-content", - from: 13, - to: 21, + From: 13, + To: 21, }}, }}, }, { @@ -39,11 +39,11 @@ func TestMML(t *testing.T) { text: "/* foo bar baz */", nodes: []*Node{{ Name: "comment", - to: 17, + To: 17, Nodes: []*Node{{ Name: "block-comment-content", - from: 2, - to: 15, + From: 2, + To: 15, }}, }}, }, { @@ -51,15 +51,15 @@ func TestMML(t *testing.T) { text: "/* foo bar */\n/* baz qux */", nodes: []*Node{{ Name: "comment", - to: 27, + To: 27, Nodes: []*Node{{ Name: "block-comment-content", - from: 2, - to: 11, + From: 2, + To: 11, }, { Name: "block-comment-content", - from: 16, - to: 25, + From: 16, + To: 25, }}, }}, }, { @@ -67,19 +67,19 @@ func TestMML(t *testing.T) { text: "// foo\n/* bar */\n// baz", nodes: []*Node{{ Name: "comment", - to: 23, + To: 23, Nodes: []*Node{{ Name: "line-comment-content", - from: 2, - to: 6, + From: 2, + To: 6, }, { Name: "block-comment-content", - from: 9, - to: 14, + From: 9, + To: 14, }, { Name: "line-comment-content", - from: 19, - to: 23, + From: 19, + To: 23, }}, }}, }, { @@ -87,159 +87,159 @@ func TestMML(t *testing.T) { text: "42", nodes: []*Node{{ Name: "int", - to: 2, + To: 2, }}, }, { msg: "ints", text: "1; 2; 3", nodes: []*Node{{ Name: "int", - to: 1, + To: 1, }, { Name: "int", - from: 3, - to: 4, + From: 3, + To: 4, }, { Name: "int", - from: 6, - to: 7, + From: 6, + To: 7, }}, }, { msg: "int, octal", text: "052", nodes: []*Node{{ Name: "int", - to: 3, + To: 3, }}, }, { msg: "int, hexa", text: "0x2a", nodes: []*Node{{ Name: "int", - to: 4, + To: 4, }}, }, { msg: "float, 0.", text: "0.", nodes: []*Node{{ Name: "float", - to: 2, + To: 2, }}, }, { msg: "float, 72.40", text: "72.40", nodes: []*Node{{ Name: "float", - to: 5, + To: 5, }}, }, { msg: "float, 072.40", text: "072.40", nodes: []*Node{{ Name: "float", - to: 6, + To: 6, }}, }, { msg: "float, 2.71828", text: "2.71828", nodes: []*Node{{ Name: "float", - to: 7, + To: 7, }}, }, { msg: "float, 6.67428e-11", text: "6.67428e-11", nodes: []*Node{{ Name: "float", - to: 11, + To: 11, }}, }, { msg: "float, 1E6", text: "1E6", nodes: []*Node{{ Name: "float", - to: 3, + To: 3, }}, }, { msg: "float, .25", text: ".25", nodes: []*Node{{ Name: "float", - to: 3, + To: 3, }}, }, { msg: "float, .12345E+5", text: ".12345E+5", nodes: []*Node{{ Name: "float", - to: 9, + To: 9, }}, }, { msg: "string, empty", text: "\"\"", nodes: []*Node{{ Name: "string", - to: 2, + To: 2, }}, }, { msg: "string", text: "\"foo\"", nodes: []*Node{{ Name: "string", - to: 5, + To: 5, }}, }, { msg: "string, with new line", text: "\"foo\nbar\"", nodes: []*Node{{ Name: "string", - to: 9, + To: 9, }}, }, { msg: "string, with escaped new line", text: "\"foo\\nbar\"", nodes: []*Node{{ Name: "string", - to: 10, + To: 10, }}, }, { msg: "string, with quotes", text: "\"foo \\\"bar\\\" baz\"", nodes: []*Node{{ Name: "string", - to: 17, + To: 17, }}, }, { msg: "bool, true", text: "true", nodes: []*Node{{ Name: "true", - to: 4, + To: 4, }}, }, { msg: "bool, false", text: "false", nodes: []*Node{{ Name: "false", - to: 5, + To: 5, }}, }, { msg: "symbol", text: "foo", nodes: []*Node{{ Name: "symbol", - to: 3, + To: 3, }}, }, { msg: "dynamic-symbol", text: "symbol(a)", nodes: []*Node{{ Name: "dynamic-symbol", - to: 9, + To: 9, Nodes: []*Node{{ Name: "symbol", - from: 7, - to: 8, + From: 7, + To: 8, }}, }}, }, { @@ -247,26 +247,26 @@ func TestMML(t *testing.T) { text: "[]", nodes: []*Node{{ Name: "list", - to: 2, + To: 2, }}, }, { msg: "list", text: "[a, b, c]", nodes: []*Node{{ Name: "list", - to: 9, + To: 9, Nodes: []*Node{{ Name: "symbol", - from: 1, - to: 2, + From: 1, + To: 2, }, { Name: "symbol", - from: 4, - to: 5, + From: 4, + To: 5, }, { Name: "symbol", - from: 7, - to: 8, + From: 7, + To: 8, }}, }}, }, { @@ -278,19 +278,19 @@ func TestMML(t *testing.T) { ]`, nodes: []*Node{{ Name: "list", - to: 20, + To: 20, Nodes: []*Node{{ Name: "symbol", - from: 5, - to: 6, + From: 5, + To: 6, }, { Name: "symbol", - from: 10, - to: 11, + From: 10, + To: 11, }, { Name: "symbol", - from: 15, - to: 16, + From: 15, + To: 16, }}, }}, }, { @@ -298,57 +298,57 @@ func TestMML(t *testing.T) { text: "[a, b, c..., [d, e], [f, [g]]...]", nodes: []*Node{{ Name: "list", - to: 33, + To: 33, Nodes: []*Node{{ Name: "symbol", - from: 1, - to: 2, + From: 1, + To: 2, }, { Name: "symbol", - from: 4, - to: 5, + From: 4, + To: 5, }, { Name: "spread-expression", - from: 7, - to: 11, + From: 7, + To: 11, Nodes: []*Node{{ Name: "symbol", - from: 7, - to: 8, + From: 7, + To: 8, }}, }, { Name: "list", - from: 13, - to: 19, + From: 13, + To: 19, Nodes: []*Node{{ Name: "symbol", - from: 14, - to: 15, + From: 14, + To: 15, }, { Name: "symbol", - from: 17, - to: 18, + From: 17, + To: 18, }}, }, { Name: "spread-expression", - from: 21, - to: 32, + From: 21, + To: 32, Nodes: []*Node{{ Name: "list", - from: 21, - to: 29, + From: 21, + To: 29, Nodes: []*Node{{ Name: "symbol", - from: 22, - to: 23, + From: 22, + To: 23, }, { Name: "list", - from: 25, - to: 28, + From: 25, + To: 28, Nodes: []*Node{{ Name: "symbol", - from: 26, - to: 27, + From: 26, + To: 27, }}, }}, }}, @@ -359,19 +359,19 @@ func TestMML(t *testing.T) { text: "~[a, b, c]", nodes: []*Node{{ Name: "mutable-list", - to: 10, + To: 10, Nodes: []*Node{{ Name: "symbol", - from: 2, - to: 3, + From: 2, + To: 3, }, { Name: "symbol", - from: 5, - to: 6, + From: 5, + To: 6, }, { Name: "symbol", - from: 8, - to: 9, + From: 8, + To: 9, }}, }}, }, { @@ -379,75 +379,75 @@ func TestMML(t *testing.T) { text: "{}", nodes: []*Node{{ Name: "struct", - to: 2, + To: 2, }}, }, { msg: "struct", text: "{foo: 1, \"bar\": 2, symbol(baz): 3, [qux]: 4}", nodes: []*Node{{ Name: "struct", - to: 44, + To: 44, Nodes: []*Node{{ Name: "entry", - from: 1, - to: 7, + From: 1, + To: 7, Nodes: []*Node{{ Name: "symbol", - from: 1, - to: 4, + From: 1, + To: 4, }, { Name: "int", - from: 6, - to: 7, + From: 6, + To: 7, }}, }, { Name: "entry", - from: 9, - to: 17, + From: 9, + To: 17, Nodes: []*Node{{ Name: "string", - from: 9, - to: 14, + From: 9, + To: 14, }, { Name: "int", - from: 16, - to: 17, + From: 16, + To: 17, }}, }, { Name: "entry", - from: 19, - to: 33, + From: 19, + To: 33, Nodes: []*Node{{ Name: "dynamic-symbol", - from: 19, - to: 30, + From: 19, + To: 30, Nodes: []*Node{{ Name: "symbol", - from: 26, - to: 29, + From: 26, + To: 29, }}, }, { Name: "int", - from: 32, - to: 33, + From: 32, + To: 33, }}, }, { Name: "entry", - from: 35, - to: 43, + From: 35, + To: 43, Nodes: []*Node{{ Name: "indexer-symbol", - from: 35, - to: 40, + From: 35, + To: 40, Nodes: []*Node{{ Name: "symbol", - from: 36, - to: 39, + From: 36, + To: 39, }}, }, { Name: "int", - from: 42, - to: 43, + From: 42, + To: 43, }}, }}, }}, @@ -456,63 +456,63 @@ func TestMML(t *testing.T) { text: "{foo: 1, {bar: 2}..., {baz: {}}...}", nodes: []*Node{{ Name: "struct", - to: 35, + To: 35, Nodes: []*Node{{ Name: "entry", - from: 1, - to: 7, + From: 1, + To: 7, Nodes: []*Node{{ Name: "symbol", - from: 1, - to: 4, + From: 1, + To: 4, }, { Name: "int", - from: 6, - to: 7, + From: 6, + To: 7, }}, }, { Name: "spread-expression", - from: 9, - to: 20, + From: 9, + To: 20, Nodes: []*Node{{ Name: "struct", - from: 9, - to: 17, + From: 9, + To: 17, Nodes: []*Node{{ Name: "entry", - from: 10, - to: 16, + From: 10, + To: 16, Nodes: []*Node{{ Name: "symbol", - from: 10, - to: 13, + From: 10, + To: 13, }, { Name: "int", - from: 15, - to: 16, + From: 15, + To: 16, }}, }}, }}, }, { Name: "spread-expression", - from: 22, - to: 34, + From: 22, + To: 34, Nodes: []*Node{{ Name: "struct", - from: 22, - to: 31, + From: 22, + To: 31, Nodes: []*Node{{ Name: "entry", - from: 23, - to: 30, + From: 23, + To: 30, Nodes: []*Node{{ Name: "symbol", - from: 23, - to: 26, + From: 23, + To: 26, }, { Name: "struct", - from: 28, - to: 30, + From: 28, + To: 30, }}, }}, }}, @@ -523,24 +523,24 @@ func TestMML(t *testing.T) { text: "{[a]: b}", nodes: []*Node{{ Name: "struct", - to: 8, + To: 8, Nodes: []*Node{{ Name: "entry", - from: 1, - to: 7, + From: 1, + To: 7, Nodes: []*Node{{ Name: "indexer-symbol", - from: 1, - to: 4, + From: 1, + To: 4, Nodes: []*Node{{ Name: "symbol", - from: 2, - to: 3, + From: 2, + To: 3, }}, }, { Name: "symbol", - from: 6, - to: 7, + From: 6, + To: 7, }}, }}, }}, @@ -549,19 +549,19 @@ func TestMML(t *testing.T) { text: "~{foo: 1}", nodes: []*Node{{ Name: "mutable-struct", - to: 9, + To: 9, Nodes: []*Node{{ Name: "entry", - from: 2, - to: 8, + From: 2, + To: 8, Nodes: []*Node{{ Name: "symbol", - from: 2, - to: 5, + From: 2, + To: 5, }, { Name: "int", - from: 7, - to: 8, + From: 7, + To: 8, }}, }}, }}, @@ -570,18 +570,18 @@ func TestMML(t *testing.T) { text: "<>", nodes: []*Node{{ Name: "channel", - to: 2, + To: 2, }}, }, { msg: "buffered channel", text: "<42>", nodes: []*Node{{ Name: "channel", - to: 4, + To: 4, Nodes: []*Node{{ Name: "int", - from: 1, - to: 3, + From: 1, + To: 3, }}, }}, }, { @@ -589,22 +589,22 @@ func TestMML(t *testing.T) { text: "and(a, b, c)", nodes: []*Node{{ Name: "function-application", - to: 12, + To: 12, Nodes: []*Node{{ Name: "symbol", - to: 3, + To: 3, }, { Name: "symbol", - from: 4, - to: 5, + From: 4, + To: 5, }, { Name: "symbol", - from: 7, - to: 8, + From: 7, + To: 8, }, { Name: "symbol", - from: 10, - to: 11, + From: 10, + To: 11, }}, }}, }, { @@ -612,22 +612,22 @@ func TestMML(t *testing.T) { text: "or(a, b, c)", nodes: []*Node{{ Name: "function-application", - to: 11, + To: 11, Nodes: []*Node{{ Name: "symbol", - to: 2, + To: 2, }, { Name: "symbol", - from: 3, - to: 4, + From: 3, + To: 4, }, { Name: "symbol", - from: 6, - to: 7, + From: 6, + To: 7, }, { Name: "symbol", - from: 9, - to: 10, + From: 9, + To: 10, }}, }}, }, { @@ -635,11 +635,11 @@ func TestMML(t *testing.T) { text: "fn () 42", nodes: []*Node{{ Name: "function", - to: 8, + To: 8, Nodes: []*Node{{ Name: "int", - from: 6, - to: 8, + From: 6, + To: 8, }}, }}, }, { @@ -647,11 +647,11 @@ func TestMML(t *testing.T) { text: "fn () {;}", nodes: []*Node{{ Name: "function", - to: 9, + To: 9, Nodes: []*Node{{ Name: "block", - from: 6, - to: 9, + From: 6, + To: 9, }}, }}, }, { @@ -659,35 +659,35 @@ func TestMML(t *testing.T) { text: "fn (a, b, c) [a, b, c]", nodes: []*Node{{ Name: "function", - to: 22, + To: 22, Nodes: []*Node{{ Name: "symbol", - from: 4, - to: 5, + From: 4, + To: 5, }, { Name: "symbol", - from: 7, - to: 8, + From: 7, + To: 8, }, { Name: "symbol", - from: 10, - to: 11, + From: 10, + To: 11, }, { Name: "list", - from: 13, - to: 22, + From: 13, + To: 22, Nodes: []*Node{{ Name: "symbol", - from: 14, - to: 15, + From: 14, + To: 15, }, { Name: "symbol", - from: 17, - to: 18, + From: 17, + To: 18, }, { Name: "symbol", - from: 20, - to: 21, + From: 20, + To: 21, }}, }}, }}, @@ -700,35 +700,35 @@ func TestMML(t *testing.T) { ) [a, b, c]`, nodes: []*Node{{ Name: "function", - to: 33, + To: 33, Nodes: []*Node{{ Name: "symbol", - from: 8, - to: 9, + From: 8, + To: 9, }, { Name: "symbol", - from: 13, - to: 14, + From: 13, + To: 14, }, { Name: "symbol", - from: 18, - to: 19, + From: 18, + To: 19, }, { Name: "list", - from: 24, - to: 33, + From: 24, + To: 33, Nodes: []*Node{{ Name: "symbol", - from: 25, - to: 26, + From: 25, + To: 26, }, { Name: "symbol", - from: 28, - to: 29, + From: 28, + To: 29, }, { Name: "symbol", - from: 31, - to: 32, + From: 31, + To: 32, }}, }}, }}, @@ -737,40 +737,40 @@ func TestMML(t *testing.T) { text: "fn (a, b, ...c) [a, b, c]", nodes: []*Node{{ Name: "function", - to: 25, + To: 25, Nodes: []*Node{{ Name: "symbol", - from: 4, - to: 5, + From: 4, + To: 5, }, { Name: "symbol", - from: 7, - to: 8, + From: 7, + To: 8, }, { Name: "collect-symbol", - from: 10, - to: 14, + From: 10, + To: 14, Nodes: []*Node{{ Name: "symbol", - from: 13, - to: 14, + From: 13, + To: 14, }}, }, { Name: "list", - from: 16, - to: 25, + From: 16, + To: 25, Nodes: []*Node{{ Name: "symbol", - from: 17, - to: 18, + From: 17, + To: 18, }, { Name: "symbol", - from: 20, - to: 21, + From: 20, + To: 21, }, { Name: "symbol", - from: 23, - to: 24, + From: 23, + To: 24, }}, }}, }}, @@ -779,11 +779,11 @@ func TestMML(t *testing.T) { text: "fn ~ () 42", nodes: []*Node{{ Name: "effect", - to: 10, + To: 10, Nodes: []*Node{{ Name: "int", - from: 8, - to: 10, + From: 8, + To: 10, }}, }}, }, { @@ -791,14 +791,14 @@ func TestMML(t *testing.T) { text: "a[42]", nodes: []*Node{{ Name: "indexer", - to: 5, + To: 5, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "int", - from: 2, - to: 4, + From: 2, + To: 4, }}, }}, }, { @@ -806,27 +806,27 @@ func TestMML(t *testing.T) { text: "a[3:9]", nodes: []*Node{{ Name: "indexer", - to: 6, + To: 6, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "range-from", - from: 2, - to: 3, + From: 2, + To: 3, Nodes: []*Node{{ Name: "int", - from: 2, - to: 3, + From: 2, + To: 3, }}, }, { Name: "range-to", - from: 4, - to: 5, + From: 4, + To: 5, Nodes: []*Node{{ Name: "int", - from: 4, - to: 5, + From: 4, + To: 5, }}, }}, }}, @@ -835,18 +835,18 @@ func TestMML(t *testing.T) { text: "a[:9]", nodes: []*Node{{ Name: "indexer", - to: 5, + To: 5, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "range-to", - from: 3, - to: 4, + From: 3, + To: 4, Nodes: []*Node{{ Name: "int", - from: 3, - to: 4, + From: 3, + To: 4, }}, }}, }}, @@ -855,18 +855,18 @@ func TestMML(t *testing.T) { text: "a[3:]", nodes: []*Node{{ Name: "indexer", - to: 5, + To: 5, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "range-from", - from: 2, - to: 3, + From: 2, + To: 3, Nodes: []*Node{{ Name: "int", - from: 2, - to: 3, + From: 2, + To: 3, }}, }}, }}, @@ -875,30 +875,30 @@ func TestMML(t *testing.T) { text: "a[b][c][d]", nodes: []*Node{{ Name: "indexer", - to: 10, + To: 10, Nodes: []*Node{{ Name: "indexer", - to: 7, + To: 7, Nodes: []*Node{{ Name: "indexer", - to: 4, + To: 4, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "symbol", - from: 2, - to: 3, + From: 2, + To: 3, }}, }, { Name: "symbol", - from: 5, - to: 6, + From: 5, + To: 6, }}, }, { Name: "symbol", - from: 8, - to: 9, + From: 8, + To: 9, }}, }}, }, { @@ -906,14 +906,14 @@ func TestMML(t *testing.T) { text: "a.b", nodes: []*Node{{ Name: "indexer", - to: 3, + To: 3, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "symbol", - from: 2, - to: 3, + From: 2, + To: 3, }}, }}, }, { @@ -921,14 +921,14 @@ func TestMML(t *testing.T) { text: "a.\"b\"", nodes: []*Node{{ Name: "indexer", - to: 5, + To: 5, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "string", - from: 2, - to: 5, + From: 2, + To: 5, }}, }}, }, { @@ -936,18 +936,18 @@ func TestMML(t *testing.T) { text: "a.symbol(b)", nodes: []*Node{{ Name: "indexer", - to: 11, + To: 11, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "dynamic-symbol", - from: 2, - to: 11, + From: 2, + To: 11, Nodes: []*Node{{ Name: "symbol", - from: 9, - to: 10, + From: 9, + To: 10, }}, }}, }}, @@ -956,30 +956,30 @@ func TestMML(t *testing.T) { text: "a.b.c.d", nodes: []*Node{{ Name: "indexer", - to: 7, + To: 7, Nodes: []*Node{{ Name: "indexer", - to: 5, + To: 5, Nodes: []*Node{{ Name: "indexer", - to: 3, + To: 3, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "symbol", - from: 2, - to: 3, + From: 2, + To: 3, }}, }, { Name: "symbol", - from: 4, - to: 5, + From: 4, + To: 5, }}, }, { Name: "symbol", - from: 6, - to: 7, + From: 6, + To: 7, }}, }}, }, { @@ -987,22 +987,22 @@ func TestMML(t *testing.T) { text: "a\n.b\n.c", nodes: []*Node{{ Name: "indexer", - to: 7, + To: 7, Nodes: []*Node{{ Name: "indexer", - to: 4, + To: 4, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "symbol", - from: 3, - to: 4, + From: 3, + To: 4, }}, }, { Name: "symbol", - from: 6, - to: 7, + From: 6, + To: 7, }}, }}, }, { @@ -1010,22 +1010,22 @@ func TestMML(t *testing.T) { text: "a.\nb.\nc", nodes: []*Node{{ Name: "indexer", - to: 7, + To: 7, Nodes: []*Node{{ Name: "indexer", - to: 4, + To: 4, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "symbol", - from: 3, - to: 4, + From: 3, + To: 4, }}, }, { Name: "symbol", - from: 6, - to: 7, + From: 6, + To: 7, }}, }}, }, { @@ -1033,10 +1033,10 @@ func TestMML(t *testing.T) { text: "f()", nodes: []*Node{{ Name: "function-application", - to: 3, + To: 3, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }}, }}, }, { @@ -1044,14 +1044,14 @@ func TestMML(t *testing.T) { text: "f(a)", nodes: []*Node{{ Name: "function-application", - to: 4, + To: 4, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "symbol", - from: 2, - to: 3, + From: 2, + To: 3, }}, }}, }, { @@ -1059,22 +1059,22 @@ func TestMML(t *testing.T) { text: "f(a, b, c)", nodes: []*Node{{ Name: "function-application", - to: 10, + To: 10, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "symbol", - from: 2, - to: 3, + From: 2, + To: 3, }, { Name: "symbol", - from: 5, - to: 6, + From: 5, + To: 6, }, { Name: "symbol", - from: 8, - to: 9, + From: 8, + To: 9, }}, }}, }, { @@ -1082,22 +1082,22 @@ func TestMML(t *testing.T) { text: "f(a\nb\nc\n)", nodes: []*Node{{ Name: "function-application", - to: 9, + To: 9, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "symbol", - from: 2, - to: 3, + From: 2, + To: 3, }, { Name: "symbol", - from: 4, - to: 5, + From: 4, + To: 5, }, { Name: "symbol", - from: 6, - to: 7, + From: 6, + To: 7, }}, }}, }, { @@ -1105,35 +1105,35 @@ func TestMML(t *testing.T) { text: "f(a, b..., c, d...)", nodes: []*Node{{ Name: "function-application", - to: 19, + To: 19, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "symbol", - from: 2, - to: 3, + From: 2, + To: 3, }, { Name: "spread-expression", - from: 5, - to: 9, + From: 5, + To: 9, Nodes: []*Node{{ Name: "symbol", - from: 5, - to: 6, + From: 5, + To: 6, }}, }, { Name: "symbol", - from: 11, - to: 12, + From: 11, + To: 12, }, { Name: "spread-expression", - from: 14, - to: 18, + From: 14, + To: 18, Nodes: []*Node{{ Name: "symbol", - from: 14, - to: 15, + From: 14, + To: 15, }}, }}, }}, @@ -1142,30 +1142,30 @@ func TestMML(t *testing.T) { text: "f(a)(b)(c)", nodes: []*Node{{ Name: "function-application", - to: 10, + To: 10, Nodes: []*Node{{ Name: "function-application", - to: 7, + To: 7, Nodes: []*Node{{ Name: "function-application", - to: 4, + To: 4, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "symbol", - from: 2, - to: 3, + From: 2, + To: 3, }}, }, { Name: "symbol", - from: 5, - to: 6, + From: 5, + To: 6, }}, }, { Name: "symbol", - from: 8, - to: 9, + From: 8, + To: 9, }}, }}, }, { @@ -1173,30 +1173,30 @@ func TestMML(t *testing.T) { text: "f(g(h(a)))", nodes: []*Node{{ Name: "function-application", - to: 10, + To: 10, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "function-application", - from: 2, - to: 9, + From: 2, + To: 9, Nodes: []*Node{{ Name: "symbol", - from: 2, - to: 3, + From: 2, + To: 3, }, { Name: "function-application", - from: 4, - to: 8, + From: 4, + To: 8, Nodes: []*Node{{ Name: "symbol", - from: 4, - to: 5, + From: 4, + To: 5, }, { Name: "symbol", - from: 6, - to: 7, + From: 6, + To: 7, }}, }}, }}, @@ -1206,23 +1206,23 @@ func TestMML(t *testing.T) { text: "if a { b() }", nodes: []*Node{{ Name: "if", - to: 12, + To: 12, Nodes: []*Node{{ Name: "symbol", - from: 3, - to: 4, + From: 3, + To: 4, }, { Name: "block", - from: 5, - to: 12, + From: 5, + To: 12, Nodes: []*Node{{ Name: "function-application", - from: 7, - to: 10, + From: 7, + To: 10, Nodes: []*Node{{ Name: "symbol", - from: 7, - to: 8, + From: 7, + To: 8, }}, }}, }}, @@ -1232,28 +1232,28 @@ func TestMML(t *testing.T) { text: "if a { b } else { c }", nodes: []*Node{{ Name: "if", - to: 21, + To: 21, Nodes: []*Node{{ Name: "symbol", - from: 3, - to: 4, + From: 3, + To: 4, }, { Name: "block", - from: 5, - to: 10, + From: 5, + To: 10, Nodes: []*Node{{ Name: "symbol", - from: 7, - to: 8, + From: 7, + To: 8, }}, }, { Name: "block", - from: 16, - to: 21, + From: 16, + To: 21, Nodes: []*Node{{ Name: "symbol", - from: 18, - to: 19, + From: 18, + To: 19, }}, }}, }}, @@ -1267,55 +1267,55 @@ func TestMML(t *testing.T) { `, nodes: []*Node{{ Name: "if", - from: 4, - to: 66, + From: 4, + To: 66, Nodes: []*Node{{ Name: "symbol", - from: 7, - to: 8, + From: 7, + To: 8, }, { Name: "block", - from: 9, - to: 14, + From: 9, + To: 14, Nodes: []*Node{{ Name: "symbol", - from: 11, - to: 12, + From: 11, + To: 12, }}, }, { Name: "symbol", - from: 26, - to: 27, + From: 26, + To: 27, }, { Name: "block", - from: 28, - to: 33, + From: 28, + To: 33, Nodes: []*Node{{ Name: "symbol", - from: 30, - to: 31, + From: 30, + To: 31, }}, }, { Name: "symbol", - from: 45, - to: 46, + From: 45, + To: 46, }, { Name: "block", - from: 47, - to: 52, + From: 47, + To: 52, Nodes: []*Node{{ Name: "symbol", - from: 49, - to: 50, + From: 49, + To: 50, }}, }, { Name: "block", - from: 61, - to: 66, + From: 61, + To: 66, Nodes: []*Node{{ Name: "symbol", - from: 63, - to: 64, + From: 63, + To: 64, }}, }}, }}, @@ -1324,11 +1324,11 @@ func TestMML(t *testing.T) { text: "switch {default:}", nodes: []*Node{{ Name: "switch", - to: 17, + To: 17, Nodes: []*Node{{ Name: "default", - from: 8, - to: 16, + From: 8, + To: 16, }}, }}, }, { @@ -1336,24 +1336,24 @@ func TestMML(t *testing.T) { text: "switch a {case b: c}", nodes: []*Node{{ Name: "switch", - to: 20, + To: 20, Nodes: []*Node{{ Name: "symbol", - from: 7, - to: 8, + From: 7, + To: 8, }, { Name: "case", - from: 10, - to: 17, + From: 10, + To: 17, Nodes: []*Node{{ Name: "symbol", - from: 15, - to: 16, + From: 15, + To: 16, }}, }, { Name: "symbol", - from: 18, - to: 19, + From: 18, + To: 19, }}, }}, }, { @@ -1361,45 +1361,45 @@ func TestMML(t *testing.T) { text: "switch a {case b: c; case d: e; default: f}", nodes: []*Node{{ Name: "switch", - to: 43, + To: 43, Nodes: []*Node{{ Name: "symbol", - from: 7, - to: 8, + From: 7, + To: 8, }, { Name: "case", - from: 10, - to: 17, + From: 10, + To: 17, Nodes: []*Node{{ Name: "symbol", - from: 15, - to: 16, + From: 15, + To: 16, }}, }, { Name: "symbol", - from: 18, - to: 19, + From: 18, + To: 19, }, { Name: "case", - from: 21, - to: 28, + From: 21, + To: 28, Nodes: []*Node{{ Name: "symbol", - from: 26, - to: 27, + From: 26, + To: 27, }}, }, { Name: "symbol", - from: 29, - to: 30, + From: 29, + To: 30, }, { Name: "default", - from: 32, - to: 40, + From: 32, + To: 40, }, { Name: "symbol", - from: 41, - to: 42, + From: 41, + To: 42, }}, }}, }, { @@ -1421,45 +1421,45 @@ func TestMML(t *testing.T) { }`, nodes: []*Node{{ Name: "switch", - to: 87, + To: 87, Nodes: []*Node{{ Name: "symbol", - from: 10, - to: 11, + From: 10, + To: 11, }, { Name: "case", - from: 20, - to: 34, + From: 20, + To: 34, Nodes: []*Node{{ Name: "symbol", - from: 28, - to: 29, + From: 28, + To: 29, }}, }, { Name: "symbol", - from: 38, - to: 39, + From: 38, + To: 39, }, { Name: "case", - from: 43, - to: 57, + From: 43, + To: 57, Nodes: []*Node{{ Name: "symbol", - from: 51, - to: 52, + From: 51, + To: 52, }}, }, { Name: "symbol", - from: 61, - to: 62, + From: 61, + To: 62, }, { Name: "default", - from: 66, - to: 78, + From: 66, + To: 78, }, { Name: "symbol", - from: 82, - to: 83, + From: 82, + To: 83, }}, }}, }, { @@ -1467,11 +1467,11 @@ func TestMML(t *testing.T) { text: "match a {}", nodes: []*Node{{ Name: "match", - to: 10, + To: 10, Nodes: []*Node{{ Name: "symbol", - from: 6, - to: 7, + From: 6, + To: 7, }}, }}, }, { @@ -1481,44 +1481,44 @@ func TestMML(t *testing.T) { }`, nodes: []*Node{{ Name: "match", - to: 45, + To: 45, Nodes: []*Node{{ Name: "symbol", - from: 6, - to: 7, + From: 6, + To: 7, }, { Name: "match-case", - from: 13, - to: 35, + From: 13, + To: 35, Nodes: []*Node{{ Name: "list-type", - from: 18, - to: 34, + From: 18, + To: 34, Nodes: []*Node{{ Name: "list-destructure-type", - from: 19, - to: 33, + From: 19, + To: 33, Nodes: []*Node{{ Name: "destructure-item", - from: 19, - to: 24, + From: 19, + To: 24, Nodes: []*Node{{ Name: "symbol", - from: 19, - to: 24, + From: 19, + To: 24, }}, }, { Name: "collect-destructure-item", - from: 26, - to: 33, + From: 26, + To: 33, Nodes: []*Node{{ Name: "destructure-item", - from: 29, - to: 33, + From: 29, + To: 33, Nodes: []*Node{{ Name: "symbol", - from: 29, - to: 33, + From: 29, + To: 33, }}, }}, }}, @@ -1526,8 +1526,8 @@ func TestMML(t *testing.T) { }}, }, { Name: "symbol", - from: 36, - to: 41, + From: 36, + To: 41, }}, }}, }, { @@ -1812,7 +1812,7 @@ func TestMML(t *testing.T) { }`, nodes: []*Node{{ Name: "select", - to: 12, + To: 12, }}, }, { msg: "select", @@ -2228,18 +2228,18 @@ func TestMML(t *testing.T) { text: "a ? b : c", nodes: []*Node{{ Name: "ternary-expression", - to: 9, + To: 9, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "symbol", - from: 4, - to: 5, + From: 4, + To: 5, }, { Name: "symbol", - from: 8, - to: 9, + From: 8, + To: 9, }}, }}, }, { @@ -2247,31 +2247,31 @@ func TestMML(t *testing.T) { text: "a ? b ? c : d : e", nodes: []*Node{{ Name: "ternary-expression", - to: 17, + To: 17, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "ternary-expression", - from: 4, - to: 13, + From: 4, + To: 13, Nodes: []*Node{{ Name: "symbol", - from: 4, - to: 5, + From: 4, + To: 5, }, { Name: "symbol", - from: 8, - to: 9, + From: 8, + To: 9, }, { Name: "symbol", - from: 12, - to: 13, + From: 12, + To: 13, }}, }, { Name: "symbol", - from: 16, - to: 17, + From: 16, + To: 17, }}, }}, }, { @@ -2279,30 +2279,30 @@ func TestMML(t *testing.T) { text: "a ? b : c ? d : e", nodes: []*Node{{ Name: "ternary-expression", - to: 17, + To: 17, Nodes: []*Node{{ Name: "symbol", - to: 1, + To: 1, }, { Name: "symbol", - from: 4, - to: 5, + From: 4, + To: 5, }, { Name: "ternary-expression", - from: 8, - to: 17, + From: 8, + To: 17, Nodes: []*Node{{ Name: "symbol", - from: 8, - to: 9, + From: 8, + To: 9, }, { Name: "symbol", - from: 12, - to: 13, + From: 12, + To: 13, }, { Name: "symbol", - from: 16, - to: 17, + From: 16, + To: 17, }}, }}, }}, diff --git a/node.go b/node.go index 438e334..539e775 100644 --- a/node.go +++ b/node.go @@ -5,67 +5,43 @@ import "fmt" type Node struct { Name string Nodes []*Node + From, To int commitType CommitType - from, to int tokens []rune } -func newNode(name string, ct CommitType, from, to int) *Node { +func newNode(name string, from, to int, ct CommitType) *Node { return &Node{ Name: name, + From: from, + To: to, commitType: ct, - from: from, - to: to, } } func (n *Node) tokenLength() int { - return n.to - n.from + return n.To - n.From } func (n *Node) nodeLength() int { return len(n.Nodes) } -func findNode(in, n *Node) { - if n == in { - panic(fmt.Errorf("found self in %s", in.Name)) - } - - for _, ni := range n.Nodes { - findNode(in, ni) - } -} - func (n *Node) append(p *Node) { - findNode(n, p) n.Nodes = append(n.Nodes, p) - // TODO: check rather if n.from <= p.from??? or panic if less? or check rather node length and commit - // happens in the end anyway? - if n.from == 0 && n.to == 0 { - n.from = p.from + if n.tokenLength() == 0 { + n.From = p.From } - n.to = p.to + n.To = p.To } -func (n *Node) clear() { - n.from = 0 - n.to = 0 - n.Nodes = nil -} - -func (n *Node) applyTokens(t []rune) { +func (n *Node) commit(t []rune) { n.tokens = t - for _, ni := range n.Nodes { - ni.applyTokens(t) - } -} -func (n *Node) commit() { var nodes []*Node for _, ni := range n.Nodes { - ni.commit() + ni.commit(t) if ni.commitType&Alias != 0 { nodes = append(nodes, ni.Nodes...) } else { @@ -77,13 +53,13 @@ func (n *Node) commit() { } func (n *Node) String() string { - if n.from >= len(n.tokens) || n.to > len(n.tokens) { + if n.From >= len(n.tokens) || n.To > len(n.tokens) { return n.Name + ":incomplete" } - return fmt.Sprintf("%s:%d:%d:%s", n.Name, n.from, n.to, n.Text()) + return fmt.Sprintf("%s:%d:%d:%s", n.Name, n.From, n.To, n.Text()) } func (n *Node) Text() string { - return string(n.tokens[n.from:n.to]) + return string(n.tokens[n.From:n.To]) } diff --git a/notes.txt b/notes.txt new file mode 100644 index 0000000..456a631 --- /dev/null +++ b/notes.txt @@ -0,0 +1,7 @@ +cleanup +error reporting +custom tokens +indentation +benchmarking +code generation go +code generation js diff --git a/parse.go b/parse.go index 16110e1..c091834 100644 --- a/parse.go +++ b/parse.go @@ -33,23 +33,6 @@ func stringsContain(ss []string, s string) bool { return false } -func copyIncludes(to, from map[string]CommitType) { - if from == nil { - return - } - - for name, ct := range from { - to[name] = ct - } -} - -func mergeIncludes(left, right map[string]CommitType) map[string]CommitType { - m := make(map[string]CommitType) - copyIncludes(m, left) - copyIncludes(m, right) - return m -} - func parse(t Trace, p parser, c *context) (*Node, error) { p.parse(t, c) if c.readErr != nil { diff --git a/parse_test.go b/parse_test.go index 7f5b5bf..b88f752 100644 --- a/parse_test.go +++ b/parse_test.go @@ -28,7 +28,7 @@ func testSyntaxReader(r io.Reader, traceLevel int) (*Syntax, error) { return nil, err } - var trace Trace = NopTrace{} + var trace Trace if traceLevel >= 0 { trace = NewTrace(traceLevel) } @@ -45,10 +45,6 @@ func testSyntaxReader(r io.Reader, traceLevel int) (*Syntax, error) { return s, nil } -func testSyntaxString(s string, traceLevel int) (*Syntax, error) { - return testSyntaxReader(bytes.NewBufferString(s), traceLevel) -} - func testSyntax(file string, traceLevel int) (*Syntax, error) { f, err := os.Open(file) if err != nil { @@ -90,13 +86,13 @@ func checkNodePosition(t *testing.T, left, right *Node, position bool) { return } - if position && left.from != right.from { - t.Error("from doesn't match", left.Name, left.from, right.from) + if position && left.From != right.From { + t.Error("from doesn't match", left.Name, left.From, right.From) return } - if position && left.to != right.to { - t.Error("to doesn't match", left.Name, left.to, right.to) + if position && left.To != right.To { + t.Error("to doesn't match", left.Name, left.To, right.To) return } @@ -177,8 +173,7 @@ func testReaderTrace(t *testing.T, r io.Reader, rootName string, traceLevel int, } else { cn(t, n, &Node{ Name: rootName, - from: 0, - to: len(ti.text), + To: len(ti.text), Nodes: ti.nodes, }) } @@ -294,7 +289,7 @@ func TestRecursion(t *testing.T) { text: "aaa", node: &Node{ Name: "A", - to: 3, + To: 3, }, }}, ) @@ -307,7 +302,7 @@ func TestRecursion(t *testing.T) { text: "aaa", node: &Node{ Name: "A", - to: 3, + To: 3, }, }}, ) @@ -320,7 +315,7 @@ func TestRecursion(t *testing.T) { text: "aaa", node: &Node{ Name: "A", - to: 3, + To: 3, }, }}, ) @@ -333,7 +328,7 @@ func TestRecursion(t *testing.T) { text: "aaa", node: &Node{ Name: "A", - to: 3, + To: 3, }, }}, ) @@ -348,14 +343,14 @@ func TestSequence(t *testing.T) { text: "abb", node: &Node{ Name: "AB", - to: 3, + To: 3, }, }, { msg: "sequence with optional items, none", text: "bb", node: &Node{ Name: "AB", - to: 2, + To: 2, }, }}, ) @@ -388,7 +383,7 @@ func TestSequence(t *testing.T) { text: "aaa", node: &Node{ Name: "A", - to: 3, + To: 3, }, }}, ) @@ -403,7 +398,7 @@ func TestQuantifiers(t *testing.T) { text: "aba", node: &Node{ Name: "A", - to: 3, + To: 3, }, }, { msg: "zero, fail", @@ -424,7 +419,7 @@ func TestQuantifiers(t *testing.T) { text: "aba", node: &Node{ Name: "A", - to: 3, + To: 3, }, }, { msg: "one, too much", @@ -445,7 +440,7 @@ func TestQuantifiers(t *testing.T) { text: "abbba", node: &Node{ Name: "A", - to: 5, + To: 5, }, }, { msg: "three, too much", @@ -462,14 +457,14 @@ func TestQuantifiers(t *testing.T) { text: "aa", node: &Node{ Name: "A", - to: 2, + To: 2, }, }, { msg: "zero or one explicit", text: "aba", node: &Node{ Name: "A", - to: 3, + To: 3, }, }, { msg: "zero or one explicit, too much", @@ -486,14 +481,14 @@ func TestQuantifiers(t *testing.T) { text: "aa", node: &Node{ Name: "A", - to: 2, + To: 2, }, }, { msg: "zero or one explicit, omit zero", text: "aba", node: &Node{ Name: "A", - to: 3, + To: 3, }, }, { msg: "zero or one explicit, omit zero, too much", @@ -510,14 +505,14 @@ func TestQuantifiers(t *testing.T) { text: "aa", node: &Node{ Name: "A", - to: 2, + To: 2, }, }, { msg: "zero or one explicit, shortcut", text: "aba", node: &Node{ Name: "A", - to: 3, + To: 3, }, }, { msg: "zero or one explicit, shortcut, too much", @@ -534,21 +529,21 @@ func TestQuantifiers(t *testing.T) { text: "aa", node: &Node{ Name: "A", - to: 2, + To: 2, }, }, { msg: "zero or three", text: "abba", node: &Node{ Name: "A", - to: 4, + To: 4, }, }, { msg: "zero or three", text: "abbba", node: &Node{ Name: "A", - to: 5, + To: 5, }, }, { msg: "zero or three, too much", @@ -565,21 +560,21 @@ func TestQuantifiers(t *testing.T) { text: "aa", node: &Node{ Name: "A", - to: 2, + To: 2, }, }, { msg: "zero or three, omit zero", text: "abba", node: &Node{ Name: "A", - to: 4, + To: 4, }, }, { msg: "zero or three, omit zero", text: "abbba", node: &Node{ Name: "A", - to: 5, + To: 5, }, }, { msg: "zero or three, omit zero, too much", @@ -600,14 +595,14 @@ func TestQuantifiers(t *testing.T) { text: "abba", node: &Node{ Name: "A", - to: 4, + To: 4, }, }, { msg: "one or three", text: "abbba", node: &Node{ Name: "A", - to: 5, + To: 5, }, }, { msg: "one or three, too much", @@ -628,14 +623,14 @@ func TestQuantifiers(t *testing.T) { text: "abbbba", node: &Node{ Name: "A", - to: 6, + To: 6, }, }, { msg: "three or five", text: "abbbbba", node: &Node{ Name: "A", - to: 7, + To: 7, }, }, { msg: "three or five, too much", @@ -652,14 +647,14 @@ func TestQuantifiers(t *testing.T) { text: "aa", node: &Node{ Name: "A", - to: 2, + To: 2, }, }, { msg: "zero or more, explicit", text: "abba", node: &Node{ Name: "A", - to: 4, + To: 4, }, }}, ) @@ -672,14 +667,14 @@ func TestQuantifiers(t *testing.T) { text: "aa", node: &Node{ Name: "A", - to: 2, + To: 2, }, }, { msg: "zero or more, shortcut", text: "abba", node: &Node{ Name: "A", - to: 4, + To: 4, }, }}, ) @@ -696,7 +691,7 @@ func TestQuantifiers(t *testing.T) { text: "abba", node: &Node{ Name: "A", - to: 4, + To: 4, }, }}, ) @@ -713,7 +708,7 @@ func TestQuantifiers(t *testing.T) { text: "abba", node: &Node{ Name: "A", - to: 4, + To: 4, }, }}, ) @@ -730,7 +725,7 @@ func TestQuantifiers(t *testing.T) { text: "abbbba", node: &Node{ Name: "A", - to: 6, + To: 6, }, }}, ) diff --git a/sequence.go b/sequence.go index ac9c3f5..be791cd 100644 --- a/sequence.go +++ b/sequence.go @@ -100,13 +100,13 @@ func (p *sequenceParser) setIncludedBy(includedBy parser, path []string) { } func (p *sequenceParser) cacheIncluded(c *context, n *Node) { - if !c.excluded(n.from, p.name) { + if !c.excluded(n.From, p.name) { return } - nc := newNode(p.name, p.commit, n.from, n.to) + nc := newNode(p.name, n.From, n.To, p.commit) nc.append(n) - c.cache.set(nc.from, p.name, nc) + c.cache.set(nc.From, p.name, nc) for _, includedBy := range p.includedBy { includedBy.cacheIncluded(c, nc) @@ -135,7 +135,7 @@ func (p *sequenceParser) parse(t Trace, c *context) { items := p.items ranges := p.ranges var currentCount int - node := newNode(p.name, p.commit, c.offset, c.offset) + node := newNode(p.name, c.offset, c.offset, p.commit) for len(items) > 0 { m, ok := c.fromCache(items[0].nodeName()) @@ -149,8 +149,8 @@ func (p *sequenceParser) parse(t Trace, c *context) { if !m { if currentCount < ranges[0][0] { // t.Out1("fail, item failed") - c.cache.set(node.from, p.name, nil) - c.fail(node.from) + c.cache.set(node.From, p.name, nil) + c.fail(node.From) return } @@ -174,7 +174,7 @@ func (p *sequenceParser) parse(t Trace, c *context) { // t.Out1("success, items parsed") - c.cache.set(node.from, p.name, node) + c.cache.set(node.From, p.name, node) for _, includedBy := range p.includedBy { includedBy.cacheIncluded(c, node) } diff --git a/syntax.go b/syntax.go index 051e3c1..08d5e55 100644 --- a/syntax.go +++ b/syntax.go @@ -36,10 +36,11 @@ var ( ErrInitFailed = errors.New("init failed") ErrNoParsersDefined = errors.New("no parsers defined") ErrInvalidInput = errors.New("invalid input") - ErrInvalidCharacter = errors.New("invalid character") // two use cases: utf8 and boot + ErrInvalidCharacter = errors.New("invalid character") // TODO: fix two use cases, utf8 and boot ErrUnexpectedCharacter = errors.New("unexpected character") ErrInvalidSyntax = errors.New("invalid syntax") ErrRootAlias = errors.New("root node cannot be an alias") + ErrNotImplemented = errors.New("not implemented") ) func duplicateDefinition(name string) error { @@ -114,7 +115,7 @@ func (s *Syntax) Read(r io.Reader) error { return ErrSyntaxInitialized } - return nil + return ErrNotImplemented } func (s *Syntax) Init() error { @@ -150,7 +151,7 @@ func (s *Syntax) Generate(w io.Writer) error { return err } - return nil + return ErrNotImplemented } func (s *Syntax) Parse(r io.Reader) (*Node, error) {