Skip to content

Commit 1ae9413

Browse files
authored
Displays file line/column on lexing
1 parent c0d50fb commit 1ae9413

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

ParserGenerator.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,8 @@ public class ParsingTreeNode
13071307
{
13081308
public string Produnction;
13091309
public string Contents;
1310-
public string UserContents;
1310+
public object UserContents;
1311+
public int ProductionRuleIndex;
13111312
public ParsingTreeNode Parent;
13121313
public List<ParsingTreeNode> Childs;
13131314

@@ -1318,15 +1319,13 @@ public static ParsingTreeNode NewNode(string production)
13181319
public static ParsingTreeNode NewNode(string production, string contents)
13191320
=> new ParsingTreeNode { Parent = null, Childs = new List<ParsingTreeNode>(), Produnction = production, Contents = contents };
13201321
}
1321-
1322-
int tree_index;
1322+
13231323
ParsingTreeNode root;
13241324

1325-
public ParsingTree(int index = 0)
1325+
public ParsingTree(ParsingTreeNode root)
13261326
{
1327-
tree_index = index;
1327+
this.root = root;
13281328
}
1329-
13301329
}
13311330

13321331
/// <summary>
@@ -1371,6 +1370,8 @@ public void Clear()
13711370
treenode_stack.Clear();
13721371
}
13731372

1373+
public ParsingTree Tree => new ParsingTree(treenode_stack.Peek());
1374+
13741375
public string Stack() => string.Join(" ", new Stack<int>(state_stack));
13751376

13761377
public void Insert(string token_name, string contents) => Insert(symbol_name_index[token_name], contents);
@@ -1426,6 +1427,7 @@ private void reduce(int index)
14261427
state_stack.Push(goto_table[state_stack.Peek()][group_table[reduce_production]]);
14271428

14281429
var reduction_parent = ParsingTree.ParsingTreeNode.NewNode(symbol_index_name[group_table[reduce_production]]);
1430+
reduction_parent.ProductionRuleIndex = reduce_production - 1;
14291431
reduce_treenodes.ForEach(x => x.Parent = reduction_parent);
14301432
reduction_parent.Contents = string.Join("", reduce_treenodes.Select(x => x.Contents));
14311433
reduction_parent.Childs = reduce_treenodes;

ScannerGenerator.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private diagram make_nfa(string pattern)
149149
break;
150150

151151
case ')':
152-
if (opstack.Peek() != '(')
152+
if (opstack.Count == 0 || opstack.Peek() != '(')
153153
{
154154
build_errors.Add($"[regex] {i} no opener!");
155155
return null;
@@ -896,6 +896,8 @@ public class Scanner
896896
bool err = false;
897897
int latest_pos;
898898
List<int> err_pos;
899+
int current_line;
900+
int current_column;
899901

900902
public Scanner(int[][] transition_table, string[] accept_table)
901903
{
@@ -907,6 +909,8 @@ public void AllocateTarget(string literal)
907909
{
908910
target = literal;
909911
pos = 0;
912+
current_line = 0;
913+
current_column = 0;
910914
err_pos = new List<int>();
911915
err = false;
912916
}
@@ -923,12 +927,15 @@ public bool Error()
923927

924928
public int Position { get { return latest_pos; } }
925929

926-
public Tuple<string, string> Next()
930+
public Tuple<string, string, int, int> Next()
927931
{
928932
var builder = new StringBuilder();
929933
var node_pos = 0;
930934
latest_pos = pos;
931935

936+
int cur_line = current_line;
937+
int cur_column = current_column;
938+
932939
for (; pos < target.Length; pos++)
933940
{
934941
int next_transition = transition_table[node_pos][target[pos]];
@@ -944,6 +951,9 @@ public Tuple<string, string> Next()
944951
latest_pos = pos;
945952
pos--;
946953
node_pos = 0;
954+
current_column--;
955+
cur_line = current_line;
956+
cur_column = current_column;
947957
continue;
948958
}
949959
if (accept_table[node_pos] == null)
@@ -952,20 +962,21 @@ public Tuple<string, string> Next()
952962
err_pos.Add(pos);
953963
continue;
954964
}
955-
return new Tuple<string, string>(accept_table[node_pos], builder.ToString());
965+
return new Tuple<string, string, int, int> (accept_table[node_pos], builder.ToString(), cur_line + 1, cur_column + 1);
956966

957967
default:
968+
if (target[pos] == '\n') { current_line++; current_column = 1; } else current_column++;
958969
builder.Append(target[pos]);
959970
break;
960971
}
961972

962973
node_pos = next_transition;
963974
}
964975

965-
return new Tuple<string, string>(accept_table[node_pos], builder.ToString());
976+
return new Tuple<string, string, int, int> (accept_table[node_pos], builder.ToString(), cur_line + 1, cur_column + 1);
966977
}
967978

968-
public Tuple<string, string> Lookahead()
979+
public Tuple<string, string, int, int> Lookahead()
969980
{
970981
var npos = pos;
971982
var result = Next();

0 commit comments

Comments
 (0)