Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

Commit 067195b

Browse files
committed
Token2Lit/Lit2Token
1 parent 8a11afb commit 067195b

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

tpl/compiler_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ factor =
138138
if text != "1.2 .3 sin/1 2 '*' '+' 3 cos/1 5 6 pow/2 7 max/4" {
139139
t.Fatal("MatchExactly failed:", text)
140140
}
141+
142+
for name, gr := range m.Grammars {
143+
code := tpl.Code(gr, m.Scanner)
144+
fmt.Println("name:", name, "=", code)
145+
}
141146
}
142147

143148
// -----------------------------------------------------------------------------

tpl/scanner.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,15 @@ scanAgain:
799799
// Source returns the scanning source.
800800
//
801801
func (s *Scanner) Source() TokenSource {
802-
803802
return TokenSource{s.file, s.src}
804803
}
804+
805+
// Ttol - token => literal
806+
func (s *Scanner) Ttol(tok uint) string {
807+
return Token2Lit(tok)
808+
}
809+
810+
// Ltot - literal => token
811+
func (s *Scanner) Ltot(lit string) (tok uint) {
812+
return Lit2Token(lit)
813+
}

tpl/token.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ func TokenLen(tok uint) int {
175175

176176
// -----------------------------------------------------------------------------
177177

178-
// Ttol - token => literal
179-
func (p *Scanner) Ttol(tok uint) (s string) {
178+
// Token2Lit - token => literal
179+
func Token2Lit(tok uint) (s string) {
180180
if tok < uint(len(tokens)) {
181181
s = tokens[tok]
182182
} else {
@@ -196,8 +196,8 @@ func init() {
196196
}
197197
}
198198

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

0 commit comments

Comments
 (0)