Skip to content

Commit

Permalink
Token2Lit/Lit2Token
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed May 17, 2020
1 parent 8a11afb commit 067195b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions tpl/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ factor =
if text != "1.2 .3 sin/1 2 '*' '+' 3 cos/1 5 6 pow/2 7 max/4" {
t.Fatal("MatchExactly failed:", text)
}

for name, gr := range m.Grammars {
code := tpl.Code(gr, m.Scanner)
fmt.Println("name:", name, "=", code)
}
}

// -----------------------------------------------------------------------------
11 changes: 10 additions & 1 deletion tpl/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,15 @@ scanAgain:
// Source returns the scanning source.
//
func (s *Scanner) Source() TokenSource {

return TokenSource{s.file, s.src}
}

// Ttol - token => literal
func (s *Scanner) Ttol(tok uint) string {
return Token2Lit(tok)
}

// Ltot - literal => token
func (s *Scanner) Ltot(lit string) (tok uint) {
return Lit2Token(lit)
}
8 changes: 4 additions & 4 deletions tpl/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ func TokenLen(tok uint) int {

// -----------------------------------------------------------------------------

// Ttol - token => literal
func (p *Scanner) Ttol(tok uint) (s string) {
// Token2Lit - token => literal
func Token2Lit(tok uint) (s string) {
if tok < uint(len(tokens)) {
s = tokens[tok]
} else {
Expand All @@ -196,8 +196,8 @@ func init() {
}
}

// Ltot - literal => token
func (p *Scanner) Ltot(lit string) (tok uint) {
// Lit2Token - literal => token
func Lit2Token(lit string) (tok uint) {
return rtokens[lit]
}

Expand Down

0 comments on commit 067195b

Please sign in to comment.