@@ -65,14 +65,17 @@ public static int skipWhitespace(Parser parser, int tokenIndex, List<LexerToken>
65
65
66
66
case OPERATOR :
67
67
if (token .text .equals ("#" )) {
68
+ // # line directive must appear at the beginning of the line
69
+ boolean maybeLineDirective = tokenIndex == 0 || tokens .get (tokenIndex - 1 ).type == LexerTokenType .NEWLINE ;
70
+
68
71
// Skip optional whitespace after '#'
69
72
tokenIndex ++;
70
73
while (tokenIndex < tokens .size () && tokens .get (tokenIndex ).type == LexerTokenType .WHITESPACE ) {
71
74
tokenIndex ++;
72
75
}
73
76
// Check if it's a "# line" directive
74
- if (tokenIndex < tokens .size () && tokens .get (tokenIndex ).text .equals ("line" )) {
75
- tokenIndex = parseLineDirective (tokenIndex , tokens );
77
+ if (maybeLineDirective && tokenIndex < tokens .size () && tokens .get (tokenIndex ).text .equals ("line" )) {
78
+ tokenIndex = parseLineDirective (parser , tokenIndex , tokens );
76
79
}
77
80
// Skip comment until end of line
78
81
while (tokenIndex < tokens .size () && tokens .get (tokenIndex ).type != LexerTokenType .NEWLINE ) {
@@ -103,7 +106,7 @@ public static int skipWhitespace(Parser parser, int tokenIndex, List<LexerToken>
103
106
return tokenIndex ;
104
107
}
105
108
106
- private static int parseLineDirective (int tokenIndex , List <LexerToken > tokens ) {
109
+ private static int parseLineDirective (Parser parser , int tokenIndex , List <LexerToken > tokens ) {
107
110
tokenIndex ++; // Skip 'line'
108
111
// Skip optional whitespace after 'line'
109
112
while (tokenIndex < tokens .size () && tokens .get (tokenIndex ).type == LexerTokenType .WHITESPACE ) {
@@ -115,6 +118,11 @@ private static int parseLineDirective(int tokenIndex, List<LexerToken> tokens) {
115
118
try {
116
119
int lineNumber = Integer .parseInt (lineNumberStr );
117
120
tokenIndex ++;
121
+
122
+ // Update the context (ErrorMessageUtil instance) with the new line number
123
+ parser .ctx .errorUtil .setLineNumber (lineNumber - 1 );
124
+ parser .ctx .errorUtil .setTokenIndex (tokenIndex - 1 );
125
+
118
126
// Skip optional whitespace before filename
119
127
while (tokenIndex < tokens .size () && tokens .get (tokenIndex ).type == LexerTokenType .WHITESPACE ) {
120
128
tokenIndex ++;
@@ -130,8 +138,9 @@ private static int parseLineDirective(int tokenIndex, List<LexerToken> tokens) {
130
138
if (tokenIndex < tokens .size () && tokens .get (tokenIndex ).type == LexerTokenType .OPERATOR && tokens .get (tokenIndex ).text .equals ("\" " )) {
131
139
tokenIndex ++; // Skip closing quote
132
140
String filename = filenameBuilder .toString ();
133
- // Handle the line number and filename as needed
134
- // For example, update a state or context object
141
+
142
+ // Update the context (ErrorMessageUtil instance) with the new file name
143
+ parser .ctx .errorUtil .setFileName (filename );
135
144
}
136
145
}
137
146
} catch (NumberFormatException e ) {
0 commit comments