Debian Codegraph @

internal/lexer/gen/golang/asciitable.go

raw symbols SCIP
1 //Copyright 2013 Vastech SA (PTY) LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package golang
16
17 import (
18 "bytes"
19 "path"
20 "text/template"
21
22 "github.com/goccmack/gocc/internal/io"
23 "github.com/goccmack/gocc/internal/lexer/symbols"
24 )
25
26 func genAsciiTable(outDir string, symbols *symbols.Symbols) {
27 tmpl, err := template.New("ascii table").Parse(asciiTabSrc)
28 if err != nil {
29 panic(err)
30 }
31 w := new(bytes.Buffer)
32 if err = tmpl.Execute(w, getAsciiTab(symbols)); err != nil {
33 panic(err)
34 }
35 io.WriteFile(path.Join(outDir, "lexer", "asciitable.go"), w.Bytes())
36 }
37
38 func getAsciiTab(symbols *symbols.Symbols) map[rune]asciiType {
39 asciiMap := make(map[rune]asciiType)
40 for i, sym := range symbols.List() {
41 if cl, exist := symbols.CharLitSymbols.GetSymbolId(sym); exist {
42 if cl.Val < 0x100 {
43 asciiMap[cl.Val] = asciiType{Type: i, Comment: sym}
44 }
45 }
46 }
47 return asciiMap
48 }
49
50 type asciiType struct {
51 Type int
52 Comment string
53 }
54
55 const asciiTabSrc = `
56 // Code generated by gocc; DO NOT EDIT.
57
58 package lexer
59
60 const asciiTabLen = 255
61
62 // asciiTable[rune] returns the lexer symbol type of rune
63 var asciiTable = func() *[asciiTabLen]int {
64 atab := new([asciiTabLen] int)
65 for i, _ := range atab {
66 atab[i] = -1
67 }
68 {{range $run, $typ := .}}atab[{{$run}}] = {{$typ.Type}} // {{$typ.Comment}}
69 {{end}}
70 return atab
71 }()
72
73 `