mirror of
https://github.com/marcin-szczepanski/jFuzzyLogic.git
synced 2024-12-19 00:35:29 +01:00
310 lines
9.0 KiB
Plaintext
310 lines
9.0 KiB
Plaintext
|
/*
|
||
|
[The "BSD licence"]
|
||
|
Copyright (c) 2005-2006 Terence Parr
|
||
|
All rights reserved.
|
||
|
|
||
|
Redistribution and use in source and binary forms, with or without
|
||
|
modification, are permitted provided that the following conditions
|
||
|
are met:
|
||
|
1. Redistributions of source code must retain the above copyright
|
||
|
notice, this list of conditions and the following disclaimer.
|
||
|
2. Redistributions in binary form must reproduce the above copyright
|
||
|
notice, this list of conditions and the following disclaimer in the
|
||
|
documentation and/or other materials provided with the distribution.
|
||
|
3. The name of the author may not be used to endorse or promote products
|
||
|
derived from this software without specific prior written permission.
|
||
|
|
||
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
*/
|
||
|
|
||
|
/** Templates for building ASTs during tree parsing.
|
||
|
*
|
||
|
* Deal with many combinations. Dimensions are:
|
||
|
* Auto build or rewrite
|
||
|
* no label, label, list label (label/no-label handled together)
|
||
|
* child, root
|
||
|
* token, set, rule, wildcard
|
||
|
*
|
||
|
* Each combination has its own template except that label/no label
|
||
|
* is combined into tokenRef, ruleRef, ...
|
||
|
*/
|
||
|
group ASTTreeParser;
|
||
|
|
||
|
/** Add a variable to track last element matched */
|
||
|
ruleDeclarations() ::= <<
|
||
|
<super.ruleDeclarations()>
|
||
|
<ASTLabelType> _last;<\n>
|
||
|
<ASTLabelType> _first_0;<\n>
|
||
|
>>
|
||
|
|
||
|
/** Add a variable to track last element matched */
|
||
|
ruleInitializations() ::= <<
|
||
|
<super.ruleInitializations()>
|
||
|
_last = NULL;<\n>
|
||
|
_first_0 = NULL;<\n>
|
||
|
>>
|
||
|
|
||
|
/** What to emit when there is no rewrite rule. For auto build
|
||
|
* mode, does nothing.
|
||
|
*/
|
||
|
noRewrite(rewriteBlockLevel, treeLevel) ::= <<
|
||
|
<if(backtracking)>if ( BACKTRACKING ==0 ) {<endif>
|
||
|
<if(rewriteMode)>
|
||
|
retval.tree = (<ASTLabelType>)_first_0;
|
||
|
if ( ADAPTOR->getParent(ADAPTOR, retval.tree) != NULL && ADAPTOR->isNilNode(ADAPTOR, ADAPTOR->getParent(ADAPTOR, retval.tree) ) )
|
||
|
{
|
||
|
retval.tree = (<ASTLabelType>)ADAPTOR->getParent(ADAPTOR, retval.tree);
|
||
|
}
|
||
|
<endif>
|
||
|
<if(backtracking)>}<endif>
|
||
|
>>
|
||
|
|
||
|
/** match ^(root children) in tree parser; override here to
|
||
|
* add tree construction actions.
|
||
|
*/
|
||
|
tree(root, actionsAfterRoot, children, nullableChildList,
|
||
|
enclosingTreeLevel, treeLevel) ::= <<
|
||
|
_last = (<ASTLabelType>)LT(1);
|
||
|
{
|
||
|
<ASTLabelType> _save_last_<treeLevel>;
|
||
|
<ASTLabelType> _first_last_<treeLevel>;
|
||
|
<if(!rewriteMode)>
|
||
|
<ASTLabelType> root_<treeLevel>;
|
||
|
<endif>
|
||
|
_save_last_<treeLevel> = _last;
|
||
|
_first_last_<treeLevel> = NULL;
|
||
|
<if(!rewriteMode)>
|
||
|
root_<treeLevel> = (<ASTLabelType>)(ADAPTOR->nilNode(ADAPTOR));
|
||
|
<endif>
|
||
|
<root:element()>
|
||
|
<if(rewriteMode)>
|
||
|
<if(backtracking)>if ( BACKTRACKING ==0 ) {<endif>
|
||
|
<if(root.el.rule)>
|
||
|
if ( _first_<enclosingTreeLevel> == NULL ) _first_<enclosingTreeLevel> = <root.el.label>.tree;
|
||
|
<else>
|
||
|
if ( _first_<enclosingTreeLevel> == NULL ) _first_<enclosingTreeLevel> = <root.el.label>;
|
||
|
<endif>
|
||
|
<if(backtracking)>}<endif>
|
||
|
<endif>
|
||
|
<actionsAfterRoot:element()>
|
||
|
<if(nullableChildList)>
|
||
|
if ( LA(1)==ANTLR3_TOKEN_DOWN ) {
|
||
|
MATCHT(ANTLR3_TOKEN_DOWN, NULL);
|
||
|
<children:element()>
|
||
|
MATCHT(ANTLR3_TOKEN_UP, NULL);
|
||
|
}
|
||
|
<else>
|
||
|
MATCHT(ANTLR3_TOKEN_DOWN, NULL);
|
||
|
<children:element()>
|
||
|
MATCHT(ANTLR3_TOKEN_UP, NULL);
|
||
|
<endif>
|
||
|
<if(!rewriteMode)>
|
||
|
ADAPTOR->addChild(ADAPTOR, root_<enclosingTreeLevel>, root_<treeLevel>);
|
||
|
<endif>
|
||
|
_last = _save_last_<treeLevel>;
|
||
|
}<\n>
|
||
|
>>
|
||
|
|
||
|
// TOKEN AST STUFF
|
||
|
|
||
|
/** ID! and output=AST (same as plain tokenRef) 'cept add
|
||
|
* setting of _last
|
||
|
*/
|
||
|
tokenRefBang(token,label,elementIndex) ::= <<
|
||
|
_last = (<ASTLabelType>)LT(1);
|
||
|
<super.tokenRef(...)>
|
||
|
>>
|
||
|
|
||
|
/** ID auto construct */
|
||
|
tokenRef(token,label,elementIndex,hetero) ::= <<
|
||
|
_last = (<ASTLabelType>)LT(1);
|
||
|
<super.tokenRef(...)>
|
||
|
<if(!rewriteMode)>
|
||
|
<if(backtracking)>if ( BACKTRACKING ==0 ) {<endif>
|
||
|
<if(hetero)>
|
||
|
<label>_tree = <hetero>New(<label>);
|
||
|
<else>
|
||
|
<label>_tree = (<ASTLabelType>)ADAPTOR->dupNode(ADAPTOR, <label>);
|
||
|
<endif>
|
||
|
ADAPTOR->addChild(ADAPTOR, root_<treeLevel>, <label>_tree);
|
||
|
<if(backtracking)>}<endif>
|
||
|
<else>
|
||
|
<if(backtracking)>if ( BACKTRACKING ==0 ) {<endif>
|
||
|
if ( _first_<treeLevel> == NULL ) _first_<treeLevel> = <label>;
|
||
|
<if(backtracking)>}<endif>
|
||
|
<endif>
|
||
|
>>
|
||
|
|
||
|
/** label+=TOKEN auto construct */
|
||
|
tokenRefAndListLabel(token,label,elementIndex,hetero) ::= <<
|
||
|
<tokenRef(...)>
|
||
|
<listLabel(elem=label,...)>
|
||
|
>>
|
||
|
|
||
|
/** ^(ID ...) auto construct */
|
||
|
tokenRefRuleRoot(token,label,elementIndex,hetero) ::= <<
|
||
|
_last = (<ASTLabelType>)LT(1);
|
||
|
<super.tokenRef(...)>
|
||
|
<if(!rewriteMode)>
|
||
|
<if(backtracking)>if ( BACKTRACKING == 0 ) {<endif>
|
||
|
<if(hetero)>
|
||
|
<label>_tree = <hetero>New(<label>);
|
||
|
<else>
|
||
|
<label>_tree = (<ASTLabelType>)ADAPTOR->dupNode(ADAPTOR, <label>);
|
||
|
<endif><\n>
|
||
|
root_<treeLevel> = (<ASTLabelType>)ADAPTOR->becomeRoot(ADAPTOR, <label>_tree, root_<treeLevel>);
|
||
|
<if(backtracking)>}<endif>
|
||
|
<endif>
|
||
|
>>
|
||
|
|
||
|
/** Match ^(label+=TOKEN ...) auto construct */
|
||
|
tokenRefRuleRootAndListLabel(token,label,elementIndex,hetero) ::= <<
|
||
|
<tokenRefRuleRoot(...)>
|
||
|
<listLabel(elem=label,...)>
|
||
|
>>
|
||
|
|
||
|
// SET AST
|
||
|
|
||
|
matchSet(s,label,hetero,elementIndex,postmatchCode) ::= <<
|
||
|
_last = (<ASTLabelType>)LT(1);
|
||
|
<super.matchSet(..., postmatchCode={
|
||
|
<if(!rewriteMode)>
|
||
|
<if(backtracking)>if ( BACKTRACKING == 0 ) {<endif>
|
||
|
<if(hetero)>
|
||
|
<label>_tree = <hetero>New(<label>);
|
||
|
<else>
|
||
|
<label>_tree = (<ASTLabelType>)ADAPTOR->dupNode(ADAPTOR, <label>);
|
||
|
<endif><\n>
|
||
|
ADAPTOR->addChild(ADAPTOR, root_<treeLevel>, <label>_tree);
|
||
|
<if(backtracking)>}<endif>
|
||
|
<endif>
|
||
|
}
|
||
|
)>
|
||
|
>>
|
||
|
|
||
|
matchRuleBlockSet(s,label,hetero,elementIndex,postmatchCode,treeLevel="0") ::= <<
|
||
|
<matchSet(...)>
|
||
|
<noRewrite()> <! set return tree !>
|
||
|
>>
|
||
|
|
||
|
matchSetBang(s,label,elementIndex,postmatchCode) ::= <<
|
||
|
_last = (<ASTLabelType>)LT(1);
|
||
|
<super.matchSet(...)>
|
||
|
>>
|
||
|
|
||
|
matchSetRuleRoot(s,label,hetero,elementIndex,debug) ::= <<
|
||
|
<super.matchSet(..., postmatchCode={
|
||
|
<if(!rewriteMode)>
|
||
|
<if(backtracking)>if ( BACKTRACKING == 0 ) {<endif>
|
||
|
<if(hetero)>
|
||
|
<label>_tree = <hetero>New(<label>);
|
||
|
<else>
|
||
|
<label>_tree = (<ASTLabelType>)ADAPTOR->dupNode(ADAPTOR, <label>);
|
||
|
<endif>
|
||
|
root_<treeLevel> = (<ASTLabelType>)ADAPTOR->becomeRoot(ADAPTOR, <label>_tree, root_<treeLevel>);
|
||
|
<if(backtracking)>}<endif>
|
||
|
<endif>
|
||
|
}
|
||
|
)>
|
||
|
>>
|
||
|
|
||
|
// RULE REF AST
|
||
|
|
||
|
/** rule auto construct */
|
||
|
ruleRef(rule,label,elementIndex,args,scope) ::= <<
|
||
|
_last = (<ASTLabelType>)LT(1);
|
||
|
<super.ruleRef(...)>
|
||
|
<if(backtracking)>if ( BACKTRACKING == 0 )
|
||
|
{
|
||
|
<endif>
|
||
|
<if(!rewriteMode)>
|
||
|
ADAPTOR->addChild(ADAPTOR, root_<treeLevel>, <label>.tree);
|
||
|
<else>
|
||
|
if ( _first_<treeLevel> == NULL ) _first_<treeLevel> = <label>.tree;
|
||
|
<endif>
|
||
|
<if(backtracking)>}<endif>
|
||
|
>>
|
||
|
|
||
|
/** x+=rule auto construct */
|
||
|
ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
|
||
|
<ruleRef(...)>
|
||
|
<super.listLabelAST(elem=label,...)>
|
||
|
>>
|
||
|
|
||
|
/** ^(rule ...) auto construct */
|
||
|
ruleRefRuleRoot(rule,label,elementIndex,args,scope) ::= <<
|
||
|
_last = (<ASTLabelType>)LT(1);
|
||
|
<super.ruleRef(...)>
|
||
|
<if(!rewriteMode)>
|
||
|
<if(backtracking)>if ( BACKTRACKING == 0 ) <endif>root_<treeLevel> = (<ASTLabelType>)(ADAPTOR->becomeRoot(ADAPTOR, <label>.tree, root_<treeLevel>));
|
||
|
<endif>
|
||
|
>>
|
||
|
|
||
|
/** ^(x+=rule ...) auto construct */
|
||
|
ruleRefRuleRootAndListLabel(rule,label,elementIndex,args,scope) ::= <<
|
||
|
<ruleRefRuleRoot(...)>
|
||
|
<super.listLabelAST(elem=label,...)>
|
||
|
>>
|
||
|
|
||
|
/** rule when output=AST and tracking for rewrite */
|
||
|
ruleRefTrack(rule,label,elementIndex,args,scope) ::= <<
|
||
|
_last = (<ASTLabelType>)LT(1);
|
||
|
<super.ruleRefTrack(...)>
|
||
|
>>
|
||
|
|
||
|
/** x+=rule when output=AST and tracking for rewrite */
|
||
|
ruleRefTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
|
||
|
_last = (<ASTLabelType>)LT(1);
|
||
|
<super.ruleRefTrackAndListLabel(...)>
|
||
|
>>
|
||
|
/** ^(rule ...) rewrite */
|
||
|
ruleRefRuleRootTrack(rule,label,elementIndex,args,scope) ::= <<
|
||
|
_last = (<ASTLabelType>)LT(1);
|
||
|
<super.ruleRefRootTrack(...)>
|
||
|
>>
|
||
|
|
||
|
/** ^(x+=rule ...) rewrite */
|
||
|
ruleRefRuleRootTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
|
||
|
_last = (<ASTLabelType>)LT(1);
|
||
|
<super.ruleRefRuleRootTrackAndListLabel(...)>
|
||
|
>>
|
||
|
|
||
|
|
||
|
/** Streams for token refs are tree nodes now; override to
|
||
|
* change nextToken to nextNode.
|
||
|
*/
|
||
|
createRewriteNodeFromElement(token,hetero,scope) ::= <<
|
||
|
<if(hetero)>
|
||
|
<hetero>New(stream_<token>->nextNode(stream_<token>))
|
||
|
<else>
|
||
|
stream_<token>->nextNode(stream_<token>)
|
||
|
<endif>
|
||
|
>>
|
||
|
|
||
|
ruleCleanUp() ::= <<
|
||
|
<super.ruleCleanUp(...)>
|
||
|
<if(backtracking)>
|
||
|
if ( BACKTRACKING==0 ) {<\n>
|
||
|
<endif>
|
||
|
<if(!ruleDescriptor.isSynPred)>
|
||
|
retval.stop = LT(-1);<\n>
|
||
|
<endif>
|
||
|
retval.tree = ADAPTOR->rulePostProcessing(ADAPTOR, root_0);
|
||
|
<if(backtracking)>
|
||
|
}
|
||
|
<endif>
|
||
|
<ruleDescriptor.allTokenRefsInAltsWithRewrites
|
||
|
:{stream_<it>->free(stream_<it>);}; separator="\n">
|
||
|
<ruleDescriptor.allRuleRefsInAltsWithRewrites
|
||
|
:{stream_<it>->free(stream_<it>);}; separator="\n">
|
||
|
>>
|