Selaa lähdekoodia

MPIDE-27: store error strings in array

No more error string literals splattered in the parser. The error
function uses the lookup table once again. Minor formatting changes.
Austin Meagher 10 vuotta sitten
vanhempi
commit
33609d3521
2 muutettua tiedostoa jossa 24 lisäystä ja 16 poistoa
  1. 18 10
      src/lib/parser/modellang.pegjs
  2. 6 6
      src/lib/parser/test/parserIntegration.js

+ 18 - 10
src/lib/parser/modellang.pegjs

@@ -11,14 +11,21 @@
         if (typeof msg === "number") msg = "ERR" + msg + ": " + errors[msg];
         if (suffix) msg += suffix;
         return new SyntaxError(msg,
-            opts.expected,
-            opts.found,
+            opts.expected || null,
+            opts.found || null,
             offset(),
             opts.line || line(),
             opts.col || column()
         );
     };
 
+    var errors = [
+        "reference to an undefined system",
+        "reference to system in a behavior",
+        "scope lower bound exceeds its upper bound",
+        "definition is recursive"
+    ];
+
     var scopify = function scopify(i, q) {
         if (q === null) q = {min:1, max:1};
         i.scope = q;
@@ -83,26 +90,27 @@
         for (var i in model.interactions) {
             model.interactions[i].body.forEach(function (sys) {
                 if (sys.type === "Selector" && !model.systems[sys.system])
-                    model.errors.push(err("ERROR: reference to an undefined system (" + sys.system + ")", null, sys.location));
+                    model.errors.push(err(0, ` (${sys.system})`, sys.location));
             });
         }
 
         for (var t in model.triggers) {
             var sys = model.triggers[t].on;
             if (sys.type === "Selector" && !model.systems[sys.system])
-                model.errors.push(err("ERROR: reference to an undefined system (" + sys.system + ")", null, sys.location));
+                model.errors.push(err(0, ` (${sys.system})`, sys.location));
         }
 
         for (var i in model.init) {
             var sys = model.init[i].system;
             if (sys && !model.systems[sys])
-                model.errors.push(err("ERROR: reference to an undefined system (" + sys + ")", null, model.init[i].location));
+                model.errors.push(err(0, ` (${sys})`, model.init[i].location));
         }
 
         for (var b in model.behaviors) {
-            model.behaviors[b].refs.forEach(function (ref) {
+            var bhv = model.behaviors[b];
+            bhv.refs.forEach(function (ref) {
                 if (model.systems[ref])
-                    model.errors.push(err("ERROR: reference to system (" + ref + ") in a behavior (" + model.behaviors[b].id + ")", null, model.behaviors[b].location));
+                    model.errors.push(err(1, ` (${ref} in ${bhv.id})`, bhv.location));
             });
         }
 
@@ -265,7 +273,7 @@ alternation
         var body = [head].concat(tail);
         for (var i = 0; i < body.length; i++) {
             if (body[i].scope.max && body[i].scope.min > body[i].scope.max)
-                ast.errors.push(err("ERROR: Scope (" + body[i].id + ") lower bound exceeds its upper bound"));
+                ast.errors.push(err(2,` (${body[i].id})`));
         }
         return new ast.Alternation(body);
     }
@@ -275,7 +283,7 @@ sequence
     {
         for (var i = 0; i < body.length; i++) {
             if (body[i].scope.max && body[i].scope.min > body[i].scope.max)
-                ast.errors.push(err("ERROR: Scope (" + body[i].id + ") lower bound exceeds its upper bound"));
+                ast.errors.push(err(2,` (${body[i].id})`));
         }
         return new ast.Sequence(body);
     }
@@ -319,7 +327,7 @@ event_ref   = id:ID
     {
         if (!ast.selector) {
             if (id === ast.toplevelname) {
-                ast.errors.push(err("ERROR: Definition is recursive (" + id + ")"));
+                ast.errors.push(err(3, ` (${id})`));
                 ast.toplevelname = null;
             } else ast.references.push(id);
         }

+ 6 - 6
src/lib/parser/test/parserIntegration.js

@@ -18,27 +18,27 @@ describe("Parser", function() {
 				let actual = Parser.parse(test.input, {}, parseOpts);
 				let expected = test.expected;
 
-				it("should parse systems correctly", function() {
+				it("should parse systems", function() {
 					assert.deepEqual(actual.systems, expected.systems);
 				});
 
-				it("should parse behaviors correctly", function() {
+				it("should parse behaviors", function() {
 					assert.deepEqual(actual.behaviors, expected.behaviors);
 				});
 
-				it("should parse interactions correctly", function() {
+				it("should parse interactions", function() {
 					assert.deepEqual(actual.interactions, expected.interactions);
 				});
 
-				it("should parse triggers correctly", function() {
+				it("should parse triggers", function() {
 					assert.deepEqual(actual.triggers, expected.triggers);
 				});
 
-				it("should parse inits correctly", function() {
+				it("should parse inits", function() {
 					assert.deepEqual(actual.init, expected.init);
 				});
 
-				it("should parse ASTs correctly", function() {
+				it("should parse ASTs", function() {
 					assert.deepEqual(actual.ast, expected.ast);
 				});
 			});