From 520bf02582be7ab36b17fd78f8931cfdb702b07f Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Tue, 25 May 2021 21:55:17 +0900 Subject: Add fragment expression A fragment entry is defined by an entry whose `fragment` field is `true`, and is referenced by a fragment expression (`\f{...}`). --- compiler/ast.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'compiler/ast.go') diff --git a/compiler/ast.go b/compiler/ast.go index a419f98..7d3965a 100644 --- a/compiler/ast.go +++ b/compiler/ast.go @@ -19,6 +19,7 @@ var ( _ astNode = &concatNode{} _ astNode = &altNode{} _ astNode = &optionNode{} + _ astNode = &fragmentNode{} ) type symbolNode struct { @@ -306,6 +307,38 @@ func (n *optionNode) last() *symbolPositionSet { return n.lastMemo } +type fragmentNode struct { + symbol string + left astNode +} + +func newFragmentNode(symbol string, ast astNode) *fragmentNode { + return &fragmentNode{ + symbol: symbol, + left: ast, + } +} + +func (n *fragmentNode) String() string { + return fmt.Sprintf("{type: fragment, symbol: %v}", n.symbol) +} + +func (n *fragmentNode) children() (astNode, astNode) { + return n.left, nil +} + +func (n *fragmentNode) nullable() bool { + return n.left.nullable() +} + +func (n *fragmentNode) first() *symbolPositionSet { + return n.left.first() +} + +func (n *fragmentNode) last() *symbolPositionSet { + return n.left.last() +} + func copyAST(src astNode) astNode { switch n := src.(type) { case *symbolNode: @@ -320,6 +353,11 @@ func copyAST(src astNode) astNode { return newRepeatNode(copyAST(n.left)) case *optionNode: return newOptionNode(copyAST(n.left)) + case *fragmentNode: + if n.left == nil { + return newFragmentNode(n.symbol, nil) + } + return newFragmentNode(n.symbol, copyAST(n.left)) } panic(fmt.Errorf("copyAST cannot handle %T type; AST: %v", src, src)) } -- cgit v1.2.3