Skip to content

Commit

Permalink
Move GetText(), SetText(), and String() from CommonToken to BaseToken
Browse files Browse the repository at this point in the history
This change means that BaseToken satisfies the Token interface.

Signed-off-by: Jerry Berg <[email protected]>
  • Loading branch information
googleberg authored and parrt committed Sep 4, 2023
1 parent a5602db commit dae2d27
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions runtime/Go/antlr/v4/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ func (b *BaseToken) GetSource() *TokenSourceCharStreamPair {
return b.source
}

func (b *BaseToken) GetText() string {
if b.text != "" {
return b.text
}
input := b.GetInputStream()
if input == nil {
return ""
}
n := input.Size()
if b.GetStart() < n && b.GetStop() < n {
return input.GetTextFromInterval(NewInterval(b.GetStart(), b.GetStop()))
}
return "<EOF>"
}

func (b *BaseToken) SetText(text string) {
b.text = text
}

func (b *BaseToken) GetTokenIndex() int {
return b.tokenIndex
}
Expand All @@ -120,6 +139,28 @@ func (b *BaseToken) GetInputStream() CharStream {
return b.source.charStream
}

func (b *BaseToken) String() string {
txt := b.GetText()
if txt != "" {
txt = strings.Replace(txt, "\n", "\\n", -1)
txt = strings.Replace(txt, "\r", "\\r", -1)
txt = strings.Replace(txt, "\t", "\\t", -1)
} else {
txt = "<no text>"
}

var ch string
if b.GetChannel() > 0 {
ch = ",channel=" + strconv.Itoa(b.GetChannel())
} else {
ch = ""
}

return "[@" + strconv.Itoa(b.GetTokenIndex()) + "," + strconv.Itoa(b.GetStart()) + ":" + strconv.Itoa(b.GetStop()) + "='" +
txt + "',<" + strconv.Itoa(b.GetTokenType()) + ">" +
ch + "," + strconv.Itoa(b.GetLine()) + ":" + strconv.Itoa(b.GetColumn()) + "]"
}

type CommonToken struct {
BaseToken
}
Expand Down Expand Up @@ -170,44 +211,3 @@ func (c *CommonToken) clone() *CommonToken {
t.text = c.GetText()
return t
}

func (c *CommonToken) GetText() string {
if c.text != "" {
return c.text
}
input := c.GetInputStream()
if input == nil {
return ""
}
n := input.Size()
if c.start < n && c.stop < n {
return input.GetTextFromInterval(NewInterval(c.start, c.stop))
}
return "<EOF>"
}

func (c *CommonToken) SetText(text string) {
c.text = text
}

func (c *CommonToken) String() string {
txt := c.GetText()
if txt != "" {
txt = strings.Replace(txt, "\n", "\\n", -1)
txt = strings.Replace(txt, "\r", "\\r", -1)
txt = strings.Replace(txt, "\t", "\\t", -1)
} else {
txt = "<no text>"
}

var ch string
if c.channel > 0 {
ch = ",channel=" + strconv.Itoa(c.channel)
} else {
ch = ""
}

return "[@" + strconv.Itoa(c.tokenIndex) + "," + strconv.Itoa(c.start) + ":" + strconv.Itoa(c.stop) + "='" +
txt + "',<" + strconv.Itoa(c.tokenType) + ">" +
ch + "," + strconv.Itoa(c.line) + ":" + strconv.Itoa(c.column) + "]"
}

0 comments on commit dae2d27

Please sign in to comment.