aboutsummaryrefslogtreecommitdiff
path: root/spec/lexer.go
blob: 14c4074df8f852449fec8f250de8941c8d2735ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//go:generate maleeni compile -l lexspec.json -o clexspec.json

package spec

import (
	_ "embed"
	"encoding/json"
	"fmt"
	"io"
	"strconv"
	"strings"

	mldriver "github.com/nihei9/maleeni/driver"
	mlspec "github.com/nihei9/maleeni/spec"
)

type tokenKind string

const (
	tokenKindKWFragment      = tokenKind("fragment")
	tokenKindID              = tokenKind("id")
	tokenKindTerminalPattern = tokenKind("terminal pattern")
	tokenKindColon           = tokenKind(":")
	tokenKindOr              = tokenKind("|")
	tokenKindSemicolon       = tokenKind(";")
	tokenKindModifierMarker  = tokenKind("@")
	tokenKindActionLeader    = tokenKind("#")
	tokenKindTreeNodeOpen    = tokenKind("'(")
	tokenKindTreeNodeClose   = tokenKind(")")
	tokenKindPosition        = tokenKind("$")
	tokenKindExpantion       = tokenKind("...")
	tokenKindEOF             = tokenKind("eof")
	tokenKindInvalid         = tokenKind("invalid")
)

type token struct {
	kind tokenKind
	text string
	num  int
}

func newSymbolToken(kind tokenKind) *token {
	return &token{
		kind: kind,
	}
}

func newIDToken(text string) *token {
	return &token{
		kind: tokenKindID,
		text: text,
	}
}

func newTerminalPatternToken(text string) *token {
	return &token{
		kind: tokenKindTerminalPattern,
		text: text,
	}
}

func newPositionToken(num int) *token {
	return &token{
		kind: tokenKindPosition,
		num:  num,
	}
}

func newEOFToken() *token {
	return &token{
		kind: tokenKindEOF,
	}
}

func newInvalidToken(text string) *token {
	return &token{
		kind: tokenKindInvalid,
		text: text,
	}
}

type lexer struct {
	s      *mlspec.CompiledLexSpec
	d      *mldriver.Lexer
	dufTok *token
}

//go:embed clexspec.json
var lexspec []byte

func newLexer(src io.Reader) (*lexer, error) {
	s := &mlspec.CompiledLexSpec{}
	err := json.Unmarshal(lexspec, s)
	if err != nil {
		return nil, err
	}
	d, err := mldriver.NewLexer(s, src)
	if err != nil {
		return nil, err
	}
	return &lexer{
		s: s,
		d: d,
	}, nil
}

func (l *lexer) next() (*token, error) {
	for {
		tok, err := l.d.Next()
		if err != nil {
			return nil, err
		}
		if tok.Invalid {
			newInvalidToken(tok.Text())
		}
		if tok.EOF {
			return newEOFToken(), nil
		}
		switch tok.KindName {
		case "white_space":
			continue
		case "newline":
			continue
		case "line_comment":
			continue
		case "kw_fragment":
			return newSymbolToken(tokenKindKWFragment), nil
		case "identifier":
			return newIDToken(tok.Text()), nil
		case "terminal_open":
			var b strings.Builder
			for {
				tok, err := l.d.Next()
				if err != nil {
					return nil, err
				}
				if tok.EOF {
					return nil, synErrUnclosedTerminal
				}
				switch tok.KindName {
				case "pattern":
					// Remove '\' character.
					fmt.Fprintf(&b, strings.ReplaceAll(tok.Text(), `\"`, `"`))
				case "escape_symbol":
					return nil, synErrIncompletedEscSeq
				case "terminal_close":
					return newTerminalPatternToken(b.String()), nil
				}
			}
		case "colon":
			return newSymbolToken(tokenKindColon), nil
		case "or":
			return newSymbolToken(tokenKindOr), nil
		case "semicolon":
			return newSymbolToken(tokenKindSemicolon), nil
		case "modifier_marker":
			return newSymbolToken(tokenKindModifierMarker), nil
		case "action_leader":
			return newSymbolToken(tokenKindActionLeader), nil
		case "tree_node_open":
			return newSymbolToken(tokenKindTreeNodeOpen), nil
		case "tree_node_close":
			return newSymbolToken(tokenKindTreeNodeClose), nil
		case "position":
			// Remove '$' character and convert to an integer.
			num, err := strconv.Atoi(tok.Text()[1:])
			if err != nil {
				return nil, err
			}
			if num == 0 {
				return nil, synErrZeroPos
			}
			return newPositionToken(num), nil
		case "expansion":
			return newSymbolToken(tokenKindExpantion), nil
		default:
			return newInvalidToken(tok.Text()), nil
		}
	}
}