diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2021-04-11 16:41:05 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2021-04-11 16:41:05 +0900 |
commit | a13655e3936ba9166051914832dedcbdb28b056c (patch) | |
tree | c6fcd27c4769aecf16d4f8d2a43f1297f62b6caa /compiler/ast.go | |
parent | Add logging to compile command (diff) | |
download | tre-a13655e3936ba9166051914832dedcbdb28b056c.tar.gz tre-a13655e3936ba9166051914832dedcbdb28b056c.tar.xz |
Fix grammar the parser accepts
* Add cases test the parse method.
* Fix the parser to pass the cases.
Diffstat (limited to 'compiler/ast.go')
-rw-r--r-- | compiler/ast.go | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/compiler/ast.go b/compiler/ast.go index e9c1b50..803d5a9 100644 --- a/compiler/ast.go +++ b/compiler/ast.go @@ -331,7 +331,7 @@ func newRepeatOneOrMoreNode(left astNode) *concatNode { return newConcatNode( left, &repeatNode{ - left: left, + left: copyAST(left), }) } @@ -369,6 +369,24 @@ func (n *optionNode) last() symbolPositionSet { return s } +func copyAST(src astNode) astNode { + switch n := src.(type) { + case *symbolNode: + return newRangeSymbolNode(n.from, n.to) + case *endMarkerNode: + return newEndMarkerNode(n.id) + case *concatNode: + return newConcatNode(copyAST(n.left), copyAST(n.right)) + case *altNode: + return newAltNode(copyAST(n.left), copyAST(n.right)) + case *repeatNode: + return newRepeatNode(copyAST(n.left)) + case *optionNode: + return newOptionNode(copyAST(n.left)) + } + panic(fmt.Errorf("copyAST cannot handle %T type; AST: %v", src, src)) +} + type followTable map[symbolPosition]symbolPositionSet func genFollowTable(root astNode) followTable { |