Skip to content

Commit

Permalink
FJ parser
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Lucángeli Obes <[email protected]>
  • Loading branch information
jlucangelio committed Jul 20, 2011
1 parent 7eee6fa commit 80a61ab
Show file tree
Hide file tree
Showing 6 changed files with 3,682 additions and 0 deletions.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

Empty file added parser/__init__.py
Empty file.
195 changes: 195 additions & 0 deletions parser/fj.g
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* Copyright 2011 Jorge Lucángeli Obes
*
* This file is part of fj-llvm.
*
* fj-llvm is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* fj-llvm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with fj-llvm. If not, see <http://www.gnu.org/licenses/>.
*/
grammar fj;

options {
language = Python;
output = AST;
backtrack = true;
}

tokens {
DOT = '.';
COMMA = ',';
SEMICOLON = ';';
LPAREN = '(';
RPAREN = ')';
LBRACE = '{';
RBRACE = '}';
ASSIGN = '=';
UNDERSCORE = '_';
CLASS = 'class';
EXTENDS = 'extends';
SUPER = 'super';
THIS = 'this';
RETURN = 'return';
NEW = 'new';
}

@header {
# Copyright 2011 Jorge Lucángeli Obes
#
# This file is part of fj-llvm.
#
# fj-llvm is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# fj-llvm is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with fj-llvm. If not, see <http://www.gnu.org/licenses/>.
}

/********* LEXER ******************************************************/
fragment
LETTER
:
'a'..'z'
| 'A'..'Z'
;

fragment
DIGIT
:
'0'..'9'
;

fragment
ANY_CHAR
:
LETTER
| UNDERSCORE
| DIGIT
;

NAME
:
(
LETTER
| UNDERSCORE
)
ANY_CHAR*
;

WHITESPACE
:
(
' '
| '\t'
| '\r'
| '\n'
)+

{
$channel = HIDDEN;
}
;

/********* PARSER *****************************************************/
program
:
classDefinition+ expression
;

classDefinition
:
CLASS NAME EXTENDS NAME LBRACE fields constructor methods RBRACE
;

fields
:
(field SEMICOLON)*
;

field
:
NAME NAME
;

constructor
:
NAME LPAREN vars RPAREN LBRACE SUPER LPAREN names RPAREN SEMICOLON inits RBRACE
;

vars
:
(var (COMMA var)*)?
;

var
:
NAME NAME
;

names
:
(NAME COMMA)*
;

inits
:
(init SEMICOLON)*
;

init
:
THIS DOT NAME ASSIGN NAME
;

methods
:
method*
;

method
:
NAME NAME LPAREN vars RPAREN LBRACE RETURN expression SEMICOLON RBRACE
;

expression
:
baseExpression
| dotExpression
;

dotExpression
:
(
baseExpression
| THIS
)
(DOT NAME parenExpressions?)+ // field or method call
;

baseExpression
:
NAME
| NEW NAME parenExpressions // new object
| LPAREN NAME RPAREN expression // cast
;

parenExpressions
:
LPAREN (expression (COMMA expression)*)? RPAREN
;
35 changes: 35 additions & 0 deletions parser/fj.tokens
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
CLASS=13
RBRACE=10
LETTER=19
LBRACE=9
NEW=18
WHITESPACE=23
UNDERSCORE=12
SEMICOLON=6
LPAREN=7
ANY_CHAR=21
RPAREN=8
NAME=22
COMMA=5
ASSIGN=11
THIS=16
RETURN=17
DIGIT=20
SUPER=15
DOT=4
EXTENDS=14
'}'=10
'class'=13
'new'=18
'extends'=14
'{'=9
'this'=16
';'=6
'return'=17
'='=11
'('=7
','=5
'_'=12
'.'=4
')'=8
'super'=15
Loading

0 comments on commit 80a61ab

Please sign in to comment.