Skip to content

Commit

Permalink
Removed testing, and starting working on the compiler.
Browse files Browse the repository at this point in the history
  • Loading branch information
daKuleMune committed Sep 1, 2011
1 parent b7d8a81 commit 1c69a9e
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 40 deletions.
1 change: 0 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Road Map
TODO
----

- There can be a faster way to search or in character data by grouping letters A-Z = 60-90 >< compare rather then is x sizeof
- The OR operation should set the syntax into groups so as only process each syntax layer once.

License
Expand Down
5 changes: 3 additions & 2 deletions examples/calculator/calc.bnf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<syntax> ::= <expression>
<syntax> ::= <expression> | <expression> <nextline> <syntax>
<expression> ::= <number> <type> <number>
<number> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<type> ::= + | - | * | /
<type> ::= + | - | * | /
<nextline> ::= "\r" "\n" | "\n"
3 changes: 1 addition & 2 deletions examples/calculator/calc.calc
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
1+1
1+1
12+12
1+1
2 changes: 1 addition & 1 deletion examples/calculator/calculator.bnf.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ i.AddRule( "digit" );
i.AddRule( "digits" );
i.IndexTokenIdList();

i.WriteRule( "rule", i.Or( r.expression, i.And( r.expression, r.nextLine, r.rule ) ) );
i.WriteRule( "syntax", i.Or( r.expression, i.And( r.expression, r.nextLine, r.syntax ) ) );
i.WriteRule( "expression", i.And( r.number, r.type, r.number ) );
i.WriteRule( "number", i.Or( r.posNumber, r.negNumber ) );
i.WriteRule( "posNumber", r.digits );
Expand Down
31 changes: 19 additions & 12 deletions lib/bnf.bnf.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var r = i.syntaxObject;
* Add all the rules and there id's
*/
i.AddRule( "rule" );
i.AddRule( "whitespace" );
i.AddRule( "optWhitespace" );
i.AddRule( "expression" );
i.AddRule( "lineEnd" );
Expand All @@ -29,26 +30,32 @@ i.AddRule( "term" );
i.AddRule( "literal" );
i.AddRule( "ruleName" );
i.AddRule( "text" );
i.AddRule( "char" );
i.AddRule( "varRule" );
i.IndexTokenIdList();

/**
* Define grammar of all the rules
*/
i.WriteRule( "syntax", i.Or( r.rule, i.And( r.rule, r.lineEnd, r.syntax ) ) );
i.WriteRule( "rule", "todo" );
i.WriteRule( "syntax", i.Or( r.rule, i.And( r.rule, r.lineEnd, r.syntax ) ) );
i.WriteRule( "lineEnd", i.Or( i.And( "\r", "\n" ), "\n" ) );
/*i.WriteRule( "rule", and( optional( r.whitespace ), "<", r.ruleName, ">",
optional( r.whitespace ), "::=", optional( r.whitespace ), r.expression, r.lineEnd ) );
i.WriteRule( "whitespace", repeat( or( " ", "\t", "\n", r.comment ) ) );
i.WriteRule( "whitespaceNoComment", repeat( or( " ", "\t", "\n" ) ) );
i.WriteRule( "expression", or( r.list, and( r.list, "|", r.expression ) ) );
i.WriteRule( "lineEnd", and( r.optWhitespace, "\n" ) );
i.WriteRule( "rule", i.And( r.optWhitespace, "<", r.ruleName, ">", r.optWhitespace, "::=",
r.optWhitespace, r.expression ) );
i.WriteRule( "optWhitespace", i.Or( i.Blank(), i.And( r.whitespace, r.optWhitespace ) ) );
i.WriteRule( "whitespace", i.Or( i.Or( " ", "\t", "\n" ) ) );
i.WriteRule( "ruleName", r.text );
i.WriteRule( "expression", i.Or( r.list, i.And( r.list, r.optWhitespace, "|", r.optWhitespace, r.expression ) ) );
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( "varRule", i.And( "<", r.text, ">" ) );
i.WriteRule( "literal", i.Or( i.And( "'", r.text, "'" ), i.And( '"', r.text, '"') ) );
i.WriteRule( "text", i.Or( r.char, i.And( r.char, r.text ) ) );
i.WriteRule( "char", i.Or( i.CharGroup( "A", "Z" ), i.CharGroup( "a", "z" ) ) );
/*
i.WriteRule( "list", or( r.term, and( r.term, r.optWhitespace, r.list ) ) );
i.WriteRule( "term", or( r.literal, and( "<", r.ruleName, ">" ), r.regEx ) );
i.WriteRule( "literal", or( and( '"', r.text, '"' ), and( "'", r.text, "'" ) ) );
i.WriteRule( "ruleName", r.text );
i.WriteRule( "text", /^[A-Za-z][A-Za-z\d]{0,}$/ );
i.WriteRule( "regEx", and( "/", /^([A-Za-z\\ \-\_\!\&\{\}\]\[\(\)\+\#\@\*\%\~\`\,\.\<\>\?\:\;\"\'\|]|\\\/){1,}$/, "/" ) );
i.WriteRule( "comment", and( "<!--", r.optWhitespaceNoComment, /^.{1,}$/, r.optWhitespaceNoComment, "-->" ) );*/
*/

exports.interpreter = i;
17 changes: 16 additions & 1 deletion lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,19 @@
var i = require( "./bnf.bnf.js" ).interpreter;
var parserObject = require( "./parser.js" ).parser;
var parser = new parserObject( i );
parser.ParseScript( "test.bnf" );
i.AddTokenEventByName( "ruleName", function( token ){
console.log( token.text );
} );
i.AddTokenEventByName( "list", function( token ){
//var left = i.SeekTokenByName( token, "number" ).text;
var term = null;
while( ( term = i.SeekTokenByName( token, "term" ) ) != null ){
console.log( term.text );
}
} );

//parser.ParseScript( "test.bnf" );

exports.Compiler = function( ){

};
74 changes: 58 additions & 16 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
exports.version = '0.0.6';
var fs = require('fs');

Array.prototype.clone = function() { return this.slice(0); };

/**
* @todo - There can be a faster way to search or in character data by grouping letters A-Z = 60-90 >< compare rather then is x sizeof
*/

/**
* Language object to be interpreted by
* @see LanguageObject.Constructor
Expand Down Expand Up @@ -47,7 +41,7 @@ exports.LanguageObject = function( ){
*/
var Constructor = function( name ){
_name = name;
this.WriteRule( "syntax", this.And( this.syntaxObject.rule, { name:'endOfFile', method:function( token ){ return ( token.charPtr + token.length == this.endOfSource ); } } ) );
this.WriteRule( "script", this.And( this.syntaxObject.syntax, { name:'endOfFile', method:function( token ){ return ( token.charPtr + token.length == this.endOfSource ); } } ) );
};
/**
* Binds an event to a token type.
Expand All @@ -64,13 +58,13 @@ exports.LanguageObject = function( ){
* An enumeration of the types of allowed rule types.
* @type Associative Array
*/
this.ruleList = { and:2, or:1, norule:0, optional:3 };
this.ruleList = { and:2, or:1, norule:0, cgroup:3, optional:4, repeat:5 };
/**
* 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.
* @type Associative Array
*/
this.syntaxObject = { syntax:{ id: 0 }, rule:{ id:1 } };
this.syntaxObject = { script:{ id: 0 }, syntax:{ id:1 } };
//////////////////
//////PUBLIC METHODS//
//////////////////
Expand Down Expand Up @@ -118,6 +112,21 @@ exports.LanguageObject = function( ){
this.And = function( ){
return { type:this.ruleList.and, syntax:_ArgumentArray.call( this, arguments) };
};
/**
* Creates a method in the grammar where the syntax will be correct regardless.
*/
this.Blank = function( ){
return { name:'blank', method:function( token ){ return true; } };
};
/**
* Way to group characters together for faster processing.
* @param low string - The lowest character to start the search
* @param high string - The highest character to end the search
* @returns syntaxRule
*/
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
Expand Down Expand Up @@ -176,23 +185,28 @@ exports.LanguageObject = function( ){
_tokenIdList[this.syntaxObject[i].id] = this.syntaxObject[i];
}
};
/**
* Short cut for creating a "optional" rule.
*/
this.Optional = function(){
return { type:this.ruleList.optional, syntax:_ArgumentArray.call( this, arguments ) };
};
/**
* Short cut for creating a "and" rule.
* @param [mixed][...] - More rules
*/
this.Or = function( ){
return { type:this.ruleList.or, syntax:_ArgumentArray.call( this, arguments ) };
};

//@TODO ABNF notations
/*this.Optional = function( ){
return { type:this.ruleList.optional, syntax:arguments };
};
/**
* Short cut for creating a "repeat at least once" rule.
*/
this.Repeat = function( ){
return { type:4, syntax:arguments };
return { type:this.ruleList.repeat, syntax:_ArgumentArray.call( this, arguments ) };
};

//@TODO ABNF notations
/*
this.Group = function( ){
return { type:5, syntax:arguments };
};*/
Expand Down Expand Up @@ -515,6 +529,15 @@ exports.parser = function( ){
else if( grammar.syntax[i].type ){ //Multi-level grammar
_ProcessRuleGrammar.call( this, grammar.syntax[i], subContainer );
}
else if( grammar.syntax[i].method != undefined ){ //Method call
var evaluated = grammar.syntax[i].method.call( this, subContainer );
if( evaluated ){
subContainer.validated = true;
}
else{
ruleContainer.expected.push( { grammar: grammar.syntax[i].name, token:null } );
}
}
else{ //Text
if( _rawScript.substr( subContainer.charPtr + subContainer.length, grammar.syntax[i].length ) == grammar.syntax[i] ){
validatedRule = i;
Expand Down Expand Up @@ -543,6 +566,25 @@ exports.parser = function( ){
}
}
break;
case _rl.cgroup:
var char = _rawScript.substr( ruleContainer.charPtr + ruleContainer.length, 1 );
var charNumber = char.charCodeAt( 0 );
if( charNumber >= grammar.low && charNumber <= grammar.high ){
ruleContainer.length += 1;
ruleContainer.text += char;
ruleContainer.validations++;
ruleContainer.validated = true;
}
else{
ruleContainer.expected.push( { grammar:"Char(" + grammar.low + "-" + grammar.high + ")" , token:null } );
}
break;
case _rl.repeat:
console.log( "repeat" );
break;
case _rl.optional:
console.log( "optional" );
break;
};

};
Expand Down
5 changes: 0 additions & 5 deletions lib/test.bnf

This file was deleted.

0 comments on commit 1c69a9e

Please sign in to comment.