Created
August 14, 2013 11:14
-
-
Save timglabisch/6230109 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Parser = require("jison").Parser; | |
Generator = require("jison").Generator; | |
class Node_Whitespace | |
constructor: (@value)-> | |
console.log "#Whitespace" | |
compile: -> "" | |
class Node_Keyword | |
constructor: (@value)-> | |
console.log "#Keyword" | |
compile: -> @value | |
class Node_Block | |
constructor: (code)-> | |
console.log "#Block" | |
@code = [code] | |
add:(code)-> | |
@code.push code | |
compile: -> @code | |
GLOBAL.Node_Literal = require './nodes/literal' | |
GLOBAL.Node_Keyword = Node_Keyword | |
GLOBAL.Node_Function = require './nodes/function' | |
GLOBAL.Node_Block = Node_Block | |
GLOBAL.Node_Whitespace = Node_Whitespace | |
o = (key, command)-> | |
[key, " $$ = (" + command.toString() + ")()"] | |
l = (regex, command) -> | |
[regex, "return (" + command.toString() + ")()"] | |
grammar = | |
startSymbol: 'Start' | |
lex: | |
rules: [ | |
l /\;/im, -> 'TERMINATOR' | |
# l /^\n/, -> 'TERMINATOR' | |
l "\\s+", -> "" # skip whitespace | |
l /^\<\?/, -> "" # skip whitespace | |
# language functions | |
l /^include/, -> 'T_INCLUDE' | |
l /^include\_once/, -> 'T_REQUIRE' | |
l /^requre/, -> 'T_REQUIRE' | |
l /^requre\_once/, -> 'T_REQUIRE' | |
# functions | |
l /^function/i, -> 'T_FUNCTION' | |
l /^\(/, -> 'T_ARGS_OPEN' | |
l /^\)/, -> 'T_ARGS_CLOSE' | |
# blocks | |
l /^\{/, -> 'T_BLOCK_OPEN' | |
l /^\}/, -> 'T_BLOCK_CLOSE' | |
l /// ^ | |
( [$A-Za-z_\x7f-\uffff][$\w\x7f-\uffff]* ) | |
( [^\n\S]* : (?!:) )? # Is this a property name? | |
///, -> 'T_IDENTIFIER' | |
l /// | |
^ 0b[01]+ | # binary | |
^ 0o[0-7]+ | # octal | |
^ 0x[\da-f]+ | # hex | |
^ \d*\.?\d+ (?:e[+-]?\d+)? # decimal | |
///i, -> 'T_NUMBER' | |
l /// | |
/^(?:\n[^\n\S]*)+/ | |
///i, -> 'T_INDENT' | |
] | |
bnf: | |
Start :[ | |
o "", -> new Node_Block | |
o "Body", -> new Node_Block $1 | |
], | |
Body: [ | |
o 'Line', -> new Node_Block [$1] | |
o 'Body TERMINATOR Line', -> new Node_Block [$1, $3] | |
o 'Body TERMINATOR', -> new Node_Block $1 | |
] | |
Line :[ | |
o "Expression", -> new Node_Block $1 | |
o "Statement", -> new Node_Block $1 | |
], | |
Expression: [ | |
o "T_FUNCTION T_IDENTIFIER T_ARGS_OPEN T_ARGS_CLOSE Block", -> new Node_Function($5, $2); | |
] | |
Block: [ | |
o "T_BLOCK_OPEN T_BLOCK_CLOSE", -> new Node_Block [] | |
o "T_BLOCK_OPEN LINE T_BLOCK_CLOSE", -> new Node_Block [$2] | |
] | |
parser = new Parser grammar | |
try | |
res = parser.parse(' | |
function FUNC_NAME() { | |
} | |
; | |
') | |
console.log res; | |
catch e | |
console.log e | |
### output ### | |
[Error: Parse error on line 1: | |
; function FUNC_NAME | |
^ | |
Expecting 'Statement', 'T_FUNCTION', got 'TERMINATOR'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment