Skip to content

Commit 851315e

Browse files
committed
Add support for \ new line skips in BNFLexer
1 parent 741d391 commit 851315e

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

src/main/java/ko/carbonel/compiler/stream/lexer/bnf/BNFLexer.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,18 @@ protected BNFLexerToken getNext() {
4040
case "/" -> lexSlashOrComment();
4141
case "{" -> lexAttributeCode();
4242
case " " -> next();
43+
case "\\" -> skipNewLine();
4344
default -> lexNonTerminal();
4445
};
4546
}
4647

48+
private BNFLexerToken skipNewLine() {
49+
FileLocation location = reader.getLocation();
50+
reader.get();
51+
if (!Objects.equals(reader.get(), "\n")) throw new LexerError(location, "Expected '\\n'");
52+
return getNext();
53+
}
54+
4755
private BNFLexerToken lexAttributeCode() {
4856
final boolean[] found = {false};
4957
return genericLexByArrayFunc(symbol -> {

src/test/resources/syntax/main_attr.bnf

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ExpAdRF ::= ExpAdR | "λ"
1717
ExpAdR ::= OpAd ExpMul ExpAdRF
1818
ExpAd ::= ExpMul ExpAdRF
1919
ExpAndF ::= ExpAndR | "λ"
20-
ExpAndR ::= "&&" {st.expWrapBin(List.of("&&"))} ExpEq ExpAndF
20+
ExpAndR ::= "&&" {st.expWrapBin(List.of(v_0))} ExpEq ExpAndF
2121
ExpAnd ::= ExpEq ExpAndF
2222
ExpEqRF ::= ExpEqR | "λ"
2323
ExpEqR ::= OpEq {st.expWrapBin(v_0)} ExpRel ExpEqRF
@@ -26,7 +26,7 @@ ExpMulF ::= ExpMulR | "λ"
2626
ExpMulR ::= OpMul ExpUn ExpMulF
2727
ExpMul ::= ExpUn ExpMulF
2828
ExpOrF ::= ExpOrR | "λ"
29-
ExpOrR ::= "||" {st.expWrapBin(List.of("||"))} ExpAnd ExpOrF
29+
ExpOrR ::= "||" {st.expWrapBin(List.of(v_0))} ExpAnd ExpOrF
3030
ExpOr ::= ExpAnd ExpOrF
3131
ExpRelF ::= OpRel {st.expWrapBin(v_0)} ExpAd | "λ"
3232
ExpRel ::= ExpAd ExpRelF
@@ -68,7 +68,13 @@ StmtFFF ::= "λ" | "ELSE" {st.ifElse()} Stmt
6868
StmtF ::= ";" | ExpOr ";"
6969
Stmt_starF ::= Stmt_star | "λ"
7070
Stmt_star ::= Stmt Stmt_starF
71-
Stmt ::= "{" MethodF | {st.newIf()} "IF" "(" ExpOr ")" {st.ifBlock()} Stmt StmtFFF {st.end()} | {st.newReturn()} "RETURN" StmtF {st.end()} | {st.newEmpty()} ";" {st.end()} | {st.newAssign()} Assign ";" {st.end()} | {st.newExpr()} "(" ExpOr ")" ";" {st.end()} | {st.newWhile()} "WHILE" "(" ExpOr ")"{st.whileBody()} Stmt {st.end()}
71+
Stmt ::= "{" MethodF \
72+
| {st.newIf()} "IF" "(" ExpOr ")" {st.ifBlock()} Stmt StmtFFF {st.end()} \
73+
| {st.newReturn()} "RETURN" StmtF {st.end()} \
74+
| {st.newEmpty()} ";" {st.end()} \
75+
| {st.newAssign()} Assign ";" {st.end()} \
76+
| {st.newExpr()} "(" ExpOr ")" ";" {st.end()} \
77+
| {st.newWhile()} "WHILE" "(" ExpOr ")"{st.whileBody()} Stmt {st.end()}
7278
Type ::= PrimType {!v_0} | "CLASS_ID" {!} | "ARRAY" PrimType {!Utils.concat(v_0, v_1)}
7379
VarF ::= "λ" | Chain | "[" ExpOr "]"
7480
VarsF ::= "λ" {!List.of()} | "," Vars {!v_1}

0 commit comments

Comments
 (0)