Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pulling in fixes #3

Merged
merged 6 commits into from
Feb 2, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
ANTLR v4 Honey Badger early access

Jan 30, 2012

* Moving to github.

Jan 28, 2012

* ~[] stuff is allowed and works inside sets etc...
Expand Down
9 changes: 9 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ANTLR v4 early access

Terence Parr, parrt at cs usfca edu
ANTLR project lead and supreme dictator for life
University of San Francisco

INTRODUCTION

Coming soon...
85 changes: 82 additions & 3 deletions tool/playground/E.g
Original file line number Diff line number Diff line change
@@ -1,3 +1,82 @@
lexer grammar E;
I : ~[ab] ~[cd]* {System.out.println("I");} ;
WS : [ \n\u000D]+ -> skip ;
/** Simple statically-typed programming language with methods and variables
* taken from "Language Implementation Patterns" book.
*/
grammar E;

// START: file
file: (methodDecl | varDecl)+ ;
// END: file

// START: var
varDecl
: type ID ('=' expr)? ';'
;
type: 'float' | 'int' | 'void' ; // user-defined types
// END: var

// START: method
methodDecl
: type ID '(' formalParameters? ')' block // "void f(int x) {...}"
;

formalParameters
: type ID (',' type ID)*
;
// END: method

// START: stat
block: '{' stat* '}' ; // possibly empty statement block

stat: block
| varDecl
| 'if' expr 'then' stat ('else' stat)?
| 'return' expr? ';'
| expr '=' expr ';' // assignment
| expr ';' // func call
;
// END: stat

/* expr below becomes the following non-left recursive rule:
expr[int _p]
: ( '-' expr[6]
| '!' expr[5]
| ID
| INT
| '(' expr ')'
)
( {8 >= $_p}? '*' expr[9]
| {7 >= $_p}? ('+'|'-') expr[8]
| {4 >= $_p}? '==' expr[5]
| {10 >= $_p}? '[' expr ']'
| {9 >= $_p}? '(' exprList? ')'
)*
;
*/

// START: expr
expr: expr '[' expr ']' // array index like a[i], a[i][j]
| expr '(' exprList? ')' // func call like f(), f(x), f(1,2)
| expr '*' expr
| expr ('+'|'-') expr
| '-' expr // unary minus
| '!' expr // boolean not
| expr '==' expr // equality comparison (lowest priority op)
| ID // variable reference
| INT
| '(' expr ')'
;

exprList : expr (',' expr)* ; // arg list
// END: expr

ID : LETTER (LETTER | [0-9])* ;
fragment
LETTER : [a-zA-Z] ;

INT : [0-9]+ ;

WS : [ \t\n\r]+ -> skip ;

SL_COMMENT
: '//' .* '\n' -> skip
;
1 change: 1 addition & 0 deletions tool/src/org/antlr/v4/automata/LexerATNFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ else if ( (i+2)<n && chars.charAt(i+1)=='-' ) { // range x-y
int x = c;
int y = chars.charAt(i+2);
if ( x<=y ) set.add(x,y);
i+=2;
}
else {
set.add(c);
Expand Down
15 changes: 5 additions & 10 deletions tool/src/org/antlr/v4/parse/ANTLRLexer.g
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,7 @@ package org.antlr.v4.parse;


@members {
public Token prevToken;
@Override
public void emit(Token token) {
super.emit(token);
if ( token.getChannel()==Token.DEFAULT_CHANNEL ) prevToken = token;
}
public boolean isLexer = false;
}

// --------
Expand Down Expand Up @@ -227,10 +222,10 @@ COMMENT

ARG_OR_CHARSET
options {k=1;}
: {prevToken.getType()!=RULE_REF}?=> LEXER_CHAR_SET {$type=LEXER_CHAR_SET;}
| {prevToken.getType()==RULE_REF}?=> ARG_ACTION {$type=ARG_ACTION;}
: {isLexer}?=> LEXER_CHAR_SET {$type=LEXER_CHAR_SET;}
| {!isLexer}?=> ARG_ACTION {$type=ARG_ACTION;}
;

fragment
LEXER_CHAR_SET
: '[' ('\\]'|'\\'|~('\\'|']'))* ']'
Expand Down Expand Up @@ -412,7 +407,7 @@ TOKENS_SPEC : 'tokens' WSNLCHARS* '{' ;

IMPORT : 'import' ;
FRAGMENT : 'fragment' ;
LEXER : 'lexer' ;
LEXER : 'lexer' {isLexer=true;} ;
PARSER : 'parser' ;
GRAMMAR : 'grammar' ;
PROTECTED : 'protected' ;
Expand Down