Skip to content

Commit

Permalink
Merge pull request #12 from 06wj/main
Browse files Browse the repository at this point in the history
Fixing bug in the preprocessor when use "\r" as a newline character
  • Loading branch information
AndrewRayCode authored Jan 27, 2024
2 parents 9d7a3b8 + e81319a commit 829c252
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/preprocessor/preprocessor-grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -156,43 +156,43 @@ control_line "control line"
return node('extension', { extension, name, colon, behavior });
}
)
wsEnd:[\n]? {
wsEnd:[\n\r]? {
return { ...line, wsEnd };
}

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

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

conditional
= ifPart:(
ifLine:if_line
wsEnd:[\n]
wsEnd:[\n\r]
body:text_or_control_lines? {
return { ...ifLine, body, wsEnd };
}
)
elseIfParts:(
token:ELIF
expression:constant_expression
wsEnd: [\n]
wsEnd: [\n\r]
elseIfBody:text_or_control_lines? {
return node('elseif', { token, expression, wsEnd, body: elseIfBody });
}
)*
elsePart:(
token:ELSE
wsEnd: [\n]
wsEnd: [\n\r]
elseBody:text_or_control_lines? {
return node('else', { token, wsEnd, body: elseBody });
}
)?
endif:ENDIF
wsEnd:[\n]? { // optional because the program can end with endif
wsEnd:[\n\r]? { // optional because the program can end with endif
return node('conditional', { ifPart, elseIfParts, elsePart, endif, wsEnd, });
}

Expand Down Expand Up @@ -339,7 +339,7 @@ comment
x:whitespace cc:comment { return xnil(x, cc); }
)* { return xnil(a, d.flat()); }

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

whitespace "whitespace" = $[ \t]+
8 changes: 8 additions & 0 deletions src/preprocessor/preprocessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,14 @@ outside endif
function_call line after program`);
});

test('different line breaks character', () => {
const program = '#ifndef x\rfloat a = 1.0;\r\n#endif';

const ast = parse(program);
const c = preprocessAst(ast);
expect(generate(ast)).toBe('float a = 1.0;\r\n');
});

/*
test('debug', () => {
const program = `
Expand Down

0 comments on commit 829c252

Please sign in to comment.