diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2021-02-20 17:36:16 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2021-02-20 17:36:16 +0900 |
commit | 9357758697305753a68b541b42452a8cb13eebe2 (patch) | |
tree | bc10befd50c9a0fd1c5ccc894e1e18cfbbdacb33 /compiler/ast.go | |
parent | Fix computation of last positions (diff) | |
download | tre-9357758697305753a68b541b42452a8cb13eebe2.tar.gz tre-9357758697305753a68b541b42452a8cb13eebe2.tar.xz |
Add + and ? operators
* a+ matches 'a' one or more times. This is equivalent to aa*.
* a? matches 'a' zero or one time.
Diffstat (limited to 'compiler/ast.go')
-rw-r--r-- | compiler/ast.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/compiler/ast.go b/compiler/ast.go index 17054d0..e4609ac 100644 --- a/compiler/ast.go +++ b/compiler/ast.go @@ -338,6 +338,48 @@ func (n *repeatNode) last() symbolPositionSet { return s } +func newRepeatOneOrMoreNode(left astNode) *concatNode { + return newConcatNode( + left, + &repeatNode{ + left: left, + }) +} + +type optionNode struct { + left astNode +} + +func newOptionNode(left astNode) *optionNode { + return &optionNode{ + left: left, + } +} + +func (n *optionNode) String() string { + return fmt.Sprintf("{type: option}") +} + +func (n *optionNode) children() (astNode, astNode) { + return n.left, nil +} + +func (n *optionNode) nullable() bool { + return true +} + +func (n *optionNode) first() symbolPositionSet { + s := newSymbolPositionSet() + s.merge(n.left.first()) + return s +} + +func (n *optionNode) last() symbolPositionSet { + s := newSymbolPositionSet() + s.merge(n.left.last()) + return s +} + type followTable map[symbolPosition]symbolPositionSet func genFollowTable(root astNode) followTable { |