Skip to content

Commit

Permalink
Added include to BNF syntax for multi script type combinations, not f…
Browse files Browse the repository at this point in the history
…inished though.

Fixed calculator example.
  • Loading branch information
daKuleMune committed Sep 30, 2011
1 parent d2bfd96 commit 1deb96b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 33 deletions.
7 changes: 3 additions & 4 deletions examples/calculator/calc.bnf
@@ -1,5 +1,4 @@
<syntax> ::= <expression> | <expression> <nextline> <syntax>
<syntax> ::= <expression>
<expression> ::= <number> <type> <number>
<number> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<type> ::= + | - | * | /
<nextline> ::= "\r" "\n" | "\n"
<number> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<type> ::= "+" | "-" | "*" | "/"
1 change: 0 additions & 1 deletion examples/calculator/calc.calc
@@ -1,2 +1 @@
1+1
1+1
26 changes: 13 additions & 13 deletions 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" );
} );
4 changes: 3 additions & 1 deletion lib/bnf.bnf.js
Expand Up @@ -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" );
Expand All @@ -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 ) ) );
Expand Down
15 changes: 15 additions & 0 deletions lib/compiler.js
Expand Up @@ -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 ){
Expand Down Expand Up @@ -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.
Expand Down
39 changes: 25 additions & 14 deletions lib/parser.js
Expand Up @@ -51,14 +51,26 @@ 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//
////////////////////
/**
* 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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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//
Expand Down Expand Up @@ -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;
};

};
Expand Down

0 comments on commit 1deb96b

Please sign in to comment.