Repository: sjbarag/brs
Pull request #536 (2020-09-08 20:09:13)

fix(parser): add else token
# Change Summary

This adds support for the parser to record the `else` token location.


Patch:
diff --git a/src/parser/Parser.ts b/src/parser/Parser.ts
index 842c3a7..586c0cc 100644
--- a/src/parser/Parser.ts
+++ b/src/parser/Parser.ts
@@ -797,6 +797,7 @@ export class Parser {
             let thenToken: Token | undefined;
             let elseIfTokens: Token[] = [];
             let endIfToken: Token | undefined;
+            let elseToken: Token | undefined;
 
             /**
              * A simple wrapper around `check`, to make tests for a `then` identifier.
@@ -873,6 +874,7 @@ export class Parser {
                 }
 
                 if (blockEnd.kind === Lexeme.Else) {
+                    elseToken = blockEnd;
                     let maybeElseBranch = block(Lexeme.EndIf);
                     if (!maybeElseBranch) {
                         throw addError(peek(), "Expected 'end if' to terminate 'else' block");
@@ -952,6 +954,7 @@ export class Parser {
                     closingToken.kind !== Lexeme.Newline &&
                     (closingToken.kind === Lexeme.Else || match(Lexeme.Else))
                 ) {
+                    elseToken = closingToken;
                     let maybeElseBranch = block(Lexeme.Newline, Lexeme.Eof);
                     if (!maybeElseBranch) {
                         throw addError(peek(), `Expected a statement to follow 'else'`);
@@ -966,6 +969,7 @@ export class Parser {
                     then: thenToken,
                     elseIfs: elseIfTokens,
                     endIf: endIfToken,
+                    else: elseToken,
                 },
                 condition,
                 thenBranch,


Test patch:
diff --git a/test/parser/controlFlow/__snapshots__/If.test.js.snap b/test/parser/controlFlow/__snapshots__/If.test.js.snap
index 30f2eee..af1d61c 100644
--- a/test/parser/controlFlow/__snapshots__/If.test.js.snap
+++ b/test/parser/controlFlow/__snapshots__/If.test.js.snap
@@ -397,6 +397,23 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": Object {
+        "isReserved": true,
+        "kind": "Else",
+        "literal": undefined,
+        "location": Object {
+          "end": Object {
+            "column": 20,
+            "line": 7,
+          },
+          "file": "",
+          "start": Object {
+            "column": 16,
+            "line": 7,
+          },
+        },
+        "text": "else",
+      },
       "elseIfs": Array [
         Object {
           "isReserved": false,
@@ -649,6 +666,7 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": undefined,
       "elseIfs": Array [],
       "endIf": Object {
         "isReserved": false,
@@ -971,6 +989,23 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": Object {
+        "isReserved": true,
+        "kind": "Else",
+        "literal": undefined,
+        "location": Object {
+          "end": Object {
+            "column": 20,
+            "line": 4,
+          },
+          "file": "",
+          "start": Object {
+            "column": 16,
+            "line": 4,
+          },
+        },
+        "text": "else",
+      },
       "elseIfs": Array [],
       "endIf": Object {
         "isReserved": false,
@@ -1426,6 +1461,23 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": Object {
+        "isReserved": true,
+        "kind": "Else",
+        "literal": undefined,
+        "location": Object {
+          "end": Object {
+            "column": 20,
+            "line": 7,
+          },
+          "file": "",
+          "start": Object {
+            "column": 16,
+            "line": 7,
+          },
+        },
+        "text": "else",
+      },
       "elseIfs": Array [
         Object {
           "isReserved": false,
@@ -1736,6 +1788,23 @@ Array [
               "type": "Block",
             },
             "tokens": Object {
+              "else": Object {
+                "isReserved": true,
+                "kind": "Else",
+                "literal": undefined,
+                "location": Object {
+                  "end": Object {
+                    "column": 24,
+                    "line": 7,
+                  },
+                  "file": "",
+                  "start": Object {
+                    "column": 20,
+                    "line": 7,
+                  },
+                },
+                "text": "else",
+              },
               "elseIfs": Array [
                 Object {
                   "isReserved": false,
@@ -1990,6 +2059,7 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": undefined,
       "elseIfs": Array [],
       "endIf": undefined,
       "if": Object {
@@ -2134,6 +2204,7 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": undefined,
       "elseIfs": Array [],
       "endIf": Object {
         "isReserved": false,
@@ -2471,6 +2542,23 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": Object {
+        "isReserved": true,
+        "kind": "Else",
+        "literal": undefined,
+        "location": Object {
+          "end": Object {
+            "column": 40,
+            "line": 2,
+          },
+          "file": "",
+          "start": Object {
+            "column": 36,
+            "line": 2,
+          },
+        },
+        "text": "else",
+      },
       "elseIfs": Array [],
       "endIf": Object {
         "isReserved": false,
@@ -2724,6 +2812,23 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": Object {
+        "isReserved": true,
+        "kind": "Else",
+        "literal": undefined,
+        "location": Object {
+          "end": Object {
+            "column": 50,
+            "line": 2,
+          },
+          "file": "",
+          "start": Object {
+            "column": 46,
+            "line": 2,
+          },
+        },
+        "text": "else",
+      },
       "elseIfs": Array [],
       "endIf": undefined,
       "if": Object {
@@ -3091,6 +3196,22 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": Object {
+        "isReserved": true,
+        "kind": "Else",
+        "literal": undefined,
+        "location": Object {
+          "end": Object {
+            "column": -9,
+            "line": -9,
+          },
+          "start": Object {
+            "column": -9,
+            "line": -9,
+          },
+        },
+        "text": "else",
+      },
       "elseIfs": Array [
         Object {
           "isReserved": false,
@@ -3278,6 +3399,7 @@ Array [
             "type": "Block",
           },
           "tokens": Object {
+            "else": undefined,
             "elseIfs": Array [],
             "endIf": undefined,
             "if": Object {
@@ -3321,6 +3443,23 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": Object {
+        "isReserved": true,
+        "kind": "Else",
+        "literal": undefined,
+        "location": Object {
+          "end": Object {
+            "column": 16,
+            "line": 4,
+          },
+          "file": "",
+          "start": Object {
+            "column": 12,
+            "line": 4,
+          },
+        },
+        "text": "else",
+      },
       "elseIfs": Array [],
       "endIf": Object {
         "isReserved": false,
@@ -4292,6 +4431,7 @@ Array [
             "type": "Block",
           },
           "tokens": Object {
+            "else": undefined,
             "elseIfs": Array [],
             "endIf": undefined,
             "if": Object {
@@ -4335,6 +4475,23 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": Object {
+        "isReserved": true,
+        "kind": "Else",
+        "literal": undefined,
+        "location": Object {
+          "end": Object {
+            "column": 16,
+            "line": 7,
+          },
+          "file": "",
+          "start": Object {
+            "column": 12,
+            "line": 7,
+          },
+        },
+        "text": "else",
+      },
       "elseIfs": Array [
         Object {
           "isReserved": false,
@@ -4524,6 +4681,7 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": undefined,
       "elseIfs": Array [],
       "endIf": undefined,
       "if": Object {
@@ -4762,6 +4920,22 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": Object {
+        "isReserved": true,
+        "kind": "Else",
+        "literal": undefined,
+        "location": Object {
+          "end": Object {
+            "column": -9,
+            "line": -9,
+          },
+          "start": Object {
+            "column": -9,
+            "line": -9,
+          },
+        },
+        "text": "else",
+      },
       "elseIfs": Array [],
       "endIf": undefined,
       "if": Object {
@@ -5127,6 +5301,22 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": Object {
+        "isReserved": true,
+        "kind": "Else",
+        "literal": undefined,
+        "location": Object {
+          "end": Object {
+            "column": -9,
+            "line": -9,
+          },
+          "start": Object {
+            "column": -9,
+            "line": -9,
+          },
+        },
+        "text": "else",
+      },
       "elseIfs": Array [
         Object {
           "isReserved": false,
@@ -5266,6 +5456,7 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": undefined,
       "elseIfs": Array [],
       "endIf": Object {
         "isReserved": false,
@@ -5527,6 +5718,23 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": Object {
+        "isReserved": true,
+        "kind": "Else",
+        "literal": undefined,
+        "location": Object {
+          "end": Object {
+            "column": 74,
+            "line": 2,
+          },
+          "file": "",
+          "start": Object {
+            "column": 70,
+            "line": 2,
+          },
+        },
+        "text": "else",
+      },
       "elseIfs": Array [
         Object {
           "isReserved": false,
@@ -5705,6 +5913,7 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": undefined,
       "elseIfs": Array [],
       "endIf": Object {
         "isReserved": false,
@@ -5923,6 +6132,7 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": undefined,
       "elseIfs": Array [],
       "endIf": Object {
         "isReserved": false,
@@ -6291,6 +6501,23 @@ Array [
               "type": "Block",
             },
             "tokens": Object {
+              "else": Object {
+                "isReserved": true,
+                "kind": "Else",
+                "literal": undefined,
+                "location": Object {
+                  "end": Object {
+                    "column": 20,
+                    "line": 7,
+                  },
+                  "file": "",
+                  "start": Object {
+                    "column": 16,
+                    "line": 7,
+                  },
+                },
+                "text": "else",
+              },
               "elseIfs": Array [
                 Object {
                   "isReserved": false,
@@ -6529,6 +6756,7 @@ Array [
       "type": "Block",
     },
     "tokens": Object {
+      "else": undefined,
       "elseIfs": Array [],
       "endIf": Object {
         "isReserved": false,
diff --git a/test/parser/statement/__snapshots__/Block.test.js.snap b/test/parser/statement/__snapshots__/Block.test.js.snap
index 7e807d1..067c3ad 100644
--- a/test/parser/statement/__snapshots__/Block.test.js.snap
+++ b/test/parser/statement/__snapshots__/Block.test.js.snap
@@ -677,6 +677,23 @@ Array [
               "type": "Block",
             },
             "tokens": Object {
+              "else": Object {
+                "isReserved": true,
+                "kind": "Else",
+                "literal": undefined,
+                "location": Object {
+                  "end": Object {
+                    "column": 20,
+                    "line": 7,
+                  },
+                  "file": "",
+                  "start": Object {
+                    "column": 16,
+                    "line": 7,
+                  },
+                },
+                "text": "else",
+              },
               "elseIfs": Array [
                 Object {
                   "isReserved": false,