Skip to content

Commit 829c252

Browse files
Merge pull request #12 from 06wj/main
Fixing bug in the preprocessor when use "\r" as a newline character
2 parents 9d7a3b8 + e81319a commit 829c252

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/preprocessor/preprocessor-grammar.pegjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,43 +156,43 @@ control_line "control line"
156156
return node('extension', { extension, name, colon, behavior });
157157
}
158158
)
159-
wsEnd:[\n]? {
159+
wsEnd:[\n\r]? {
160160
return { ...line, wsEnd };
161161
}
162162

163163
// Any series of characters on the same line,
164164
// for example "abc 123" in "#define A abc 123"
165-
token_string "token string" = $([^\n]+)
165+
token_string "token string" = $([^\n\r]+)
166166

167167
// Any non-control line. Ending newline for text is optional because program
168168
// might end on a non-newline
169-
text "text" = $(!(whitespace? "#") [^\n]+ [\n]? / [\n])
169+
text "text" = $(!(whitespace? "#") [^\n\r]+ [\n\r]? / [\n\r])
170170

171171
conditional
172172
= ifPart:(
173173
ifLine:if_line
174-
wsEnd:[\n]
174+
wsEnd:[\n\r]
175175
body:text_or_control_lines? {
176176
return { ...ifLine, body, wsEnd };
177177
}
178178
)
179179
elseIfParts:(
180180
token:ELIF
181181
expression:constant_expression
182-
wsEnd: [\n]
182+
wsEnd: [\n\r]
183183
elseIfBody:text_or_control_lines? {
184184
return node('elseif', { token, expression, wsEnd, body: elseIfBody });
185185
}
186186
)*
187187
elsePart:(
188188
token:ELSE
189-
wsEnd: [\n]
189+
wsEnd: [\n\r]
190190
elseBody:text_or_control_lines? {
191191
return node('else', { token, wsEnd, body: elseBody });
192192
}
193193
)?
194194
endif:ENDIF
195-
wsEnd:[\n]? { // optional because the program can end with endif
195+
wsEnd:[\n\r]? { // optional because the program can end with endif
196196
return node('conditional', { ifPart, elseIfParts, elsePart, endif, wsEnd, });
197197
}
198198

@@ -339,7 +339,7 @@ comment
339339
x:whitespace cc:comment { return xnil(x, cc); }
340340
)* { return xnil(a, d.flat()); }
341341

342-
single_comment = $('//' [^\n]*)
342+
single_comment = $('//' [^\n\r]*)
343343
multiline_comment = $("/*" inner:(!"*/" i:. { return i; })* "*/")
344344

345345
whitespace "whitespace" = $[ \t]+

src/preprocessor/preprocessor.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,14 @@ outside endif
451451
function_call line after program`);
452452
});
453453

454+
test('different line breaks character', () => {
455+
const program = '#ifndef x\rfloat a = 1.0;\r\n#endif';
456+
457+
const ast = parse(program);
458+
const c = preprocessAst(ast);
459+
expect(generate(ast)).toBe('float a = 1.0;\r\n');
460+
});
461+
454462
/*
455463
test('debug', () => {
456464
const program = `

0 commit comments

Comments
 (0)