aboutsummaryrefslogtreecommitdiff
path: root/compiler/dfa.go
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2021-08-01 17:17:53 +0900
committerRyo Nihei <nihei.dev@gmail.com>2021-08-01 21:18:34 +0900
commit2433c27f26bc1be2d9b33f6550482abc48fa31ef (patch)
treec7cbc22929045e2d5dccdc37cc978138b59c1bdb /compiler/dfa.go
parentAdd unique kind IDs to tokens (diff)
downloadtre-2433c27f26bc1be2d9b33f6550482abc48fa31ef.tar.gz
tre-2433c27f26bc1be2d9b33f6550482abc48fa31ef.tar.xz
Change APIs
Change fields of tokens, results of lexical analysis, as follows: - Rename: mode -> mode_id - Rename: kind_id -> mode_kind_id - Add: kind_id The kind ID is unique across all modes, but the mode kind ID is unique only within a mode. Change fields of a transition table as follows: - Rename: initial_mode -> initial_mode_id - Rename: modes -> mode_names - Rename: kinds -> kind_names - Rename: specs[].kinds -> specs[].kind_names - Rename: specs[].dfa.initial_state -> specs[].dfa.initial_state_id Change public types defined in the spec package as follows: - Rename: LexModeNum -> LexModeID - Rename: LexKind -> LexKindName - Add: LexKindID - Add: StateID
Diffstat (limited to 'compiler/dfa.go')
-rw-r--r--compiler/dfa.go18
1 files changed, 9 insertions, 9 deletions
diff --git a/compiler/dfa.go b/compiler/dfa.go
index b94fca8..1d0b26a 100644
--- a/compiler/dfa.go
+++ b/compiler/dfa.go
@@ -9,7 +9,7 @@ import (
type DFA struct {
States []string
InitialState string
- AcceptingStatesTable map[string]int
+ AcceptingStatesTable map[string]spec.LexModeKindID
TransitionTable map[string][256]string
}
@@ -65,7 +65,7 @@ func genDFA(root astNode, symTab *symbolTable) *DFA {
}
}
- accTab := map[string]int{}
+ accTab := map[string]spec.LexModeKindID{}
{
for h, s := range stateMap {
for _, pos := range s.set() {
@@ -104,33 +104,33 @@ func genDFA(root astNode, symTab *symbolTable) *DFA {
}
func genTransitionTable(dfa *DFA) (*spec.TransitionTable, error) {
- state2Num := map[string]int{}
+ stateHash2ID := map[string]spec.StateID{}
for i, s := range dfa.States {
// Since 0 represents an invalid value in a transition table,
// assign a number greater than or equal to 1 to states.
- state2Num[s] = i + 1
+ stateHash2ID[s] = spec.StateID(i + spec.StateIDMin.Int())
}
- acc := make([]int, len(dfa.States)+1)
+ acc := make([]spec.LexModeKindID, len(dfa.States)+1)
for _, s := range dfa.States {
id, ok := dfa.AcceptingStatesTable[s]
if !ok {
continue
}
- acc[state2Num[s]] = id
+ acc[stateHash2ID[s]] = id
}
rowCount := len(dfa.States) + 1
colCount := 256
- tran := make([]int, rowCount*colCount)
+ tran := make([]spec.StateID, rowCount*colCount)
for s, tab := range dfa.TransitionTable {
for v, to := range tab {
- tran[state2Num[s]*256+v] = state2Num[to]
+ tran[stateHash2ID[s].Int()*256+v] = stateHash2ID[to]
}
}
return &spec.TransitionTable{
- InitialState: state2Num[dfa.InitialState],
+ InitialStateID: stateHash2ID[dfa.InitialState],
AcceptingStates: acc,
UncompressedTransition: tran,
RowCount: rowCount,