-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.cs
144 lines (115 loc) · 4.54 KB
/
Lexer.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
[Serializable]
public class Lexer
{
const char SoT = '\0';
readonly IList<Rule> grammar;
Lexer(Bimachine bm, IList<Rule> grammar)
{
this.Bm = bm;
this.grammar = grammar;
}
public Bimachine Bm { get; private set; }
public InputStream Input { get; set; }
public IEnumerable<Token> GetNextToken()
{
var rPath = this.Bm.Right.ReverseRecognitionPath(this.Input);
if (rPath.Count != this.Input.Size + 1)
throw new ArgumentException(
$"Unrecognized input symbol. {this.Input.CharAt(this.Input.Size - rPath.Count)}");
var leftState = this.Bm.Left.Initial;
var token = new StringBuilder();
var tokenIndex = 0;
var tokenStartPos = 0;
for (this.Input.SetToStart(); !this.Input.IsExhausted; this.Input.MoveForward())
{
var ch = this.Input.Peek();
var rightIndex = rPath.Count - 2 - this.Input.Pos;
var triple = (leftState, ch, rPath[rightIndex]);
if (!this.Bm.Output.ContainsKey(triple))
throw new ArgumentException($"Unrecognized token '{token.ToString() + ch}'");
token.Append(Bm.Output[triple]);
if (token[token.Length - 1] > RegExp.AlphabetMax)
{
if (token[0] != SoT)
{
var unrecognizedToken = new StringBuilder();
for (var k = 0; k < token.Length && token[k] != SoT; k++)
unrecognizedToken.Append(token[k]);
throw new ArgumentException($"Invalid token \"{unrecognizedToken.ToString()}\"");
}
var typeIndex = token[token.Length - 1] - RegExp.AlphabetMax - 1;
token.Remove(0, 1); // remove the start of token marker
token.Remove(token.Length - 1, 1); // remove the end of token marker
yield return new Token
{
Index = tokenIndex,
Position = (tokenStartPos, this.Input.Pos),
Text = token.ToString(),
Type = this.grammar[int.Parse(typeIndex.ToString())].Name
};
token.Clear();
tokenIndex++;
tokenStartPos = this.Input.Pos + 1;
}
if (!this.Bm.Left.Transitions.ContainsKey((leftState, ch)))
throw new ArgumentException($"Unrecognized input. {ch}");
leftState = this.Bm.Left.Transitions[(leftState, ch)];
}
if (token.Length > 0)
throw new ArgumentException($"Unrecognized token '{token.ToString()}'");
}
public static Lexer Create(IList<Rule> grammar)
{
var tokenFsts = new List<Fst>();
for (int i = 0; i < grammar.Count; i++)
{
var ruleFsa = new RegExp(grammar[i].Pattern).Automaton;
var eot = (char)(RegExp.AlphabetMax + 1 + i);
// { <ε,SoT> } · Id(L(R)) · { <ε,EoT_R> }
var tokenFst = FstBuilder.FromWordPair(string.Empty, SoT.ToString())
.Concat(ruleFsa.Identity())
.Concat(FstBuilder.FromWordPair(string.Empty, eot.ToString()));
tokenFsts.Add(tokenFst);
}
var unionTokenFst = tokenFsts.Aggregate((u, f) => u.Union(f));
var alphabet = unionTokenFst.InputAlphabet
.Where(x => !string.IsNullOrEmpty(x))
.Select(c => c.Single())
.ToHashSet();
var lmlFst = unionTokenFst.ToLmlRewriter(alphabet);
var bm = lmlFst.ToBimachine(alphabet);
return new Lexer(bm, grammar);
}
public void ExportToFile(string path)
{
var stream = new FileStream(path, FileMode.Create, FileAccess.Write);
var formatter = new BinaryFormatter();
formatter.Serialize(
stream,
new LexerExport
{
grammar = this.grammar,
bm = this.Bm.PseudoMinimal()
});
stream.Close();
}
public static Lexer LoadFromFile(string path)
{
var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
var formatter = new BinaryFormatter();
var exp = (LexerExport)formatter.Deserialize(stream);
return new Lexer(exp.bm, exp.grammar);
}
[Serializable]
class LexerExport
{
public IList<Rule> grammar;
public Bimachine bm;
}
}