]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
rework identifier parsing
authorMichael D. Lowis <mike@mdlowis.com>
Wed, 23 May 2018 02:41:20 +0000 (22:41 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Wed, 23 May 2018 02:41:20 +0000 (22:41 -0400)
source/parser.c
spec/lexer_spec.rb

index 0d13b7a16a67a4d4ec9a9b5b6d0b81bf33e2e723..ab0f2df279298e48796cf760f124c95c48cf5149 100644 (file)
@@ -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) {
index 030bb2e71f60f3d691a31220af15f77d71caa1ec..8936dbc99ec7d7fa2edb945d6caea318fad1dc9c 100644 (file)
@@ -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