From: Michael D. Lowis Date: Wed, 23 May 2018 02:41:20 +0000 (-0400) Subject: rework identifier parsing X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=a918f521b79a232f0d1a6a504e33cd20c53580d8;p=proto%2Fsclpl.git rework identifier parsing --- diff --git a/source/parser.c b/source/parser.c index 0d13b7a..ab0f2df 100644 --- a/source/parser.c +++ b/source/parser.c @@ -7,6 +7,8 @@ static AST* const_definition(Parser* p); static AST* const_expression(Parser* p); static AST* definition(Parser* p); static AST* expression(Parser* p); +static AST* identifier(Parser* p); + static AST* function(Parser* p); static void type_annotation(Parser* p); static AST* literal(Parser* p); @@ -91,7 +93,7 @@ static AST* const_expression(Parser* p) { expr = const_expression(p); expect(p, T_RPAR); } else if (match(p, T_ID)) { - expr = Ident(expect_val(p, T_ID)); + expr = identifier(p); } else { expr = literal(p); } @@ -119,7 +121,8 @@ static AST* literal(Parser* p) { case T_STRING: case T_INT: case T_FLOAT: - ret = token_to_tree(expect_val(p, tok->type)); + ret = token_to_tree(tok); + tok->type = T_NONE; break; default: error(p, "Expected a literal"); @@ -139,6 +142,18 @@ static AST* token_to_tree(Tok* tok) { } } +static AST* identifier(Parser* p) { + Tok* tok = peek(p); + if (tok->type == T_ID) { + AST* ast = Ident(tok); + tok->type = T_NONE; + return ast; + } else { + error(p, "Expected an identifier"); + return NULL; + } +} + #if 0 static AST* definition(Parser* p) { diff --git a/spec/lexer_spec.rb b/spec/lexer_spec.rb index 030bb2e..8936dbc 100644 --- a/spec/lexer_spec.rb +++ b/spec/lexer_spec.rb @@ -109,10 +109,6 @@ describe "lexer" do it "should recognize 'c'" do expect(lexer('\c')).to eq ['T_CHAR:\c'] end - - it "should recognize invalid named characters as identifiers" do - expect(lexer('\foobar')).to eq ['T_ID:\foobar'] - end end context "numbers" do @@ -128,10 +124,6 @@ describe "lexer" do it "should recognize negitve integer with sign" do expect(lexer('-123')).to eq ['T_INT:-123'] end - - it "should recognize invalid ints as identifiers" do - expect(lexer('123a')).to eq ['T_ID:123a'] - end end context "radix integers" do @@ -150,10 +142,6 @@ describe "lexer" do it "should recognize decimal integer" do expect(lexer('0hf0f')).to eq ['T_INT:3855'] end - - it "should recognize invalid radix ints as identifiers" do - expect(lexer('0b012')).to eq ['T_ID:0b012'] - end end context "floating point" do @@ -168,10 +156,6 @@ describe "lexer" do it "should recognize negative float with sign" do expect(lexer('-123.0')).to eq ['T_FLOAT:-123.000000'] end - - it "should recognize invalid floats as identifiers" do - expect(lexer('123..0')).to eq ['T_ID:123..0'] - end end end