Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add text method to element #8

Merged
merged 13 commits into from
Nov 16, 2021
Merged
4 changes: 4 additions & 0 deletions choice.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func (choice *Choice) String() string {
return fmt.Sprintf("<Choice gid:%d elements:%v>", choice.gid, choice.elements)
}

func (choice *Choice) Text() string {
return fmt.Sprintf("%v",choice.elements)
}

func (choice *Choice) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {
if choice.mostGreedy {
return choice.parseMostGreedy(p, parent, r)
Expand Down
1 change: 1 addition & 0 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Element interface {
Gid() int
String() string
parse(p *parser, parent *Node, r *ruleStore) (*Node, error)
Text() string
}

type element struct {
Expand Down
4 changes: 4 additions & 0 deletions eos.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func (eos EndOfStatement) String() string {
return "<EndOfStatement>"
}

func (eos EndOfStatement) Text() string {
return "<EndOfStatement>"
}

func (eos EndOfStatement) parse(p *parser, n *Node, r *ruleStore) (*Node, error) {
return nil, nil
}
3 changes: 0 additions & 3 deletions go.mod

This file was deleted.

3 changes: 3 additions & 0 deletions goleri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func TestToken(t *testing.T) {
assertEquals(t, false, parse(t, grammar, "++").IsValid())
assertEquals(t, false, parse(t, grammar, "").IsValid())
assertEquals(t, "<Token gid:0 token:+>", token.String())
assertEquals(t, "+", token.Text())
assertEquals(t, []Element{}, parse(t, grammar, "+").GetExpecting())
assertEquals(t, []Element{token}, parse(t, grammar, "").GetExpecting())
assertEquals(t, 0, parse(t, grammar, "").Pos())
Expand All @@ -178,6 +179,7 @@ func TestTokenMultiChars(t *testing.T) {
assertEquals(t, false, parse(t, grammar, "+").IsValid())
assertEquals(t, false, parse(t, grammar, "").IsValid())
assertEquals(t, "<Token gid:0 token:+=>", token.String())
assertEquals(t, "+=", token.Text())
assertEquals(t, []Element{}, parse(t, grammar, "+=").GetExpecting())
assertEquals(t, []Element{token}, parse(t, grammar, "").GetExpecting())
assertEquals(t, 0, parse(t, grammar, "").Pos())
Expand Down Expand Up @@ -252,6 +254,7 @@ func TestListEndDelimiter(t *testing.T) {
assertEquals(t, []Element{hi}, parse(t, grammar, "").GetExpecting())
}


func TestRepeat(t *testing.T) {
hi := NewKeyword(0, "hi", true)
repeat := NewRepeat(0, hi, 1, 3)
Expand Down
2 changes: 1 addition & 1 deletion grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (g *Grammar) Parse(s string) (*Result, error) {
return nil, err
}

pr = &Result{n != nil, 0, p.expect, nd}
pr = &Result{n != nil, 0, p.expect, nd, s}
end := p.l

// ignore white space at end
Expand Down
4 changes: 4 additions & 0 deletions keyword.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func (keyword *Keyword) String() string {
return fmt.Sprintf("<Keyword gid:%d keyword:%v>", keyword.gid, keyword.keyword)
}

func (keyword *Keyword) Text() string {
return fmt.Sprintf(keyword.keyword)
}

func (keyword *Keyword) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {
var match bool
var nd *Node
Expand Down
4 changes: 4 additions & 0 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (list *List) String() string {
return fmt.Sprintf("<List gid:%d elem:%v delimiter:%v>", list.gid, list.elem, list.delimiter)
}

func (list *List) Text() string {
return fmt.Sprintf("%v",list.elem)
}

func (list *List) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {

nd := newNode(list, parent.end)
Expand Down
4 changes: 4 additions & 0 deletions optional.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (optional *Optional) String() string {
return fmt.Sprintf("<Optional gid:%d elem:%v>", optional.gid, optional.elem)
}

func (optional *Optional) Text() string {
return fmt.Sprintf("%v", optional.elem)
}

func (optional *Optional) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {
nd := newNode(optional, parent.end)
n, err := p.walk(nd, optional.elem, r, modeOptional)
Expand Down
4 changes: 4 additions & 0 deletions prio.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func (prio *Prio) String() string {
return fmt.Sprintf("<Prio gid:%d elements:%v>", prio.gid, prio.elements)
}

func (prio *Prio) Text() string {
return fmt.Sprintf("%v",prio.elements)
}

func (prio *Prio) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {

if r.depth > PrioMaxRecursionDepth {
Expand Down
4 changes: 4 additions & 0 deletions ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func (ref *Ref) String() string {
return fmt.Sprintf("<Ref isSet:%t>", ref.IsSet())
}

func (ref *Ref) Text() string {
return fmt.Sprintf("%t", ref.IsSet())
}

// Set reference element.
func (ref *Ref) Set(elem Element) {
ref.elem = elem
Expand Down
4 changes: 4 additions & 0 deletions regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (regex *Regex) String() string {
return fmt.Sprintf("<Regex gid:%d regex:%v>", regex.gid, regex.regex)
}

func (regex *Regex) Text() string {
return fmt.Sprintf("%v",regex.regex)
}

func (regex *Regex) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {
var nd *Node
s := p.s[parent.end:]
Expand Down
4 changes: 4 additions & 0 deletions repeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func (repeat *Repeat) String() string {
return fmt.Sprintf("<Repeat gid:%d elem:%v>", repeat.gid, repeat.elem)
}

func (repeat *Repeat) Text() string {
return fmt.Sprintf("%v",repeat.elem)
}

func (repeat *Repeat) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {

nd := newNode(repeat, parent.end)
Expand Down
11 changes: 7 additions & 4 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package goleri

// Result is used as a Parse return value.
type Result struct {
isValid bool
pos int
expect *expecting
tree *Node
isValid bool
pos int
expect *expecting
tree *Node
expression string
}

// IsValid returns true when a parse result is valid.
Expand All @@ -21,3 +22,5 @@ func (r *Result) GetExpecting() []Element {

// Tree returns the node tree.
func (r *Result) Tree() *Node { return r.tree }


4 changes: 4 additions & 0 deletions rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func (rule *Rule) String() string {
return rule.elem.String()
}

func (rule *Rule) Text() string {
return rule.elem.String()
}

func (rule *Rule) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {

nd := newNode(rule, parent.end)
Expand Down
4 changes: 4 additions & 0 deletions sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (sequence *Sequence) String() string {
return fmt.Sprintf("<Sequence gid:%d elements:%v>", sequence.gid, sequence.elements)
}

func (sequence *Sequence) Text() string {
return fmt.Sprintf("%v",sequence.elements)
}

func (sequence *Sequence) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {
nd := newNode(sequence, parent.end)

Expand Down
4 changes: 4 additions & 0 deletions this.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func (t This) String() string {
return "<This>"
}

func (t This) Text() string {
return "<This>"
}

func (t *This) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {
var nd *Node
var ok bool
Expand Down
4 changes: 4 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (token *Token) String() string {
return fmt.Sprintf("<Token gid:%d token:%v>", token.gid, token.token)
}

func (token *Token) Text() string {
return fmt.Sprintf(token.token)
}

func (token *Token) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {
var nd *Node
match := true
Expand Down
4 changes: 4 additions & 0 deletions tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (tokens *Tokens) String() string {
return fmt.Sprintf("<Tokens gid:%d tokens:%v>", tokens.gid, tokens.tokens)
}

func (tokens *Tokens) Text() string {
return fmt.Sprintf("%v",tokens.tokens)
}

func (tokens *Tokens) parse(p *parser, parent *Node, r *ruleStore) (*Node, error) {
var nd *Node
var match bool
Expand Down