825 lines
29 KiB
Go
825 lines
29 KiB
Go
|
|
/*
|
|
This file was generated with treerack (https://code.squareroundforest.org/arpio/treerack).
|
|
|
|
The contents of this file fall under different licenses.
|
|
|
|
The code between the "// head" and "// eo head" lines falls under the same
|
|
license as the source code of treerack (https://code.squareroundforest.org/arpio/treerack),
|
|
unless explicitly stated otherwise, if treerack's license allows changing the
|
|
license of this source code.
|
|
|
|
Treerack's license: MIT https://opensource.org/licenses/MIT
|
|
where YEAR=2017, COPYRIGHT HOLDER=Arpad Ryszka (arpad.ryszka@gmail.com)
|
|
|
|
The rest of the content of this file falls under the same license as the one
|
|
that the user of treerack generating this file declares for it, or it is
|
|
unlicensed.
|
|
*/
|
|
|
|
|
|
package main
|
|
|
|
// head
|
|
import (
|
|
"strconv"
|
|
"errors"
|
|
"io"
|
|
"strings"
|
|
"unicode"
|
|
"fmt"
|
|
"bufio"
|
|
)
|
|
|
|
type charParser struct {
|
|
name string
|
|
id int
|
|
not bool
|
|
chars []rune
|
|
ranges [][]rune
|
|
}
|
|
type charBuilder struct {
|
|
name string
|
|
id int
|
|
}
|
|
|
|
func (p *charParser) nodeName() string {
|
|
return p.name
|
|
}
|
|
func (p *charParser) nodeID() int {
|
|
return p.id
|
|
}
|
|
func (p *charParser) commitType() commitType {
|
|
return alias
|
|
}
|
|
func matchChar(chars []rune, ranges [][]rune, not bool, char rune) bool {
|
|
for _, ci := range chars {
|
|
if ci == char {
|
|
return !not
|
|
}
|
|
}
|
|
for _, ri := range ranges {
|
|
if char >= ri[0] && char <= ri[1] {
|
|
return !not
|
|
}
|
|
}
|
|
return not
|
|
}
|
|
func (p *charParser) match(t rune) bool {
|
|
return matchChar(p.chars, p.ranges, p.not, t)
|
|
}
|
|
func (p *charParser) parse(c *context) {
|
|
if tok, ok := c.token(); !ok || !p.match(tok) {
|
|
if c.offset > c.failOffset {
|
|
c.failOffset = c.offset
|
|
c.failingParser = nil
|
|
}
|
|
c.fail(c.offset)
|
|
return
|
|
}
|
|
c.success(c.offset + 1)
|
|
}
|
|
func (b *charBuilder) nodeName() string {
|
|
return b.name
|
|
}
|
|
func (b *charBuilder) nodeID() int {
|
|
return b.id
|
|
}
|
|
func (b *charBuilder) build(c *context) ([]*node, bool) {
|
|
return nil, false
|
|
}
|
|
|
|
type sequenceParser struct {
|
|
name string
|
|
id int
|
|
commit commitType
|
|
items []parser
|
|
ranges [][]int
|
|
generalizations []int
|
|
allChars bool
|
|
}
|
|
type sequenceBuilder struct {
|
|
name string
|
|
id int
|
|
commit commitType
|
|
items []builder
|
|
ranges [][]int
|
|
generalizations []int
|
|
allChars bool
|
|
}
|
|
|
|
func (p *sequenceParser) nodeName() string {
|
|
return p.name
|
|
}
|
|
func (p *sequenceParser) nodeID() int {
|
|
return p.id
|
|
}
|
|
func (p *sequenceParser) commitType() commitType {
|
|
return p.commit
|
|
}
|
|
func (p *sequenceParser) parse(c *context) {
|
|
if !p.allChars {
|
|
if c.results.pending(c.offset, p.id) {
|
|
c.fail(c.offset)
|
|
return
|
|
}
|
|
c.results.markPending(c.offset, p.id)
|
|
}
|
|
var (
|
|
currentCount int
|
|
parsed bool
|
|
)
|
|
itemIndex := 0
|
|
from := c.offset
|
|
to := c.offset
|
|
for itemIndex < len(p.items) {
|
|
p.items[itemIndex].parse(c)
|
|
if !c.matchLast {
|
|
if currentCount >= p.ranges[itemIndex][0] {
|
|
itemIndex++
|
|
currentCount = 0
|
|
continue
|
|
}
|
|
c.offset = from
|
|
if c.fromResults(p) {
|
|
if to > c.failOffset {
|
|
c.failOffset = -1
|
|
c.failingParser = nil
|
|
}
|
|
if !p.allChars {
|
|
c.results.unmarkPending(from, p.id)
|
|
}
|
|
return
|
|
}
|
|
if c.failingParser == nil && p.commit&userDefined != 0 && p.commit&whitespace == 0 && p.commit&failPass == 0 {
|
|
c.failingParser = p
|
|
}
|
|
c.fail(from)
|
|
if !p.allChars {
|
|
c.results.unmarkPending(from, p.id)
|
|
}
|
|
return
|
|
}
|
|
parsed = c.offset > to
|
|
if parsed {
|
|
currentCount++
|
|
}
|
|
to = c.offset
|
|
if !parsed || p.ranges[itemIndex][1] > 0 && currentCount == p.ranges[itemIndex][1] {
|
|
itemIndex++
|
|
currentCount = 0
|
|
}
|
|
}
|
|
if p.commit&noKeyword != 0 && c.isKeyword(from, to) {
|
|
if c.failingParser == nil && p.commit&userDefined != 0 && p.commit&whitespace == 0 && p.commit&failPass == 0 {
|
|
c.failingParser = p
|
|
}
|
|
c.fail(from)
|
|
if !p.allChars {
|
|
c.results.unmarkPending(from, p.id)
|
|
}
|
|
return
|
|
}
|
|
for _, g := range p.generalizations {
|
|
if c.results.pending(from, g) {
|
|
c.results.setMatch(from, g, to)
|
|
}
|
|
}
|
|
if to > c.failOffset {
|
|
c.failOffset = -1
|
|
c.failingParser = nil
|
|
}
|
|
c.results.setMatch(from, p.id, to)
|
|
c.success(to)
|
|
if !p.allChars {
|
|
c.results.unmarkPending(from, p.id)
|
|
}
|
|
}
|
|
func (b *sequenceBuilder) nodeName() string {
|
|
return b.name
|
|
}
|
|
func (b *sequenceBuilder) nodeID() int {
|
|
return b.id
|
|
}
|
|
func (b *sequenceBuilder) build(c *context) ([]*node, bool) {
|
|
to, ok := c.results.longestMatch(c.offset, b.id)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
from := c.offset
|
|
parsed := to > from
|
|
if b.allChars {
|
|
c.offset = to
|
|
if b.commit&alias != 0 {
|
|
return nil, true
|
|
}
|
|
return []*node{{Name: b.name, From: from, To: to, tokens: c.tokens}}, true
|
|
} else if parsed {
|
|
c.results.dropMatchTo(c.offset, b.id, to)
|
|
for _, g := range b.generalizations {
|
|
c.results.dropMatchTo(c.offset, g, to)
|
|
}
|
|
} else {
|
|
if c.results.pending(c.offset, b.id) {
|
|
return nil, false
|
|
}
|
|
c.results.markPending(c.offset, b.id)
|
|
for _, g := range b.generalizations {
|
|
c.results.markPending(c.offset, g)
|
|
}
|
|
}
|
|
var (
|
|
itemIndex int
|
|
currentCount int
|
|
nodes []*node
|
|
)
|
|
for itemIndex < len(b.items) {
|
|
itemFrom := c.offset
|
|
n, ok := b.items[itemIndex].build(c)
|
|
if !ok {
|
|
itemIndex++
|
|
currentCount = 0
|
|
continue
|
|
}
|
|
if c.offset > itemFrom {
|
|
nodes = append(nodes, n...)
|
|
currentCount++
|
|
if b.ranges[itemIndex][1] > 0 && currentCount == b.ranges[itemIndex][1] {
|
|
itemIndex++
|
|
currentCount = 0
|
|
}
|
|
continue
|
|
}
|
|
if currentCount < b.ranges[itemIndex][0] {
|
|
for i := 0; i < b.ranges[itemIndex][0]-currentCount; i++ {
|
|
nodes = append(nodes, n...)
|
|
}
|
|
}
|
|
itemIndex++
|
|
currentCount = 0
|
|
}
|
|
if !parsed {
|
|
c.results.unmarkPending(from, b.id)
|
|
for _, g := range b.generalizations {
|
|
c.results.unmarkPending(from, g)
|
|
}
|
|
}
|
|
if b.commit&alias != 0 {
|
|
return nodes, true
|
|
}
|
|
return []*node{{Name: b.name, From: from, To: to, Nodes: nodes, tokens: c.tokens}}, true
|
|
}
|
|
|
|
type choiceParser struct {
|
|
name string
|
|
id int
|
|
commit commitType
|
|
options []parser
|
|
generalizations []int
|
|
}
|
|
type choiceBuilder struct {
|
|
name string
|
|
id int
|
|
commit commitType
|
|
options []builder
|
|
generalizations []int
|
|
}
|
|
|
|
func (p *choiceParser) nodeName() string {
|
|
return p.name
|
|
}
|
|
func (p *choiceParser) nodeID() int {
|
|
return p.id
|
|
}
|
|
func (p *choiceParser) commitType() commitType {
|
|
return p.commit
|
|
}
|
|
func (p *choiceParser) parse(c *context) {
|
|
if c.fromResults(p) {
|
|
return
|
|
}
|
|
if c.results.pending(c.offset, p.id) {
|
|
c.fail(c.offset)
|
|
return
|
|
}
|
|
c.results.markPending(c.offset, p.id)
|
|
var (
|
|
match bool
|
|
optionIndex int
|
|
foundMatch bool
|
|
failingParser parser
|
|
)
|
|
from := c.offset
|
|
to := c.offset
|
|
initialFailOffset := c.failOffset
|
|
initialFailingParser := c.failingParser
|
|
failOffset := initialFailOffset
|
|
for {
|
|
foundMatch = false
|
|
optionIndex = 0
|
|
for optionIndex < len(p.options) {
|
|
p.options[optionIndex].parse(c)
|
|
optionIndex++
|
|
if !c.matchLast {
|
|
if c.failOffset > failOffset {
|
|
failOffset = c.failOffset
|
|
failingParser = c.failingParser
|
|
}
|
|
}
|
|
if !c.matchLast || match && c.offset <= to {
|
|
c.offset = from
|
|
continue
|
|
}
|
|
match = true
|
|
foundMatch = true
|
|
to = c.offset
|
|
c.offset = from
|
|
c.results.setMatch(from, p.id, to)
|
|
}
|
|
if !foundMatch {
|
|
break
|
|
}
|
|
}
|
|
if match {
|
|
if p.commit&noKeyword != 0 && c.isKeyword(from, to) {
|
|
if c.failingParser == nil && p.commit&userDefined != 0 && p.commit&whitespace == 0 && p.commit&failPass == 0 {
|
|
c.failingParser = p
|
|
}
|
|
c.fail(from)
|
|
c.results.unmarkPending(from, p.id)
|
|
return
|
|
}
|
|
if failOffset > to {
|
|
c.failOffset = failOffset
|
|
c.failingParser = failingParser
|
|
} else if to > initialFailOffset {
|
|
c.failOffset = -1
|
|
c.failingParser = nil
|
|
} else {
|
|
c.failOffset = initialFailOffset
|
|
c.failingParser = initialFailingParser
|
|
}
|
|
c.success(to)
|
|
c.results.unmarkPending(from, p.id)
|
|
return
|
|
}
|
|
if failOffset > initialFailOffset {
|
|
c.failOffset = failOffset
|
|
c.failingParser = failingParser
|
|
if c.failingParser == nil && p.commitType()&userDefined != 0 && p.commitType()&whitespace == 0 && p.commitType()&failPass == 0 {
|
|
c.failingParser = p
|
|
}
|
|
}
|
|
c.results.setNoMatch(from, p.id)
|
|
c.fail(from)
|
|
c.results.unmarkPending(from, p.id)
|
|
}
|
|
func (b *choiceBuilder) nodeName() string {
|
|
return b.name
|
|
}
|
|
func (b *choiceBuilder) nodeID() int {
|
|
return b.id
|
|
}
|
|
func (b *choiceBuilder) build(c *context) ([]*node, bool) {
|
|
to, ok := c.results.longestMatch(c.offset, b.id)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
from := c.offset
|
|
parsed := to > from
|
|
if parsed {
|
|
c.results.dropMatchTo(c.offset, b.id, to)
|
|
for _, g := range b.generalizations {
|
|
c.results.dropMatchTo(c.offset, g, to)
|
|
}
|
|
} else {
|
|
if c.results.pending(c.offset, b.id) {
|
|
return nil, false
|
|
}
|
|
c.results.markPending(c.offset, b.id)
|
|
for _, g := range b.generalizations {
|
|
c.results.markPending(c.offset, g)
|
|
}
|
|
}
|
|
var option builder
|
|
for _, o := range b.options {
|
|
if c.results.hasMatchTo(c.offset, o.nodeID(), to) {
|
|
option = o
|
|
break
|
|
}
|
|
}
|
|
n, _ := option.build(c)
|
|
if !parsed {
|
|
c.results.unmarkPending(from, b.id)
|
|
for _, g := range b.generalizations {
|
|
c.results.unmarkPending(from, g)
|
|
}
|
|
}
|
|
if b.commit&alias != 0 {
|
|
return n, true
|
|
}
|
|
return []*node{{Name: b.name, From: from, To: to, Nodes: n, tokens: c.tokens}}, true
|
|
}
|
|
|
|
type idSet struct{ ids []uint }
|
|
|
|
func divModBits(id int) (int, int) {
|
|
return id / strconv.IntSize, id % strconv.IntSize
|
|
}
|
|
func (s *idSet) set(id int) {
|
|
d, m := divModBits(id)
|
|
if d >= len(s.ids) {
|
|
if d < cap(s.ids) {
|
|
s.ids = s.ids[:d+1]
|
|
} else {
|
|
s.ids = s.ids[:cap(s.ids)]
|
|
for i := cap(s.ids); i <= d; i++ {
|
|
s.ids = append(s.ids, 0)
|
|
}
|
|
}
|
|
}
|
|
s.ids[d] |= 1 << uint(m)
|
|
}
|
|
func (s *idSet) unset(id int) {
|
|
d, m := divModBits(id)
|
|
if d >= len(s.ids) {
|
|
return
|
|
}
|
|
s.ids[d] &^= 1 << uint(m)
|
|
}
|
|
func (s *idSet) has(id int) bool {
|
|
d, m := divModBits(id)
|
|
if d >= len(s.ids) {
|
|
return false
|
|
}
|
|
return s.ids[d]&(1<<uint(m)) != 0
|
|
}
|
|
|
|
type results struct {
|
|
noMatch []*idSet
|
|
match [][]int
|
|
isPending [][]int
|
|
}
|
|
|
|
func ensureOffsetInts(ints [][]int, offset int) [][]int {
|
|
if len(ints) > offset {
|
|
return ints
|
|
}
|
|
if cap(ints) > offset {
|
|
ints = ints[:offset+1]
|
|
return ints
|
|
}
|
|
ints = ints[:cap(ints)]
|
|
for i := len(ints); i <= offset; i++ {
|
|
ints = append(ints, nil)
|
|
}
|
|
return ints
|
|
}
|
|
func ensureOffsetIDs(ids []*idSet, offset int) []*idSet {
|
|
if len(ids) > offset {
|
|
return ids
|
|
}
|
|
if cap(ids) > offset {
|
|
ids = ids[:offset+1]
|
|
return ids
|
|
}
|
|
ids = ids[:cap(ids)]
|
|
for i := len(ids); i <= offset; i++ {
|
|
ids = append(ids, nil)
|
|
}
|
|
return ids
|
|
}
|
|
func (r *results) setMatch(offset, id, to int) {
|
|
r.match = ensureOffsetInts(r.match, offset)
|
|
for i := 0; i < len(r.match[offset]); i += 2 {
|
|
if r.match[offset][i] != id || r.match[offset][i+1] != to {
|
|
continue
|
|
}
|
|
return
|
|
}
|
|
r.match[offset] = append(r.match[offset], id, to)
|
|
}
|
|
func (r *results) setNoMatch(offset, id int) {
|
|
if len(r.match) > offset {
|
|
for i := 0; i < len(r.match[offset]); i += 2 {
|
|
if r.match[offset][i] != id {
|
|
continue
|
|
}
|
|
return
|
|
}
|
|
}
|
|
r.noMatch = ensureOffsetIDs(r.noMatch, offset)
|
|
if r.noMatch[offset] == nil {
|
|
r.noMatch[offset] = &idSet{}
|
|
}
|
|
r.noMatch[offset].set(id)
|
|
}
|
|
func (r *results) hasMatchTo(offset, id, to int) bool {
|
|
if len(r.match) <= offset {
|
|
return false
|
|
}
|
|
for i := 0; i < len(r.match[offset]); i += 2 {
|
|
if r.match[offset][i] != id {
|
|
continue
|
|
}
|
|
if r.match[offset][i+1] == to {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
func (r *results) longestMatch(offset, id int) (int, bool) {
|
|
if len(r.match) <= offset {
|
|
return 0, false
|
|
}
|
|
var found bool
|
|
to := -1
|
|
for i := 0; i < len(r.match[offset]); i += 2 {
|
|
if r.match[offset][i] != id {
|
|
continue
|
|
}
|
|
if r.match[offset][i+1] > to {
|
|
to = r.match[offset][i+1]
|
|
}
|
|
found = true
|
|
}
|
|
return to, found
|
|
}
|
|
func (r *results) longestResult(offset, id int) (int, bool, bool) {
|
|
if len(r.noMatch) > offset && r.noMatch[offset] != nil && r.noMatch[offset].has(id) {
|
|
return 0, false, true
|
|
}
|
|
to, ok := r.longestMatch(offset, id)
|
|
return to, ok, ok
|
|
}
|
|
func (r *results) dropMatchTo(offset, id, to int) {
|
|
for i := 0; i < len(r.match[offset]); i += 2 {
|
|
if r.match[offset][i] != id {
|
|
continue
|
|
}
|
|
if r.match[offset][i+1] == to {
|
|
r.match[offset][i] = -1
|
|
return
|
|
}
|
|
}
|
|
}
|
|
func (r *results) resetPending() {
|
|
r.isPending = nil
|
|
}
|
|
func (r *results) pending(offset, id int) bool {
|
|
if len(r.isPending) <= id {
|
|
return false
|
|
}
|
|
for i := range r.isPending[id] {
|
|
if r.isPending[id][i] == offset {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
func (r *results) markPending(offset, id int) {
|
|
r.isPending = ensureOffsetInts(r.isPending, id)
|
|
for i := range r.isPending[id] {
|
|
if r.isPending[id][i] == -1 {
|
|
r.isPending[id][i] = offset
|
|
return
|
|
}
|
|
}
|
|
r.isPending[id] = append(r.isPending[id], offset)
|
|
}
|
|
func (r *results) unmarkPending(offset, id int) {
|
|
for i := range r.isPending[id] {
|
|
if r.isPending[id][i] == offset {
|
|
r.isPending[id][i] = -1
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
type context struct {
|
|
reader io.RuneReader
|
|
keywords []parser
|
|
offset int
|
|
readOffset int
|
|
consumed int
|
|
offsetLimit int
|
|
failOffset int
|
|
failingParser parser
|
|
readErr error
|
|
eof bool
|
|
results *results
|
|
tokens []rune
|
|
matchLast bool
|
|
}
|
|
|
|
func newContext(r io.RuneReader, keywords []parser) *context {
|
|
return &context{reader: r, keywords: keywords, results: &results{}, offsetLimit: -1, failOffset: -1}
|
|
}
|
|
func (c *context) read() bool {
|
|
if c.eof || c.readErr != nil {
|
|
return false
|
|
}
|
|
token, n, err := c.reader.ReadRune()
|
|
if err != nil {
|
|
if errors.Is(err, io.EOF) {
|
|
if n == 0 {
|
|
c.eof = true
|
|
return false
|
|
}
|
|
} else {
|
|
c.readErr = err
|
|
return false
|
|
}
|
|
}
|
|
c.readOffset++
|
|
if token == unicode.ReplacementChar {
|
|
c.readErr = errInvalidUnicodeCharacter
|
|
return false
|
|
}
|
|
c.tokens = append(c.tokens, token)
|
|
return true
|
|
}
|
|
func (c *context) token() (rune, bool) {
|
|
if c.offset == c.offsetLimit {
|
|
return 0, false
|
|
}
|
|
if c.offset == c.readOffset {
|
|
if !c.read() {
|
|
return 0, false
|
|
}
|
|
}
|
|
return c.tokens[c.offset], true
|
|
}
|
|
func (c *context) fromResults(p parser) bool {
|
|
to, m, ok := c.results.longestResult(c.offset, p.nodeID())
|
|
if !ok {
|
|
return false
|
|
}
|
|
if m {
|
|
c.success(to)
|
|
} else {
|
|
c.fail(c.offset)
|
|
}
|
|
return true
|
|
}
|
|
func (c *context) isKeyword(from, to int) bool {
|
|
ol := c.offsetLimit
|
|
c.offsetLimit = to
|
|
defer func() {
|
|
c.offsetLimit = ol
|
|
}()
|
|
for _, kw := range c.keywords {
|
|
c.offset = from
|
|
kw.parse(c)
|
|
if c.matchLast && c.offset == to {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
func (c *context) success(to int) {
|
|
c.offset = to
|
|
c.matchLast = true
|
|
if to > c.consumed {
|
|
c.consumed = to
|
|
}
|
|
}
|
|
func (c *context) fail(offset int) {
|
|
c.offset = offset
|
|
c.matchLast = false
|
|
}
|
|
func findLine(tokens []rune, offset int) (line, column int) {
|
|
tokens = tokens[:offset]
|
|
for i := range tokens {
|
|
column++
|
|
if tokens[i] == '\n' {
|
|
column = 0
|
|
line++
|
|
}
|
|
}
|
|
return
|
|
}
|
|
func (c *context) parseError(p parser) error {
|
|
definition := p.nodeName()
|
|
flagIndex := strings.Index(definition, ":")
|
|
if flagIndex > 0 {
|
|
definition = definition[:flagIndex]
|
|
}
|
|
if c.failingParser == nil {
|
|
c.failOffset = c.consumed
|
|
}
|
|
line, col := findLine(c.tokens, c.failOffset)
|
|
return &parseError{Offset: c.failOffset, Line: line, Column: col, Definition: definition}
|
|
}
|
|
func (c *context) finalizeParse(root parser) error {
|
|
fp := c.failingParser
|
|
if fp == nil {
|
|
fp = root
|
|
}
|
|
to, match, found := c.results.longestResult(0, root.nodeID())
|
|
if !found || !match || found && match && to < c.readOffset {
|
|
return c.parseError(fp)
|
|
}
|
|
c.read()
|
|
if c.eof {
|
|
return nil
|
|
}
|
|
if c.readErr != nil {
|
|
return c.readErr
|
|
}
|
|
return c.parseError(root)
|
|
}
|
|
|
|
type node struct {
|
|
Name string
|
|
Nodes []*node
|
|
From int
|
|
To int
|
|
tokens []rune
|
|
}
|
|
|
|
func (n *node) Tokens() []rune {
|
|
return n.tokens
|
|
}
|
|
func (n *node) String() string {
|
|
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])
|
|
}
|
|
|
|
type commitType int
|
|
|
|
const (
|
|
none commitType = 0
|
|
alias commitType = 1 << iota
|
|
whitespace
|
|
noWhitespace
|
|
keyword
|
|
noKeyword
|
|
failPass
|
|
root
|
|
userDefined
|
|
)
|
|
|
|
type formatFlags int
|
|
|
|
const (
|
|
formatNone formatFlags = 0
|
|
formatPretty formatFlags = 1 << iota
|
|
formatIncludeComments
|
|
)
|
|
|
|
type parseError struct {
|
|
Input string
|
|
Offset int
|
|
Line int
|
|
Column int
|
|
Definition string
|
|
}
|
|
type parser interface {
|
|
nodeName() string
|
|
nodeID() int
|
|
commitType() commitType
|
|
parse(*context)
|
|
}
|
|
type builder interface {
|
|
nodeName() string
|
|
nodeID() int
|
|
build(*context) ([]*node, bool)
|
|
}
|
|
|
|
var errInvalidUnicodeCharacter = errors.New("invalid unicode character")
|
|
|
|
func (pe *parseError) Error() string {
|
|
return fmt.Sprintf("%s:%d:%d:parse failed, parsing: %s", pe.Input, pe.Line+1, pe.Column+1, pe.Definition)
|
|
}
|
|
func parseInput(r io.Reader, p parser, b builder, kw []parser) (*node, error) {
|
|
c := newContext(bufio.NewReader(r), kw)
|
|
p.parse(c)
|
|
if c.readErr != nil {
|
|
return nil, c.readErr
|
|
}
|
|
if err := c.finalizeParse(p); err != nil {
|
|
if perr, ok := err.(*parseError); ok {
|
|
perr.Input = "<input>"
|
|
}
|
|
return nil, err
|
|
}
|
|
c.offset = 0
|
|
c.results.resetPending()
|
|
n, _ := b.build(c)
|
|
return n[0], nil
|
|
}
|
|
|
|
// eo head
|
|
|
|
func parse(r io.Reader) (*node, error) {
|
|
|
|
var p65 = sequenceParser{id: 65, commit: 128,ranges: [][]int{{0, -1},{1, 1},{0, -1},},};var p63 = choiceParser{id: 63, commit: 2,};var p62 = choiceParser{id: 62, commit: 262,name: "ignore",generalizations: []int{63,},};var p2 = sequenceParser{id: 2, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},generalizations: []int{62,63,},};var p1 = charParser{id: 1,chars: []rune{32,},};p2.items = []parser{&p1,};var p4 = sequenceParser{id: 4, commit: 2,allChars: true,ranges: [][]int{{1, 1},},generalizations: []int{62,63,},};var p3 = charParser{id: 3,chars: []rune{9,},};p4.items = []parser{&p3,};var p6 = sequenceParser{id: 6, commit: 2,allChars: true,ranges: [][]int{{1, 1},},generalizations: []int{62,63,},};var p5 = charParser{id: 5,chars: []rune{13,},};p6.items = []parser{&p5,};var p8 = sequenceParser{id: 8, commit: 2,allChars: true,ranges: [][]int{{1, 1},},generalizations: []int{62,63,},};var p7 = charParser{id: 7,chars: []rune{10,},};p8.items = []parser{&p7,};p62.options = []parser{&p2,&p4,&p6,&p8,};p63.options = []parser{&p62,};var p64 = choiceParser{id: 64, commit: 258,name: "result:wsroot",};var p56 = choiceParser{id: 56, commit: 258,name: "expression",generalizations: []int{64,},};var p31 = sequenceParser{id: 31, commit: 264,name: "num",ranges: [][]int{{0, 1},{1, 1},{0, 1},{0, 1},{0, 1},{1, 1},{0, 1},{0, 1},},generalizations: []int{56,47,48,64,},};var p10 = sequenceParser{id: 10, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var p9 = charParser{id: 9,chars: []rune{45,},};p10.items = []parser{&p9,};var p18 = choiceParser{id: 18, commit: 10,};var p12 = sequenceParser{id: 12, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},generalizations: []int{18,},};var p11 = charParser{id: 11,chars: []rune{48,},};p12.items = []parser{&p11,};var p17 = sequenceParser{id: 17, commit: 10,ranges: [][]int{{1, 1},{0, -1},{1, 1},{0, -1},},generalizations: []int{18,},};var p14 = sequenceParser{id: 14, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var p13 = charParser{id: 13,ranges: [][]rune{{49, 57},},};p14.items = []parser{&p13,};var p16 = sequenceParser{id: 16, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var p15 = charParser{id: 15,ranges: [][]rune{{48, 57},},};p16.items = []parser{&p15,};p17.items = []parser{&p14,&p16,};p18.options = []parser{&p12,&p17,};var p23 = sequenceParser{id: 23, commit: 10,ranges: [][]int{{1, 1},{1, -1},{1, 1},{1, -1},},};var p20 = sequenceParser{id: 20, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var p19 = charParser{id: 19,chars: []rune{46,},};p20.items = []parser{&p19,};var p22 = sequenceParser{id: 22, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var p21 = charParser{id: 21,ranges: [][]rune{{48, 57},},};p22.items = []parser{&p21,};p23.items = []parser{&p20,&p22,};var p30 = sequenceParser{id: 30, commit: 10,ranges: [][]int{{1, 1},{0, 1},{1, -1},{1, 1},{0, 1},{1, -1},},};var p25 = sequenceParser{id: 25, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var p24 = charParser{id: 24,chars: []rune{101,69,},};p25.items = []parser{&p24,};var p27 = sequenceParser{id: 27, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var p26 = charParser{id: 26,chars: []rune{43,45,},};p27.items = []parser{&p26,};var p29 = sequenceParser{id: 29, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var p28 = charParser{id: 28,ranges: [][]rune{{48, 57},},};p29.items = []parser{&p28,};p30.items = []parser{&p25,&p27,&p29,};p31.items = []parser{&p10,&p18,&p23,&p30,};var p44 = sequenceParser{id: 44, commit: 258,name: "group",ranges: [][]int{{1, 1},{0, -1},{1, 1},{0, -1},{1, 1},},generalizations: []int{56,47,48,64,},};var p41 = sequenceParser{id: 41, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var p40 = charParser{id: 40,chars: []rune{40,},};p41.items = []parser{&p40,};var p43 = sequenceParser{id: 43, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var p42 = charParser{id: 42,chars: []rune{41,},};p43.items = []parser{&p42,};p44.items = []parser{&p41,&p63,&p56,&p63,&p43,};var p55 = choiceParser{id: 55, commit: 258,name: "binary",generalizations: []int{56,64,},};var p51 = sequenceParser{id: 51, commit: 256,name: "binary0",ranges: [][]int{{1, 1},{0, -1},{1, 1},{0, -1},},generalizations: []int{55,48,56,64,},};var p47 = choiceParser{id: 47, commit: 258,name: "operand0",generalizations: []int{48,},};p47.options = []parser{&p31,&p44,};var p49 = sequenceParser{id: 49, commit: 2,ranges: [][]int{{1, 1},{0, -1},{1, 1},},};var p45 = choiceParser{id: 45, commit: 258,name: "op0",};var p37 = sequenceParser{id: 37, commit: 264,name: "mul",allChars: true,ranges: [][]int{{1, 1},{1, 1},},generalizations: []int{45,},};var p36 = charParser{id: 36,chars: []rune{42,},};p37.items = []parser{&p36,};var p39 = sequenceParser{id: 39, commit: 264,name: "div",allChars: true,ranges: [][]int{{1, 1},{1, 1},},generalizations: []int{45,},};var p38 = charParser{id: 38,chars: []rune{47,},};p39.items = []parser{&p38,};p45.options = []parser{&p37,&p39,};p49.items = []parser{&p45,&p63,&p47,};var p50 = sequenceParser{id: 50, commit: 2,ranges: [][]int{{0, -1},{1, 1},},};p50.items = []parser{&p63,&p49,};p51.items = []parser{&p47,&p63,&p49,&p50,};var p54 = sequenceParser{id: 54, commit: 256,name: "binary1",ranges: [][]int{{1, 1},{0, -1},{1, 1},{0, -1},},generalizations: []int{55,56,64,},};var p48 = choiceParser{id: 48, commit: 258,name: "operand1",};p48.options = []parser{&p47,&p51,};var p52 = sequenceParser{id: 52, commit: 2,ranges: [][]int{{1, 1},{0, -1},{1, 1},},};var p46 = choiceParser{id: 46, commit: 258,name: "op1",};var p33 = sequenceParser{id: 33, commit: 264,name: "add",allChars: true,ranges: [][]int{{1, 1},{1, 1},},generalizations: []int{46,},};var p32 = charParser{id: 32,chars: []rune{43,},};p33.items = []parser{&p32,};var p35 = sequenceParser{id: 35, commit: 264,name: "sub",allChars: true,ranges: [][]int{{1, 1},{1, 1},},generalizations: []int{46,},};var p34 = charParser{id: 34,chars: []rune{45,},};p35.items = []parser{&p34,};p46.options = []parser{&p33,&p35,};p52.items = []parser{&p46,&p63,&p48,};var p53 = sequenceParser{id: 53, commit: 2,ranges: [][]int{{0, -1},{1, 1},},};p53.items = []parser{&p63,&p52,};p54.items = []parser{&p48,&p63,&p52,&p53,};p55.options = []parser{&p51,&p54,};p56.options = []parser{&p31,&p44,&p55,};var p61 = sequenceParser{id: 61, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},{1, 1},{1, 1},{1, 1},{1, 1},{1, 1},{1, 1},},generalizations: []int{64,},};var p57 = charParser{id: 57,chars: []rune{101,},};var p58 = charParser{id: 58,chars: []rune{120,},};var p59 = charParser{id: 59,chars: []rune{105,},};var p60 = charParser{id: 60,chars: []rune{116,},};p61.items = []parser{&p57,&p58,&p59,&p60,};p64.options = []parser{&p56,&p61,};p65.items = []parser{&p63,&p64,&p63,};var b65 = sequenceBuilder{id: 65, commit: 128,name: "result",ranges: [][]int{{0, -1},{1, 1},{0, -1},},};var b63 = choiceBuilder{id: 63, commit: 2,};var b62 = choiceBuilder{id: 62, commit: 262,generalizations: []int{63,},};var b2 = sequenceBuilder{id: 2, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},generalizations: []int{62,63,},};var b1 = charBuilder{};b2.items = []builder{&b1,};var b4 = sequenceBuilder{id: 4, commit: 2,allChars: true,ranges: [][]int{{1, 1},},generalizations: []int{62,63,},};var b3 = charBuilder{};b4.items = []builder{&b3,};var b6 = sequenceBuilder{id: 6, commit: 2,allChars: true,ranges: [][]int{{1, 1},},generalizations: []int{62,63,},};var b5 = charBuilder{};b6.items = []builder{&b5,};var b8 = sequenceBuilder{id: 8, commit: 2,allChars: true,ranges: [][]int{{1, 1},},generalizations: []int{62,63,},};var b7 = charBuilder{};b8.items = []builder{&b7,};b62.options = []builder{&b2,&b4,&b6,&b8,};b63.options = []builder{&b62,};var b64 = choiceBuilder{id: 64, commit: 258,};var b56 = choiceBuilder{id: 56, commit: 258,generalizations: []int{64,},};var b31 = sequenceBuilder{id: 31, commit: 264,name: "num",ranges: [][]int{{0, 1},{1, 1},{0, 1},{0, 1},{0, 1},{1, 1},{0, 1},{0, 1},},generalizations: []int{56,47,48,64,},};var b10 = sequenceBuilder{id: 10, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var b9 = charBuilder{};b10.items = []builder{&b9,};var b18 = choiceBuilder{id: 18, commit: 10,};var b12 = sequenceBuilder{id: 12, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},generalizations: []int{18,},};var b11 = charBuilder{};b12.items = []builder{&b11,};var b17 = sequenceBuilder{id: 17, commit: 10,ranges: [][]int{{1, 1},{0, -1},{1, 1},{0, -1},},generalizations: []int{18,},};var b14 = sequenceBuilder{id: 14, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var b13 = charBuilder{};b14.items = []builder{&b13,};var b16 = sequenceBuilder{id: 16, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var b15 = charBuilder{};b16.items = []builder{&b15,};b17.items = []builder{&b14,&b16,};b18.options = []builder{&b12,&b17,};var b23 = sequenceBuilder{id: 23, commit: 10,ranges: [][]int{{1, 1},{1, -1},{1, 1},{1, -1},},};var b20 = sequenceBuilder{id: 20, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var b19 = charBuilder{};b20.items = []builder{&b19,};var b22 = sequenceBuilder{id: 22, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var b21 = charBuilder{};b22.items = []builder{&b21,};b23.items = []builder{&b20,&b22,};var b30 = sequenceBuilder{id: 30, commit: 10,ranges: [][]int{{1, 1},{0, 1},{1, -1},{1, 1},{0, 1},{1, -1},},};var b25 = sequenceBuilder{id: 25, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var b24 = charBuilder{};b25.items = []builder{&b24,};var b27 = sequenceBuilder{id: 27, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var b26 = charBuilder{};b27.items = []builder{&b26,};var b29 = sequenceBuilder{id: 29, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var b28 = charBuilder{};b29.items = []builder{&b28,};b30.items = []builder{&b25,&b27,&b29,};b31.items = []builder{&b10,&b18,&b23,&b30,};var b44 = sequenceBuilder{id: 44, commit: 258,ranges: [][]int{{1, 1},{0, -1},{1, 1},{0, -1},{1, 1},},generalizations: []int{56,47,48,64,},};var b41 = sequenceBuilder{id: 41, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var b40 = charBuilder{};b41.items = []builder{&b40,};var b43 = sequenceBuilder{id: 43, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},},};var b42 = charBuilder{};b43.items = []builder{&b42,};b44.items = []builder{&b41,&b63,&b56,&b63,&b43,};var b55 = choiceBuilder{id: 55, commit: 258,generalizations: []int{56,64,},};var b51 = sequenceBuilder{id: 51, commit: 256,name: "binary0",ranges: [][]int{{1, 1},{0, -1},{1, 1},{0, -1},},generalizations: []int{55,48,56,64,},};var b47 = choiceBuilder{id: 47, commit: 258,generalizations: []int{48,},};b47.options = []builder{&b31,&b44,};var b49 = sequenceBuilder{id: 49, commit: 2,ranges: [][]int{{1, 1},{0, -1},{1, 1},},};var b45 = choiceBuilder{id: 45, commit: 258,};var b37 = sequenceBuilder{id: 37, commit: 264,name: "mul",allChars: true,ranges: [][]int{{1, 1},{1, 1},},generalizations: []int{45,},};var b36 = charBuilder{};b37.items = []builder{&b36,};var b39 = sequenceBuilder{id: 39, commit: 264,name: "div",allChars: true,ranges: [][]int{{1, 1},{1, 1},},generalizations: []int{45,},};var b38 = charBuilder{};b39.items = []builder{&b38,};b45.options = []builder{&b37,&b39,};b49.items = []builder{&b45,&b63,&b47,};var b50 = sequenceBuilder{id: 50, commit: 2,ranges: [][]int{{0, -1},{1, 1},},};b50.items = []builder{&b63,&b49,};b51.items = []builder{&b47,&b63,&b49,&b50,};var b54 = sequenceBuilder{id: 54, commit: 256,name: "binary1",ranges: [][]int{{1, 1},{0, -1},{1, 1},{0, -1},},generalizations: []int{55,56,64,},};var b48 = choiceBuilder{id: 48, commit: 258,};b48.options = []builder{&b47,&b51,};var b52 = sequenceBuilder{id: 52, commit: 2,ranges: [][]int{{1, 1},{0, -1},{1, 1},},};var b46 = choiceBuilder{id: 46, commit: 258,};var b33 = sequenceBuilder{id: 33, commit: 264,name: "add",allChars: true,ranges: [][]int{{1, 1},{1, 1},},generalizations: []int{46,},};var b32 = charBuilder{};b33.items = []builder{&b32,};var b35 = sequenceBuilder{id: 35, commit: 264,name: "sub",allChars: true,ranges: [][]int{{1, 1},{1, 1},},generalizations: []int{46,},};var b34 = charBuilder{};b35.items = []builder{&b34,};b46.options = []builder{&b33,&b35,};b52.items = []builder{&b46,&b63,&b48,};var b53 = sequenceBuilder{id: 53, commit: 2,ranges: [][]int{{0, -1},{1, 1},},};b53.items = []builder{&b63,&b52,};b54.items = []builder{&b48,&b63,&b52,&b53,};b55.options = []builder{&b51,&b54,};b56.options = []builder{&b31,&b44,&b55,};var b61 = sequenceBuilder{id: 61, commit: 10,allChars: true,ranges: [][]int{{1, 1},{1, 1},{1, 1},{1, 1},{1, 1},{1, 1},{1, 1},{1, 1},},generalizations: []int{64,},};var b57 = charBuilder{};var b58 = charBuilder{};var b59 = charBuilder{};var b60 = charBuilder{};b61.items = []builder{&b57,&b58,&b59,&b60,};b64.options = []builder{&b56,&b61,};b65.items = []builder{&b63,&b64,&b63,};
|
|
|
|
var keywords = []parser{}
|
|
|
|
return parseInput(r, &p65, &b65, keywords)
|
|
}
|