123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- "use strict";
- var Class = require("rtl.js").Class;
- var Code = require("js/Code.js");
- var Context = require("context.js");
- var Errors = require("js/Errors.js");
- var oc = require("oc.js");
- var RTL = require("rtl_code.js").RTL;
- var Scope = require("js/Scope.js");
- var Stream = require("js/Stream.js");
- var Test = require("test.js");
- var TestError = Test.TestError;
- function context(grammar, source){
- return {grammar: grammar, source: source};
- }
- function pass(/*...*/){return Array.prototype.slice.call(arguments);}
- function fail(/*...*/){return Array.prototype.slice.call(arguments);}
- var TestModuleGenerator = Class.extend({
- init: function TestModuleGenerator(){},
- prolog: function(){return undefined;},
- epilog: function(){return undefined;}
- });
- var TestContext = Context.Context.extend({
- init: function TestContext(language){
- Context.Context.prototype.init.call(
- this,
- { codeGenerator: Code.nullGenerator(),
- moduleGenerator: function(){return new TestModuleGenerator();},
- rtl: new RTL(),
- types: language.types,
- stdSymbols: language.stdSymbols
- });
- this.pushScope(Scope.makeModule("test", language.stdSymbols));
- },
- qualifyScope: function(){return "";}
- });
- function makeContext(language){return new TestContext(language);}
- function testWithSetup(setup, pass, fail){
- return function(){
- var test = setup();
- var i;
- for(i = 0; i < pass.length; ++i)
- test.expectOK(pass[i]);
-
- if (fail)
- for(i = 0; i < fail.length; ++i){
- var f = fail[i];
- test.expectError(f[0], f[1]);
- }
- };
- }
- function parseInContext(grammar, s, context){
- var stream = Stream.make(s);
- if (!grammar(stream, context) || !Stream.eof(stream))
- throw new Errors.Error("not parsed");
- }
- function runAndHandleErrors(action, s, handlerError){
- try {
- action(s);
- }
- catch (x){
- if (!(x instanceof Errors.Error))
- throw new Error("'" + s + "': " + x + "\n"
- + (x.stack ? x.stack : "(no stack)"));
-
- if (handlerError)
- handlerError(x);
- //else
- // throw x;
- // console.log(s + ": " + x);
- return false;
- }
- return true;
- }
- function setup(run){
- return {
- expectOK: function(s){
- function handleError(e){throw new TestError(s + "\n\t" + e);}
- if (!runAndHandleErrors(run, s, handleError))
- throw new TestError(s + ": not parsed");
- },
- expectError: function(s, error){
- function handleError(actualError){
- var sErr = actualError.toString();
- if (sErr != error)
- throw new TestError(s + "\n\texpected error: " + error + "\n\tgot: " + sErr );
- }
- if (runAndHandleErrors(run, s, handleError))
- throw new TestError(s + ": should not be parsed, expect error: " + error);
- }
- };
- }
- function parseUsingGrammar(parser, language, s, cxFactory){
- var baseContext = makeContext(language);
- var context = cxFactory ? cxFactory(baseContext) : baseContext;
- parseInContext(parser, s, context);
- context.currentScope().close();
- }
- function setupParser(parser, language, contextFactory){
- function parseImpl(s){
- return parseUsingGrammar(parser, language, s, contextFactory);
- }
- return setup(parseImpl);
- }
- function setupWithContext(grammar, contextGrammar, language, source){
- function innerMakeContext(){
- var context = makeContext(language);
- try {
- parseInContext(contextGrammar, source, context);
- }
- catch (x) {
- if (x instanceof Errors.Error)
- throw new TestError("setup error: " + x + "\n" + source);
- throw x;
- }
- return context;
- }
- return setupParser(grammar, language, innerMakeContext);
- }
- function testWithContext(context, contextGrammar, language, pass, fail){
- return testWithSetup(
- function(){return setupWithContext(context.grammar, contextGrammar, language, context.source);},
- pass,
- fail);
- }
- function testWithGrammar(parser, language, pass, fail){
- return testWithSetup(
- function(){return setupParser(parser, language);},
- pass,
- fail);
- }
- var TestContextWithModule = TestContext.extend({
- init: function(module, language){
- TestContext.prototype.init.call(this, language);
- this.__module = module;
- },
- findModule: function(){return this.__module;}
- });
- function testWithModule(src, language, pass, fail){
- var grammar = language.grammar;
- return testWithSetup(
- function(){
- var imported = oc.compileModule(grammar, Stream.make(src), makeContext(language));
- var module = imported.symbol().info();
- return setup(function(s){
- oc.compileModule(grammar,
- Stream.make(s),
- new TestContextWithModule(module, language));
- });},
- pass,
- fail);
- }
- exports.context = context;
- exports.pass = pass;
- exports.fail = fail;
- exports.setupParser = setupParser;
- exports.testWithContext = testWithContext;
- exports.testWithGrammar = testWithGrammar;
- exports.testWithModule = testWithModule;
- exports.testWithSetup = testWithSetup;
|