Преглед изворни кода

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 година
родитељ
комит
33609d3521
2 измењених фајлова са 24 додато и 16 уклоњено
  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 (typeof msg === "number") msg = "ERR" + msg + ": " + errors[msg];
         if (suffix) msg += suffix;
         if (suffix) msg += suffix;
         return new SyntaxError(msg,
         return new SyntaxError(msg,
-            opts.expected,
-            opts.found,
+            opts.expected || null,
+            opts.found || null,
             offset(),
             offset(),
             opts.line || line(),
             opts.line || line(),
             opts.col || column()
             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) {
     var scopify = function scopify(i, q) {
         if (q === null) q = {min:1, max:1};
         if (q === null) q = {min:1, max:1};
         i.scope = q;
         i.scope = q;
@@ -83,26 +90,27 @@
         for (var i in model.interactions) {
         for (var i in model.interactions) {
             model.interactions[i].body.forEach(function (sys) {
             model.interactions[i].body.forEach(function (sys) {
                 if (sys.type === "Selector" && !model.systems[sys.system])
                 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) {
         for (var t in model.triggers) {
             var sys = model.triggers[t].on;
             var sys = model.triggers[t].on;
             if (sys.type === "Selector" && !model.systems[sys.system])
             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) {
         for (var i in model.init) {
             var sys = model.init[i].system;
             var sys = model.init[i].system;
             if (sys && !model.systems[sys])
             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) {
         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])
                 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);
         var body = [head].concat(tail);
         for (var i = 0; i < body.length; i++) {
         for (var i = 0; i < body.length; i++) {
             if (body[i].scope.max && body[i].scope.min > body[i].scope.max)
             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);
         return new ast.Alternation(body);
     }
     }
@@ -275,7 +283,7 @@ sequence
     {
     {
         for (var i = 0; i < body.length; i++) {
         for (var i = 0; i < body.length; i++) {
             if (body[i].scope.max && body[i].scope.min > body[i].scope.max)
             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);
         return new ast.Sequence(body);
     }
     }
@@ -319,7 +327,7 @@ event_ref   = id:ID
     {
     {
         if (!ast.selector) {
         if (!ast.selector) {
             if (id === ast.toplevelname) {
             if (id === ast.toplevelname) {
-                ast.errors.push(err("ERROR: Definition is recursive (" + id + ")"));
+                ast.errors.push(err(3, ` (${id})`));
                 ast.toplevelname = null;
                 ast.toplevelname = null;
             } else ast.references.push(id);
             } 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 actual = Parser.parse(test.input, {}, parseOpts);
 				let expected = test.expected;
 				let expected = test.expected;
 
 
-				it("should parse systems correctly", function() {
+				it("should parse systems", function() {
 					assert.deepEqual(actual.systems, expected.systems);
 					assert.deepEqual(actual.systems, expected.systems);
 				});
 				});
 
 
-				it("should parse behaviors correctly", function() {
+				it("should parse behaviors", function() {
 					assert.deepEqual(actual.behaviors, expected.behaviors);
 					assert.deepEqual(actual.behaviors, expected.behaviors);
 				});
 				});
 
 
-				it("should parse interactions correctly", function() {
+				it("should parse interactions", function() {
 					assert.deepEqual(actual.interactions, expected.interactions);
 					assert.deepEqual(actual.interactions, expected.interactions);
 				});
 				});
 
 
-				it("should parse triggers correctly", function() {
+				it("should parse triggers", function() {
 					assert.deepEqual(actual.triggers, expected.triggers);
 					assert.deepEqual(actual.triggers, expected.triggers);
 				});
 				});
 
 
-				it("should parse inits correctly", function() {
+				it("should parse inits", function() {
 					assert.deepEqual(actual.init, expected.init);
 					assert.deepEqual(actual.init, expected.init);
 				});
 				});
 
 
-				it("should parse ASTs correctly", function() {
+				it("should parse ASTs", function() {
 					assert.deepEqual(actual.ast, expected.ast);
 					assert.deepEqual(actual.ast, expected.ast);
 				});
 				});
 			});
 			});