aboutsummaryrefslogtreecommitdiff
path: root/grammar/symbol.go
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2021-07-31 16:30:56 +0900
committerRyo Nihei <nihei.dev@gmail.com>2021-07-31 16:30:56 +0900
commit7db2c43fb274dcd1302f10fe20825b27f08ea5be (patch)
tree27c3a2bf2de6c0793a841682d4e33875d8e570bc /grammar/symbol.go
parentDetect duplicate names between terminals and non-terminals (diff)
downloadurubu-7db2c43fb274dcd1302f10fe20825b27f08ea5be.tar.gz
urubu-7db2c43fb274dcd1302f10fe20825b27f08ea5be.tar.xz
Write terminals to a description file
Diffstat (limited to 'grammar/symbol.go')
-rw-r--r--grammar/symbol.go19
1 files changed, 17 insertions, 2 deletions
diff --git a/grammar/symbol.go b/grammar/symbol.go
index ae6000c..9c0e9bd 100644
--- a/grammar/symbol.go
+++ b/grammar/symbol.go
@@ -2,6 +2,7 @@ package grammar
import (
"fmt"
+ "sort"
)
type symbolKind string
@@ -224,14 +225,28 @@ func (t *symbolTable) toText(sym symbol) (string, bool) {
return text, ok
}
-func (t *symbolTable) getTerminalTexts() ([]string, error) {
+func (t *symbolTable) terminalSymbols() []symbol {
+ syms := make([]symbol, 0, t.termNum.Int()-terminalNumMin.Int())
+ for sym := range t.sym2Text {
+ if !sym.isTerminal() || sym.isNil() || sym.isEOF() {
+ continue
+ }
+ syms = append(syms, sym)
+ }
+ sort.Slice(syms, func(i, j int) bool {
+ return syms[i] < syms[j]
+ })
+ return syms
+}
+
+func (t *symbolTable) terminalTexts() ([]string, error) {
if t.termNum == terminalNumMin {
return nil, fmt.Errorf("symbol table has no terminals")
}
return t.termTexts, nil
}
-func (t *symbolTable) getNonTerminalTexts() ([]string, error) {
+func (t *symbolTable) nonTerminalTexts() ([]string, error) {
if t.nonTermNum == nonTerminalNumMin || t.nonTermTexts[symbolStart.num().Int()] == "" {
return nil, fmt.Errorf("symbol table has no terminals or no start symbol")
}