From 1deb96b9fb16344f1bd7909d35139d1867a947c9 Mon Sep 17 00:00:00 2001 From: Steven Adams Date: Fri, 30 Sep 2011 13:25:25 -0700 Subject: [PATCH] Added include to BNF syntax for multi script type combinations, not finished though. Fixed calculator example. --- examples/calculator/calc.bnf | 7 +++---- examples/calculator/calc.calc | 1 - examples/calculator/index.js | 26 +++++++++++------------ lib/bnf.bnf.js | 4 +++- lib/compiler.js | 15 ++++++++++++++ lib/parser.js | 39 ++++++++++++++++++++++------------- 6 files changed, 59 insertions(+), 33 deletions(-) diff --git a/examples/calculator/calc.bnf b/examples/calculator/calc.bnf index c6c57eb..77e4a60 100644 --- a/examples/calculator/calc.bnf +++ b/examples/calculator/calc.bnf @@ -1,5 +1,4 @@ - ::= | + ::= ::= - ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 - ::= + | - | * | / - ::= "\r" "\n" | "\n" \ No newline at end of file + ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" + ::= "+" | "-" | "*" | "/" \ No newline at end of file diff --git a/examples/calculator/calc.calc b/examples/calculator/calc.calc index c20fc1e..07d91dc 100644 --- a/examples/calculator/calc.calc +++ b/examples/calculator/calc.calc @@ -1,2 +1 @@ -1+1 1+1 \ No newline at end of file diff --git a/examples/calculator/index.js b/examples/calculator/index.js index 250663a..d508956 100644 --- a/examples/calculator/index.js +++ b/examples/calculator/index.js @@ -1,21 +1,21 @@ -console.log( "This is a simple calculator language that looks at a file for basic math, and computes each line.") +console.log( "This is a simple calculator language that looks at a file for basic math, and computes each line."); -var i = require( "./calculator.bnf.js" ).interpreter; -i.AddTokenEventByName( "expression", function( token ){ - var left = i.SeekTokenByName( token, "number" ).text; - var type = i.SeekTokenByName( token, "type" ).text; - var right = i.SeekTokenByName( token, "number" ).text; +var events = { "expression":function( token ){ + var left = this.SeekNext( "number" ).text; + var type = this.SeekNext( "type" ).text; + var right = this.SeekNext( "number" ).text; //Equate var equate = -1; eval( "equate = " + left + type + right + ";" ); console.log( "File found syntax " + equate ); -} ); +} }; -var parserObject = require( "../../lib/parser.js" ).parser; -var parser = new parserObject( i ); - -parser.ParseScript( "calc.calc" ); - -//var interpreter = new require( "./parser.js" ).languageScript( "calc.bnf" ); +var parser = null; +var Compiler = require('../../lib/compiler.js').Compiler; +var compiler = new Compiler(); +compiler.CompileScript( __dirname + "/calc.bnf", "doq", function( interpreter ){ + var parser = compiler.CreateParser( interpreter, events ); + parser.ParseScript( __dirname + "/calc.calc" ); +} ); \ No newline at end of file diff --git a/lib/bnf.bnf.js b/lib/bnf.bnf.js index a3fa4d6..884f59c 100644 --- a/lib/bnf.bnf.js +++ b/lib/bnf.bnf.js @@ -28,6 +28,7 @@ i.AddRule( "lineEnd" ); i.AddRule( "list" ); i.AddRule( "orlist" ); i.AddRule( "term" ); +i.AddRule( "include" ); i.AddRule( "literal" ); i.AddRule( "ruleName" ); i.AddRule( "text" ); @@ -53,7 +54,8 @@ i.WriteRule( "ruleName", r.text ); i.WriteRule( "expression", i.Or( r.list, i.And( r.list, r.optWhitespace, r.orlist, r.optWhitespace, r.expression ) ) ); i.WriteRule( "orlist", "|" ); i.WriteRule( "list", i.Or( r.term, i.And( r.term, r.optWhitespace, r.list ) ) ); -i.WriteRule( "term", i.Or( r.literal, r.varRule ) ); +i.WriteRule( "term", i.Or( r.literal, r.varRule, r.include ) ); +i.WriteRule( "include", i.And( "#", r.text ) ); i.WriteRule( "varRule", i.And( "<", r.text, ">" ) ); i.WriteRule( "literal", i.Or( i.And( "'", r.textSingleQuotes, "'" ), i.And( '"', r.textDoubleQuotes, '"' ) ) ); i.WriteRule( "text", i.Or( r.char, i.And( r.char, r.text ) ) ); diff --git a/lib/compiler.js b/lib/compiler.js index e1c8b84..d91869d 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -49,6 +49,14 @@ exports.Compiler = function( ){ tokenStore.type = "rule"; tokenStore.text = token.tokens[0].text.substring( 1, token.tokens[0].text.length - 1 ); } + else if( token.tokens[0].name == "include" ){ + tokenStore.type = "include"; + tokenStore.text = token.tokens[0].text.substring( 1 ); + } + else{ + console.log( "Unknown token name '" + token.tokens[0].name + "' please add a parse event for it." ); + process.exit(); + } this.currentRuleContainer[ this.currentRuleContainer.length - 1 ].push( tokenStore ); }, "orlist":function( token ){ @@ -164,6 +172,13 @@ exports.Compiler = function( ){ else if( syntax.type == "rule" ){ return "r." + syntax.text; } + else if( syntax.type == "include" ){ + return "i.Include( '"+syntax.text+"' )"; + } + else{ + console.log( "Unknown syntax type, '" + syntax.type + "' please add a rule generator for it." ); + process.exit(); + } } /** * Generates a rule and trees into a rule output. diff --git a/lib/parser.js b/lib/parser.js index 688f9b5..3cd9276 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -51,6 +51,18 @@ exports.LanguageObject = function( ){ function _AddTokenEvent( token, event ){ token.binding.push(event); }; + /** + * Turns method arguments into a array rather then having them as an object. + * @param arguments Object - The object containing the arguments to turn into an array + * @returns Array - The array of arguments. + */ + function _ArgumentArray( arguments ){ + var argumentArray = []; + for( var i = 0; i < arguments.length; i++ ){ + argumentArray.push( arguments[i] ); + } + return argumentArray; + }; //////////////////// //////PUBLIC VARIABLES// //////////////////// @@ -58,7 +70,7 @@ exports.LanguageObject = function( ){ * An enumeration of the types of allowed rule types. * @type Associative Array */ - this.ruleList = { and:2, or:1, norule:0, cgroup:3, optional:4, repeat:5 }; + this.ruleList = { and:2, or:1, norule:0, cgroup:3, optional:4, repeat:5, include:6 }; /** * All the grammar for the language, in an associative array, where the name the rest is an object by type. * @todo make a object for reference of what can go into these objects. @@ -127,18 +139,6 @@ exports.LanguageObject = function( ){ this.CharGroup = function( low, high ){ return { type:this.ruleList.cgroup, low:low.charCodeAt(0), high:high.charCodeAt(0) }; }; - /** - * Turns method arguments into a array rather then having them as an object. - * @param arguments Object - The object containing the arguments to turn into an array - * @returns Array - The array of arguments. - */ - function _ArgumentArray( arguments ){ - var argumentArray = []; - for( var i = 0; i < arguments.length; i++ ){ - argumentArray.push( arguments[i] ); - } - return argumentArray; - }; /** * Executes any methods/events bound to the token. * @param token Token - The token with bound events. @@ -178,6 +178,9 @@ exports.LanguageObject = function( ){ } return firedToken; }; + this.Include = function(){ + return { type:this.ruleList.include, syntax:_ArgumentArray.call( this, arguments ) }; + }; /** * Index all the token types for quick access into the _tokenIdList, should be called once all the types are added. */ @@ -348,7 +351,7 @@ Script = function( ){ * @param tokenName string - The name of the token to seek to. */ this.SeekNext = function( tokenName ){ - _interpreter.SeekTokenByName( tokenName, this.rootToken, this ); + return _interpreter.SeekTokenByName( this.rootToken, tokenName, this ); }; //CALL TO CONSTRUCTOR// @@ -684,6 +687,14 @@ exports.parser = function(){ case _rl.optional: console.log( "optional" ); break; + case _rl.include: + console.log( "include" ); + process.exit(); + break; + default: + console.log( "Unknown grammar type, '"+grammar.type+"' please add a case for the grammar." ); + process.exit(); + break; }; };