// Automatically generated code. Edit at your own risk!
// Generated by bali2javacc v2002.09.04.
//-----------------------------------//
// Options block:
//-----------------------------------//
options {
CACHE_TOKENS = true ;
JAVA_UNICODE_ESCAPE = true ;
//OPTIMIZE_TOKEN_MANAGER = true ;
STATIC = false ;
}
//-----------------------------------//
// Parser code block:
//-----------------------------------//
PARSER_BEGIN(BaliParser)
package tmp.generated_bali;
import java.io.*;
import java.util.*;
import cide.gast.*;
import cide.gparser.*;
public class BaliParser {
private static BaliParse parseRoot = null ;
public static BaliParse getStartRoot () {
return parseRoot ;
}
public ISourceFile getRoot() throws ParseException {
return BaliParse();
}
//*************************************************************************
// Code inserted from "bali.b" source grammar:
//*************************************************************************
/**
* Append the given {@link Token} and any preceding special tokens to a
* given {@link StringBuffer}.
*
* @param token the given JavaCC {@link Token} object
* @param buffer the buffer to which to append token
**/
final private static void accumulate (Token token, StringBuffer buffer) {
// Append preceding special tokens to buffer:
//
Token special = firstSpecial (token) ;
if (special != token)
while (special != null) {
buffer.append (special.toString ()) ;
special = special.next ;
}
// Finally, append the token itself:
//
buffer.append (token.toString ()) ;
}
/**
* Accumulate {@list Token} objects from the token stream, respecting
* nested code inside open and close pairs,
* until an unmatched close is the next token in the stream.
* This method assumes that an open token has just been read
* from the stream so the initial nesting level is 1. The method returns
* when a matching close token is the next token in the token
* stream. The close token is left in the stream!
*
* @return the accumulated tokens as a {@link String}.
*
* @throws ParseException
* if an end-of-file is found before an unmatched close token.
**/
final private Token accumulateNestedRegion (int open, int close)
throws ParseException {
StringBuffer buffer = new StringBuffer () ;
// Initialize result with known information (starting position, etc.):
//
Token result = Token.newToken (OTHER) ;
result.specialToken = null ;
Token startToken = firstSpecial (getToken (1)) ;
result.beginColumn = startToken.beginColumn ;
result.beginLine = startToken.beginLine ;
// Accumulate tokens until a close token is found:
//
for (int nesting = 1 ; nesting > 0 ; ) {
token = getToken (1) ;
// Update information in result:
//
result.endColumn = token.endColumn ;
result.endLine = token.endLine ;
result.next = token.next ;
if (token.kind == EOF)
throw new ParseException (
"accumulating from line "
+ result.beginLine
+ " at column "
+ result.beginColumn
+ ": EOF reached before ending "
+ tokenImage [close]
+ " found"
) ;
if (token.kind == open)
++ nesting ;
else if (token.kind == close) {
if (nesting == 1)
break ;
-- nesting ;
}
accumulate (token, buffer) ;
getNextToken () ;
}
result.image = buffer.toString () ;
return result ;
}
/**
* Accumulate {@link Token} objects from the token stream until a token
* matching tokenKind is consumed from the stream. The
* tokens are accumulated in buffer, including the terminating
* token.
*
* @return a {@link Token}
* formed by concatenating all intervening tokens and special tokens.
**/
final private Token accumulateUntilToken (int tokenKind)
throws ParseException {
StringBuffer buffer = new StringBuffer () ;
Token token = getNextToken () ;
// Initialize result with known information (starting position, etc.):
//
Token result = Token.newToken (OTHER) ;
result.specialToken = null ;
Token startToken = firstSpecial (token) ;
result.beginColumn = startToken.beginColumn ;
result.beginLine = startToken.beginLine ;
// Accumulate tokens until a tokenKind token is found:
//
while (token.kind != tokenKind) {
// Update information in result:
//
result.endColumn = token.endColumn ;
result.endLine = token.endLine ;
result.next = token.next ;
if (token.kind == EOF)
throw new ParseException (
"from line "
+ result.beginLine
+ " at column "
+ result.beginColumn
+ ": EOF reached before "
+ tokenImage [tokenKind]
+ " found"
) ;
accumulate (token, buffer) ;
token = getNextToken () ;
}
accumulate (token, buffer) ;
result.image = buffer.toString () ;
return result ;
}
/**
* Finds the first token, special or otherwise, in the list of special
* tokens preceding this {@link Token}. If this list is non-empty, the
* result will be a special token. Otherwise, it will be the starting
* token.
*
* @param token the given {@link Token}.
* @return the first special token preceding token.
**/
final private static Token firstSpecial (Token token) {
while (token.specialToken != null)
token = token.specialToken ;
return token ;
}
}
PARSER_END(BaliParser)
//-----------------------------------//
// Token manager declarations:
//-----------------------------------//
// No TOKEN_MGR_DECLS defined in Bali grammar.
//-----------------------------------//
// Standard token definitions:
//-----------------------------------//
SPECIAL_TOKEN : {" "|"\f"|"\n"|"\r"|"\t"}
// COMMENTS:
MORE : {
"//" : IN_SINGLE_LINE_COMMENT
| <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
| "/*" : IN_MULTI_LINE_COMMENT
}
SPECIAL_TOKEN : {
: DEFAULT
}
SPECIAL_TOKEN : {
: DEFAULT
}
SPECIAL_TOKEN : {
: DEFAULT
}
MORE : { < ~[] > }
TOKEN : {
<#LETTER: ["a"-"z", "A"-"Z", "_", "$"]>
| <#DIGIT: ["0"-"9"]>
}
//-----------------------------------//
// Bali tokens from grammar:
//-----------------------------------//
TOKEN : {
">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| <_CODE: "code">
| <_EOF: "EOF">
| <_IGNORE_CASE: "IGNORE_CASE">
| <_JAVACODE: "JAVACODE">
| <_LOOKAHEAD: "LOOKAHEAD">
| <_MORE: "MORE">
| <_OPTIONS: "options">
| <_PARSER_BEGIN: "PARSER_BEGIN">
| <_PARSER_END: "PARSER_END">
| <_SKIP: "SKIP">
| <_SPECIAL_TOKEN: "SPECIAL_TOKEN">
| <_TOKEN: "TOKEN">
| <_TOKEN_MGR_DECLS: "TOKEN_MGR_DECLS">
}
//-----------------------------------//
// Regular-expression tokens from grammar:
//-----------------------------------//
TOKEN: {
( | )*> |
<#UPPERCASE: ["A"-"Z", "_", "$"]> |
|
)+>
}
TOKEN : {
( | )*>
|
}
//-----------------------------------//
// JAVACODE blocks from grammar:
//-----------------------------------//
JAVACODE
Token findBlockBegin () {
return accumulateUntilToken (LBRACE) ;
}
JAVACODE
Token findBlockEnd () {
return accumulateNestedRegion (LBRACE, RBRACE) ;
}
JAVACODE
Token findCloseAngle () {
return accumulateNestedRegion (OPENANGLE, CLOSEANGLE) ;
}
JAVACODE
Token findCloseParen () {
return accumulateNestedRegion (OPENPAREN, CLOSEPAREN) ;
}
GRAMMARSTART
BaliParse
: [OptionsNode] @! [ParserCode] @! [Statements] :: BaliParseNode
;
OptionsNode
: <_OPTIONS> Block <_OPTIONS>
;
ParserCode
: <_CODE> Block <_CODE> :: ParserCodeNode
;
Block
: "{" JAVATOKEN(findBlockEnd) "}" :: BlockNode
;
Statements
: (Statement @! @!)+
;
Statement
: BaliGrammarRule
| BaliTokenDefinition
| JavacodeProduction
| RegexTokenDefinition
| TokenManagerDeclarations
;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// The simple statement types:
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
BaliTokenDefinition
: @RefTarget(BaliToken) :: BaliTokenDefineNode
;
JavacodeProduction
: <_JAVACODE> ScanBlock :: JavacodeNode
;
TokenManagerDeclarations
: <_TOKEN_MGR_DECLS> ":" ScanBlock :: TokenManagerNode
;
ScanBlock
: JAVATOKEN(findBlockBegin) JAVATOKEN(findBlockEnd) "}" :: ScanBlockNode
;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Statement type -- Bali grammar rule:
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
BaliGrammarRule
: @RefTarget(Grammar) ":" &LI Production ("|" &LI Production)* ";" :: BaliGrammarNode
;
Production
: [Lookahead] Rewrite :: ProductionNode
;
Lookahead
: <_LOOKAHEAD> "(" JAVATOKEN(findCloseParen) ")" :: LookaheadNode
;
Rewrite
: "(" [Lookahead] Primitive ")" "+" :: SimpleListNode
| Primitive PrimitiveRewrite :: PrimitiveRewriteNode
;
PrimitiveRewrite
: "(" [Lookahead] Primitive Primitive ")" "*" :: ComplexListNode
| [Pattern] [ClassName] :: PatternNode
;
Pattern
: (Primitive)+
;
ClassName
: "::" :: ClassNameNode
;
Primitive
: OptionalNode{Terminal}
| Terminal
;
OptionalNode
: "[" [Lookahead] Terminal! "]";
Terminal
: @Reference(BaliToken) :: BaliTokenNode
| @Reference(Grammar) :: IdentifierNode
| :: StringNode
;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Statement type -- JavaCC token definition:
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
RegexTokenDefinition
: [StateSet] REKind [CaseFlag] ":" "{" REList "}"
:: RegexDefinitionNode
;
StateSet
: "<" StatesSpecifier ">" :: StatesNode
;
StatesSpecifier
: "*" :: StarStatesNode
| StatesList :: ListStatesNode
;
StatesList
: &LI StateName ("," &LI StateName)*
;
StateName
: :: StateNameNode
;
REKind
: <_TOKEN> :: TokenKindNode
| <_SPECIAL_TOKEN> :: SpecialKindNode
| <_SKIP> :: SkipKindNode
| <_MORE> :: MoreKindNode
;
CaseFlag
: "[" <_IGNORE_CASE> "]" :: CaseFlagNode
;
REList
: &LI RegexBlock ("|" &LI RegexBlock)*
;
RegexBlock
: Regex [Block] [NextState] :: RegexBlockNode
;
NextState
: ":" :: NextStateNode
;
Regex
: :: StringRegexNode
| "<" AngleRegex :: AngleRegexNode
;
AngleRegex
: LOOK_AHEAD(2) ">" :: BaliRegexNode
| LOOK_AHEAD(2) [Label] ComplexRegex :: ComplexRegexNode
;
ComplexRegex
: LOOK_AHEAD(2) ">" :: StringComplexNode
| JAVATOKEN(findCloseAngle) ">" :: AngleComplexNode
;
Label
: ["#"] ":" :: LabelNode
;