Debian Codegraph @

internal/lexer/gen/golang/transtab.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 "fmt"
20 "path"
21 "text/template"
22
23 "github.com/goccmack/gocc/internal/io"
24 "github.com/goccmack/gocc/internal/lexer/items"
25 )
26
27 func genTransitionTable(pkg, outDir, header string, itemSets *items.ItemSets) {
28 fname := path.Join(outDir, "lexer", "transitiontable.go")
29 io.WriteFile(fname, getTransitionTable(itemSets, header))
30 }
31
32 func getTransitionTable(itemsets *items.ItemSets, header string) []byte {
33 tmpl, err := template.New("lexer transition table").Parse(transTabSrc[1:])
34 if err != nil {
35 panic(err)
36 }
37 wr := new(bytes.Buffer)
38 data := &transitionTableData{
39 Header: header,
40 Rows: getTransitionTableData(itemsets),
41 }
42 err = tmpl.Execute(wr, data)
43 if err != nil {
44 panic(err)
45 }
46 return wr.Bytes()
47 }
48
49 type transitionTableData struct {
50 Header string
51 Rows []transitionTableRowData
52 }
53
54 func getTransitionTableData(itemsets *items.ItemSets) []transitionTableRowData {
55 data := make([]transitionTableRowData, itemsets.Size())
56 for setNo, set := range itemsets.List() {
57 if set.SymbolClasses.MatchAny {
58 data[setNo].MatchAny = true
59 data[setNo].MatchAnyState = set.DotTransition
60 } else {
61 data[setNo].MatchAnyState = -1
62 }
63 data[setNo].SymRange = make([]transitionTableSymRangeData, set.SymbolClasses.Size())
64 for rngNo, rng := range set.SymbolClasses.List() {
65 data[setNo].SymRange[rngNo].Range = rng.String()
66 data[setNo].SymRange[rngNo].Test = rangeTest(rng)
67 data[setNo].SymRange[rngNo].State = set.Transitions[rngNo]
68 }
69 data[setNo].Imports = make([]importTransitionData, len(set.Symbols.ImportIdList))
70 for impI, imprt := range set.Symbols.ImportIdList {
71 if nextSet := set.ImportTransitions[impI]; nextSet != -1 {
72 data[setNo].Imports[impI].Import = imprt
73 data[setNo].Imports[impI].ExtFunc = set.Symbols.ExternalFunction(imprt)
74 data[setNo].Imports[impI].State = nextSet
75 }
76 }
77 }
78 return data
79 }
80
81 func rangeTest(rng items.CharRange) string {
82 if rng.From == rng.To {
83 return fmt.Sprintf("r == %d", rng.From)
84 }
85 return fmt.Sprintf("%d <= r && r <= %d", rng.From, rng.To)
86 }
87
88 type transitionTableRowData struct {
89 MatchAny bool
90 MatchAnyState int
91 SymRange []transitionTableSymRangeData
92 Imports []importTransitionData
93 }
94
95 type importTransitionData struct {
96 Import string
97 ExtFunc string
98 State int
99 }
100
101 type transitionTableSymRangeData struct {
102 Range string
103 Test string
104 State int
105 }
106
107 const transTabSrc = `
108 // Code generated by gocc; DO NOT EDIT.
109
110 package lexer
111 {{if .Header}}
112 {{.Header}}
113 {{end}}
114 /*
115 Let s be the current state
116 Let r be the current input rune
117 transitionTable[s](r) returns the next state.
118 */
119 type TransitionTable [NumStates]func(rune) int
120
121 var TransTab = TransitionTable{
122 {{- range $sno, $state := .Rows }}
123 // S{{$sno}}
124 func(r rune) int {
125 switch {
126 {{- range $rng := $state.SymRange }}
127 case {{$rng.Test}}: // {{$rng.Range}}
128 return {{$rng.State}}
129 {{- end }}
130 {{- range $imp := $state.Imports }}
131 {{- if $imp.State }}
132 case {{$imp.ExtFunc}}(r): // {{$imp.Import}}
133 return {{$imp.State}}
134 {{- end }}
135 {{- end }}
136 {{- if $state.MatchAny }}
137 default:
138 return {{$state.MatchAnyState}}
139 }
140 {{- else }}
141 }
142 return NoState
143 {{- end }}
144 },
145 {{- end }}
146 }
147 `