package parser // Code generated by peg parser/grammer.peg DO NOT EDIT. import ( "fmt" "io" "os" "sort" "strconv" "strings" ) const endSymbol rune = 1114112 /* The rule types inferred from the grammar are below. */ type pegRule uint8 const ( ruleUnknown pegRule = iota ruleroot ruleignore rulecolors ruletext rulecolor rulestandard_color ruleextended_color ruleextended_color_256 ruleextended_color_rgb ruleextended_color_prefix ruletext_attributes rulezero rulenumber ruleprefix ruleescape_sequence rulecolor_suffix rulenon_color_suffix ruledelimiter ruleAction0 rulePegText ruleAction1 ruleAction2 ruleAction3 ruleAction4 ruleAction5 ruleAction6 ruleAction7 ruleAction8 ruleAction9 ruleAction10 ruleAction11 ) var rul3s = [...]string{ "Unknown", "root", "ignore", "colors", "text", "color", "standard_color", "extended_color", "extended_color_256", "extended_color_rgb", "extended_color_prefix", "text_attributes", "zero", "number", "prefix", "escape_sequence", "color_suffix", "non_color_suffix", "delimiter", "Action0", "PegText", "Action1", "Action2", "Action3", "Action4", "Action5", "Action6", "Action7", "Action8", "Action9", "Action10", "Action11", } type token32 struct { pegRule begin, end uint32 } func (t *token32) String() string { return fmt.Sprintf("\x1B[34m%v\x1B[m %v %v", rul3s[t.pegRule], t.begin, t.end) } type node32 struct { token32 up, next *node32 } func (node *node32) print(w io.Writer, pretty bool, buffer string) { var print func(node *node32, depth int) print = func(node *node32, depth int) { for node != nil { for c := 0; c < depth; c++ { fmt.Fprintf(w, " ") } rule := rul3s[node.pegRule] quote := strconv.Quote(string(([]rune(buffer)[node.begin:node.end]))) if !pretty { fmt.Fprintf(w, "%v %v\n", rule, quote) } else { fmt.Fprintf(w, "\x1B[36m%v\x1B[m %v\n", rule, quote) } if node.up != nil { print(node.up, depth+1) } node = node.next } } print(node, 0) } func (node *node32) Print(w io.Writer, buffer string) { node.print(w, false, buffer) } func (node *node32) PrettyPrint(w io.Writer, buffer string) { node.print(w, true, buffer) } type tokens32 struct { tree []token32 } func (t *tokens32) Trim(length uint32) { t.tree = t.tree[:length] } func (t *tokens32) Print() { for _, token := range t.tree { fmt.Println(token.String()) } } func (t *tokens32) AST() *node32 { type element struct { node *node32 down *element } tokens := t.Tokens() var stack *element for _, token := range tokens { if token.begin == token.end { continue } node := &node32{token32: token} for stack != nil && stack.node.begin >= token.begin && stack.node.end <= token.end { stack.node.next = node.up node.up = stack.node stack = stack.down } stack = &element{node: node, down: stack} } if stack != nil { return stack.node } return nil } func (t *tokens32) PrintSyntaxTree(buffer string) { t.AST().Print(os.Stdout, buffer) } func (t *tokens32) WriteSyntaxTree(w io.Writer, buffer string) { t.AST().Print(w, buffer) } func (t *tokens32) PrettyPrintSyntaxTree(buffer string) { t.AST().PrettyPrint(os.Stdout, buffer) } func (t *tokens32) Add(rule pegRule, begin, end, index uint32) { tree, i := t.tree, int(index) if i >= len(tree) { t.tree = append(tree, token32{pegRule: rule, begin: begin, end: end}) return } tree[i] = token32{pegRule: rule, begin: begin, end: end} } func (t *tokens32) Tokens() []token32 { return t.tree } type Parser struct { ParserFunc Buffer string buffer []rune rules [32]func() bool parse func(rule ...int) error reset func() Pretty bool tokens32 } func (p *Parser) Parse(rule ...int) error { return p.parse(rule...) } func (p *Parser) Reset() { p.reset() } type textPosition struct { line, symbol int } type textPositionMap map[int]textPosition func translatePositions(buffer []rune, positions []int) textPositionMap { length, translations, j, line, symbol := len(positions), make(textPositionMap, len(positions)), 0, 1, 0 sort.Ints(positions) search: for i, c := range buffer { if c == '\n' { line, symbol = line+1, 0 } else { symbol++ } if i == positions[j] { translations[positions[j]] = textPosition{line, symbol} for j++; j < length; j++ { if i != positions[j] { continue search } } break search } } return translations } type parseError struct { p *Parser max token32 } func (e *parseError) Error() string { tokens, err := []token32{e.max}, "\n" positions, p := make([]int, 2*len(tokens)), 0 for _, token := range tokens { positions[p], p = int(token.begin), p+1 positions[p], p = int(token.end), p+1 } translations := translatePositions(e.p.buffer, positions) format := "parse error near %v (line %v symbol %v - line %v symbol %v):\n%v\n" if e.p.Pretty { format = "parse error near \x1B[34m%v\x1B[m (line %v symbol %v - line %v symbol %v):\n%v\n" } for _, token := range tokens { begin, end := int(token.begin), int(token.end) err += fmt.Sprintf(format, rul3s[token.pegRule], translations[begin].line, translations[begin].symbol, translations[end].line, translations[end].symbol, strconv.Quote(string(e.p.buffer[begin:end]))) } return err } func (p *Parser) PrintSyntaxTree() { if p.Pretty { p.tokens32.PrettyPrintSyntaxTree(p.Buffer) } else { p.tokens32.PrintSyntaxTree(p.Buffer) } } func (p *Parser) WriteSyntaxTree(w io.Writer) { p.tokens32.WriteSyntaxTree(w, p.Buffer) } func (p *Parser) SprintSyntaxTree() string { var bldr strings.Builder p.WriteSyntaxTree(&bldr) return bldr.String() } func (p *Parser) Execute() { buffer, _buffer, text, begin, end := p.Buffer, p.buffer, "", 0, 0 for _, token := range p.Tokens() { switch token.pegRule { case rulePegText: begin, end = int(token.begin), int(token.end) text = string(_buffer[begin:end]) case ruleAction0: p.pushResetColor() case ruleAction1: p.pushText(text) case ruleAction2: p.pushStandardColorWithCategory(text) case ruleAction3: p.pushResetForegroundColor() case ruleAction4: p.pushResetBackgroundColor() case ruleAction5: p.setExtendedColor256(text) case ruleAction6: p.setExtendedColorR(text) case ruleAction7: p.setExtendedColorG(text) case ruleAction8: p.setExtendedColorB(text) case ruleAction9: p.pushExtendedColor(text) case ruleAction10: p.pushResetColor() case ruleAction11: p.pushReverseColor() } } _, _, _, _, _ = buffer, _buffer, text, begin, end } func Pretty(pretty bool) func(*Parser) error { return func(p *Parser) error { p.Pretty = pretty return nil } } func Size(size int) func(*Parser) error { return func(p *Parser) error { p.tokens32 = tokens32{tree: make([]token32, 0, size)} return nil } } func (p *Parser) Init(options ...func(*Parser) error) error { var ( max token32 position, tokenIndex uint32 buffer []rune ) for _, option := range options { err := option(p) if err != nil { return err } } p.reset = func() { max = token32{} position, tokenIndex = 0, 0 p.buffer = []rune(p.Buffer) if len(p.buffer) == 0 || p.buffer[len(p.buffer)-1] != endSymbol { p.buffer = append(p.buffer, endSymbol) } buffer = p.buffer } p.reset() _rules := p.rules tree := p.tokens32 p.parse = func(rule ...int) error { r := 1 if len(rule) > 0 { r = rule[0] } matches := p.rules[r]() p.tokens32 = tree if matches { p.Trim(tokenIndex) return nil } return &parseError{p, max} } add := func(rule pegRule, begin uint32) { tree.Add(rule, begin, position, tokenIndex) tokenIndex++ if begin != position && position > max.end { max = token32{rule, begin, position} } } matchDot := func() bool { if buffer[position] != endSymbol { position++ return true } return false } /*matchChar := func(c byte) bool { if buffer[position] == c { position++ return true } return false }*/ /*matchRange := func(lower byte, upper byte) bool { if c := buffer[position]; c >= lower && c <= upper { position++ return true } return false }*/ _rules = [...]func() bool{ nil, /* 0 root <- <(colors / ignore / text)*> */ func() bool { { position1 := position l2: { position3, tokenIndex3 := position, tokenIndex { position4, tokenIndex4 := position, tokenIndex if !_rules[rulecolors]() { goto l5 } goto l4 l5: position, tokenIndex = position4, tokenIndex4 if !_rules[ruleignore]() { goto l6 } goto l4 l6: position, tokenIndex = position4, tokenIndex4 if !_rules[ruletext]() { goto l3 } } l4: goto l2 l3: position, tokenIndex = position3, tokenIndex3 } add(ruleroot, position1) } return true }, /* 1 ignore <- <((prefix number? non_color_suffix) / escape_sequence)> */ func() bool { position7, tokenIndex7 := position, tokenIndex { position8 := position { position9, tokenIndex9 := position, tokenIndex if !_rules[ruleprefix]() { goto l10 } { position11, tokenIndex11 := position, tokenIndex if !_rules[rulenumber]() { goto l11 } goto l12 l11: position, tokenIndex = position11, tokenIndex11 } l12: if !_rules[rulenon_color_suffix]() { goto l10 } goto l9 l10: position, tokenIndex = position9, tokenIndex9 if !_rules[ruleescape_sequence]() { goto l7 } } l9: add(ruleignore, position8) } return true l7: position, tokenIndex = position7, tokenIndex7 return false }, /* 2 colors <- <((prefix color_suffix Action0) / (prefix color (delimiter color)* color_suffix))> */ func() bool { position13, tokenIndex13 := position, tokenIndex { position14 := position { position15, tokenIndex15 := position, tokenIndex if !_rules[ruleprefix]() { goto l16 } if !_rules[rulecolor_suffix]() { goto l16 } if !_rules[ruleAction0]() { goto l16 } goto l15 l16: position, tokenIndex = position15, tokenIndex15 if !_rules[ruleprefix]() { goto l13 } if !_rules[rulecolor]() { goto l13 } l17: { position18, tokenIndex18 := position, tokenIndex if !_rules[ruledelimiter]() { goto l18 } if !_rules[rulecolor]() { goto l18 } goto l17 l18: position, tokenIndex = position18, tokenIndex18 } if !_rules[rulecolor_suffix]() { goto l13 } } l15: add(rulecolors, position14) } return true l13: position, tokenIndex = position13, tokenIndex13 return false }, /* 3 text <- <(<(!'\x1b' .)+> Action1)> */ func() bool { position19, tokenIndex19 := position, tokenIndex { position20 := position { position21 := position { position24, tokenIndex24 := position, tokenIndex if buffer[position] != rune('\x1b') { goto l24 } position++ goto l19 l24: position, tokenIndex = position24, tokenIndex24 } if !matchDot() { goto l19 } l22: { position23, tokenIndex23 := position, tokenIndex { position25, tokenIndex25 := position, tokenIndex if buffer[position] != rune('\x1b') { goto l25 } position++ goto l23 l25: position, tokenIndex = position25, tokenIndex25 } if !matchDot() { goto l23 } goto l22 l23: position, tokenIndex = position23, tokenIndex23 } add(rulePegText, position21) } if !_rules[ruleAction1]() { goto l19 } add(ruletext, position20) } return true l19: position, tokenIndex = position19, tokenIndex19 return false }, /* 4 color <- <(standard_color / extended_color / text_attributes)> */ func() bool { position26, tokenIndex26 := position, tokenIndex { position27 := position { position28, tokenIndex28 := position, tokenIndex if !_rules[rulestandard_color]() { goto l29 } goto l28 l29: position, tokenIndex = position28, tokenIndex28 if !_rules[ruleextended_color]() { goto l30 } goto l28 l30: position, tokenIndex = position28, tokenIndex28 if !_rules[ruletext_attributes]() { goto l26 } } l28: add(rulecolor, position27) } return true l26: position, tokenIndex = position26, tokenIndex26 return false }, /* 5 standard_color <- <((zero <(('3' / '4' / '9' / ('1' '0')) [0-7])> Action2) / (zero <(('3' / '9') '9')> Action3) / (zero <(('4' / ('1' '0')) '9')> Action4))> */ func() bool { position31, tokenIndex31 := position, tokenIndex { position32 := position { position33, tokenIndex33 := position, tokenIndex if !_rules[rulezero]() { goto l34 } { position35 := position { position36, tokenIndex36 := position, tokenIndex if buffer[position] != rune('3') { goto l37 } position++ goto l36 l37: position, tokenIndex = position36, tokenIndex36 if buffer[position] != rune('4') { goto l38 } position++ goto l36 l38: position, tokenIndex = position36, tokenIndex36 if buffer[position] != rune('9') { goto l39 } position++ goto l36 l39: position, tokenIndex = position36, tokenIndex36 if buffer[position] != rune('1') { goto l34 } position++ if buffer[position] != rune('0') { goto l34 } position++ } l36: if c := buffer[position]; c < rune('0') || c > rune('7') { goto l34 } position++ add(rulePegText, position35) } if !_rules[ruleAction2]() { goto l34 } goto l33 l34: position, tokenIndex = position33, tokenIndex33 if !_rules[rulezero]() { goto l40 } { position41 := position { position42, tokenIndex42 := position, tokenIndex if buffer[position] != rune('3') { goto l43 } position++ goto l42 l43: position, tokenIndex = position42, tokenIndex42 if buffer[position] != rune('9') { goto l40 } position++ } l42: if buffer[position] != rune('9') { goto l40 } position++ add(rulePegText, position41) } if !_rules[ruleAction3]() { goto l40 } goto l33 l40: position, tokenIndex = position33, tokenIndex33 if !_rules[rulezero]() { goto l31 } { position44 := position { position45, tokenIndex45 := position, tokenIndex if buffer[position] != rune('4') { goto l46 } position++ goto l45 l46: position, tokenIndex = position45, tokenIndex45 if buffer[position] != rune('1') { goto l31 } position++ if buffer[position] != rune('0') { goto l31 } position++ } l45: if buffer[position] != rune('9') { goto l31 } position++ add(rulePegText, position44) } if !_rules[ruleAction4]() { goto l31 } } l33: add(rulestandard_color, position32) } return true l31: position, tokenIndex = position31, tokenIndex31 return false }, /* 6 extended_color <- <(extended_color_256 / extended_color_rgb)> */ func() bool { position47, tokenIndex47 := position, tokenIndex { position48 := position { position49, tokenIndex49 := position, tokenIndex if !_rules[ruleextended_color_256]() { goto l50 } goto l49 l50: position, tokenIndex = position49, tokenIndex49 if !_rules[ruleextended_color_rgb]() { goto l47 } } l49: add(ruleextended_color, position48) } return true l47: position, tokenIndex = position47, tokenIndex47 return false }, /* 7 extended_color_256 <- <(extended_color_prefix delimiter zero '5' delimiter Action5)> */ func() bool { position51, tokenIndex51 := position, tokenIndex { position52 := position if !_rules[ruleextended_color_prefix]() { goto l51 } if !_rules[ruledelimiter]() { goto l51 } if !_rules[rulezero]() { goto l51 } if buffer[position] != rune('5') { goto l51 } position++ if !_rules[ruledelimiter]() { goto l51 } { position53 := position if !_rules[rulenumber]() { goto l51 } add(rulePegText, position53) } if !_rules[ruleAction5]() { goto l51 } add(ruleextended_color_256, position52) } return true l51: position, tokenIndex = position51, tokenIndex51 return false }, /* 8 extended_color_rgb <- <(extended_color_prefix delimiter zero '2' delimiter Action6 delimiter Action7 delimiter Action8)> */ func() bool { position54, tokenIndex54 := position, tokenIndex { position55 := position if !_rules[ruleextended_color_prefix]() { goto l54 } if !_rules[ruledelimiter]() { goto l54 } if !_rules[rulezero]() { goto l54 } if buffer[position] != rune('2') { goto l54 } position++ if !_rules[ruledelimiter]() { goto l54 } { position56 := position if !_rules[rulenumber]() { goto l54 } add(rulePegText, position56) } if !_rules[ruleAction6]() { goto l54 } if !_rules[ruledelimiter]() { goto l54 } { position57 := position if !_rules[rulenumber]() { goto l54 } add(rulePegText, position57) } if !_rules[ruleAction7]() { goto l54 } if !_rules[ruledelimiter]() { goto l54 } { position58 := position if !_rules[rulenumber]() { goto l54 } add(rulePegText, position58) } if !_rules[ruleAction8]() { goto l54 } add(ruleextended_color_rgb, position55) } return true l54: position, tokenIndex = position54, tokenIndex54 return false }, /* 9 extended_color_prefix <- <(zero <(('3' / '4') '8')> Action9)> */ func() bool { position59, tokenIndex59 := position, tokenIndex { position60 := position if !_rules[rulezero]() { goto l59 } { position61 := position { position62, tokenIndex62 := position, tokenIndex if buffer[position] != rune('3') { goto l63 } position++ goto l62 l63: position, tokenIndex = position62, tokenIndex62 if buffer[position] != rune('4') { goto l59 } position++ } l62: if buffer[position] != rune('8') { goto l59 } position++ add(rulePegText, position61) } if !_rules[ruleAction9]() { goto l59 } add(ruleextended_color_prefix, position60) } return true l59: position, tokenIndex = position59, tokenIndex59 return false }, /* 10 text_attributes <- <(('0' Action10) / ('7' Action11) / ('1' / '4' / '5' / '8'))+> */ func() bool { position64, tokenIndex64 := position, tokenIndex { position65 := position { position68, tokenIndex68 := position, tokenIndex if buffer[position] != rune('0') { goto l69 } position++ if !_rules[ruleAction10]() { goto l69 } goto l68 l69: position, tokenIndex = position68, tokenIndex68 if buffer[position] != rune('7') { goto l70 } position++ if !_rules[ruleAction11]() { goto l70 } goto l68 l70: position, tokenIndex = position68, tokenIndex68 { position71, tokenIndex71 := position, tokenIndex if buffer[position] != rune('1') { goto l72 } position++ goto l71 l72: position, tokenIndex = position71, tokenIndex71 if buffer[position] != rune('4') { goto l73 } position++ goto l71 l73: position, tokenIndex = position71, tokenIndex71 if buffer[position] != rune('5') { goto l74 } position++ goto l71 l74: position, tokenIndex = position71, tokenIndex71 if buffer[position] != rune('8') { goto l64 } position++ } l71: } l68: l66: { position67, tokenIndex67 := position, tokenIndex { position75, tokenIndex75 := position, tokenIndex if buffer[position] != rune('0') { goto l76 } position++ if !_rules[ruleAction10]() { goto l76 } goto l75 l76: position, tokenIndex = position75, tokenIndex75 if buffer[position] != rune('7') { goto l77 } position++ if !_rules[ruleAction11]() { goto l77 } goto l75 l77: position, tokenIndex = position75, tokenIndex75 { position78, tokenIndex78 := position, tokenIndex if buffer[position] != rune('1') { goto l79 } position++ goto l78 l79: position, tokenIndex = position78, tokenIndex78 if buffer[position] != rune('4') { goto l80 } position++ goto l78 l80: position, tokenIndex = position78, tokenIndex78 if buffer[position] != rune('5') { goto l81 } position++ goto l78 l81: position, tokenIndex = position78, tokenIndex78 if buffer[position] != rune('8') { goto l67 } position++ } l78: } l75: goto l66 l67: position, tokenIndex = position67, tokenIndex67 } add(ruletext_attributes, position65) } return true l64: position, tokenIndex = position64, tokenIndex64 return false }, /* 11 zero <- <'0'*> */ func() bool { { position83 := position l84: { position85, tokenIndex85 := position, tokenIndex if buffer[position] != rune('0') { goto l85 } position++ goto l84 l85: position, tokenIndex = position85, tokenIndex85 } add(rulezero, position83) } return true }, /* 12 number <- <[0-9]+> */ func() bool { position86, tokenIndex86 := position, tokenIndex { position87 := position if c := buffer[position]; c < rune('0') || c > rune('9') { goto l86 } position++ l88: { position89, tokenIndex89 := position, tokenIndex if c := buffer[position]; c < rune('0') || c > rune('9') { goto l89 } position++ goto l88 l89: position, tokenIndex = position89, tokenIndex89 } add(rulenumber, position87) } return true l86: position, tokenIndex = position86, tokenIndex86 return false }, /* 13 prefix <- <(escape_sequence '[')> */ func() bool { position90, tokenIndex90 := position, tokenIndex { position91 := position if !_rules[ruleescape_sequence]() { goto l90 } if buffer[position] != rune('[') { goto l90 } position++ add(ruleprefix, position91) } return true l90: position, tokenIndex = position90, tokenIndex90 return false }, /* 14 escape_sequence <- <'\x1b'> */ func() bool { position92, tokenIndex92 := position, tokenIndex { position93 := position if buffer[position] != rune('\x1b') { goto l92 } position++ add(ruleescape_sequence, position93) } return true l92: position, tokenIndex = position92, tokenIndex92 return false }, /* 15 color_suffix <- <'m'> */ func() bool { position94, tokenIndex94 := position, tokenIndex { position95 := position if buffer[position] != rune('m') { goto l94 } position++ add(rulecolor_suffix, position95) } return true l94: position, tokenIndex = position94, tokenIndex94 return false }, /* 16 non_color_suffix <- <([A-H] / 'f' / 'S' / 'T' / 'J' / 'K')> */ func() bool { position96, tokenIndex96 := position, tokenIndex { position97 := position { position98, tokenIndex98 := position, tokenIndex if c := buffer[position]; c < rune('A') || c > rune('H') { goto l99 } position++ goto l98 l99: position, tokenIndex = position98, tokenIndex98 if buffer[position] != rune('f') { goto l100 } position++ goto l98 l100: position, tokenIndex = position98, tokenIndex98 if buffer[position] != rune('S') { goto l101 } position++ goto l98 l101: position, tokenIndex = position98, tokenIndex98 if buffer[position] != rune('T') { goto l102 } position++ goto l98 l102: position, tokenIndex = position98, tokenIndex98 if buffer[position] != rune('J') { goto l103 } position++ goto l98 l103: position, tokenIndex = position98, tokenIndex98 if buffer[position] != rune('K') { goto l96 } position++ } l98: add(rulenon_color_suffix, position97) } return true l96: position, tokenIndex = position96, tokenIndex96 return false }, /* 17 delimiter <- <';'> */ func() bool { position104, tokenIndex104 := position, tokenIndex { position105 := position if buffer[position] != rune(';') { goto l104 } position++ add(ruledelimiter, position105) } return true l104: position, tokenIndex = position104, tokenIndex104 return false }, /* 19 Action0 <- <{ p.pushResetColor() }> */ func() bool { { add(ruleAction0, position) } return true }, nil, /* 21 Action1 <- <{ p.pushText(text) }> */ func() bool { { add(ruleAction1, position) } return true }, /* 22 Action2 <- <{ p.pushStandardColorWithCategory(text) }> */ func() bool { { add(ruleAction2, position) } return true }, /* 23 Action3 <- <{ p.pushResetForegroundColor() }> */ func() bool { { add(ruleAction3, position) } return true }, /* 24 Action4 <- <{ p.pushResetBackgroundColor() }> */ func() bool { { add(ruleAction4, position) } return true }, /* 25 Action5 <- <{ p.setExtendedColor256(text) }> */ func() bool { { add(ruleAction5, position) } return true }, /* 26 Action6 <- <{ p.setExtendedColorR(text) }> */ func() bool { { add(ruleAction6, position) } return true }, /* 27 Action7 <- <{ p.setExtendedColorG(text) }> */ func() bool { { add(ruleAction7, position) } return true }, /* 28 Action8 <- <{ p.setExtendedColorB(text) }> */ func() bool { { add(ruleAction8, position) } return true }, /* 29 Action9 <- <{ p.pushExtendedColor(text) }> */ func() bool { { add(ruleAction9, position) } return true }, /* 30 Action10 <- <{ p.pushResetColor() }> */ func() bool { { add(ruleAction10, position) } return true }, /* 31 Action11 <- <{ p.pushReverseColor() }> */ func() bool { { add(ruleAction11, position) } return true }, } p.rules = _rules return nil }