From 95b02a948385370092bae845a83fd5f14af82438 Mon Sep 17 00:00:00 2001 From: lemon Date: Thu, 1 Sep 2022 09:31:49 +0200 Subject: relax block comment lexing requirements --- src/cffc.hff | 1 + src/parse.cff | 26 ++++++++++++++++++++++++-- test/2.cff | 11 +++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/cffc.hff b/src/cffc.hff index 135e8d8..6ae2c6d 100644 --- a/src/cffc.hff +++ b/src/cffc.hff @@ -149,6 +149,7 @@ struct Parser { curloc Loc, eof bool, error bool, + is_comment bool, is_header bool, peekchr Option, peektok Option, diff --git a/src/parse.cff b/src/parse.cff index f5beb23..ce32475 100644 --- a/src/parse.cff +++ b/src/parse.cff @@ -339,6 +339,10 @@ fn lex(P *Parser) Tok { tok.loc = (P.tokloc = P.curloc); if isdigit(c = chrpeek(P)) { let s [80]u8 = {}; + if P.is_comment { + readtilsep(P, s[0::], #t); + return tok; + } if readtilsep(P, s[0::], #t) < 0 { fatal(P, tok.loc, "bad number literal"); } @@ -352,6 +356,10 @@ fn lex(P *Parser) Tok { } if isalpha(c) or c == '_' or c == '$' or c > 0x7F { let s [120]u8; + if P.is_comment { + readtilsep(P, s[0::], #t); + return tok; + } if readtilsep(P, s[0::], #f) < 0 { fatal(P, tok.loc, "identifier too long"); } @@ -370,12 +378,18 @@ fn lex(P *Parser) Tok { } if c == '#' { let s [100]u8 = {}; + if P.is_comment { + readtilhsep(P, s[0::], #t); + return tok; + } if readtilhsep(P, s[0::], #f) < 0 { fatal(P, P.tokloc, "invalid #keyword"); } switch { case streq(s, "#") and chrpeek(P) == '{'; chr(P); + let is_comment = P.is_comment; + P.is_comment = #t; let bal = 1; while bal != 0 { switch lex(P).t { @@ -384,6 +398,7 @@ fn lex(P *Parser) Tok { case :eof; fatal(P, tok.loc, "unterminated comment"); } } + P.is_comment = is_comment; return lex(P); case streq(s, "#"); tok.t = '#'; @@ -428,9 +443,12 @@ fn lex(P *Parser) Tok { delim == '"' ? "string" : "character"); } if c != '\\' { - str->push(c); + if !P.is_comment { + str->push(c); + } continue; } + if P.is_comment { continue; } switch ((c = chr(P))) { case 0, '\n'; fatal(P, P.tokloc, "unterminated %s literal", @@ -452,6 +470,8 @@ fn lex(P *Parser) Tok { } } + if P.is_comment { return tok; } + if delim == '"' { tok.t = :str; str->push('\0'); @@ -556,7 +576,9 @@ fn lex(P *Parser) Tok { tok.t = :eof; return tok; } - fatal(P, tok.loc, "stray %qc in program", c); + if !P.is_comment { + fatal(P, tok.loc, "stray %qc in program", c); + } } fn lexpeek(P *Parser) Tok { diff --git a/test/2.cff b/test/2.cff index 43fc148..65165d1 100644 --- a/test/2.cff +++ b/test/2.cff @@ -6,6 +6,17 @@ bitfield Foo : u16 { num 10 signed, } +#{ + Test block comment, + contains tokens " ", + this does not end the comment -> '}' + 8ab + #test + i`m having to use backtick instead of single quote for the apostrophe in this phrase, + so the main use is for commenting out code, + and inline comments to annotate e.g. parameter names +} + extern fn main() int { let foo Foo = {}; printf("0x%X: %d, %d, %d\n", foo.#raw, foo.tag, foo.flag, foo.num); -- cgit v1.2.3