diff --git a/antlr-3.1.3/BUILD.txt b/antlr-3.1.3/BUILD.txt new file mode 100644 index 0000000..8694f21 --- /dev/null +++ b/antlr-3.1.3/BUILD.txt @@ -0,0 +1,422 @@ + [The "BSD licence"] + Copyright (c) 2005-2008 Terence Parr + Maven Plugin - Copyright (c) 2009 Jim Idle + + 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. + +============================================================================ + +This file contains the build instructions for the ANTLR toolset as +of version 3.1.3 and beyond. + +The ANTLR toolset must be built using the Maven build system as +this build system updates the version numbers and controls the +whole build process. However, if you just want the latest build +and do not care to learn anything about Maven, then visit: + + http://antlr.org/hudson + +And download the current complete jar from the Tool_Daily +Hudson project (just follow the links for last successful build). +At the time of writing, the link for the last successful +snapshot build is: + + http://antlr.org/hudson/job/ANTLR_Tool_Daily/lastSuccessfulBuild/org.antlr$antlr/ + +If you are looking for the latest released version of ANTLR, then +visit the downloads page on the main antlr.org website. + +These instructions are mainly for the ANTLR development team, +though you are free to build ANTLR yourself of course. + +Source code Structure +----------------------- + +The main development branch of ANTLR is stored within the Perforce SCM at: + + //depot/code/antlr/main/... + +release branches are stored in Perforce like so: + + //depot/code/antlr/release-3.1.3/... + +In this top level directory, you will find a master build file for Maven called pom.xml and +you will also note that there are a number of subdirectories: + + tool - The ANTLR tool itself + runtime/Java - The ANTLR Java runtime + runtime/X - The runtime for language target X + gunit - The grammar test tool + antlr3-maven-plugin - The plugin tool for Maven that allows Maven projects to process + ANTLR grammars. + +Each of these sub-directories also contains a file pom.xml that controls the build of each +sub-component (or module in Maven parlance). + +Build Parameters +----------------- + +Alongside each pom.xml (other than for the antlr3-maven-plugin), you will see that there +is a file called antlr.config. This file is called a filter and should contain a set of key/value +pairs in the same manner as Java properties files: + +antlr.something="Some config thang!" + +When the build of any component happens, any values in the antlr.config for the master +build file and any values in the antlr.config file for each component are made available +to the build. This is mainly used by the resource processor, which will filter any file it +finds under: src/main/resources/** and replace any references such as ${antlr.something} +with the actual value at the time of the build. + +Building +-------- + +Building ANTLR is trivial, assuming that you have loaded Maven version 2.0.9 or +better on to your build system and installed it as explained here: + +http://maven.apache.org/download.html + +If you are unfamiliar with Maven (and even if you are), the best resource for learning +about it is The Definitive Guide: + +http://www.sonatype.com/books/maven-book/reference/public-book.html + +The instructions here assume that Maven is installed and working correctly. + +If this is the first time you have built the ANTLR toolset, you will possibly +need to install the master pom in your local repository (however the build +may be able to locate this in the ANTLR snapshot or release repository). If you try +to build sub-modules on their own (as in run the mvn command in the sub directory +for that tool, such as runtime/Java), and you receive a message that +maven cannot find the master pom, then execute this in the main (or release) +directory: + +mvn -N install + +This command will install the master build pom in your local maven repository +(it's ~/.m2 on UNIX) and individual builds of sub-modules will now work correctly. + +To build then, simply cd into the master build directory (e.g. $P4ROOT//code/antlr/main) +and type: + +mvn -Dmaven.test.skip=true + +Assuming that everything is correctly installed and synchronized, then ANTLR will build +and skip any unit tests in the modules (the ANTLR tool tests can take a long time). + +This command will build each of the tools in the correct order and will create the jar +artifacts of all the components in your local development Maven repository (which +takes precedence over remote repositories by default). At the end of the build you +should see: + +[INFO] ------------------------------------------------------------------------ +[INFO] Reactor Summary: +[INFO] ------------------------------------------------------------------------ +[INFO] ANTLR Master build control POM ........................ SUCCESS [1.373s] +[INFO] Antlr 3 Runtime ....................................... SUCCESS [0.879s] +[INFO] ANTLR Grammar Tool .................................... SUCCESS [5.431s] +[INFO] Maven plugin for ANTLR V3 ............................. SUCCESS [1.277s] +[INFO] ANTLR gUnit ........................................... SUCCESS [1.566s] +[INFO] ------------------------------------------------------------------------ +[INFO] ------------------------------------------------------------------------ +[INFO] BUILD SUCCESSFUL +[INFO] ------------------------------------------------------------------------ +[INFO] Total time: 11 seconds + +However, unless you are using Maven exclusively in your projects, you will most +likely want to build the ANTLR Uber Jar, which is an executable jar containing +all the components that ANTLR needs to build and run parsers (note that at +runtime, you need only the runtime components you use, such as the Java +runtime and say stringtemplate). + +Because the Uber jar is not something we want to deploy to Maven repositories +it is built with a special invocation of Maven: + +mvn -Dmaven.test.skip=true package assembly:assembly + +Note that Maven will appear to build everything twice, which is a quirk of how +it calculates the dependencies and makes sure it has everything packaged up +so it can build the uber-jar assembly. + +Somewhere in the build output (towards the end), you will find a line like this: + +[INFO] Building jar: /home/jimi/antlrsrc/code/antlr/main/target/antlr-master-3.1.3-SNAPSHOT-completejar.jar + +This is the executable jar that you need and you can either copy it somewhere or, +like me, you can create this script (assuming UNIX) somewhere in your PATH: + +#! /bin/bash +java -jar ~/antlrsrc/code/antlr/main/target/antlr-master-3.1.3-SNAPSHOT-completejar.jar $* + +Version Numbering +------------------- + +The first and Golden rule is that any pom files stored under the main branch of the toolset +should never be modified to contain a release version number. They should always contain +a.b.c-SNAPSHOT (e.g. 3.1.3-SNAPSHOT). Only release branches should have their +pom version numbers set to a release version. You can release as many SNAPSHOTS +as you like, but only one release version. However, release versions may be updated +with a patch level: 3.1.3-1, 3.1.3-2 and so on. + +Fortunately, Maven helps us with the version numbering in a number of ways. Firstly, +the pom.xml files for the various modules do not specify a version of the +artifacts themselves. They pick up their version number from the master build pom. +However, there is a catch, because they need to know what version of the parent pom +they inherit from and so they DO mention the version number. However, this does +prevent accidentally releasing different versions of sub-modules than the master pom +describes. + +Fortunately once again, Maven has a neat way of helping us with change the version. +All you need do is check out all the pom.xml files from perforce, then modify the +a.b.c-SNAPSHOT in the master pom. When the version number +is correct in the master pom, you make sure your working directory is the location +of the master pom and type: + +mvn versions:update-child-modules + +This command will then update the child pom.xml files to reflect the version number +defined in the master pom.xml. + +There is unfortunately one last catch here though and that is that the antlr3-maven-plugin +is not able to use the parent pom. The reason for this is subtle but makes sense as +doing so would create a circular dependency between the ANTLR tool (which uses the +plugin to build its own grammar files), and the plugin (which uses the tool to build +grammar files). + +This catch-22 situation means that the pom.xml file in the antlr3-maven-plugin directory +MUST be updated manually (or we must write a script to do this). + +Deploying +---------- +Deploying the tools at the current version is relatively easy, but to deploy to the +ANTLR repositories (snapshot or release) you must have been granted access +to the antlr.org server and supplied an ssh key. Few people will have this access of +course. + +Assuming that you have ssh access to antlr.org, then you will need to do the following +before deployment will authorize and work correctly (UNIX assumed here): + +$ eval `ssh-agent` +Agent PID nnnnn +$ ssh-add +Enter passphrase for /home/you/.ssh/id_rsa: +Identity added.... + +Next, because we do not publish access information for antlr.org, you will need +to configure the repository server names locally. You do this by creating (or +adding to) the file: + +~/.m2/settings.xml + +Which should look like this: + + + + + + + antlr-snapshot + mavensync + passphrase for your private key + /home/youruserlogin/.ssh/id_rsa + + + antlr-repo + mavensync + passphrase for your private key + /home/youruserlogin/.ssh/id_rsa + + + + +When this configuration is in place, you will be able to deploy the components, +either individually or from the master directory: + +mvn -Dmaven.test.skip=true deploy + +You will then see lots of information about checking existing version information +and so on, and the components will be deployed. + +Note that so long as the artifacts are versioned with a.b.c-SNAPSHOT then +deployment will always be to the development snapshot directory. When the +artifacts are versioned with a release version then deployment will be to the +antlr.org release repository, which will then be mirrored around the world. It +is important not to deploy a release until you have built and tested it to your +satisfaction. + +Release Checklist +------------------ + +Here is the procedure to use to make a release of ANTLR. Note that we should +really use the mvn release:release command, but the perforce plugin for Maven is +not commercial quality and I want to rewrite it. + +For this checklist, let's assume that the current development version of ANTLR +is 3.1.3-SNAPSHOT. This means that it will probably (but not necessarily) +become release version 3.1.3 and that the development version will bump +to 3.1.4-SNAPSHOT. + +0) Run a build of the main branch and check that it is builds and passes as many + tests as you want it to. + +1) First make a branch from main into the target release directory. Then submit + this to perforce. You could change versions numbers before submitting, but + doing that in separate stages will keep things sane; + +--- Use main development branch from here --- + +2) Before we deploy the release, we want to update the versions of the development + branch, so we don't deploy what is now the new release as an older snapshot (this + is not super important, but procedure is good right?). + + Check out all the pom.xml files (and if you are using any antlr.config parameters + that must change, then do that too). + +3) Edit the master pom.xml in the main directory and change the version from + 3.1.3-SNAPSHOT to 3.1.4-SNAPSHOT. + +4) Edit the pom.xml file for antlr3-maven-plugin under the main directory and + change the version from 3.1.3-SNAPSHOT to 3.1.4-SNAPSHOT. + +5) Now (from the main directory), run the command: + + mvn versions:update-child-modules + + You should see: + + [INFO] [versions:update-child-modules] + [INFO] Module: gunit + [INFO] Parent is org.antlr:antlr-master:3.1.4-SNAPSHOT + [INFO] Module: runtime/Java + [INFO] Parent is org.antlr:antlr-master:3.1.4-SNAPSHOT + [INFO] Module: tool + [INFO] Parent is org.antlr:antlr-master:3.1.4-SNAPSHOT + +6) Run a build of the main branch: + + mvn -Dmaven.test.skip=true + + All should be good. + +7) Submit the pom changes of the main branch to perforce. + +8) Deploy the new snapshot as a placeholder for the next release. It + will go to the snapshot repository of course: + + mvn -N deploy + mvn -Dmaven.test.skip=true deploy + +9) You are now finished with the main development branch and change + working directories to the release branch you made earlier. + +--- Use release branch from here --- + +10) Check out all the pom.xml files in the release branch (and if you are + using any antlr.config parameters that must change, then do that too). + +11) Edit the master pom.xml in the release-3.1.3 directory and change the version from + 3.1.3-SNAPSHOT to 3.1.3. + +12) Edit the pom.xml file for antlr3-maven-plugin under the release-3.1.3 directory and + change the version from 3.1.3-SNAPSHOT to 3.1.3. + +13) Now (from the release-3.1.3 directory), run the command: + + mvn versions:update-child-modules + + You should see: + + [INFO] [versions:update-child-modules] + [INFO] Module: gunit + [INFO] Parent was org.antlr:antlr-master:3.1.3-SNAPSHOT, + now org.antlr:antlr-master:3.1.3 + [INFO] Module: runtime/Java + [INFO] Parent was org.antlr:antlr-master:3.1.3-SNAPSHOT, + now org.antlr:antlr-master:3.1.3 + [INFO] Module: tool + [INFO] Parent was org.antlr:antlr-master:3.1.3-SNAPSHOT, + now org.antlr:antlr-master:3.1.3 + +14) Run a build of the release-3.1.3 branch: + + mvn # Note I am letting unit tests run here! + + All should be good, or as good as it gets ;-) + +15) Submit the pom changes of the release-3.1.3 branch to perforce. + +16) Deploy the new release (this is it guys, make sure you are happy): + + mvn -N deploy + mvn -Dmaven.test.skip=true deploy + + Note that we must skip the tests as Maven will not let you deploy releases + that fail any junit tests. + +17) Reward anyone around you with good beer. + + +Miscellany +----------- + +It was a little tricky to get all the interdependencies correct because ANTLR builds +itself using itself and the maven plugin references the ANTLR Tool as well. Hence +the maven tool is not a child project of the master pom.xml file, even though it is +built by it. + +An observant person will not that when the assembly:assembly phase is run, that +it invokes the build of the ANTLR tool using the version of the Maven plugin that +it has just built, and this results in the plugin using the version of ANTLR tool that +it has just built. This is safe because everything will already be up to date and so +we package up the version of the tool that we expect, but the Maven plugin we +deploy will use the correct version of ANTLR, even though there is technically +a circular dependency. + +The master pom.xml does give us a way to cause the build of the ANTLR tool +to use itself to build itself. This is because in dependencyManagement in the +master pom.xml, we can reference the current version of the Tool and the +Maven plugin, even though in the pom.xml for the tool itself refers to the previous +version of the plugin. + +What happens is that if we first cd into the tool and maven directories and build ANTLR, it will +build itself with the prior version and this will deploy locally (.m2). We can then +clean build from the master pom and when ANTLR asks for the prior version of the tool, +the master pom.xml will override it and build with the interim versions we just built manually. + +However, strictly speaking, we need a third build where we rebuild the tool again with +the version of the tool that was built with itself and not deploy the version that was +built buy the version of itself that was built by a prior version of itself. I decided that this +was not particularly useful and complicates things too much. Building with a prior +version of the tool is fine and if there was ever a need to, we could release twice +in quick succession. + + +Jim Idle - March 2009 + diff --git a/antlr-3.1.3/antlr.config b/antlr-3.1.3/antlr.config new file mode 100644 index 0000000..00ac54e --- /dev/null +++ b/antlr-3.1.3/antlr.config @@ -0,0 +1 @@ +fred=99 diff --git a/antlr-3.1.3/gunit/CHANGES.txt b/antlr-3.1.3/gunit/CHANGES.txt new file mode 100644 index 0000000..af7758e --- /dev/null +++ b/antlr-3.1.3/gunit/CHANGES.txt @@ -0,0 +1,81 @@ +gUnit 1.0.5 +Nov 25, 2008 + +Leon, Jen-Yuan Su +leonsu at mac com + +CHANGES + +Feb 17, 2009 + +* added new interfaces for GUI editor + +* recognizes invalid input as a FAIL case instead of throwing an exception + +Steve Ebersole provided a patch for the following two fixes. +* allows setting an output directory (for JUnitCodeGen) + +* allows providing a classloader (for both JUnitCodeGen and gUnitExecutor) + +Nov 25, 2008 + +* fixed external test file path issue. if an input test file is not found under the current dir, then try to look for it under the package dir also. + +* fixed multiple-line input indentation issue. + +* fixed bug: FileNotFoundException terminated gUnit tests due to any non-existent input test file. + +* display escaped text for newline characters in the test result for comparing expected and actual string. + +Nov 20, 2008 + +* added new functionality of testing lexical rules + +* fixed bug of using PipedInput/Output Stream and changed to ByteArrayOutputStream. Jared Bunting provided a patch on this issue. + +* improved jUnit translation mode and moved supporting codes into gUnitBaseTest. + +Oct 31, 2008 + +* fixed bug of testing a tree grammar's template output + +July 9, 2008 + +* fixed bug: program exited upon InvocationTargetException + Sumanto Biswas pointed out the issue and provided suggestions. + +* Better handle on test rule's StringTemplate output + +May 10, 2008 + +* added exit code functionality + +* fixed string escaping bug for junit generator + +1.0.2 - Apr 01, 2008 + +* fixed grammar bug: multiple-line input, AST output + +* adjusted the output of test result + +Mar 20, 2008 + +* moved test result to string template (gUnitTestResult.stg) + +* added the display of line of test in the test result + +Feb 19, 2008 + +* fixed bug of displaying test sequence and error message from ANTLR + +Feb 8, 2008 + +* made compatible with ANTLR 3.1b1 + +1.0.1 - Jan 11, 2008 + +* Kenny MacDermid helps with code refactoring + +1.0 - Aug 20, 2007 + +Initial early access release diff --git a/antlr-3.1.3/gunit/LICENSE.txt b/antlr-3.1.3/gunit/LICENSE.txt new file mode 100644 index 0000000..b6ea2eb --- /dev/null +++ b/antlr-3.1.3/gunit/LICENSE.txt @@ -0,0 +1,26 @@ +[The "BSD licence"] +Copyright (c) 2007-2008 Leon, Jen-Yuan Su +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. diff --git a/antlr-3.1.3/gunit/README.txt b/antlr-3.1.3/gunit/README.txt new file mode 100644 index 0000000..f8d83c9 --- /dev/null +++ b/antlr-3.1.3/gunit/README.txt @@ -0,0 +1,56 @@ +gUnit 1.0.5 +Feb 21, 2009 + +Leon, Jen-Yuan Su +leonsu at mac com + +INTRODUCTION + +Welcome to gUnit! I've been working on gUnit from 2007 summer and +this is a project in USF CS, sponsored by professor Terence. + +You should use the latest ANTLR v3.1 with gUnit: + +http://www.antlr.org/download.html + +See the wiki document: + +http://www.antlr.org/wiki/display/ANTLR3/gUnit+-+Grammar+Unit+Testing + +Per the license in LICENSE.txt, this software is not guaranteed to +work and might even destroy all life on this planet: + +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. + +---------------------------------------------------------------------- + +EXAMPLES + +See the wiki tutorial of gUnit: + + http://www.antlr.org/wiki/display/ANTLR3/gUnit+-+Grammar+Unit+Testing + +---------------------------------------------------------------------- + +What is gUnit? + +gUnit is an unit testing framework for ANTLR grammars. It provides a +simple way to write and run automated tests for grammars in a manner +similar to what jUnit does for unit testing. + +---------------------------------------------------------------------- + +How do I install gUnit? + +Just add gunit-1.0.5.jar to your CLASSPATH, and also make sure that +both ANTLR and StringTemplate jars lie in CLASSPATH. diff --git a/antlr-3.1.3/gunit/antlr.config b/antlr-3.1.3/gunit/antlr.config new file mode 100644 index 0000000..e69de29 diff --git a/antlr-3.1.3/gunit/pom.xml b/antlr-3.1.3/gunit/pom.xml new file mode 100644 index 0000000..ce4cc64 --- /dev/null +++ b/antlr-3.1.3/gunit/pom.xml @@ -0,0 +1,168 @@ + + + 4.0.0 + org.antlr + gunit + jar + + ANTLR gUnit + + + org.antlr + antlr-master + 3.1.3 + + + http://www.antlr.org/wiki/display/ANTLR3/gUnit+-+Grammar+Unit+Testing + + + + + + antlr-repo + ANTLR Testing repository + scpexe://antlr.org/home/mavensync/antlr-repo + + + + antlr-snapshot + ANTLR Testing Snapshot Repository + scpexe://antlr.org/home/mavensync/antlr-snapshot + + + + + + + + + + antlr-snapshot + ANTLR Testing Snapshot Repository + http://antlr.org/antlr-snapshot + + always + + + + + + + + + + junit + junit + 4.5 + compile + + + + org.antlr + antlr + ${project.version} + compile + + + + + org.antlr + stringtemplate + 3.2 + compile + + + + + + + + + install + + + + + org.antlr + antlr3-maven-plugin + ${project.version} + + + + + antlr + + + + + + + maven-compiler-plugin + + 1.5 + 1.5 + src + + + + + maven-surefire-plugin + + + + org.codehaus.mojo + findbugs-maven-plugin + + true + true + true + + + + + + + + org.apache.maven.wagon + wagon-ssh-external + 1.0-beta-2 + + + + + + + diff --git a/antlr-3.1.3/gunit/src/main/antlr3/org/antlr/gunit/gUnit.g b/antlr-3.1.3/gunit/src/main/antlr3/org/antlr/gunit/gUnit.g new file mode 100644 index 0000000..156df40 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/antlr3/org/antlr/gunit/gUnit.g @@ -0,0 +1,330 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Leon Jen-Yuan Su +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. +*/ +grammar gUnit; +options {language=Java;} +tokens { + OK = 'OK'; + FAIL = 'FAIL'; + DOC_COMMENT; +} +@header {package org.antlr.gunit;} +@lexer::header { +package org.antlr.gunit; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +} +@members { +public GrammarInfo grammarInfo; +public gUnitParser(TokenStream input, GrammarInfo grammarInfo) { + super(input); + this.grammarInfo = grammarInfo; +} +} + +gUnitDef: 'gunit' g1=id ('walks' g2=id)? ';' + { + if ( $g2.text!=null ) { + grammarInfo.setGrammarName($g2.text); + grammarInfo.setTreeGrammarName($g1.text); + } + else { + grammarInfo.setGrammarName($g1.text); + } + } + header? testsuite* + ; + +header : '@header' ACTION + { + int pos1, pos2; + if ( (pos1=$ACTION.text.indexOf("package"))!=-1 && (pos2=$ACTION.text.indexOf(';'))!=-1 ) { + grammarInfo.setHeader($ACTION.text.substring(pos1+8, pos2).trim()); // substring the package path + } + else { + System.err.println("error(line "+$ACTION.getLine()+"): invalid header"); + } + } + ; + +testsuite // gUnit test suite based on individual rule +scope { +boolean isLexicalRule; +} +@init { +gUnitTestSuite ts = null; +$testsuite::isLexicalRule = false; +} + : ( r1=RULE_REF ('walks' r2=RULE_REF)? + { + if ( $r2==null ) ts = new gUnitTestSuite($r1.text); + else ts = new gUnitTestSuite($r1.text, $r2.text); + } + | t=TOKEN_REF + { + ts = new gUnitTestSuite(); + ts.setLexicalRuleName($t.text); + $testsuite::isLexicalRule = true; + } + ) + ':' + testcase[ts]+ {grammarInfo.addRuleTestSuite(ts);} + ; + +// TODO : currently gUnit just ignores illegal test for lexer rule, but should also emit a reminding message +testcase[gUnitTestSuite ts] // individual test within a (rule)testsuite + : input expect {$ts.addTestCase($input.in, $expect.out);} + ; + +input returns [gUnitTestInput in] +@init { +String testInput = null; +boolean inputIsFile = false; +int line = -1; +} +@after { +in = new gUnitTestInput(testInput, inputIsFile, line); +} + : STRING + { + testInput = $STRING.text.replace("\\n", "\n").replace("\\r", "\r").replace("\\t", "\t") + .replace("\\b", "\b").replace("\\f", "\f").replace("\\\"", "\"").replace("\\'", "\'").replace("\\\\", "\\"); + line = $STRING.line; + } + | ML_STRING + { + testInput = $ML_STRING.text; + line = $ML_STRING.line; + } + | file + { + testInput = $file.text; + inputIsFile = true; + line = $file.line; + } + ; + +expect returns [AbstractTest out] + : OK {$out = new BooleanTest(true);} + | FAIL {$out = new BooleanTest(false);} + | 'returns' RETVAL {if ( !$testsuite::isLexicalRule ) $out = new ReturnTest($RETVAL);} + | '->' output {if ( !$testsuite::isLexicalRule ) $out = new OutputTest($output.token);} + ; + +output returns [Token token] + : STRING + { + $STRING.setText($STRING.text.replace("\\n", "\n").replace("\\r", "\r").replace("\\t", "\t") + .replace("\\b", "\b").replace("\\f", "\f").replace("\\\"", "\"").replace("\\'", "\'").replace("\\\\", "\\")); + $token = $STRING; + } + | ML_STRING {$token = $ML_STRING;} + | AST {$token = $AST;} + | ACTION {$token = $ACTION;} + ; + +file returns [int line] + : id EXT? {$line = $id.line;} + ; + +id returns [int line] + : TOKEN_REF {$line = $TOKEN_REF.line;} + | RULE_REF {$line = $RULE_REF.line;} + ; + +// L E X I C A L R U L E S + +SL_COMMENT + : '//' ~('\r'|'\n')* '\r'? '\n' {$channel=HIDDEN;} + ; + +ML_COMMENT + : '/*' {$channel=HIDDEN;} .* '*/' + ; + +STRING : '"' ( ESC | ~('\\'|'"') )* '"' {setText(getText().substring(1, getText().length()-1));} + ; + +ML_STRING + : {// we need to determine the number of spaces or tabs (indentation) for multi-line input + StringBuffer buf = new StringBuffer(); + int i = -1; + int c = input.LA(-1); + while ( c==' ' || c=='\t' ) { + buf.append((char)c); + c = input.LA(--i); + } + String indentation = buf.reverse().toString(); + } + '<<' .* '>>' + {// also determine the appropriate newline separator and get info of the first and last 2 characters (exclude '<<' and '>>') + String newline = System.getProperty("line.separator"); + String front, end; + int oldFrontIndex = 2; + int oldEndIndex = getText().length()-2; + int newFrontIndex, newEndIndex; + if ( newline.length()==1 ) { + front = getText().substring(2, 3); + end = getText().substring(getText().length()-3, getText().length()-2); + newFrontIndex = 3; + newEndIndex = getText().length()-3; + } + else {// must be 2, e.g. Windows System which uses \r\n as a line separator + front = getText().substring(2, 4); + end = getText().substring(getText().length()-4, getText().length()-2); + newFrontIndex = 4; + newEndIndex = getText().length()-4; + } + // strip unwanted characters, e.g. '<<' (including a newline after it) or '>>' (including a newline before it) + String temp = null; + if ( front.equals(newline) && end.equals(newline) ) { + // need to handle the special case: <<\n>> or <<\r\n>> + if ( newline.length()==1 && getText().length()==5 ) temp = ""; + else if ( newline.length()==2 && getText().length()==6 ) temp = ""; + else temp = getText().substring(newFrontIndex, newEndIndex); + } + else if ( front.equals(newline) ) { + temp = getText().substring(newFrontIndex, oldEndIndex); + } + else if ( end.equals(newline) ) { + temp = getText().substring(oldFrontIndex, newEndIndex); + } + else { + temp = getText().substring(oldFrontIndex, oldEndIndex); + } + // finally we need to prpcess the indentation line by line + BufferedReader bufReader = new BufferedReader(new StringReader(temp)); + buf = new StringBuffer(); + String line = null; + int count = 0; + try { + while((line = bufReader.readLine()) != null) { + if ( line.startsWith(indentation) ) line = line.substring(indentation.length()); + if ( count>0 ) buf.append(newline); + buf.append(line); + count++; + } + setText(buf.toString()); + } + catch (IOException ioe) { + setText(temp); + } + } + ; + +TOKEN_REF + : 'A'..'Z' ('a'..'z'|'A'..'Z'|'_'|'0'..'9')* + ; + +RULE_REF + : 'a'..'z' ('a'..'z'|'A'..'Z'|'_'|'0'..'9')* + ; + +EXT : '.'('a'..'z'|'A'..'Z'|'0'..'9')+; + +RETVAL : NESTED_RETVAL {setText(getText().substring(1, getText().length()-1));} + ; + +fragment +NESTED_RETVAL : + '[' + ( options {greedy=false;} + : NESTED_RETVAL + | . + )* + ']' + ; + +AST : NESTED_AST (' '? NESTED_AST)*; + +fragment +NESTED_AST : + '(' + ( options {greedy=false;} + : NESTED_AST + | . + )* + ')' + ; + +ACTION + : NESTED_ACTION {setText(getText().substring(1, getText().length()-1));} + ; + +fragment +NESTED_ACTION : + '{' + ( options {greedy=false; k=3;} + : NESTED_ACTION + | STRING_LITERAL + | CHAR_LITERAL + | . + )* + '}' + ; + +fragment +CHAR_LITERAL + : '\'' ( ESC | ~('\''|'\\') ) '\'' + ; + +fragment +STRING_LITERAL + : '"' ( ESC | ~('\\'|'"') )* '"' + ; + +fragment +ESC : '\\' + ( 'n' + | 'r' + | 't' + | 'b' + | 'f' + | '"' + | '\'' + | '\\' + | '>' + | 'u' XDIGIT XDIGIT XDIGIT XDIGIT + | . // unknown, leave as it is + ) + ; + +fragment +XDIGIT : + '0' .. '9' + | 'a' .. 'f' + | 'A' .. 'F' + ; + +WS : ( ' ' + | '\t' + | '\r'? '\n' + )+ + {$channel=HIDDEN;} + ; diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/AbstractTest.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/AbstractTest.java new file mode 100644 index 0000000..158bf04 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/AbstractTest.java @@ -0,0 +1,83 @@ +/* + [The "BSD license"] + Copyright (c) 2007 Kenny MacDermid + 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. +*/ +package org.antlr.gunit; + +public abstract class AbstractTest implements ITestCase { + // store essential individual test result for string template + protected String header; + protected String actual; + + protected boolean hasErrorMsg; + + private String testedRuleName; + private int testCaseIndex; + + // TODO: remove these. They're only used as part of a refactor to keep the + // code cleaner. It is a mock-instanceOf() replacement. + public abstract int getType(); + public abstract String getText(); + + public abstract String getExpected(); + // return an escaped string of the expected result + public String getExpectedResult() { + String expected = getExpected(); + if ( expected!=null ) expected = JUnitCodeGen.escapeForJava(expected); + return expected; + } + public abstract String getResult(gUnitTestResult testResult); + public String getHeader() { return this.header; } + public String getActual() { return this.actual; } + // return an escaped string of the actual result + public String getActualResult() { + String actual = getActual(); + // there is no need to escape the error message from ANTLR + if ( actual!=null && !hasErrorMsg ) actual = JUnitCodeGen.escapeForJava(actual); + return actual; + } + + public String getTestedRuleName() { return this.testedRuleName; } + public int getTestCaseIndex() { return this.testCaseIndex; } + + public void setHeader(String rule, String lexicalRule, String treeRule, int numOfTest, int line) { + StringBuffer buf = new StringBuffer(); + buf.append("test" + numOfTest + " ("); + if ( treeRule!=null ) { + buf.append(treeRule+" walks "); + } + if ( lexicalRule!=null ) { + buf.append(lexicalRule + ", line"+line+")" + " - "); + } + else buf.append(rule + ", line"+line+")" + " - "); + this.header = buf.toString(); + } + public void setActual(String actual) { this.actual = actual; } + + public void setTestedRuleName(String testedRuleName) { this.testedRuleName = testedRuleName; } + public void setTestCaseIndex(int testCaseIndex) { this.testCaseIndex = testCaseIndex; } + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/BooleanTest.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/BooleanTest.java new file mode 100644 index 0000000..4426e77 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/BooleanTest.java @@ -0,0 +1,63 @@ +/* + [The "BSD license"] + Copyright (c) 2007 Kenny MacDermid + 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. +*/ +package org.antlr.gunit; + +public class BooleanTest extends AbstractTest { + private boolean ok; + + public BooleanTest(boolean ok) { + this.ok = ok; + } + + @Override + public String getText() { + return (ok)? "OK" : "FAIL"; + } + + @Override + public int getType() { + return (ok)? gUnitParser.OK : gUnitParser.FAIL; + } + + @Override + public String getResult(gUnitTestResult testResult) { + if ( testResult.isLexerTest() ) { + if ( testResult.isSuccess() ) return "OK"; + else { + hasErrorMsg = true; // return error message for boolean test of lexer + return testResult.getError(); + } + } + return (testResult.isSuccess())? "OK" : "FAIL"; + } + + @Override + public String getExpected() { + return (ok)? "OK" : "FAIL"; + } +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/GrammarInfo.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/GrammarInfo.java new file mode 100644 index 0000000..7d22ef2 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/GrammarInfo.java @@ -0,0 +1,87 @@ +/* + [The "BSD licence"] + Copyright (c) 2007-2008 Leon, Jen-Yuan Su + 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. +*/ +package org.antlr.gunit; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class GrammarInfo { + + private String grammarName; // targeted grammar for unit test + private String treeGrammarName = null; // optional, required for testing tree grammar + private String header = null; // optional, required if using java package + private List ruleTestSuites = new ArrayList(); // testsuites for each testing rule + private StringBuffer unitTestResult = new StringBuffer(); + + public String getGrammarName() { + return grammarName; + } + + public void setGrammarName(String grammarName) { + this.grammarName = grammarName; + } + + public String getTreeGrammarName() { + return treeGrammarName; + } + + public void setTreeGrammarName(String treeGrammarName) { + this.treeGrammarName = treeGrammarName; + } + + public String getHeader() { + return header; + } + + public void setHeader(String header) { + this.header = header; + } + + public List getRuleTestSuites() { + // Make this list unmodifiable so that we can refactor knowing it's not changed. + return Collections.unmodifiableList(ruleTestSuites); + } + + public void addRuleTestSuite(gUnitTestSuite testSuite) { + this.ruleTestSuites.add(testSuite); + } + + public void appendUnitTestResult(String result) { + this.unitTestResult.append(result); + } + + // We don't want people messing with the string buffer here, so don't return it. + public String getUnitTestResult() { + return unitTestResult.toString(); + } + + public void setUnitTestResult(StringBuffer unitTestResult) { + this.unitTestResult = unitTestResult; + } +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/ITestCase.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/ITestCase.java new file mode 100644 index 0000000..437b789 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/ITestCase.java @@ -0,0 +1,63 @@ +/* + [The "BSD license"] + Copyright (c) 2009 Shaoting Cai + 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. +*/ +package org.antlr.gunit; + +/** + * ITestCase object locates one test case in a gUnit script by specifying the + * tested rule and the index number of the test case in that group. + * + * For example: + * ---------------------- + * ... + * varDef: + * "int i;" OK + * "float 2f;" FAIL + * ... + * ---------------------- + * The "testedRuleName" for these two test cases will be "varDef". + * The "index" for the "int"-test will be 0. + * The "index" for the "float"-test will be 1. And so on. + * + * @see ITestSuite + */ +public interface ITestCase { + + /** + * Get the name of the rule that is tested by this test case. + * @return name of the tested rule. + */ + public String getTestedRuleName(); + + /** + * Get the index of the test case in the test group for a rule. Starting + * from 0. + * @return index number of the test case. + */ + public int getTestCaseIndex(); + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/ITestSuite.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/ITestSuite.java new file mode 100644 index 0000000..c01673f --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/ITestSuite.java @@ -0,0 +1,45 @@ +/* + [The "BSD license"] + Copyright (c) 2009 Shaoting Cai + 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. +*/ +package org.antlr.gunit; + +/** + * A gUnit script file is an Antlr "test suite". The interface is defined to + * allow the Swing GUI test runner be notified when gUnit interpreter runner + * runs a passed/failed test case. + * + * CHANGES: + * 2009-03-01: SHAOTING + * - change method return void, parameter test object. + */ +public interface ITestSuite { + + public void onPass(ITestCase passTest); + + public void onFail(ITestCase failTest); + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/Interp.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/Interp.java new file mode 100644 index 0000000..07001b0 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/Interp.java @@ -0,0 +1,91 @@ +/* + [The "BSD licence"] + Copyright (c) 2007-2008 Leon Jen-Yuan Su + 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. +*/ +package org.antlr.gunit; +import java.io.File; +import java.io.IOException; + +import org.antlr.runtime.*; + +/** The main gUnit interpreter entry point. + * Read a gUnit script, run unit tests or generate a junit file. + */ +public class Interp { + + public static void main(String[] args) throws IOException, ClassNotFoundException, RecognitionException { + /** Pull char from where? */ + CharStream input = null; + /** If the input source is a testsuite file, where is it? */ + String testsuiteDir = System.getProperty("user.dir"); + + /** Generate junit codes */ + if ( args.length>0 && args[0].equals("-o") ) { + if ( args.length==2 ) { + input = new ANTLRFileStream(args[1]); + File f = new File(args[1]); + testsuiteDir = getTestsuiteDir(f.getCanonicalPath(), f.getName()); + } + else + input = new ANTLRInputStream(System.in); + JUnitCodeGen generater = new JUnitCodeGen(parse(input), testsuiteDir); + generater.compile(); + return; + } + + + /** Run gunit tests */ + if ( args.length==1 ) { + input = new ANTLRFileStream(args[0]); + File f = new File(args[0]); + testsuiteDir = getTestsuiteDir(f.getCanonicalPath(), f.getName()); + } + else + input = new ANTLRInputStream(System.in); + + gUnitExecutor executer = new gUnitExecutor(parse(input), testsuiteDir); + + System.out.print(executer.execTest()); // unit test result + + //return an error code of the number of failures + System.exit(executer.failures.size() + executer.invalids.size()); + } + + public static GrammarInfo parse(CharStream input) throws RecognitionException { + gUnitLexer lexer = new gUnitLexer(input); + CommonTokenStream tokens = new CommonTokenStream(lexer); + + GrammarInfo grammarInfo = new GrammarInfo(); + gUnitParser parser = new gUnitParser(tokens, grammarInfo); + parser.gUnitDef(); // parse gunit script and save elements to grammarInfo + return grammarInfo; + } + + public static String getTestsuiteDir(String fullPath, String fileName) { + return fullPath.substring(0, fullPath.length()-fileName.length()); + } + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/InvalidInputException.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/InvalidInputException.java new file mode 100644 index 0000000..d6e9dfc --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/InvalidInputException.java @@ -0,0 +1,34 @@ +/* + [The "BSD license"] + Copyright (c) 2007 Kenny MacDermid + 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. +*/ +package org.antlr.gunit; + +public class InvalidInputException extends Exception { + + private static final long serialVersionUID = 1L; + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/JUnitCodeGen.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/JUnitCodeGen.java new file mode 100644 index 0000000..140134d --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/JUnitCodeGen.java @@ -0,0 +1,325 @@ +/* + [The "BSD licence"] + Copyright (c) 2007-2008 Leon Jen-Yuan Su + 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. +*/ +package org.antlr.gunit; + +import org.antlr.stringtemplate.StringTemplate; +import org.antlr.stringtemplate.StringTemplateGroup; +import org.antlr.stringtemplate.StringTemplateGroupLoader; +import org.antlr.stringtemplate.CommonGroupLoader; +import org.antlr.stringtemplate.language.AngleBracketTemplateLexer; + +import java.io.*; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.logging.ConsoleHandler; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class JUnitCodeGen { + public GrammarInfo grammarInfo; + public Map ruleWithReturn; + private final String testsuiteDir; + private String outputDirectoryPath = "."; + + private final static Handler console = new ConsoleHandler(); + private static final Logger logger = Logger.getLogger(JUnitCodeGen.class.getName()); + static { + logger.addHandler(console); + } + + public JUnitCodeGen(GrammarInfo grammarInfo, String testsuiteDir) throws ClassNotFoundException { + this( grammarInfo, determineClassLoader(), testsuiteDir); + } + + private static ClassLoader determineClassLoader() { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + if ( classLoader == null ) { + classLoader = JUnitCodeGen.class.getClassLoader(); + } + return classLoader; + } + + public JUnitCodeGen(GrammarInfo grammarInfo, ClassLoader classLoader, String testsuiteDir) throws ClassNotFoundException { + this.grammarInfo = grammarInfo; + this.testsuiteDir = testsuiteDir; + /** Map the name of rules having return value to its return type */ + ruleWithReturn = new HashMap(); + Class parserClass = locateParserClass( grammarInfo, classLoader ); + Method[] methods = parserClass.getDeclaredMethods(); + for(Method method : methods) { + if ( !method.getReturnType().getName().equals("void") ) { + ruleWithReturn.put(method.getName(), method.getReturnType().getName().replace('$', '.')); + } + } + } + + private Class locateParserClass(GrammarInfo grammarInfo, ClassLoader classLoader) throws ClassNotFoundException { + String parserClassName = grammarInfo.getGrammarName() + "Parser"; + if ( grammarInfo.getHeader() != null ) { + parserClassName = grammarInfo.getHeader()+ "." + parserClassName; + } + return classLoader.loadClass( parserClassName ); + } + + public String getOutputDirectoryPath() { + return outputDirectoryPath; + } + + public void setOutputDirectoryPath(String outputDirectoryPath) { + this.outputDirectoryPath = outputDirectoryPath; + } + + public void compile() throws IOException{ + String junitFileName; + if ( grammarInfo.getTreeGrammarName()!=null ) { + junitFileName = "Test"+grammarInfo.getTreeGrammarName(); + } + else { + junitFileName = "Test"+grammarInfo.getGrammarName(); + } + String lexerName = grammarInfo.getGrammarName()+"Lexer"; + String parserName = grammarInfo.getGrammarName()+"Parser"; + + StringTemplateGroupLoader loader = new CommonGroupLoader("org/antlr/gunit", null); + StringTemplateGroup.registerGroupLoader(loader); + StringTemplateGroup.registerDefaultLexer(AngleBracketTemplateLexer.class); + StringBuffer buf = compileToBuffer(junitFileName, lexerName, parserName); + writeTestFile(".", junitFileName+".java", buf.toString()); + } + + public StringBuffer compileToBuffer(String className, String lexerName, String parserName) { + StringTemplateGroup group = StringTemplateGroup.loadGroup("junit"); + StringBuffer buf = new StringBuffer(); + buf.append(genClassHeader(group, className, lexerName, parserName)); + buf.append(genTestRuleMethods(group)); + buf.append("\n\n}"); + return buf; + } + + protected String genClassHeader(StringTemplateGroup group, String junitFileName, String lexerName, String parserName) { + StringTemplate classHeaderST = group.getInstanceOf("classHeader"); + if ( grammarInfo.getHeader()!=null ) { // Set up class package if there is + classHeaderST.setAttribute("header", "package "+grammarInfo.getHeader()+";"); + } + classHeaderST.setAttribute("junitFileName", junitFileName); + + String lexerPath = null; + String parserPath = null; + String treeParserPath = null; + String packagePath = null; + boolean isTreeGrammar = false; + boolean hasPackage = false; + /** Set up appropriate class path for parser/tree parser if using package */ + if ( grammarInfo.getHeader()!=null ) { + hasPackage = true; + packagePath = "./"+grammarInfo.getHeader().replace('.', '/'); + lexerPath = grammarInfo.getHeader()+"."+lexerName; + parserPath = grammarInfo.getHeader()+"."+parserName; + if ( grammarInfo.getTreeGrammarName()!=null ) { + treeParserPath = grammarInfo.getHeader()+"."+grammarInfo.getTreeGrammarName(); + isTreeGrammar = true; + } + } + else { + lexerPath = lexerName; + parserPath = parserName; + if ( grammarInfo.getTreeGrammarName()!=null ) { + treeParserPath = grammarInfo.getTreeGrammarName(); + isTreeGrammar = true; + } + } + classHeaderST.setAttribute("hasPackage", hasPackage); + classHeaderST.setAttribute("packagePath", packagePath); + classHeaderST.setAttribute("lexerPath", lexerPath); + classHeaderST.setAttribute("parserPath", parserPath); + classHeaderST.setAttribute("treeParserPath", treeParserPath); + classHeaderST.setAttribute("isTreeGrammar", isTreeGrammar); + return classHeaderST.toString(); + } + + protected String genTestRuleMethods(StringTemplateGroup group) { + StringBuffer buf = new StringBuffer(); + if ( grammarInfo.getTreeGrammarName()!=null ) { // Generate junit codes of for tree grammar rule + for ( gUnitTestSuite ts: grammarInfo.getRuleTestSuites() ) { + int i = 0; + for ( gUnitTestInput input: ts.testSuites.keySet() ) { // each rule may contain multiple tests + i++; + StringTemplate testRuleMethodST; + /** If rule has multiple return values or ast*/ + if ( ts.testSuites.get(input).getType()==gUnitParser.ACTION && ruleWithReturn.containsKey(ts.getTreeRuleName()) ) { + testRuleMethodST = group.getInstanceOf("testTreeRuleMethod2"); + String inputString = escapeForJava(input.testInput); + String outputString = ts.testSuites.get(input).getText(); + testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(ts.getTreeRuleName())+"_walks_"+ + changeFirstCapital(ts.getRuleName())+i); + testRuleMethodST.setAttribute("testTreeRuleName", '"'+ts.getTreeRuleName()+'"'); + testRuleMethodST.setAttribute("testRuleName", '"'+ts.getRuleName()+'"'); + testRuleMethodST.setAttribute("testInput", '"'+inputString+'"'); + testRuleMethodST.setAttribute("returnType", ruleWithReturn.get(ts.getTreeRuleName())); + testRuleMethodST.setAttribute("isFile", input.inputIsFile); + testRuleMethodST.setAttribute("expecting", outputString); + } + else { + testRuleMethodST = group.getInstanceOf("testTreeRuleMethod"); + String inputString = escapeForJava(input.testInput); + String outputString = ts.testSuites.get(input).getText(); + testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(ts.getTreeRuleName())+"_walks_"+ + changeFirstCapital(ts.getRuleName())+i); + testRuleMethodST.setAttribute("testTreeRuleName", '"'+ts.getTreeRuleName()+'"'); + testRuleMethodST.setAttribute("testRuleName", '"'+ts.getRuleName()+'"'); + testRuleMethodST.setAttribute("testInput", '"'+inputString+'"'); + testRuleMethodST.setAttribute("isFile", input.inputIsFile); + testRuleMethodST.setAttribute("tokenType", getTypeString(ts.testSuites.get(input).getType())); + + if ( ts.testSuites.get(input).getType()==gUnitParser.ACTION ) { // trim ';' at the end of ACTION if there is... + //testRuleMethodST.setAttribute("expecting", outputString.substring(0, outputString.length()-1)); + testRuleMethodST.setAttribute("expecting", outputString); + } + else if ( ts.testSuites.get(input).getType()==gUnitParser.RETVAL ) { // Expected: RETVAL + testRuleMethodST.setAttribute("expecting", outputString); + } + else { // Attach "" to expected STRING or AST + testRuleMethodST.setAttribute("expecting", '"'+escapeForJava(outputString)+'"'); + } + } + buf.append(testRuleMethodST.toString()); + } + } + } + else { // Generate junit codes of for grammar rule + for ( gUnitTestSuite ts: grammarInfo.getRuleTestSuites() ) { + int i = 0; + for ( gUnitTestInput input: ts.testSuites.keySet() ) { // each rule may contain multiple tests + i++; + StringTemplate testRuleMethodST; + /** If rule has multiple return values or ast*/ + if ( ts.testSuites.get(input).getType()==gUnitParser.ACTION && ruleWithReturn.containsKey(ts.getRuleName()) ) { + testRuleMethodST = group.getInstanceOf("testRuleMethod2"); + String inputString = escapeForJava(input.testInput); + String outputString = ts.testSuites.get(input).getText(); + testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(ts.getRuleName())+i); + testRuleMethodST.setAttribute("testRuleName", '"'+ts.getRuleName()+'"'); + testRuleMethodST.setAttribute("testInput", '"'+inputString+'"'); + testRuleMethodST.setAttribute("returnType", ruleWithReturn.get(ts.getRuleName())); + testRuleMethodST.setAttribute("isFile", input.inputIsFile); + testRuleMethodST.setAttribute("expecting", outputString); + } + else { + String testRuleName; + // need to determine whether it's a test for parser rule or lexer rule + if ( ts.isLexicalRule() ) testRuleName = ts.getLexicalRuleName(); + else testRuleName = ts.getRuleName(); + testRuleMethodST = group.getInstanceOf("testRuleMethod"); + String inputString = escapeForJava(input.testInput); + String outputString = ts.testSuites.get(input).getText(); + testRuleMethodST.setAttribute("isLexicalRule", ts.isLexicalRule()); + testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(testRuleName)+i); + testRuleMethodST.setAttribute("testRuleName", '"'+testRuleName+'"'); + testRuleMethodST.setAttribute("testInput", '"'+inputString+'"'); + testRuleMethodST.setAttribute("isFile", input.inputIsFile); + testRuleMethodST.setAttribute("tokenType", getTypeString(ts.testSuites.get(input).getType())); + + if ( ts.testSuites.get(input).getType()==gUnitParser.ACTION ) { // trim ';' at the end of ACTION if there is... + //testRuleMethodST.setAttribute("expecting", outputString.substring(0, outputString.length()-1)); + testRuleMethodST.setAttribute("expecting", outputString); + } + else if ( ts.testSuites.get(input).getType()==gUnitParser.RETVAL ) { // Expected: RETVAL + testRuleMethodST.setAttribute("expecting", outputString); + } + else { // Attach "" to expected STRING or AST + testRuleMethodST.setAttribute("expecting", '"'+escapeForJava(outputString)+'"'); + } + } + buf.append(testRuleMethodST.toString()); + } + } + } + return buf.toString(); + } + + // return a meaningful gUnit token type name instead of using the magic number + public String getTypeString(int type) { + String typeText; + switch (type) { + case gUnitParser.OK : + typeText = "org.antlr.gunit.gUnitParser.OK"; + break; + case gUnitParser.FAIL : + typeText = "org.antlr.gunit.gUnitParser.FAIL"; + break; + case gUnitParser.STRING : + typeText = "org.antlr.gunit.gUnitParser.STRING"; + break; + case gUnitParser.ML_STRING : + typeText = "org.antlr.gunit.gUnitParser.ML_STRING"; + break; + case gUnitParser.RETVAL : + typeText = "org.antlr.gunit.gUnitParser.RETVAL"; + break; + case gUnitParser.AST : + typeText = "org.antlr.gunit.gUnitParser.AST"; + break; + default : + typeText = "org.antlr.gunit.gUnitParser.EOF"; + break; + } + return typeText; + } + + protected void writeTestFile(String dir, String fileName, String content) { + try { + File f = new File(dir, fileName); + FileWriter w = new FileWriter(f); + BufferedWriter bw = new BufferedWriter(w); + bw.write(content); + bw.close(); + w.close(); + } + catch (IOException ioe) { + logger.log(Level.SEVERE, "can't write file", ioe); + } + } + + public static String escapeForJava(String inputString) { + // Gotta escape literal backslash before putting in specials that use escape. + inputString = inputString.replace("\\", "\\\\"); + // Then double quotes need escaping (singles are OK of course). + inputString = inputString.replace("\"", "\\\""); + // note: replace newline to String ".\n", replace tab to String ".\t" + inputString = inputString.replace("\n", "\\n").replace("\t", "\\t").replace("\r", "\\r").replace("\b", "\\b").replace("\f", "\\f"); + + return inputString; + } + + protected String changeFirstCapital(String ruleName) { + String firstChar = String.valueOf(ruleName.charAt(0)); + return firstChar.toUpperCase()+ruleName.substring(1); + } +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/OutputTest.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/OutputTest.java new file mode 100644 index 0000000..71359e5 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/OutputTest.java @@ -0,0 +1,67 @@ +/* + [The "BSD license"] + Copyright (c) 2007 Kenny MacDermid + 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. +*/ +package org.antlr.gunit; + +import org.antlr.runtime.Token; + +/** OutputTest represents a test for not only standard output string, + * but also AST output which is actually a return value from a parser. + */ +public class OutputTest extends AbstractTest { + private final Token token; + + public OutputTest(Token token) { + this.token = token; + } + + @Override + public String getText() { + return token.getText(); + } + + @Override + public int getType() { + return token.getType(); + } + + @Override + // return ANTLR error msg if test failed + public String getResult(gUnitTestResult testResult) { + // Note: we treat the standard output string as a return value also + if ( testResult.isSuccess() ) return testResult.getReturned(); + else { + hasErrorMsg = true; + return testResult.getError(); + } + } + + @Override + public String getExpected() { + return token.getText(); + } +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/ReturnTest.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/ReturnTest.java new file mode 100644 index 0000000..8ec5a4e --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/ReturnTest.java @@ -0,0 +1,69 @@ +/* + [The "BSD license"] + Copyright (c) 2007 Kenny MacDermid + 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. +*/ +package org.antlr.gunit; + +import org.antlr.runtime.Token; + +public class ReturnTest extends AbstractTest { + private final Token retval; + + public ReturnTest(Token retval) { + this.retval = retval; + } + + @Override + public String getText() { + return retval.getText(); + } + + @Override + public int getType() { + return retval.getType(); + } + + @Override + // return ANTLR error msg if test failed + public String getResult(gUnitTestResult testResult) { + if ( testResult.isSuccess() ) return testResult.getReturned(); + else { + hasErrorMsg = true; + return testResult.getError(); + } + } + + @Override + public String getExpected() { + String expect = retval.getText(); + + if ( expect.charAt(0)=='"' && expect.charAt(expect.length()-1)=='"' ) { + expect = expect.substring(1, expect.length()-1); + } + + return expect; + } +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/gUnitBaseTest.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/gUnitBaseTest.java new file mode 100644 index 0000000..a8b5492 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/gUnitBaseTest.java @@ -0,0 +1,456 @@ +/* + [The "BSD licence"] + Copyright (c) 2007-2008 Leon, Jen-Yuan Su + 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. +*/ +package org.antlr.gunit; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.PrintStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import org.antlr.runtime.ANTLRFileStream; +import org.antlr.runtime.ANTLRStringStream; +import org.antlr.runtime.CharStream; +import org.antlr.runtime.CommonTokenStream; +import org.antlr.runtime.Lexer; +import org.antlr.runtime.TokenStream; +import org.antlr.runtime.tree.CommonTree; +import org.antlr.runtime.tree.CommonTreeNodeStream; +import org.antlr.runtime.tree.TreeNodeStream; +import org.antlr.stringtemplate.StringTemplate; + +import junit.framework.TestCase; + +/** All gUnit-generated JUnit class should extend this class + * which implements the essential methods for triggering + * ANTLR parser/tree walker + */ +public abstract class gUnitBaseTest extends TestCase { + + public String packagePath; + public String lexerPath; + public String parserPath; + public String treeParserPath; + + protected String stdout; + protected String stderr; + + private PrintStream console = System.out; + private PrintStream consoleErr = System.err; + + // Invoke target lexer.rule + public String execLexer(String testRuleName, String testInput, boolean isFile) throws Exception { + CharStream input; + /** Set up ANTLR input stream based on input source, file or String */ + if ( isFile ) { + String filePath = testInput; + File testInputFile = new File(filePath); + // if input test file is not found under the current dir, also try to look for it under the package dir + if ( !testInputFile.exists() && packagePath!=null ) { + testInputFile = new File(packagePath, filePath); + if ( testInputFile.exists() ) filePath = testInputFile.getCanonicalPath(); + } + input = new ANTLRFileStream(filePath); + } + else { + input = new ANTLRStringStream(testInput); + } + Class lexer = null; + PrintStream ps = null; // for redirecting stdout later + PrintStream ps2 = null; // for redirecting stderr later + try { + /** Use Reflection to create instances of lexer and parser */ + lexer = Class.forName(lexerPath); + Class[] lexArgTypes = new Class[]{CharStream.class}; // assign type to lexer's args + Constructor lexConstructor = lexer.getConstructor(lexArgTypes); + Object[] lexArgs = new Object[]{input}; // assign value to lexer's args + Object lexObj = lexConstructor.newInstance(lexArgs); // makes new instance of lexer + + Method ruleName = lexer.getMethod("m"+testRuleName, new Class[0]); + + /** Start of I/O Redirecting */ + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream(); + ps = new PrintStream(out); + ps2 = new PrintStream(err); + System.setOut(ps); + System.setErr(ps2); + /** End of redirecting */ + + /** Invoke lexer rule, and get the current index in CharStream */ + ruleName.invoke(lexObj, new Object[0]); + Method ruleName2 = lexer.getMethod("getCharIndex", new Class[0]); + int currentIndex = (Integer) ruleName2.invoke(lexObj, new Object[0]); + if ( currentIndex!=input.size() ) { + ps2.println("extra text found, '"+input.substring(currentIndex, input.size()-1)+"'"); + } + + this.stdout = null; + this.stderr = null; + + if ( err.toString().length()>0 ) { + this.stderr = err.toString(); + return this.stderr; + } + if ( out.toString().length()>0 ) { + this.stdout = out.toString(); + } + if ( err.toString().length()==0 && out.toString().length()==0 ) { + return null; + } + } catch (ClassNotFoundException e) { + e.printStackTrace(); System.exit(1); + } catch (SecurityException e) { + e.printStackTrace(); System.exit(1); + } catch (NoSuchMethodException e) { + e.printStackTrace(); System.exit(1); + } catch (IllegalArgumentException e) { + e.printStackTrace(); System.exit(1); + } catch (InstantiationException e) { + e.printStackTrace(); System.exit(1); + } catch (IllegalAccessException e) { + e.printStackTrace(); System.exit(1); + } catch (InvocationTargetException e) { // This exception could be caused from ANTLR Runtime Exception, e.g. MismatchedTokenException + if ( e.getCause()!=null ) this.stderr = e.getCause().toString(); + else this.stderr = e.toString(); + return this.stderr; + } finally { + try { + if ( ps!=null ) ps.close(); + if ( ps2!=null ) ps2.close(); + System.setOut(console); // Reset standard output + System.setErr(consoleErr); // Reset standard err out + } catch (Exception e) { + e.printStackTrace(); + } + } + return this.stdout; + } + + // Invoke target parser.rule + public Object execParser(String testRuleName, String testInput, boolean isFile) throws Exception { + CharStream input; + /** Set up ANTLR input stream based on input source, file or String */ + if ( isFile ) { + String filePath = testInput; + File testInputFile = new File(filePath); + // if input test file is not found under the current dir, also try to look for it under the package dir + if ( !testInputFile.exists() && packagePath!=null ) { + testInputFile = new File(packagePath, filePath); + if ( testInputFile.exists() ) filePath = testInputFile.getCanonicalPath(); + } + input = new ANTLRFileStream(filePath); + } + else { + input = new ANTLRStringStream(testInput); + } + Class lexer = null; + Class parser = null; + PrintStream ps = null; // for redirecting stdout later + PrintStream ps2 = null; // for redirecting stderr later + try { + /** Use Reflection to create instances of lexer and parser */ + lexer = Class.forName(lexerPath); + Class[] lexArgTypes = new Class[]{CharStream.class}; // assign type to lexer's args + Constructor lexConstructor = lexer.getConstructor(lexArgTypes); + Object[] lexArgs = new Object[]{input}; // assign value to lexer's args + Object lexObj = lexConstructor.newInstance(lexArgs); // makes new instance of lexer + + CommonTokenStream tokens = new CommonTokenStream((Lexer) lexObj); + parser = Class.forName(parserPath); + Class[] parArgTypes = new Class[]{TokenStream.class}; // assign type to parser's args + Constructor parConstructor = parser.getConstructor(parArgTypes); + Object[] parArgs = new Object[]{tokens}; // assign value to parser's args + Object parObj = parConstructor.newInstance(parArgs); // makes new instance of parser + + Method ruleName = parser.getMethod(testRuleName); + + /** Start of I/O Redirecting */ + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream(); + ps = new PrintStream(out); + ps2 = new PrintStream(err); + System.setOut(ps); + System.setErr(ps2); + /** End of redirecting */ + + /** Invoke grammar rule, and store if there is a return value */ + Object ruleReturn = ruleName.invoke(parObj); + String astString = null; + String stString = null; + /** If rule has return value, determine if it contains an AST or a ST */ + if ( ruleReturn!=null ) { + if ( ruleReturn.getClass().toString().indexOf(testRuleName+"_return")>0 ) { + try { // NullPointerException may happen here... + Class _return = Class.forName(parserPath+"$"+testRuleName+"_return"); + Method[] methods = _return.getDeclaredMethods(); + for(Method method : methods) { + if ( method.getName().equals("getTree") ) { + Method returnName = _return.getMethod("getTree"); + CommonTree tree = (CommonTree) returnName.invoke(ruleReturn); + astString = tree.toStringTree(); + } + else if ( method.getName().equals("getTemplate") ) { + Method returnName = _return.getMethod("getTemplate"); + StringTemplate st = (StringTemplate) returnName.invoke(ruleReturn); + stString = st.toString(); + } + } + } + catch(Exception e) { + System.err.println(e); // Note: If any exception occurs, the test is viewed as failed. + } + } + } + + this.stdout = null; + this.stderr = null; + + /** Invalid input */ + if ( tokens.index()!=tokens.size() ) { + throw new InvalidInputException(); + } + + // retVal could be actual return object from rule, stderr or stdout + if ( err.toString().length()>0 ) { + this.stderr = err.toString(); + return this.stderr; + } + if ( out.toString().length()>0 ) { + this.stdout = out.toString(); + } + if ( astString!=null ) { // Return toStringTree of AST + return astString; + } + else if ( stString!=null ) {// Return toString of ST + return stString; + } + if ( ruleReturn!=null ) { + return ruleReturn; + } + if ( err.toString().length()==0 && out.toString().length()==0 ) { + return null; + } + } catch (ClassNotFoundException e) { + e.printStackTrace(); System.exit(1); + } catch (SecurityException e) { + e.printStackTrace(); System.exit(1); + } catch (NoSuchMethodException e) { + e.printStackTrace(); System.exit(1); + } catch (IllegalAccessException e) { + e.printStackTrace(); System.exit(1); + } catch (InvocationTargetException e) { + if ( e.getCause()!=null ) this.stderr = e.getCause().toString(); + else this.stderr = e.toString(); + return this.stderr; + } finally { + try { + if ( ps!=null ) ps.close(); + if ( ps2!=null ) ps2.close(); + System.setOut(console); // Reset standard output + System.setErr(consoleErr); // Reset standard err out + } catch (Exception e) { + e.printStackTrace(); + } + } + return this.stdout; + } + + // Invoke target parser.rule + public Object execTreeParser(String testTreeRuleName, String testRuleName, String testInput, boolean isFile) throws Exception { + CharStream input; + if ( isFile ) { + String filePath = testInput; + File testInputFile = new File(filePath); + // if input test file is not found under the current dir, also try to look for it under the package dir + if ( !testInputFile.exists() && packagePath!=null ) { + testInputFile = new File(packagePath, filePath); + if ( testInputFile.exists() ) filePath = testInputFile.getCanonicalPath(); + } + input = new ANTLRFileStream(filePath); + } + else { + input = new ANTLRStringStream(testInput); + } + Class lexer = null; + Class parser = null; + Class treeParser = null; + PrintStream ps = null; // for redirecting stdout later + PrintStream ps2 = null; // for redirecting stderr later + try { + /** Use Reflection to create instances of lexer and parser */ + lexer = Class.forName(lexerPath); + Class[] lexArgTypes = new Class[]{CharStream.class}; // assign type to lexer's args + Constructor lexConstructor = lexer.getConstructor(lexArgTypes); + Object[] lexArgs = new Object[]{input}; // assign value to lexer's args + Object lexObj = lexConstructor.newInstance(lexArgs); // makes new instance of lexer + + CommonTokenStream tokens = new CommonTokenStream((Lexer) lexObj); + + parser = Class.forName(parserPath); + Class[] parArgTypes = new Class[]{TokenStream.class}; // assign type to parser's args + Constructor parConstructor = parser.getConstructor(parArgTypes); + Object[] parArgs = new Object[]{tokens}; // assign value to parser's args + Object parObj = parConstructor.newInstance(parArgs); // makes new instance of parser + + Method ruleName = parser.getMethod(testRuleName); + + /** Start of I/O Redirecting */ + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream(); + ps = new PrintStream(out); + ps2 = new PrintStream(err); + System.setOut(ps); + System.setErr(ps2); + /** End of redirecting */ + + /** Invoke grammar rule, and get the return value */ + Object ruleReturn = ruleName.invoke(parObj); + + Class _return = Class.forName(parserPath+"$"+testRuleName+"_return"); + Method returnName = _return.getMethod("getTree"); + CommonTree tree = (CommonTree) returnName.invoke(ruleReturn); + + // Walk resulting tree; create tree nodes stream first + CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree); + // AST nodes have payload that point into token stream + nodes.setTokenStream(tokens); + // Create a tree walker attached to the nodes stream + treeParser = Class.forName(treeParserPath); + Class[] treeParArgTypes = new Class[]{TreeNodeStream.class}; // assign type to tree parser's args + Constructor treeParConstructor = treeParser.getConstructor(treeParArgTypes); + Object[] treeParArgs = new Object[]{nodes}; // assign value to tree parser's args + Object treeParObj = treeParConstructor.newInstance(treeParArgs); // makes new instance of tree parser + // Invoke the tree rule, and store the return value if there is + Method treeRuleName = treeParser.getMethod(testTreeRuleName); + Object treeRuleReturn = treeRuleName.invoke(treeParObj); + + String astString = null; + String stString = null; + /** If tree rule has return value, determine if it contains an AST or a ST */ + if ( treeRuleReturn!=null ) { + if ( treeRuleReturn.getClass().toString().indexOf(testTreeRuleName+"_return")>0 ) { + try { // NullPointerException may happen here... + Class _treeReturn = Class.forName(treeParserPath+"$"+testTreeRuleName+"_return"); + Method[] methods = _treeReturn.getDeclaredMethods(); + for(Method method : methods) { + if ( method.getName().equals("getTree") ) { + Method treeReturnName = _treeReturn.getMethod("getTree"); + CommonTree returnTree = (CommonTree) treeReturnName.invoke(treeRuleReturn); + astString = returnTree.toStringTree(); + } + else if ( method.getName().equals("getTemplate") ) { + Method treeReturnName = _return.getMethod("getTemplate"); + StringTemplate st = (StringTemplate) treeReturnName.invoke(treeRuleReturn); + stString = st.toString(); + } + } + } + catch(Exception e) { + System.err.println(e); // Note: If any exception occurs, the test is viewed as failed. + } + } + } + + this.stdout = null; + this.stderr = null; + + /** Invalid input */ + if ( tokens.index()!=tokens.size() ) { + throw new InvalidInputException(); + } + + // retVal could be actual return object from rule, stderr or stdout + if ( err.toString().length()>0 ) { + this.stderr = err.toString(); + return this.stderr; + } + if ( out.toString().length()>0 ) { + this.stdout = out.toString(); + } + if ( astString!=null ) { // Return toStringTree of AST + return astString; + } + else if ( stString!=null ) {// Return toString of ST + return stString; + } + if ( treeRuleReturn!=null ) { + return treeRuleReturn; + } + if ( err.toString().length()==0 && out.toString().length()==0 ) { + return null; + } + } catch (ClassNotFoundException e) { + e.printStackTrace(); System.exit(1); + } catch (SecurityException e) { + e.printStackTrace(); System.exit(1); + } catch (NoSuchMethodException e) { + e.printStackTrace(); System.exit(1); + } catch (IllegalAccessException e) { + e.printStackTrace(); System.exit(1); + } catch (InvocationTargetException e) { + if ( e.getCause()!=null ) this.stderr = e.getCause().toString(); + else this.stderr = e.toString(); + return this.stderr; + } finally { + try { + if ( ps!=null ) ps.close(); + if ( ps2!=null ) ps2.close(); + System.setOut(console); // Reset standard output + System.setErr(consoleErr); // Reset standard err out + } catch (Exception e) { + e.printStackTrace(); + } + } + return stdout; + } + + // Modify the return value if the expected token type is OK or FAIL + public Object examineExecResult(int tokenType, Object retVal) { + if ( tokenType==gUnitParser.OK ) { // expected Token: OK + if ( this.stderr==null ) { + return "OK"; + } + else { + return "FAIL, "+this.stderr; + } + } + else if ( tokenType==gUnitParser.FAIL ) { // expected Token: FAIL + if ( this.stderr!=null ) { + return "FAIL"; + } + else { + return "OK"; + } + } + else { // return the same object for the other token types + return retVal; + } + } + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/gUnitExecutor.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/gUnitExecutor.java new file mode 100644 index 0000000..50740ff --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/gUnitExecutor.java @@ -0,0 +1,620 @@ +/* + [The "BSD licence"] + Copyright (c) 2007-2008 Leon Jen-Yuan Su + 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. +*/ +package org.antlr.gunit; + +import java.io.*; +import java.util.List; +import java.util.ArrayList; +import java.lang.reflect.*; +import org.antlr.runtime.*; +import org.antlr.runtime.tree.*; +import org.antlr.stringtemplate.CommonGroupLoader; +import org.antlr.stringtemplate.StringTemplate; +import org.antlr.stringtemplate.StringTemplateGroup; +import org.antlr.stringtemplate.StringTemplateGroupLoader; +import org.antlr.stringtemplate.language.AngleBracketTemplateLexer; + +public class gUnitExecutor implements ITestSuite { + public GrammarInfo grammarInfo; + + private final ClassLoader grammarClassLoader; + + private final String testsuiteDir; + + public int numOfTest; + + public int numOfSuccess; + + public int numOfFailure; + + private String title; + + public int numOfInvalidInput; + + private String parserName; + + private String lexerName; + + public List failures; + public List invalids; + + private PrintStream console = System.out; + private PrintStream consoleErr = System.err; + + public gUnitExecutor(GrammarInfo grammarInfo, String testsuiteDir) { + this( grammarInfo, determineClassLoader(), testsuiteDir); + } + + private static ClassLoader determineClassLoader() { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + if ( classLoader == null ) { + classLoader = gUnitExecutor.class.getClassLoader(); + } + return classLoader; + } + + public gUnitExecutor(GrammarInfo grammarInfo, ClassLoader grammarClassLoader, String testsuiteDir) { + this.grammarInfo = grammarInfo; + this.grammarClassLoader = grammarClassLoader; + this.testsuiteDir = testsuiteDir; + numOfTest = 0; + numOfSuccess = 0; + numOfFailure = 0; + numOfInvalidInput = 0; + failures = new ArrayList(); + invalids = new ArrayList(); + } + + protected ClassLoader getGrammarClassLoader() { + return grammarClassLoader; + } + + protected final Class classForName(String name) throws ClassNotFoundException { + return getGrammarClassLoader().loadClass( name ); + } + + public String execTest() throws IOException{ + // Set up string template for testing result + StringTemplate testResultST = getTemplateGroup().getInstanceOf("testResult"); + try { + /** Set up appropriate path for parser/lexer if using package */ + if (grammarInfo.getHeader()!=null ) { + parserName = grammarInfo.getHeader()+"."+grammarInfo.getGrammarName()+"Parser"; + lexerName = grammarInfo.getHeader()+"."+grammarInfo.getGrammarName()+"Lexer"; + } + else { + parserName = grammarInfo.getGrammarName()+"Parser"; + lexerName = grammarInfo.getGrammarName()+"Lexer"; + } + + /*** Start Unit/Functional Testing ***/ + // Execute unit test of for parser, lexer and tree grammar + if ( grammarInfo.getTreeGrammarName()!=null ) { + title = "executing testsuite for tree grammar:"+grammarInfo.getTreeGrammarName()+" walks "+parserName; + } + else { + title = "executing testsuite for grammar:"+grammarInfo.getGrammarName(); + } + executeTests(); + // End of exection of unit testing + + // Fill in the template holes with the test results + testResultST.setAttribute("title", title); + testResultST.setAttribute("num_of_test", numOfTest); + testResultST.setAttribute("num_of_failure", numOfFailure); + if ( numOfFailure>0 ) { + testResultST.setAttribute("failure", failures); + } + if ( numOfInvalidInput>0 ) { + testResultST.setAttribute("has_invalid", true); + testResultST.setAttribute("num_of_invalid", numOfInvalidInput); + testResultST.setAttribute("invalid", invalids); + } + } + catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + return testResultST.toString(); + } + + private StringTemplateGroup getTemplateGroup() { + StringTemplateGroupLoader loader = new CommonGroupLoader("org/antlr/gunit", null); + StringTemplateGroup.registerGroupLoader(loader); + StringTemplateGroup.registerDefaultLexer(AngleBracketTemplateLexer.class); + StringTemplateGroup group = StringTemplateGroup.loadGroup("gUnitTestResult"); + return group; + } + + // TODO: throw more specific exceptions + private gUnitTestResult runCorrectParser(String parserName, String lexerName, String rule, String lexicalRule, String treeRule, gUnitTestInput input) throws Exception + { + if ( lexicalRule!=null ) return runLexer(lexerName, lexicalRule, input); + else if ( treeRule!=null ) return runTreeParser(parserName, lexerName, rule, treeRule, input); + else return runParser(parserName, lexerName, rule, input); + } + + private void executeTests() throws Exception { + for ( gUnitTestSuite ts: grammarInfo.getRuleTestSuites() ) { + String rule = ts.getRuleName(); + String lexicalRule = ts.getLexicalRuleName(); + String treeRule = ts.getTreeRuleName(); + for ( gUnitTestInput input: ts.testSuites.keySet() ) { // each rule may contain multiple tests + numOfTest++; + // Run parser, and get the return value or stdout or stderr if there is + gUnitTestResult result = null; + AbstractTest test = ts.testSuites.get(input); + try { + // TODO: create a -debug option to turn on logging, which shows progress of running tests + //System.out.print(numOfTest + ". Running rule: " + rule + "; input: '" + input.testInput + "'"); + result = runCorrectParser(parserName, lexerName, rule, lexicalRule, treeRule, input); + // TODO: create a -debug option to turn on logging, which shows progress of running tests + //System.out.println("; Expecting " + test.getExpected() + "; Success?: " + test.getExpected().equals(test.getResult(result))); + } catch ( InvalidInputException e) { + numOfInvalidInput++; + test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.getLine()); + test.setActual(input.testInput); + invalids.add(test); + continue; + } // TODO: ensure there's no other exceptions required to be handled here... + + String expected = test.getExpected(); + String actual = test.getResult(result); + test.setActual(actual); + + if (actual == null) { + numOfFailure++; + test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.getLine()); + test.setActual("null"); + failures.add(test); + onFail(test); + } + // the 2nd condition is used for the assertFAIL test of lexer rule because BooleanTest return err msg instead of 'FAIL' if isLexerTest + else if ( expected.equals(actual) || (expected.equals("FAIL")&&!actual.equals("OK") ) ) { + numOfSuccess++; + onPass(test); + } + // TODO: something with ACTIONS - at least create action test type and throw exception. + else if ( ts.testSuites.get(input).getType()==gUnitParser.ACTION ) { // expected Token: ACTION + numOfFailure++; + test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.getLine()); + test.setActual("\t"+"{ACTION} is not supported in the grammarInfo yet..."); + failures.add(test); + onFail(test); + } + else { + numOfFailure++; + test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.getLine()); + failures.add(test); + onFail(test); + } + } // end of 2nd for-loop: tests for individual rule + } // end of 1st for-loop: testsuites for grammar + } + + // TODO: throw proper exceptions + protected gUnitTestResult runLexer(String lexerName, String testRuleName, gUnitTestInput testInput) throws Exception { + CharStream input; + Class lexer = null; + PrintStream ps = null; // for redirecting stdout later + PrintStream ps2 = null; // for redirecting stderr later + try { + /** Set up ANTLR input stream based on input source, file or String */ + input = getANTLRInputStream(testInput); + + /** Use Reflection to create instances of lexer and parser */ + lexer = classForName(lexerName); + Class[] lexArgTypes = new Class[]{CharStream.class}; // assign type to lexer's args + Constructor lexConstructor = lexer.getConstructor(lexArgTypes); + Object[] lexArgs = new Object[]{input}; // assign value to lexer's args + Object lexObj = lexConstructor.newInstance(lexArgs); // makes new instance of lexer + + Method ruleName = lexer.getMethod("m"+testRuleName, new Class[0]); + + /** Start of I/O Redirecting */ + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream(); + ps = new PrintStream(out); + ps2 = new PrintStream(err); + System.setOut(ps); + System.setErr(ps2); + /** End of redirecting */ + + /** Invoke lexer rule, and get the current index in CharStream */ + ruleName.invoke(lexObj, new Object[0]); + Method ruleName2 = lexer.getMethod("getCharIndex", new Class[0]); + int currentIndex = (Integer) ruleName2.invoke(lexObj, new Object[0]); + if ( currentIndex!=input.size() ) { + ps2.print("extra text found, '"+input.substring(currentIndex, input.size()-1)+"'"); + } + + if ( err.toString().length()>0 ) { + gUnitTestResult testResult = new gUnitTestResult(false, err.toString(), true); + testResult.setError(err.toString()); + return testResult; + } + String stdout = null; + if ( out.toString().length()>0 ) { + stdout = out.toString(); + } + return new gUnitTestResult(true, stdout, true); + } catch (IOException e) { + return getTestExceptionResult(e); + } catch (ClassNotFoundException e) { + e.printStackTrace(); System.exit(1); + } catch (SecurityException e) { + e.printStackTrace(); System.exit(1); + } catch (NoSuchMethodException e) { + e.printStackTrace(); System.exit(1); + } catch (IllegalArgumentException e) { + e.printStackTrace(); System.exit(1); + } catch (InstantiationException e) { + e.printStackTrace(); System.exit(1); + } catch (IllegalAccessException e) { + e.printStackTrace(); System.exit(1); + } catch (InvocationTargetException e) { // This exception could be caused from ANTLR Runtime Exception, e.g. MismatchedTokenException + return getTestExceptionResult(e); + } finally { + try { + if ( ps!=null ) ps.close(); + if ( ps2!=null ) ps2.close(); + System.setOut(console); // Reset standard output + System.setErr(consoleErr); // Reset standard err out + } catch (Exception e) { + e.printStackTrace(); + } + } + // TODO: verify this: + throw new Exception("This should be unreachable?"); + } + + // TODO: throw proper exceptions + protected gUnitTestResult runParser(String parserName, String lexerName, String testRuleName, gUnitTestInput testInput) throws Exception { + CharStream input; + Class lexer = null; + Class parser = null; + PrintStream ps = null; // for redirecting stdout later + PrintStream ps2 = null; // for redirecting stderr later + try { + /** Set up ANTLR input stream based on input source, file or String */ + input = getANTLRInputStream(testInput); + + /** Use Reflection to create instances of lexer and parser */ + lexer = classForName(lexerName); + Class[] lexArgTypes = new Class[]{CharStream.class}; // assign type to lexer's args + Constructor lexConstructor = lexer.getConstructor(lexArgTypes); + Object[] lexArgs = new Object[]{input}; // assign value to lexer's args + Object lexObj = lexConstructor.newInstance(lexArgs); // makes new instance of lexer + + CommonTokenStream tokens = new CommonTokenStream((Lexer) lexObj); + + parser = classForName(parserName); + Class[] parArgTypes = new Class[]{TokenStream.class}; // assign type to parser's args + Constructor parConstructor = parser.getConstructor(parArgTypes); + Object[] parArgs = new Object[]{tokens}; // assign value to parser's args + Object parObj = parConstructor.newInstance(parArgs); // makes new instance of parser + + Method ruleName = parser.getMethod(testRuleName); + + /** Start of I/O Redirecting */ + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream(); + ps = new PrintStream(out); + ps2 = new PrintStream(err); + System.setOut(ps); + System.setErr(ps2); + /** End of redirecting */ + + /** Invoke grammar rule, and store if there is a return value */ + Object ruleReturn = ruleName.invoke(parObj); + String astString = null; + String stString = null; + /** If rule has return value, determine if it contains an AST or a ST */ + if ( ruleReturn!=null ) { + if ( ruleReturn.getClass().toString().indexOf(testRuleName+"_return")>0 ) { + try { // NullPointerException may happen here... + Class _return = classForName(parserName+"$"+testRuleName+"_return"); + Method[] methods = _return.getDeclaredMethods(); + for(Method method : methods) { + if ( method.getName().equals("getTree") ) { + Method returnName = _return.getMethod("getTree"); + CommonTree tree = (CommonTree) returnName.invoke(ruleReturn); + astString = tree.toStringTree(); + } + else if ( method.getName().equals("getTemplate") ) { + Method returnName = _return.getMethod("getTemplate"); + StringTemplate st = (StringTemplate) returnName.invoke(ruleReturn); + stString = st.toString(); + } + } + } + catch(Exception e) { + System.err.println(e); // Note: If any exception occurs, the test is viewed as failed. + } + } + } + + /** Invalid input */ + if ( tokens.index()!=tokens.size() ) { + //throw new InvalidInputException(); + ps2.print("Invalid input"); + } + + if ( err.toString().length()>0 ) { + gUnitTestResult testResult = new gUnitTestResult(false, err.toString()); + testResult.setError(err.toString()); + return testResult; + } + String stdout = null; + // TODO: need to deal with the case which has both ST return value and stdout + if ( out.toString().length()>0 ) { + stdout = out.toString(); + } + if ( astString!=null ) { // Return toStringTree of AST + return new gUnitTestResult(true, stdout, astString); + } + else if ( stString!=null ) {// Return toString of ST + return new gUnitTestResult(true, stdout, stString); + } + + if ( ruleReturn!=null ) { + // TODO: currently only works for a single return with int or String value + return new gUnitTestResult(true, stdout, String.valueOf(ruleReturn)); + } + return new gUnitTestResult(true, stdout, stdout); + } catch (IOException e) { + return getTestExceptionResult(e); + } catch (ClassNotFoundException e) { + e.printStackTrace(); System.exit(1); + } catch (SecurityException e) { + e.printStackTrace(); System.exit(1); + } catch (NoSuchMethodException e) { + e.printStackTrace(); System.exit(1); + } catch (IllegalArgumentException e) { + e.printStackTrace(); System.exit(1); + } catch (InstantiationException e) { + e.printStackTrace(); System.exit(1); + } catch (IllegalAccessException e) { + e.printStackTrace(); System.exit(1); + } catch (InvocationTargetException e) { // This exception could be caused from ANTLR Runtime Exception, e.g. MismatchedTokenException + return getTestExceptionResult(e); + } finally { + try { + if ( ps!=null ) ps.close(); + if ( ps2!=null ) ps2.close(); + System.setOut(console); // Reset standard output + System.setErr(consoleErr); // Reset standard err out + } catch (Exception e) { + e.printStackTrace(); + } + } + // TODO: verify this: + throw new Exception("This should be unreachable?"); + } + + protected gUnitTestResult runTreeParser(String parserName, String lexerName, String testRuleName, String testTreeRuleName, gUnitTestInput testInput) throws Exception { + CharStream input; + String treeParserPath; + Class lexer = null; + Class parser = null; + Class treeParser = null; + PrintStream ps = null; // for redirecting stdout later + PrintStream ps2 = null; // for redirecting stderr later + try { + /** Set up ANTLR input stream based on input source, file or String */ + input = getANTLRInputStream(testInput); + + /** Set up appropriate path for tree parser if using package */ + if ( grammarInfo.getHeader()!=null ) { + treeParserPath = grammarInfo.getHeader()+"."+grammarInfo.getTreeGrammarName(); + } + else { + treeParserPath = grammarInfo.getTreeGrammarName(); + } + + /** Use Reflection to create instances of lexer and parser */ + lexer = classForName(lexerName); + Class[] lexArgTypes = new Class[]{CharStream.class}; // assign type to lexer's args + Constructor lexConstructor = lexer.getConstructor(lexArgTypes); + Object[] lexArgs = new Object[]{input}; // assign value to lexer's args + Object lexObj = lexConstructor.newInstance(lexArgs); // makes new instance of lexer + + CommonTokenStream tokens = new CommonTokenStream((Lexer) lexObj); + + parser = classForName(parserName); + Class[] parArgTypes = new Class[]{TokenStream.class}; // assign type to parser's args + Constructor parConstructor = parser.getConstructor(parArgTypes); + Object[] parArgs = new Object[]{tokens}; // assign value to parser's args + Object parObj = parConstructor.newInstance(parArgs); // makes new instance of parser + + Method ruleName = parser.getMethod(testRuleName); + + /** Start of I/O Redirecting */ + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream(); + ps = new PrintStream(out); + ps2 = new PrintStream(err); + System.setOut(ps); + System.setErr(ps2); + /** End of redirecting */ + + /** Invoke grammar rule, and get the return value */ + Object ruleReturn = ruleName.invoke(parObj); + + Class _return = classForName(parserName+"$"+testRuleName+"_return"); + Method returnName = _return.getMethod("getTree"); + CommonTree tree = (CommonTree) returnName.invoke(ruleReturn); + + // Walk resulting tree; create tree nodes stream first + CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree); + // AST nodes have payload that point into token stream + nodes.setTokenStream(tokens); + // Create a tree walker attached to the nodes stream + treeParser = classForName(treeParserPath); + Class[] treeParArgTypes = new Class[]{TreeNodeStream.class}; // assign type to tree parser's args + Constructor treeParConstructor = treeParser.getConstructor(treeParArgTypes); + Object[] treeParArgs = new Object[]{nodes}; // assign value to tree parser's args + Object treeParObj = treeParConstructor.newInstance(treeParArgs); // makes new instance of tree parser + // Invoke the tree rule, and store the return value if there is + Method treeRuleName = treeParser.getMethod(testTreeRuleName); + Object treeRuleReturn = treeRuleName.invoke(treeParObj); + + String astString = null; + String stString = null; + /** If tree rule has return value, determine if it contains an AST or a ST */ + if ( treeRuleReturn!=null ) { + if ( treeRuleReturn.getClass().toString().indexOf(testTreeRuleName+"_return")>0 ) { + try { // NullPointerException may happen here... + Class _treeReturn = classForName(treeParserPath+"$"+testTreeRuleName+"_return"); + Method[] methods = _treeReturn.getDeclaredMethods(); + for(Method method : methods) { + if ( method.getName().equals("getTree") ) { + Method treeReturnName = _treeReturn.getMethod("getTree"); + CommonTree returnTree = (CommonTree) treeReturnName.invoke(treeRuleReturn); + astString = returnTree.toStringTree(); + } + else if ( method.getName().equals("getTemplate") ) { + Method treeReturnName = _return.getMethod("getTemplate"); + StringTemplate st = (StringTemplate) treeReturnName.invoke(treeRuleReturn); + stString = st.toString(); + } + } + } + catch(Exception e) { + System.err.println(e); // Note: If any exception occurs, the test is viewed as failed. + } + } + } + + /** Invalid input */ + if ( tokens.index()!=tokens.size() ) { + //throw new InvalidInputException(); + ps2.print("Invalid input"); + } + + if ( err.toString().length()>0 ) { + gUnitTestResult testResult = new gUnitTestResult(false, err.toString()); + testResult.setError(err.toString()); + return testResult; + } + + String stdout = null; + // TODO: need to deal with the case which has both ST return value and stdout + if ( out.toString().length()>0 ) { + stdout = out.toString(); + } + if ( astString!=null ) { // Return toStringTree of AST + return new gUnitTestResult(true, stdout, astString); + } + else if ( stString!=null ) {// Return toString of ST + return new gUnitTestResult(true, stdout, stString); + } + + if ( treeRuleReturn!=null ) { + // TODO: again, currently only works for a single return with int or String value + return new gUnitTestResult(true, stdout, String.valueOf(treeRuleReturn)); + } + return new gUnitTestResult(true, stdout, stdout); + } catch (IOException e) { + return getTestExceptionResult(e); + } catch (ClassNotFoundException e) { + e.printStackTrace(); System.exit(1); + } catch (SecurityException e) { + e.printStackTrace(); System.exit(1); + } catch (NoSuchMethodException e) { + e.printStackTrace(); System.exit(1); + } catch (IllegalArgumentException e) { + e.printStackTrace(); System.exit(1); + } catch (InstantiationException e) { + e.printStackTrace(); System.exit(1); + } catch (IllegalAccessException e) { + e.printStackTrace(); System.exit(1); + } catch (InvocationTargetException e) { // note: This exception could be caused from ANTLR Runtime Exception... + return getTestExceptionResult(e); + } finally { + try { + if ( ps!=null ) ps.close(); + if ( ps2!=null ) ps2.close(); + System.setOut(console); // Reset standard output + System.setErr(consoleErr); // Reset standard err out + } catch (Exception e) { + e.printStackTrace(); + } + } + // TODO: verify this: + throw new Exception("Should not be reachable?"); + } + + // Create ANTLR input stream based on input source, file or String + private CharStream getANTLRInputStream(gUnitTestInput testInput) throws IOException { + CharStream input; + if ( testInput.inputIsFile ) { + String filePath = testInput.testInput; + File testInputFile = new File(filePath); + // if input test file is not found under the current dir, try to look for it from dir where the testsuite file locates + if ( !testInputFile.exists() ) { + testInputFile = new File(this.testsuiteDir, filePath); + if ( testInputFile.exists() ) filePath = testInputFile.getCanonicalPath(); + // if still not found, also try to look for it under the package dir + else if ( grammarInfo.getHeader()!=null ) { + testInputFile = new File("."+File.separator+grammarInfo.getHeader().replace(".", File.separator), filePath); + if ( testInputFile.exists() ) filePath = testInputFile.getCanonicalPath(); + } + } + input = new ANTLRFileStream(filePath); + } + else { + input = new ANTLRStringStream(testInput.testInput); + } + return input; + } + + // set up the cause of exception or the exception name into a gUnitTestResult instance + private gUnitTestResult getTestExceptionResult(Exception e) { + gUnitTestResult testResult; + if ( e.getCause()!=null ) { + testResult = new gUnitTestResult(false, e.getCause().toString(), true); + testResult.setError(e.getCause().toString()); + } + else { + testResult = new gUnitTestResult(false, e.toString(), true); + testResult.setError(e.toString()); + } + return testResult; + } + + + public void onPass(ITestCase passTest) { + + } + + public void onFail(ITestCase failTest) { + + } + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/gUnitTestInput.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/gUnitTestInput.java new file mode 100644 index 0000000..4e3519f --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/gUnitTestInput.java @@ -0,0 +1,45 @@ +/* + [The "BSD licence"] + Copyright (c) 2007-2008 Leon, Jen-Yuan Su + 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. +*/ +package org.antlr.gunit; + +/** A class which contains input information of an individual testuite */ +public class gUnitTestInput { + protected String testInput; // a test input string for a testsuite + + protected boolean inputIsFile; // if true, the testInput represents a filename + + protected int line; // number of line in the script + + public gUnitTestInput(String testInput, boolean inputIsFile, int line) { + this.testInput = testInput; + this.inputIsFile = inputIsFile; + this.line = line; + } + + public int getLine() { return this.line; } +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/gUnitTestResult.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/gUnitTestResult.java new file mode 100644 index 0000000..f8263b6 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/gUnitTestResult.java @@ -0,0 +1,77 @@ +/* + [The "BSD license"] + Copyright (c) 2007 Kenny MacDermid + 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. +*/ +package org.antlr.gunit; + +public class gUnitTestResult { + + private boolean success; + private String output; // stdout + private String error; // stderr + private String returned; // AST (toStringTree) or ST (toString) + private boolean isLexerTest; + + public gUnitTestResult(boolean success, String output) { + this.success = success; + this.output = output; + } + + public gUnitTestResult(boolean success, String output, boolean isLexerTest) { + this(success, output); + this.isLexerTest = isLexerTest; + } + + public gUnitTestResult(boolean success, String output, String returned) { + this(success, output); + this.returned = returned; + } + + public boolean isSuccess() { + return success; + } + + public String getOutput() { + return output; + } + + public String getError() { + return error; + } + + public String getReturned() { + return returned; + } + + public boolean isLexerTest() { + return isLexerTest; + } + + public void setError(String error) { + this.error = error; + } + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/gUnitTestSuite.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/gUnitTestSuite.java new file mode 100644 index 0000000..d4e7f19 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/gUnitTestSuite.java @@ -0,0 +1,76 @@ +/* + [The "BSD licence"] + Copyright (c) 2007-2008 Leon Jen-Yuan Su + 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. +*/ +package org.antlr.gunit; + +/** A class which wraps all testsuites for an individual rule */ +import java.util.Map; +import java.util.LinkedHashMap; + +public class gUnitTestSuite { + protected String rule = null; // paeser rule name for unit testing + protected String lexicalRule = null; // lexical rule name + protected String treeRule = null; // optional, required for testing tree grammar rule + protected boolean isLexicalRule = false; + + /** A map which stores input/output pairs (individual testsuites). + * In other words, it maps input data for unit test (gUnitTestInput object) + * to an expected output (Token object). + */ + protected Map testSuites = new LinkedHashMap(); + + public gUnitTestSuite() { + ; + } + + public gUnitTestSuite(String rule) { + this.rule = rule; + } + + public gUnitTestSuite(String treeRule, String rule) { + this.rule = rule; + this.treeRule = treeRule; + } + + public void setRuleName(String ruleName) { this.rule = ruleName; } + public void setLexicalRuleName(String lexicalRule) { this.lexicalRule = lexicalRule; this.isLexicalRule = true; } + public void setTreeRuleName(String treeRuleName) { this.treeRule = treeRuleName; } + + public String getRuleName() { return this.rule; } + public String getLexicalRuleName() { return this.lexicalRule; } + public String getTreeRuleName() { return this.treeRule; } + public boolean isLexicalRule() { return this.isLexicalRule; } + + public void addTestCase(gUnitTestInput input, AbstractTest expect) { + if ( input!=null && expect!=null ) { + expect.setTestedRuleName(this.rule); + expect.setTestCaseIndex(this.testSuites.size()); + this.testSuites.put(input, expect); + } + } + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/AbstractInputEditor.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/AbstractInputEditor.java new file mode 100644 index 0000000..ddbd47c --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/AbstractInputEditor.java @@ -0,0 +1,28 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.antlr.gunit.swingui; + +import org.antlr.gunit.swingui.model.ITestCaseInput; +import java.awt.event.ActionListener; +import javax.swing.JComponent; + +/** + * + * @author scai + */ +public abstract class AbstractInputEditor { + + protected ITestCaseInput input; + public void setInput(ITestCaseInput input) { + this.input = input; + } + + protected JComponent comp; + public JComponent getControl() { return comp; } + + abstract public void addActionListener(ActionListener l) ; + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/AwAdapter.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/AwAdapter.java new file mode 100644 index 0000000..b600e44 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/AwAdapter.java @@ -0,0 +1,30 @@ +package org.antlr.gunit.swingui; + +import javax.swing.JComponent; +import javax.swing.JSplitPane; +import org.antlr.gunit.swingui.model.TestSuite; + +public class AwAdapter { + + private JSplitPane splitMain ; + private RunnerController runner; + private TestCaseEditController editor; + private TestSuite testSuite; + + public AwAdapter() { + initComponents(); + testSuite = new TestSuite(); + } + + public JComponent getView() { + return splitMain; + } + + private void initComponents() { + runner = new RunnerController(); + editor = new TestCaseEditController(); + + splitMain = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, + editor.getView(), runner.getView()); + } +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/IController.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/IController.java new file mode 100644 index 0000000..ceda3d0 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/IController.java @@ -0,0 +1,8 @@ +package org.antlr.gunit.swingui; + +import java.awt.Component; + +public interface IController { + public Object getModel() ; + public Component getView() ; +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/RuleListController.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/RuleListController.java new file mode 100644 index 0000000..b14e7c4 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/RuleListController.java @@ -0,0 +1,133 @@ + +package org.antlr.gunit.swingui; + +import javax.swing.event.ListDataListener; +import org.antlr.gunit.swingui.model.Rule; +import org.antlr.gunit.swingui.images.ImageFactory; +import java.awt.Color; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.List; +import javax.swing.BorderFactory; +import javax.swing.DefaultListModel; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JScrollPane; +import javax.swing.ListCellRenderer; +import javax.swing.ListModel; +import javax.swing.ListSelectionModel; +import javax.swing.event.ListSelectionListener; +import org.antlr.gunit.swingui.model.TestSuite; + +public class RuleListController implements IController { + + /* Sub-controls */ + private final JList list = new JList(); + private final JScrollPane scroll = new JScrollPane( list, + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, + JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + + /* Model */ + private ListModel model = null; + private TestSuite testSuite = null; + + public RuleListController() { + this.initComponents(); + } + + public JScrollPane getView() { + return scroll; + } + + private void setTestSuite(TestSuite newTestSuite) { + testSuite = newTestSuite; + model = new RuleListModel(); + list.setModel(model); + } + + public void initialize(TestSuite ts) { + setTestSuite(ts); + if(model.getSize() > 0) list.setSelectedIndex(0); + list.updateUI(); + } + + + /** + * Initialize view. + */ + private void initComponents() { + + scroll.setViewportBorder(BorderFactory.createEtchedBorder()); + scroll.setBorder(BorderFactory.createTitledBorder( + BorderFactory.createEmptyBorder(), "Rules")); + scroll.setOpaque(false); + + list.setOpaque(false); + list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); + list.setLayoutOrientation(JList.VERTICAL); + list.setCellRenderer(new RuleListItemRenderer()); + } + + public void setListSelectionListener(ListSelectionListener l) { + this.list.addListSelectionListener(l); + } + + public Object getModel() { + return model; + } + + + /* ITEM RENDERER */ + + private class RuleListItemRenderer extends JLabel implements ListCellRenderer{ + + public RuleListItemRenderer() { + this.setPreferredSize(new Dimension(50, 18)); + } + + public Component getListCellRendererComponent( + JList list, Object value, int index, + boolean isSelected, boolean hasFocus) { + + if(value instanceof Rule) { + final Rule item = (Rule) value; + setText(item.toString()); + setForeground(list.getForeground()); + + setIcon(item.getNotEmpty() ? ImageFactory.FAV16 : null); + + if(list.getSelectedValue() == item ) { + setBackground(Color.LIGHT_GRAY); + setOpaque(true); + } else { + setOpaque(false); + } + + } else { + this.setText("Error!"); + } + return this; + } + } + + private class RuleListModel implements ListModel { + + public RuleListModel() { + if(testSuite == null) + throw new NullPointerException("Null test suite"); + } + + public int getSize() { + return testSuite.getRuleCount(); + } + + public Object getElementAt(int index) { + return testSuite.getRule(index); + } + + public void addListDataListener(ListDataListener l) {} + public void removeListDataListener(ListDataListener l) {} + } +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/RunnerController.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/RunnerController.java new file mode 100644 index 0000000..5177970 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/RunnerController.java @@ -0,0 +1,209 @@ +package org.antlr.gunit.swingui; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Component; +import java.awt.Dimension; +import javax.swing.BorderFactory; +import javax.swing.BoxLayout; +import javax.swing.JLabel; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; +import javax.swing.JTree; +import javax.swing.event.TreeModelListener; +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.DefaultTreeModel; +import javax.swing.tree.TreeCellRenderer; +import javax.swing.tree.TreeModel; +import javax.swing.tree.TreePath; +import org.antlr.gunit.swingui.images.ImageFactory; +import org.antlr.gunit.swingui.model.*; + +/** + * + * @author scai + */ +public class RunnerController implements IController { + + /* MODEL */ + //private TestSuite testSuite; + + /* VIEW */ + private RunnerView view = new RunnerView(); + public class RunnerView extends JPanel { + + private JTextArea textArea = new JTextArea(); + + private JTree tree = new JTree(); + + private JScrollPane scroll = new JScrollPane(tree, + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, + JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + + public void initComponents() { + //textArea.setOpaque(false); + tree.setOpaque(false); + scroll.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY)); + scroll.setOpaque(false); + this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + this.add(scroll); + this.setBorder(BorderFactory.createEmptyBorder()); + this.setOpaque(false); + } + + }; + + public RunnerController() { + } + + public Object getModel() { + return null; + } + + public Component getView() { + return view; + } + + public void update() { + view.initComponents(); + } + + public void OnShowSuiteResult(TestSuite suite) { + update(); + view.tree.setModel(new RunnerTreeModel(suite)); + view.tree.setCellRenderer(new RunnerTreeRenderer()); + } + + public void OnShowRuleResult(Rule rule) { + update(); + + + + /* + StringBuffer result = new StringBuffer(); + + result.append("Testing result for rule: " + rule.getName()); + result.append("\n--------------------\n\n"); + + for(TestCase testCase: rule.getTestCases()){ + result.append(testCase.isPass() ? "PASS" : "FAIL"); + result.append("\n"); + } + result.append("\n--------------------\n"); + view.textArea.setText(result.toString()); + */ + } + + + + private class TestSuiteTreeNode extends DefaultMutableTreeNode { + + private TestSuite data ; + + public TestSuiteTreeNode(TestSuite suite) { + super(suite.getGrammarName()); + for(int i=0; i 0; + + return String.format("%s (pass %d, fail %d)", + data.getName(), iPass, iFail); + } + } ; + + private class TestCaseTreeNode extends DefaultMutableTreeNode { + + private TestCase data; + + private TestCaseTreeNode(TestCase tc) { + super(tc.toString()); + data = tc; + } + } ; + + private class RunnerTreeModel extends DefaultTreeModel { + + public RunnerTreeModel(TestSuite testSuite) { + super(new TestSuiteTreeNode(testSuite)); + } + } + + private class RunnerTreeRenderer implements TreeCellRenderer { + + public Component getTreeCellRendererComponent(JTree tree, Object value, + boolean selected, boolean expanded, boolean leaf, int row, + boolean hasFocus) { + + JLabel label = new JLabel(); + + if(value instanceof TestSuiteTreeNode) { + + label.setText(value.toString()); + label.setIcon(ImageFactory.TESTSUITE); + + } else if(value instanceof TestGroupTreeNode) { + + TestGroupTreeNode node = (TestGroupTreeNode) value; + label.setText(value.toString()); + label.setIcon( node.hasFail ? ImageFactory.TESTGROUPX : ImageFactory.TESTGROUP); + + } else if(value instanceof TestCaseTreeNode) { + + TestCaseTreeNode node = (TestCaseTreeNode) value; + label.setIcon( (node.data.isPass())? + ImageFactory.RUN_PASS : ImageFactory.RUN_FAIL); + label.setText(value.toString()); + + } else { + throw new IllegalArgumentException( + "Invalide tree node type + " + value.getClass().getName()); + } + + return label; + + } + + } + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/StatusBarController.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/StatusBarController.java new file mode 100644 index 0000000..8f5c92f --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/StatusBarController.java @@ -0,0 +1,66 @@ +package org.antlr.gunit.swingui; + +import java.awt.Dimension; +import java.awt.FlowLayout; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JProgressBar; + +public class StatusBarController implements IController { + + private final JPanel panel = new JPanel(); + + private final JLabel labelText = new JLabel("Ready"); + private final JLabel labelRuleName = new JLabel(""); + private final JProgressBar progress = new JProgressBar(); + + public StatusBarController() { + initComponents(); + } + + private void initComponents() { + labelText.setPreferredSize(new Dimension(300, 20)); + labelText.setHorizontalTextPosition(JLabel.LEFT); + progress.setPreferredSize(new Dimension(100, 15)); + + final JLabel labRuleHint = new JLabel("Rule: "); + + FlowLayout layout = new FlowLayout(); + layout.setAlignment(FlowLayout.LEFT); + panel.setLayout(layout); + panel.add(labelText); + panel.add(progress); + panel.add(labRuleHint); + panel.add(labelRuleName); + panel.setOpaque(false); + panel.setBorder(javax.swing.BorderFactory.createEmptyBorder()); + + } + + public void setText(String text) { + labelText.setText(text); + } + + public void setRule(String name) { + this.labelRuleName.setText(name); + } + + public Object getModel() { + throw new UnsupportedOperationException("Not supported yet."); + } + + public JPanel getView() { + return panel; + } + + public void setProgressIndetermined(boolean value) { + this.progress.setIndeterminate(value); + } + + public void setProgress(int value) { + this.progress.setIndeterminate(false); + this.progress.setValue(value); + } + + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/TestCaseEditController.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/TestCaseEditController.java new file mode 100644 index 0000000..8a0162f --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/TestCaseEditController.java @@ -0,0 +1,605 @@ + +package org.antlr.gunit.swingui; + +import org.antlr.gunit.swingui.model.*; +import org.antlr.gunit.swingui.images.ImageFactory; +import java.awt.*; +import java.awt.event.*; +import java.util.HashMap; +import javax.swing.*; +import javax.swing.event.*; + +/** + * + * @author scai + */ +public class TestCaseEditController implements IController { + + private JPanel view = new JPanel(); + + private JScrollPane scroll; + private JPanel paneDetail; + private AbstractEditorPane paneDetailInput, paneDetailOutput; + private JToolBar toolbar; + private JList listCases; + private ListModel listModel ; + + public ActionListener onTestCaseNumberChange; + + /* EDITORS */ + private InputFileEditor editInputFile; + private InputStringEditor editInputString; + private InputMultiEditor editInputMulti; + private OutputResultEditor editOutputResult; + private OutputAstEditor editOutputAST; + private OutputStdEditor editOutputStd; + private OutputReturnEditor editOutputReturn; + + private JComboBox comboInputType, comboOutputType; + + /* TYPE NAME */ + private static final String IN_TYPE_STRING = "Single-line Text"; + private static final String IN_TYPE_MULTI = "Multi-line Text"; + private static final String IN_TYPE_FILE = "Disk File"; + private static final String OUT_TYPE_BOOL = "OK or Fail"; + private static final String OUT_TYPE_AST = "AST"; + private static final String OUT_TYPE_STD = "Standard Output"; + private static final String OUT_TYPE_RET = "Return Value"; + + private static final String DEFAULT_IN_SCRIPT = ""; + private static final String DEFAULT_OUT_SCRIPT = ""; + + private static final Object[] INPUT_TYPE = { + IN_TYPE_STRING, IN_TYPE_MULTI, IN_TYPE_FILE + }; + + private static final Object[] OUTPUT_TYPE = { + OUT_TYPE_BOOL, OUT_TYPE_AST, OUT_TYPE_STD, OUT_TYPE_RET + }; + + /* SIZE */ + private static final int TEST_CASE_DETAIL_WIDTH = 300; + private static final int TEST_EDITOR_WIDTH = 280; + private static final int TEST_CASE_DETAIL_HEIGHT = 250; + private static final int TEST_EDITOR_HEIGHT = 120; + + /* MODEL */ + private Rule currentRule = null; + private TestCase currentTestCase = null; + + /* END OF MODEL*/ + + private static final HashMap TypeNameTable; + static { + TypeNameTable = new HashMap (); + TypeNameTable.put(TestCaseInputString.class, IN_TYPE_STRING); + TypeNameTable.put(TestCaseInputMultiString.class, IN_TYPE_MULTI); + TypeNameTable.put(TestCaseInputFile.class, IN_TYPE_FILE); + + TypeNameTable.put(TestCaseOutputResult.class, OUT_TYPE_BOOL); + TypeNameTable.put(TestCaseOutputAST.class, OUT_TYPE_AST); + TypeNameTable.put(TestCaseOutputStdOut.class, OUT_TYPE_STD); + TypeNameTable.put(TestCaseOutputReturn.class, OUT_TYPE_RET); + } + + //private WorkSpaceView owner; + + public TestCaseEditController(WorkSpaceView workspace) { + //this.owner = workspace; + initComponents(); + } + + public TestCaseEditController() { + initComponents(); + } + + public void OnLoadRule(Rule rule) { + if(rule == null) throw new IllegalArgumentException("Null"); + this.currentRule = rule; + this.currentTestCase = null; + this.listModel = rule; + this.listCases.setModel(this.listModel); + } + + public void setCurrentTestCase(TestCase testCase) { + if(testCase == null) throw new IllegalArgumentException("Null"); + this.listCases.setSelectedValue(testCase, true); + this.currentTestCase = testCase; + } + + public Rule getCurrentRule() { + return this.currentRule; + } + + private void initComponents() { + + /* CASE LIST */ + listCases = new JList(); + listCases.addListSelectionListener(new TestCaseListSelectionListener()); + listCases.setCellRenderer(listRenderer); + listCases.setOpaque(false); + + scroll = new JScrollPane(listCases); + scroll.setBorder(BorderFactory.createTitledBorder( + BorderFactory.createEmptyBorder(), "Test Cases")); + scroll.setOpaque(false); + scroll.setViewportBorder(BorderFactory.createEtchedBorder()); + + /* CASE DETAIL */ + + editInputString = new InputStringEditor(); + editInputMulti = new InputMultiEditor(); + editInputFile = new InputFileEditor(); + + editOutputResult = new OutputResultEditor(); + editOutputAST = new OutputAstEditor(); + editOutputStd = new OutputStdEditor(); + editOutputReturn = new OutputReturnEditor(); + + paneDetail = new JPanel(); + paneDetail.setBorder(BorderFactory.createEmptyBorder()); + paneDetail.setOpaque(false); + + comboInputType = new JComboBox(INPUT_TYPE); + comboInputType.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent event) { + OnInputTestCaseTypeChanged(comboInputType.getSelectedItem()); + } + }); + comboOutputType = new JComboBox(OUTPUT_TYPE); + comboOutputType.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent event) { + OnOutputTestCaseTypeChanged(comboOutputType.getSelectedItem()); + } + }); + paneDetailInput = new InputEditorPane(comboInputType); + paneDetailOutput = new OutputEditorPane(comboOutputType); + + BoxLayout layout = new BoxLayout(paneDetail, BoxLayout.PAGE_AXIS); + paneDetail.setLayout(layout); + + paneDetail.add(this.paneDetailInput); + paneDetail.add(this.paneDetailOutput); + + /* TOOLBAR */ + toolbar = new JToolBar("Edit TestCases", JToolBar.VERTICAL); + toolbar.setFloatable(false); + toolbar.add(new AddTestCaseAction()); + toolbar.add(new RemoveTestCaseAction()); + + /* COMPOSITE */ + view.setLayout(new BorderLayout()); + view.setBorder(BorderFactory.createEmptyBorder()); + view.setOpaque(false); + view.add(toolbar, BorderLayout.WEST); + view.add(scroll, BorderLayout.CENTER); + view.add(paneDetail, BorderLayout.EAST); + } + + private void updateInputEditor() { + JComponent editor = null; + + if(currentTestCase != null ) { + ITestCaseInput input = this.currentTestCase.getInput(); + if(input instanceof TestCaseInputString) { + this.editInputString.setText(input.getScript()); + editor = this.editInputString; + comboInputType.setSelectedItem(IN_TYPE_STRING); + } else if(input instanceof TestCaseInputMultiString) { + this.editInputMulti.setText(input.getScript()); + editor = this.editInputMulti.getView(); + comboInputType.setSelectedItem(IN_TYPE_MULTI); + } else if(input instanceof TestCaseInputFile) { + this.editInputFile.setText(input.getScript()); + editor = this.editInputFile; + comboInputType.setSelectedItem(IN_TYPE_FILE); + } else { + throw new Error("Wrong type"); + } + } + + paneDetailInput.setEditor(editor); + } + + private void updateOutputEditor() { + JComponent editor = null; + + if(currentTestCase != null) { + + ITestCaseOutput output = this.currentTestCase.getOutput(); + + if(output instanceof TestCaseOutputAST) { + + this.editOutputAST.setText(output.getScript()); + editor = this.editOutputAST.getView(); + comboOutputType.setSelectedItem(OUT_TYPE_AST); + + } else if(output instanceof TestCaseOutputResult) { + + this.editOutputResult.setValue(output.getScript()); + editor = this.editOutputResult; + comboOutputType.setSelectedItem(OUT_TYPE_BOOL); + + } else if(output instanceof TestCaseOutputStdOut) { + + this.editOutputStd.setText(output.getScript()); + editor = this.editOutputStd.getView(); + comboOutputType.setSelectedItem(OUT_TYPE_STD); + + } else if(output instanceof TestCaseOutputReturn) { + + this.editOutputReturn.setText(output.getScript()); + editor = this.editOutputReturn.getView(); + comboOutputType.setSelectedItem(OUT_TYPE_RET); + + } else { + + throw new Error("Wrong type"); + + } + + } + this.paneDetailOutput.setEditor(editor); + } + + private void OnInputTestCaseTypeChanged(Object inputTypeStr) { + if(this.currentTestCase != null) { + ITestCaseInput input ; + if(inputTypeStr == IN_TYPE_STRING) { + input = new TestCaseInputString(DEFAULT_IN_SCRIPT); + } else if(inputTypeStr == IN_TYPE_MULTI) { + input = new TestCaseInputMultiString(DEFAULT_IN_SCRIPT); + } else if(inputTypeStr == IN_TYPE_FILE) { + input = new TestCaseInputFile(DEFAULT_IN_SCRIPT); + } else { + throw new Error("Wrong Type"); + } + + if(input.getClass().equals(this.currentTestCase.getInput().getClass())) + return ; + + this.currentTestCase.setInput(input); + } + this.updateInputEditor(); + } + + private void OnOutputTestCaseTypeChanged(Object outputTypeStr) { + if(this.currentTestCase != null) { + + ITestCaseOutput output ; + if(outputTypeStr == OUT_TYPE_AST) { + output = new TestCaseOutputAST(DEFAULT_OUT_SCRIPT); + } else if(outputTypeStr == OUT_TYPE_BOOL) { + output = new TestCaseOutputResult(false); + } else if(outputTypeStr == OUT_TYPE_STD) { + output = new TestCaseOutputStdOut(DEFAULT_OUT_SCRIPT); + } else if(outputTypeStr == OUT_TYPE_RET) { + output = new TestCaseOutputReturn(DEFAULT_OUT_SCRIPT); + } else { + throw new Error("Wrong Type"); + } + + if(output.getClass().equals(this.currentTestCase.getOutput().getClass())) + return ; + + this.currentTestCase.setOutput(output); + } + this.updateOutputEditor(); + } + + + private void OnTestCaseSelected(TestCase testCase) { + //if(testCase == null) throw new RuntimeException("Null TestCase"); + this.currentTestCase = testCase; + updateInputEditor(); + updateOutputEditor(); + + } + + private void OnAddTestCase() { + if(currentRule == null) return; + + final TestCase newCase = new TestCase( + new TestCaseInputString(""), + new TestCaseOutputResult(true)); + this.currentRule.addTestCase(newCase); + setCurrentTestCase(newCase); + + this.listCases.setSelectedValue(newCase, true); + this.listCases.updateUI(); + this.OnTestCaseSelected(newCase); + this.onTestCaseNumberChange.actionPerformed(null); + } + + private void OnRemoveTestCase() { + if(currentTestCase == null) return; + currentRule.removeElement(currentTestCase); + listCases.updateUI(); + + final TestCase nextActiveCase = listCases.isSelectionEmpty() ? + null : (TestCase) listCases.getSelectedValue() ; + OnTestCaseSelected(nextActiveCase); + this.onTestCaseNumberChange.actionPerformed(null); + } + + public Object getModel() { + return currentRule; + } + + public Component getView() { + return view; + } + + /* EDITOR CONTAINER */ + + abstract public class AbstractEditorPane extends JPanel { + + private JComboBox combo; + private JComponent editor; + private String title; + private JLabel placeHolder = new JLabel(); + + public AbstractEditorPane(JComboBox comboBox, String title) { + this.combo = comboBox; + this.editor = placeHolder; + this.title = title; + this.initComponents(); + } + + private void initComponents() { + placeHolder.setPreferredSize(new Dimension( + TEST_CASE_DETAIL_WIDTH, TEST_CASE_DETAIL_HEIGHT)); + this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + this.add(combo, BorderLayout.NORTH); + this.add(editor, BorderLayout.CENTER); + this.setOpaque(false); + this.setBorder(BorderFactory.createTitledBorder(title)); + this.setPreferredSize(new Dimension( + TEST_CASE_DETAIL_WIDTH, TEST_CASE_DETAIL_HEIGHT)); + } + + public void setEditor(JComponent newEditor) { + if(newEditor == null) newEditor = placeHolder; + this.remove(editor); + this.add(newEditor); + this.editor = newEditor; + this.updateUI(); + } + } + + public class InputEditorPane extends AbstractEditorPane { + public InputEditorPane(JComboBox comboBox) { + super(comboBox, "Input"); + } + } + + public class OutputEditorPane extends AbstractEditorPane { + public OutputEditorPane(JComboBox comboBox) { + super(comboBox, "Output"); + } + } + + /* INPUT EDITORS */ + + public class InputStringEditor extends JTextField implements CaretListener { + public InputStringEditor() { + super(); + + this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY)); + this.addCaretListener(this); + } + + public void caretUpdate(CaretEvent arg0) { + currentTestCase.getInput().setScript(getText()); + listCases.updateUI(); + } + } + + public class InputMultiEditor implements CaretListener { + private JTextArea textArea = new JTextArea(20, 30); + private JScrollPane scroll = new JScrollPane(textArea, + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, + JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); + + public InputMultiEditor() { + super(); + scroll.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY)); + textArea.addCaretListener(this); + } + + public void caretUpdate(CaretEvent arg0) { + currentTestCase.getInput().setScript(getText()); + listCases.updateUI(); + } + + public String getText() { + return textArea.getText(); + } + + public void setText(String text) { + textArea.setText(text); + } + + public JComponent getView() { + return scroll; + } + } + + public class InputFileEditor extends InputStringEditor {}; + + public class OutputResultEditor extends JPanel implements ActionListener { + + private JToggleButton tbFail, tbOk; + + public OutputResultEditor() { + super(); + + tbFail = new JToggleButton("Fail"); + tbOk = new JToggleButton("OK"); + ButtonGroup group = new ButtonGroup(); + group.add(tbFail); + group.add(tbOk); + + this.add(tbFail); + this.add(tbOk); + + this.tbFail.addActionListener(this); + this.tbOk.addActionListener(this); + + this.setPreferredSize( + new Dimension(TEST_EDITOR_WIDTH, 100)); + } + + public void actionPerformed(ActionEvent e) { + TestCaseOutputResult output = + (TestCaseOutputResult) currentTestCase.getOutput(); + + if(e.getSource() == tbFail) { + output.setScript(false); + } else { + output.setScript(true); + } + + listCases.updateUI(); + } + + public void setValue(String value) { + if(TestCaseOutputResult.OK.equals(value)) { + this.tbOk.setSelected(true); + } else { + this.tbFail.setSelected(true); + } + } + } + + + public class OutputAstEditor implements CaretListener { + private JTextArea textArea = new JTextArea(20, 30); + private JScrollPane scroll = new JScrollPane(textArea, + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, + JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); + + public OutputAstEditor() { + super(); + scroll.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY)); + textArea.addCaretListener(this); + } + + public void caretUpdate(CaretEvent arg0) { + currentTestCase.getOutput().setScript(getText()); + listCases.updateUI(); + } + + public void setText(String text) { + this.textArea.setText(text); + } + + public String getText() { + return this.textArea.getText(); + } + + public JScrollPane getView() { + return this.scroll; + } + } + + + public class OutputStdEditor extends OutputAstEditor {} + public class OutputReturnEditor extends OutputAstEditor {} + + /* EVENT HANDLERS */ + + private class TestCaseListSelectionListener implements ListSelectionListener { + + public void valueChanged(ListSelectionEvent e) { + + if(e.getValueIsAdjusting()) return; + final JList list = (JList) e.getSource(); + final TestCase value = (TestCase) list.getSelectedValue(); + if(value != null) OnTestCaseSelected(value); + + } + + } + + /* ACTIONS */ + + private class AddTestCaseAction extends AbstractAction { + public AddTestCaseAction() { + super("Add", ImageFactory.ADD); + putValue(SHORT_DESCRIPTION, "Add a gUnit test case."); + } + public void actionPerformed(ActionEvent e) { + OnAddTestCase(); + } + } + + private class RemoveTestCaseAction extends AbstractAction { + public RemoveTestCaseAction() { + super("Remove", ImageFactory.DELETE); + putValue(SHORT_DESCRIPTION, "Remove a gUnit test case."); + } + public void actionPerformed(ActionEvent e) { + OnRemoveTestCase(); + } + } + + /* CELL RENDERERS */ + + private static TestCaseListRenderer listRenderer + = new TestCaseListRenderer(); + + private static class TestCaseListRenderer implements ListCellRenderer { + + private static Font IN_FONT = new Font("mono", Font.PLAIN, 12); + private static Font OUT_FONT = new Font("default", Font.BOLD, 12); + + public static String clamp(String text, int len) { + if(text.length() > len) { + return text.substring(0, len - 3).concat("..."); + } else { + return text; + } + } + + public static String clampAtNewLine(String text) { + int pos = text.indexOf('\n'); + if(pos >= 0) { + return text.substring(0, pos).concat("..."); + } else { + return text; + } + } + + public Component getListCellRendererComponent( + JList list, Object value, int index, + boolean isSelected, boolean hasFocus) { + + final JPanel pane = new JPanel(); + + if (value instanceof TestCase) { + final TestCase item = (TestCase) value; + + // create components + final JLabel labIn = new JLabel( + clamp(clampAtNewLine(item.getInput().getScript()), 18)); + final JLabel labOut = new JLabel( + clamp(clampAtNewLine(item.getOutput().getScript()), 18)); + labOut.setFont(OUT_FONT); + labIn.setFont(IN_FONT); + + labIn.setIcon(item.getInput() instanceof TestCaseInputFile ? + ImageFactory.FILE16 : ImageFactory.EDIT16); + + pane.setBorder(BorderFactory.createEtchedBorder()); + pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); + pane.add(labIn); + pane.add(labOut); + pane.setBackground(isSelected ? Color.LIGHT_GRAY : Color.WHITE); + } + + return pane; + } + } + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/Tool.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/Tool.java new file mode 100644 index 0000000..288b6de --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/Tool.java @@ -0,0 +1,27 @@ +package org.antlr.gunit.swingui; + +import java.io.IOException; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; + +public class Tool { + + public static void main(String[] args) throws IOException { + try { + UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); + } + catch (Exception e) { + } + + + SwingUtilities.invokeLater(new Runnable() { + public void run() { + WorkSpaceController control = new WorkSpaceController(); + control.show(); + } + }); + } + + + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/Translator.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/Translator.java new file mode 100644 index 0000000..e24e48a --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/Translator.java @@ -0,0 +1,171 @@ +package org.antlr.gunit.swingui; + +import org.antlr.gunit.swingui.parsers.StGUnitLexer; +import org.antlr.gunit.swingui.parsers.StGUnitParser; +import org.antlr.gunit.swingui.parsers.ANTLRv3Parser; +import org.antlr.gunit.swingui.parsers.ANTLRv3Lexer; +import java.io.*; +import java.util.ArrayList; +import java.util.List; +import org.antlr.gunit.swingui.model.*; +import org.antlr.runtime.*; +import org.antlr.stringtemplate.*; + +public class Translator { + + private static String TEMPLATE_FILE = "gunit.stg"; + private static StringTemplateGroup templates; + + static { + InputStream in = Translator.class.getResourceAsStream(TEMPLATE_FILE); + Reader rd = new InputStreamReader(in); + templates = new StringTemplateGroup(rd); + } + + + /* From program model to text. */ + public static String toScript(TestSuite testSuite) { + if(testSuite == null) return ""; + StringTemplate gUnitScript = templates.getInstanceOf("gUnitFile"); + gUnitScript.setAttribute("testSuite", testSuite); + return gUnitScript.toString(); + + } + + /* From textual script to program model. */ + public static TestSuite toTestSuite(File file, List ruleList) { + final TestSuite result = new TestSuite(); + try { + final Reader reader = new BufferedReader( + new FileReader(file)); + final StGUnitLexer lexer = new StGUnitLexer( + new ANTLRReaderStream(reader)); + final CommonTokenStream tokens = new CommonTokenStream(lexer); + final StGUnitParser parser = new StGUnitParser(tokens); + final TestSuiteAdapter adapter = new TestSuiteAdapter(result); + parser.adapter = adapter; + parser.gUnitDef(); + result.setTokens(tokens); + reader.close(); + + // if the tested grammar exists in the save directory, load rules. + final String sGrammarFile = file.getParentFile().getAbsolutePath() + + File.separator + result.getGrammarName() + ".g"; + final File fileGrammar = new File(sGrammarFile); + if(fileGrammar.exists() && fileGrammar.isFile()) { + //System.out.println("Found tested grammar file."); + List completeRuleList = loadRulesFromGrammar(fileGrammar); + //System.out.println(String.format("%d / %d", result.getRuleCount(), completeRuleList.size())); + for(Rule rule: completeRuleList) { + if(!result.hasRule(rule)) { + result.addRule(rule); + //System.out.println("Add rule:" + rule); + } + } + } else { + //System.out.println("Tested grammar not found." + sGrammarFile); + } + } catch (IOException ex) { + ex.printStackTrace(); + } finally { + return result; + } + } + + + /* Load rules from an ANTLR grammar file. */ + public static List loadRulesFromGrammar(File grammarFile) { + final List ruleNames = new ArrayList(); + try { + final Reader reader = new BufferedReader( + new FileReader(grammarFile)); + final ANTLRv3Lexer lexer = new ANTLRv3Lexer( + new ANTLRReaderStream(reader)); + CommonTokenStream tokens = new CommonTokenStream(lexer); + final ANTLRv3Parser parser = new ANTLRv3Parser(tokens); + parser.rules = ruleNames; + parser.grammarDef(); + reader.close(); + } catch (Exception e) { + e.printStackTrace(); + } + + final List ruleList = new ArrayList(); + for(String str: ruleNames) { + ruleList.add(new Rule(str)); + } + + return ruleList; + } + + + public static class TestSuiteAdapter { + private TestSuite model ; + private Rule currentRule; + + public TestSuiteAdapter(TestSuite testSuite) { + model = testSuite; + } + + public void setGrammarName(String name) { + model.setGrammarName(name); + } + + public void startRule(String name) { + currentRule = new Rule(name); + } + + public void endRule() { + model.addRule(currentRule); + currentRule = null; + } + + public void addTestCase(ITestCaseInput in, ITestCaseOutput out) { + TestCase testCase = new TestCase(in, out); + currentRule.addTestCase(testCase); + } + + private static String trimChars(String text, int numOfChars) { + return text.substring(numOfChars, text.length() - numOfChars); + } + + public static ITestCaseInput createFileInput(String fileName) { + if(fileName == null) throw new IllegalArgumentException("null"); + return new TestCaseInputFile(fileName); + } + + public static ITestCaseInput createStringInput(String line) { + if(line == null) throw new IllegalArgumentException("null"); + // trim double quotes + return new TestCaseInputString(trimChars(line, 1)); + } + + public static ITestCaseInput createMultiInput(String text) { + if(text == null) throw new IllegalArgumentException("null"); + // trim << and >> + return new TestCaseInputMultiString(trimChars(text, 2)); + } + + public static ITestCaseOutput createBoolOutput(boolean bool) { + return new TestCaseOutputResult(bool); + } + + public static ITestCaseOutput createAstOutput(String ast) { + if(ast == null) throw new IllegalArgumentException("null"); + return new TestCaseOutputAST(ast); + } + + public static ITestCaseOutput createStdOutput(String text) { + if(text == null) throw new IllegalArgumentException("null"); + // trim double quotes + return new TestCaseOutputStdOut(trimChars(text, 1)); + } + + public static ITestCaseOutput createReturnOutput(String text) { + if(text == null) throw new IllegalArgumentException("null"); + // trim square brackets + return new TestCaseOutputReturn(trimChars(text, 1)); + } + } + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/WorkSpaceController.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/WorkSpaceController.java new file mode 100644 index 0000000..d9cf7cd --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/WorkSpaceController.java @@ -0,0 +1,274 @@ + +package org.antlr.gunit.swingui; + +import org.antlr.gunit.swingui.runner.gUnitAdapter; +import java.awt.*; +import java.io.IOException; +import org.antlr.gunit.swingui.model.*; +import org.antlr.gunit.swingui.images.ImageFactory; +import java.awt.event.*; +import java.io.File; +import java.io.FileWriter; +import java.util.ArrayList; +import javax.swing.*; +import javax.swing.event.*; + +/** + * + * @author scai + */ +public class WorkSpaceController implements IController{ + + /* MODEL */ + private TestSuite currentTestSuite; + private gUnitAdapter adapter = new gUnitAdapter(); + private String testSuiteFileName = null; // path + file + + /* VIEW */ + private final WorkSpaceView view = new WorkSpaceView(); + + /* SUB-CONTROL */ + private final RunnerController runner = new RunnerController(); + + public WorkSpaceController() { + view.resultPane = (JPanel) runner.getView(); + view.initComponents(); + this.initEventHandlers(); + this.initToolbar(); + } + + public void show() { + this.view.setTitle("gUnitEditor"); + this.view.setVisible(true); + this.view.pack(); + } + + public Component getEmbeddedView() { + return view.paneEditor.getView(); + } + + private void initEventHandlers() { + this.view.tabEditors.addChangeListener(new TabChangeListener()); + this.view.listRules.setListSelectionListener(new RuleListSelectionListener()); + this.view.paneEditor.onTestCaseNumberChange = new ActionListener() { + public void actionPerformed(ActionEvent e) { + view.listRules.getView().updateUI(); + } + }; + } + + private void OnCreateTest() { + JFileChooser jfc = new JFileChooser(); + jfc.setDialogTitle("Create test suite from grammar"); + jfc.setDialogType(JFileChooser.OPEN_DIALOG); + if( jfc.showOpenDialog(view) != JFileChooser.APPROVE_OPTION ) return; + + view.paneStatus.setProgressIndetermined(true); + final File grammarFile = jfc.getSelectedFile(); + //final String sName = grammarFile.getName().substring( + // 0, grammarFile.getName().length() - 2); + currentTestSuite = new TestSuite(grammarFile, Translator.loadRulesFromGrammar(grammarFile)); + // trim ".g" + //currentTestSuite.setGrammarName(sName); + //currentTestSuite.setRules(Translator.loadRulesFromGrammar(grammarFile)); + view.listRules.initialize(currentTestSuite); + view.tabEditors.setSelectedIndex(0); + view.paneStatus.setText("Grammar: " + currentTestSuite.getGrammarName()); + view.paneStatus.setProgressIndetermined(false); + + testSuiteFileName = null; + } + + private void OnSaveTest() { + File file; + view.paneStatus.setProgressIndetermined(true); + try { + + if(testSuiteFileName == null || testSuiteFileName.equals("")) { + JFileChooser jfc = new JFileChooser(); + jfc.setDialogTitle("Save test suite"); + jfc.setDialogType(JFileChooser.SAVE_DIALOG); + if( jfc.showSaveDialog(view) != JFileChooser.APPROVE_OPTION ) return; + + file = jfc.getSelectedFile(); + testSuiteFileName = file.getCanonicalPath(); + } else { + file = new File(testSuiteFileName); + } + + FileWriter fw = new FileWriter(file); + fw.write(Translator.toScript(currentTestSuite)); + fw.flush(); + fw.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + + view.paneStatus.setProgressIndetermined(false); + + } + + private void OnSaveAsTest() { + + } + + private void OnOpenTest() { + + JFileChooser jfc = new JFileChooser(); + jfc.setDialogTitle("Open existing gUnit test suite"); + jfc.setDialogType(JFileChooser.OPEN_DIALOG); + if( jfc.showOpenDialog(view) != JFileChooser.APPROVE_OPTION ) return; + + final File testSuiteFile = jfc.getSelectedFile(); + try { + testSuiteFileName = testSuiteFile.getCanonicalPath(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + view.paneStatus.setProgressIndetermined(true); + + currentTestSuite = Translator.toTestSuite(testSuiteFile, new ArrayList()); + view.listRules.initialize(currentTestSuite); + view.paneStatus.setText(currentTestSuite.getGrammarName()); + view.tabEditors.setSelectedIndex(0); + + view.paneStatus.setProgressIndetermined(false); + } + + private void OnSelectRule(Rule rule) { + if(rule == null) throw new IllegalArgumentException("Null"); + this.view.paneEditor.OnLoadRule(rule); + this.view.paneStatus.setRule(rule.getName()); + + // run result + this.runner.OnShowRuleResult(rule); + } + + private void OnSelectTextPane() { + Thread worker = new Thread () { + @Override + public void run() { + view.paneStatus.setProgressIndetermined(true); + view.txtEditor.setText( + Translator.toScript(currentTestSuite)); + view.paneStatus.setProgressIndetermined(false); + } + }; + + worker.start(); + } + + private void OnRunTest() { + if(currentTestSuite == null) return; + adapter.run(testSuiteFileName, currentTestSuite); + view.tabEditors.addTab("Test Result", ImageFactory.FILE16, runner.getView()); + runner.OnShowSuiteResult(currentTestSuite); + } + + private void initToolbar() { + view.toolbar.add(new JButton(new CreateAction())); + view.toolbar.add(new JButton(new OpenAction())); + view.toolbar.add(new JButton(new SaveAction())); + view.toolbar.add(new JButton(new RunAction())); + + } + + public Object getModel() { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Component getView() { + return view; + } + + + /** Event handler for rule list selection. */ + private class RuleListSelectionListener implements ListSelectionListener { + public void valueChanged(ListSelectionEvent event) { + if(event.getValueIsAdjusting()) return; + final JList list = (JList) event.getSource(); + final Rule rule = (Rule) list.getSelectedValue(); + if(rule != null) OnSelectRule(rule); + } + } + + + /** Event handler for switching between editor view and script view. */ + public class TabChangeListener implements ChangeListener { + public void stateChanged(ChangeEvent evt) { + if(view.tabEditors.getSelectedIndex() == 1) { + OnSelectTextPane(); + } + } + + } + + + + + + + + + + /** Create test suite action. */ + private class CreateAction extends AbstractAction { + public CreateAction() { + super("Create", ImageFactory.ADDFILE); + putValue(SHORT_DESCRIPTION, "Create a test suite from an ANTLR grammar"); + } + public void actionPerformed(ActionEvent e) { + OnCreateTest(); + } + } + + + /** Save test suite action. */ + private class SaveAction extends AbstractAction { + public SaveAction() { + super("Save", ImageFactory.SAVE); + putValue(SHORT_DESCRIPTION, "Save the test suite"); + } + public void actionPerformed(ActionEvent e) { + OnSaveTest(); + } + } + + /** Save as test suite action. */ + private class SaveAsAction extends AbstractAction { + public SaveAsAction() { + super("Save as", ImageFactory.SAVEAS); + putValue(SHORT_DESCRIPTION, "Save a copy"); + } + public void actionPerformed(ActionEvent e) { + OnSaveAsTest(); + } + } + + /** Open test suite action. */ + private class OpenAction extends AbstractAction { + public OpenAction() { + super("Open", ImageFactory.OPEN); + putValue(SHORT_DESCRIPTION, "Open an existing test suite"); + putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke( + KeyEvent.VK_O, InputEvent.CTRL_MASK)); + } + public void actionPerformed(ActionEvent e) { + OnOpenTest(); + } + } + + /** Run test suite action. */ + private class RunAction extends AbstractAction { + public RunAction() { + super("Run", ImageFactory.NEXT); + putValue(SHORT_DESCRIPTION, "Run the current test suite"); + putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke( + KeyEvent.VK_R, InputEvent.CTRL_MASK)); + } + public void actionPerformed(ActionEvent e) { + OnRunTest(); + } + } +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/WorkSpaceView.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/WorkSpaceView.java new file mode 100644 index 0000000..1216fd9 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/WorkSpaceView.java @@ -0,0 +1,76 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.antlr.gunit.swingui; + +import org.antlr.gunit.swingui.images.ImageFactory; +import java.awt.*; +import javax.swing.*; + +/** + * + * @author scai + */ +public class WorkSpaceView extends JFrame { + + protected JSplitPane splitListClient ; + protected JTabbedPane tabEditors; + protected JPanel paneToolBar; + protected StatusBarController paneStatus; + protected TestCaseEditController paneEditor; + protected JToolBar toolbar; + protected JTextArea txtEditor; + protected RuleListController listRules; + protected JMenuBar menuBar; + protected JScrollPane scrollCode; + protected JPanel resultPane; + + protected JButton btnOpenGrammar; + + public WorkSpaceView() { + super(); + } + + protected void initComponents() { + + this.paneEditor = new TestCaseEditController(this); + this.paneStatus = new StatusBarController(); + + this.toolbar = new JToolBar(); + this.toolbar.setBorder(BorderFactory.createEmptyBorder()); + this.toolbar.setFloatable(false); + this.toolbar.setBorder(BorderFactory.createEmptyBorder()); + + this.txtEditor = new JTextArea(); + this.txtEditor.setLineWrap(false); + this.txtEditor.setFont(new Font("Courier New", Font.PLAIN, 13)); + this.scrollCode = new JScrollPane(txtEditor, + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, + JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + this.scrollCode.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY)); + + this.tabEditors = new JTabbedPane(); + this.tabEditors.addTab("Case Editor", ImageFactory.TEXTFILE16, this.paneEditor.getView()); + this.tabEditors.addTab("Script Source", ImageFactory.WINDOW16, this.scrollCode); + + this.listRules = new RuleListController(); + + this.splitListClient = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, + this.listRules.getView(), this.tabEditors); + this.splitListClient.setResizeWeight(0.4); + this.splitListClient.setBorder(BorderFactory.createEmptyBorder()); + + + + this.getContentPane().add(this.toolbar, BorderLayout.NORTH); + this.getContentPane().add(this.splitListClient, BorderLayout.CENTER); + this.getContentPane().add(this.paneStatus.getView(), BorderLayout.SOUTH); + + // self + this.setPreferredSize(new Dimension(900, 500)); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/gunit.stg b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/gunit.stg new file mode 100644 index 0000000..c17178c --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/gunit.stg @@ -0,0 +1,19 @@ +group gunit; + +gUnitFile(testSuite) ::= <; + + +>> + +testGroup() ::= << + + +//------------------- +: + + + + +>> + +testCase() ::= " " \ No newline at end of file diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/ImageFactory.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/ImageFactory.java new file mode 100644 index 0000000..a896b22 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/ImageFactory.java @@ -0,0 +1,59 @@ +package org.antlr.gunit.swingui.images; + +import javax.swing.ImageIcon; + +public class ImageFactory { + public static final ImageIcon ACCEPT = new ImageIcon( + ImageFactory.class.getResource("accept.png")); + public static final ImageIcon ADD = new ImageIcon( + ImageFactory.class.getResource("add.png")); + public static final ImageIcon DELETE = new ImageIcon( + ImageFactory.class.getResource("delete24.png")); + public static final ImageIcon HELP = new ImageIcon( + ImageFactory.class.getResource("help24.png")); + public static final ImageIcon NEXT = new ImageIcon( + ImageFactory.class.getResource("next24.png")); + public static final ImageIcon REDO = new ImageIcon( + ImageFactory.class.getResource("redo24.png")); + public static final ImageIcon UNDO = new ImageIcon( + ImageFactory.class.getResource("undo24.png")); + public static final ImageIcon REFRESH = new ImageIcon( + ImageFactory.class.getResource("refresh24.png")); + public static final ImageIcon TEXTFILE = new ImageIcon( + ImageFactory.class.getResource("textfile24.png")); + public static final ImageIcon ADDFILE = new ImageIcon( + ImageFactory.class.getResource("addfile24.png")); + + public static final ImageIcon TEXTFILE16 = new ImageIcon( + ImageFactory.class.getResource("textfile16.png")); + public static final ImageIcon WINDOW16 = new ImageIcon( + ImageFactory.class.getResource("windowb16.png")); + public static final ImageIcon FAV16 = new ImageIcon( + ImageFactory.class.getResource("favb16.png")); + public static final ImageIcon SAVE = new ImageIcon( + ImageFactory.class.getResource("floppy24.png")); + public static final ImageIcon SAVEAS = new ImageIcon( + ImageFactory.class.getResource("saveas24.png")); + + public static final ImageIcon OPEN = new ImageIcon( + ImageFactory.class.getResource("folder24.png")); + public static final ImageIcon FILESEARCH = new ImageIcon( + ImageFactory.class.getResource("filesearch24.png")); + public static final ImageIcon EDIT16 = new ImageIcon( + ImageFactory.class.getResource("edit16.png")); + public static final ImageIcon FILE16 = new ImageIcon( + ImageFactory.class.getResource("file16.png")); + + + public static final ImageIcon RUN_PASS = new ImageIcon( + ImageFactory.class.getResource("runpass.png")); + public static final ImageIcon RUN_FAIL = new ImageIcon( + ImageFactory.class.getResource("runfail.png")); + public static final ImageIcon TESTSUITE = new ImageIcon( + ImageFactory.class.getResource("testsuite.png")); + public static final ImageIcon TESTGROUP = new ImageIcon( + ImageFactory.class.getResource("testgroup.png")); + public static final ImageIcon TESTGROUPX = new ImageIcon( + ImageFactory.class.getResource("testgroupx.png")); + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/accept.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/accept.png new file mode 100644 index 0000000..e7d91e5 Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/accept.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/add.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/add.png new file mode 100644 index 0000000..b6f090b Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/add.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/addfile24.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/addfile24.png new file mode 100644 index 0000000..408b6b4 Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/addfile24.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/delete24.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/delete24.png new file mode 100644 index 0000000..bf852ba Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/delete24.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/edit16.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/edit16.png new file mode 100644 index 0000000..2e82c2e Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/edit16.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/favb16.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/favb16.png new file mode 100644 index 0000000..3c252de Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/favb16.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/favb24.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/favb24.png new file mode 100644 index 0000000..05f7f1f Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/favb24.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/file16.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/file16.png new file mode 100644 index 0000000..2e13243 Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/file16.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/filesearch24.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/filesearch24.png new file mode 100644 index 0000000..e8a2d99 Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/filesearch24.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/floppy24.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/floppy24.png new file mode 100644 index 0000000..1a46470 Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/floppy24.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/folder24.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/folder24.png new file mode 100644 index 0000000..179998b Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/folder24.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/help24.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/help24.png new file mode 100644 index 0000000..fcdfb43 Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/help24.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/next24.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/next24.png new file mode 100644 index 0000000..1258622 Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/next24.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/redo24.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/redo24.png new file mode 100644 index 0000000..8eea6a9 Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/redo24.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/refresh24.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/refresh24.png new file mode 100644 index 0000000..2683e98 Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/refresh24.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/runfail.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/runfail.png new file mode 100755 index 0000000..186f17f Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/runfail.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/runpass.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/runpass.png new file mode 100755 index 0000000..34c3893 Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/runpass.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/saveas24.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/saveas24.png new file mode 100644 index 0000000..bfe8802 Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/saveas24.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/testgroup.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/testgroup.png new file mode 100755 index 0000000..65aaaa7 Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/testgroup.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/testgroupx.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/testgroupx.png new file mode 100644 index 0000000..1d63fa4 Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/testgroupx.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/testsuite.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/testsuite.png new file mode 100755 index 0000000..a85fe14 Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/testsuite.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/textfile16.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/textfile16.png new file mode 100644 index 0000000..c37328e Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/textfile16.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/textfile24.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/textfile24.png new file mode 100644 index 0000000..a8a67db Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/textfile24.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/undo24.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/undo24.png new file mode 100644 index 0000000..3f0c5e5 Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/undo24.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/windowb16.png b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/windowb16.png new file mode 100644 index 0000000..c595f9b Binary files /dev/null and b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/images/windowb16.png differ diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/ITestCaseInput.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/ITestCaseInput.java new file mode 100644 index 0000000..b247280 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/ITestCaseInput.java @@ -0,0 +1,7 @@ +package org.antlr.gunit.swingui.model; + +public interface ITestCaseInput { + + public void setScript(String script); + public String getScript(); +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/ITestCaseOutput.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/ITestCaseOutput.java new file mode 100644 index 0000000..c0da191 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/ITestCaseOutput.java @@ -0,0 +1,15 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.antlr.gunit.swingui.model; + +/** + * + * @author scai + */ +public interface ITestCaseOutput { + public void setScript(String script); + public String getScript() ; +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/Rule.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/Rule.java new file mode 100644 index 0000000..4e1e435 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/Rule.java @@ -0,0 +1,47 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.antlr.gunit.swingui.model; + +import java.util.ArrayList; +import java.util.List; +import javax.swing.DefaultListModel; + +/** + * ANTLR v3 Rule Information. + * @author scai + */ +public class Rule extends DefaultListModel { + + private String name; + + public Rule(String name) { + this.name = name; + } + + public String getName() { return name; } + + public boolean getNotEmpty() { + return !this.isEmpty(); + } + + @Override + public String toString() { + return this.name; + } + + public void addTestCase(TestCase newItem) { + this.addElement(newItem); + } + + // for string template + public List getTestCases() { + List result = new ArrayList(); + for(int i=0; i[%s]", input.getScript(), output.getScript()); + } + + public void setInput(ITestCaseInput in) { + this.input = in; + } + + public void setOutput(ITestCaseOutput out) { + this.output = out; + } + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseInputFile.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseInputFile.java new file mode 100644 index 0000000..108e31d --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseInputFile.java @@ -0,0 +1,35 @@ + +package org.antlr.gunit.swingui.model; + +import javax.swing.JComponent; +import javax.swing.JLabel; + +/** + * + * @author scai + */ +public class TestCaseInputFile implements ITestCaseInput { + + private String fileName; + + public TestCaseInputFile(String file) { + this.fileName = file; + } + + public String getLabel() { + return "FILE:" + fileName; + } + + public void setScript(String script) { + this.fileName = script; + } + + @Override + public String toString() { + return fileName; + } + + public String getScript() { + return this.fileName; + } +} \ No newline at end of file diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseInputMultiString.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseInputMultiString.java new file mode 100644 index 0000000..ee383ee --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseInputMultiString.java @@ -0,0 +1,30 @@ + +package org.antlr.gunit.swingui.model; + + +/** + * + * @author scai + */ +public class TestCaseInputMultiString implements ITestCaseInput { + + private String script; + + public TestCaseInputMultiString(String text) { + this.script = text; + } + + @Override + public String toString() { + return "<<" + script + ">>"; + } + + public void setScript(String script) { + this.script = script; + } + + public String getScript() { + return this.script; + } + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseInputString.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseInputString.java new file mode 100644 index 0000000..2a1a1be --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseInputString.java @@ -0,0 +1,32 @@ + +package org.antlr.gunit.swingui.model; + +/** + * + * @author scai + */ +public class TestCaseInputString implements ITestCaseInput { + + private String script; + + public TestCaseInputString(String text) { + this.script = text; + } + + @Override + public String toString() { + return '"' + script + '"'; + } + + + + public void setScript(String script) { + this.script = script; + } + + public String getScript() { + return this.script; + } + + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseOutputAST.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseOutputAST.java new file mode 100644 index 0000000..7cc4548 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseOutputAST.java @@ -0,0 +1,30 @@ + +package org.antlr.gunit.swingui.model; + +/** + * + * @author scai + */ +public class TestCaseOutputAST implements ITestCaseOutput { + + private String treeString; + + public TestCaseOutputAST(String script) { + this.treeString = script; + } + + public void setScript(String script) { + this.treeString = script; + } + + public String getScript() { + return this.treeString; + } + + + @Override + public String toString() { + return String.format(" -> %s", treeString); + } + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseOutputResult.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseOutputResult.java new file mode 100644 index 0000000..2ec8253 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseOutputResult.java @@ -0,0 +1,36 @@ + +package org.antlr.gunit.swingui.model; + +/** + * + * @author scai + */ +public class TestCaseOutputResult implements ITestCaseOutput { + + public static String OK = "OK"; + public static String FAIL = "FAIL"; + + private boolean success ; + + public TestCaseOutputResult(boolean result) { + this.success = result; + } + + @Override + public String toString() { + return getScript(); + } + + public String getScript() { + return success ? OK : FAIL; + } + + public void setScript(boolean value) { + this.success = value; + } + + public void setScript(String script) { + success = Boolean.parseBoolean(script); + } + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseOutputReturn.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseOutputReturn.java new file mode 100644 index 0000000..ae3cef2 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseOutputReturn.java @@ -0,0 +1,22 @@ +package org.antlr.gunit.swingui.model; + +public class TestCaseOutputReturn implements ITestCaseOutput { + private String script; + + public TestCaseOutputReturn(String text) { + this.script = text; + } + + @Override + public String toString() { + return String.format(" returns [%s]", script); + } + + public void setScript(String script) { + this.script = script; + } + + public String getScript() { + return this.script; + } +} \ No newline at end of file diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseOutputStdOut.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseOutputStdOut.java new file mode 100644 index 0000000..74ce689 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestCaseOutputStdOut.java @@ -0,0 +1,26 @@ +package org.antlr.gunit.swingui.model; + +/** + * + * @author scai + */ +public class TestCaseOutputStdOut implements ITestCaseOutput { + private String script; + + public TestCaseOutputStdOut(String text) { + this.script = text; + } + + @Override + public String toString() { + return String.format(" -> \"%s\"", script); + } + + public void setScript(String script) { + this.script = script; + } + + public String getScript() { + return this.script; + } +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestSuite.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestSuite.java new file mode 100644 index 0000000..09feb66 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/model/TestSuite.java @@ -0,0 +1,86 @@ +package org.antlr.gunit.swingui.model; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import org.antlr.runtime.CommonTokenStream; + +public class TestSuite { + + private List rules = new ArrayList(); + private String grammarName = ""; + private String grammarDir = ""; + private CommonTokenStream tokens; + + private static final String TEST_SUITE_EXT = ".gunit"; + private static final String GRAMMAR_EXT = ".g"; + + public TestSuite() {} + + public TestSuite(File grammarFile, List rules) { + final String grammarFileName = grammarFile.getName(); + grammarName = grammarFileName.substring( + 0, grammarFileName.lastIndexOf(".")); + + grammarDir = grammarFile.getParent(); + } + + /* Get the gUnit test suite file name. */ + public String getTestSuiteFileName() { + return grammarDir + File.separator + grammarName + TEST_SUITE_EXT; + } + + /* Get the ANTLR grammar file name. */ + public String getGrammarFileName() { + return grammarDir + File.separator + grammarName + GRAMMAR_EXT; + } + + public void addRule(Rule currentRule) { + if(currentRule == null) throw new IllegalArgumentException("Null rule"); + rules.add(currentRule); + } + + // test rule name + public boolean hasRule(Rule rule) { + for(Rule r: rules) { + if(r.getName().equals(rule.getName())) { + return true; + } + } + return false; + } + + public int getRuleCount() { + return rules.size(); + } + + public void setRules(List newRules) { + rules.clear(); + rules.addAll(newRules); + } + + /* GETTERS AND SETTERS */ + + public void setGrammarName(String name) { grammarName = name;} + + public String getGrammarName() { return grammarName; } + + public Rule getRule(int index) { return rules.get(index); } + + public CommonTokenStream getTokens() { return tokens; } + + public void setTokens(CommonTokenStream ts) { tokens = ts; } + + public Rule getRule(String name) { + for(Rule rule: rules) { + if(rule.getName().equals(name)) { + return rule; + } + } + return null; + } + + // only for stringtemplate use + public List getRulesForStringTemplate() {return rules;} + +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/ANTLRv3.g b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/ANTLRv3.g new file mode 100644 index 0000000..5657072 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/ANTLRv3.g @@ -0,0 +1,619 @@ +/* + [The "BSD licence"] + Copyright (c) 2005-2007 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. +*/ + +/** ANTLR v3 grammar written in ANTLR v3 with AST construction */ +grammar ANTLRv3; + +options { + output=AST; + ASTLabelType=CommonTree; +} + +tokens { + DOC_COMMENT; + PARSER; + LEXER; + RULE; + BLOCK; + OPTIONAL; + CLOSURE; + POSITIVE_CLOSURE; + SYNPRED; + RANGE; + CHAR_RANGE; + EPSILON; + ALT; + EOR; + EOB; + EOA; // end of alt + ID; + ARG; + ARGLIST; + RET; + LEXER_GRAMMAR; + PARSER_GRAMMAR; + TREE_GRAMMAR; + COMBINED_GRAMMAR; + INITACTION; + LABEL; // $x used in rewrite rules + TEMPLATE; + SCOPE='scope'; + SEMPRED; + GATED_SEMPRED; // {p}? => + SYN_SEMPRED; // (...) => it's a manually-specified synpred converted to sempred + BACKTRACK_SEMPRED; // auto backtracking mode syn pred converted to sempred + FRAGMENT='fragment'; + TREE_BEGIN='^('; + ROOT='^'; + BANG='!'; + RANGE='..'; + REWRITE='->'; +} + +@members { + int gtype; + public List rules; +} + +@header { +package org.antlr.gunit.swingui.parsers; + +import java.util.List; +} + +@lexer::header { +package org.antlr.gunit.swingui.parsers; +} + + +grammarDef + : DOC_COMMENT? + ( 'lexer' {gtype=LEXER_GRAMMAR;} // pure lexer + | 'parser' {gtype=PARSER_GRAMMAR;} // pure parser + | 'tree' {gtype=TREE_GRAMMAR;} // a tree parser + | {gtype=COMBINED_GRAMMAR;} // merged parser/lexer + ) + g='grammar' id ';' optionsSpec? tokensSpec? attrScope* action* + rule+ + EOF + -> ^( {adaptor.create(gtype,$g)} + id DOC_COMMENT? optionsSpec? tokensSpec? attrScope* action* rule+ + ) + ; + +tokensSpec + : TOKENS tokenSpec+ '}' -> ^(TOKENS tokenSpec+) + ; + +tokenSpec + : TOKEN_REF + ( '=' (lit=STRING_LITERAL|lit=CHAR_LITERAL) -> ^('=' TOKEN_REF $lit) + | -> TOKEN_REF + ) + ';' + ; + +attrScope + : 'scope' id ACTION -> ^('scope' id ACTION) + ; + +/** Match stuff like @parser::members {int i;} */ +action + : '@' (actionScopeName '::')? id ACTION -> ^('@' actionScopeName? id ACTION) + ; + +/** Sometimes the scope names will collide with keywords; allow them as + * ids for action scopes. + */ +actionScopeName + : id + | l='lexer' -> ID[$l] + | p='parser' -> ID[$p] + ; + +optionsSpec + : OPTIONS (option ';')+ '}' -> ^(OPTIONS option+) + ; + +option + : id '=' optionValue -> ^('=' id optionValue) + ; + +optionValue + : id + | STRING_LITERAL + | CHAR_LITERAL + | INT + | s='*' -> STRING_LITERAL[$s] // used for k=* + ; + +rule +scope { + String name; +} +@after{ + this.rules.add($rule::name); +} + : DOC_COMMENT? + ( modifier=('protected'|'public'|'private'|'fragment') )? + id {$rule::name = $id.text;} + '!'? + ( arg=ARG_ACTION )? + ( 'returns' rt=ARG_ACTION )? + throwsSpec? optionsSpec? ruleScopeSpec? ruleAction* + ':' altList ';' + exceptionGroup? + -> ^( RULE id {modifier!=null?adaptor.create(modifier):null} ^(ARG $arg)? ^(RET $rt)? + optionsSpec? ruleScopeSpec? ruleAction* + altList + exceptionGroup? + EOR["EOR"] + ) + ; + +/** Match stuff like @init {int i;} */ +ruleAction + : '@' id ACTION -> ^('@' id ACTION) + ; + +throwsSpec + : 'throws' id ( ',' id )* -> ^('throws' id+) + ; + +ruleScopeSpec + : 'scope' ACTION -> ^('scope' ACTION) + | 'scope' id (',' id)* ';' -> ^('scope' id+) + | 'scope' ACTION + 'scope' id (',' id)* ';' + -> ^('scope' ACTION id+ ) + ; + +block + : lp='(' + ( (opts=optionsSpec)? ':' )? + a1=alternative rewrite ( '|' a2=alternative rewrite )* + rp=')' + -> ^( BLOCK[$lp,"BLOCK"] optionsSpec? (alternative rewrite?)+ EOB[$rp,"EOB"] ) + ; + +altList +@init { + // must create root manually as it's used by invoked rules in real antlr tool. + // leave here to demonstrate use of {...} in rewrite rule + // it's really BLOCK[firstToken,"BLOCK"]; set line/col to previous ( or : token. + CommonTree blkRoot = (CommonTree)adaptor.create(BLOCK,input.LT(-1),"BLOCK"); +} + : a1=alternative rewrite ( '|' a2=alternative rewrite )* + -> ^( {blkRoot} (alternative rewrite?)+ EOB["EOB"] ) + ; + +alternative +@init { + Token firstToken = input.LT(1); + Token prevToken = input.LT(-1); // either : or | I think +} + : element+ -> ^(ALT[firstToken,"ALT"] element+ EOA["EOA"]) + | -> ^(ALT[prevToken,"ALT"] EPSILON[prevToken,"EPSILON"] EOA["EOA"]) + ; + +exceptionGroup + : ( exceptionHandler )+ ( finallyClause )? + | finallyClause + ; + +exceptionHandler + : 'catch' ARG_ACTION ACTION -> ^('catch' ARG_ACTION ACTION) + ; + +finallyClause + : 'finally' ACTION -> ^('finally' ACTION) + ; + +element + : elementNoOptionSpec + ; + +elementNoOptionSpec + : id (labelOp='='|labelOp='+=') atom + ( ebnfSuffix -> ^( ebnfSuffix ^(BLOCK["BLOCK"] ^(ALT["ALT"] ^($labelOp id atom) EOA["EOA"]) EOB["EOB"])) + | -> ^($labelOp id atom) + ) + | id (labelOp='='|labelOp='+=') block + ( ebnfSuffix -> ^( ebnfSuffix ^(BLOCK["BLOCK"] ^(ALT["ALT"] ^($labelOp id block) EOA["EOA"]) EOB["EOB"])) + | -> ^($labelOp id block) + ) + | atom + ( ebnfSuffix -> ^( ebnfSuffix ^(BLOCK["BLOCK"] ^(ALT["ALT"] atom EOA["EOA"]) EOB["EOB"]) ) + | -> atom + ) + | ebnf + | ACTION + | SEMPRED ( '=>' -> GATED_SEMPRED | -> SEMPRED ) + | treeSpec + ( ebnfSuffix -> ^( ebnfSuffix ^(BLOCK["BLOCK"] ^(ALT["ALT"] treeSpec EOA["EOA"]) EOB["EOB"]) ) + | -> treeSpec + ) + ; + +atom: range ( (op='^'|op='!') -> ^($op range) | -> range ) + | terminal + | notSet ( (op='^'|op='!') -> ^($op notSet) | -> notSet ) + | RULE_REF ( arg=ARG_ACTION )? ( (op='^'|op='!') )? + -> {$arg!=null&&op!=null}? ^($op RULE_REF $arg) + -> {$arg!=null}? ^(RULE_REF $arg) + -> {$op!=null}? ^($op RULE_REF) + -> RULE_REF + ; + +notSet + : '~' + ( notTerminal -> ^('~' notTerminal) + | block -> ^('~' block) + ) + ; + +treeSpec + : '^(' element ( element )+ ')' -> ^(TREE_BEGIN element+) + ; + +/** Matches ENBF blocks (and token sets via block rule) */ +ebnf +@init { + Token firstToken = input.LT(1); +} +@after { + $ebnf.tree.getToken().setLine(firstToken.getLine()); + $ebnf.tree.getToken().setCharPositionInLine(firstToken.getCharPositionInLine()); +} + : block + ( op='?' -> ^(OPTIONAL[op] block) + | op='*' -> ^(CLOSURE[op] block) + | op='+' -> ^(POSITIVE_CLOSURE[op] block) + | '=>' // syntactic predicate + -> {gtype==COMBINED_GRAMMAR && + Character.isUpperCase($rule::name.charAt(0))}? + // if lexer rule in combined, leave as pred for lexer + ^(SYNPRED["=>"] block) + // in real antlr tool, text for SYN_SEMPRED is predname + -> SYN_SEMPRED + | -> block + ) + ; + +range! + : c1=CHAR_LITERAL RANGE c2=CHAR_LITERAL -> ^(CHAR_RANGE[$c1,".."] $c1 $c2) + ; + +terminal + : ( CHAR_LITERAL -> CHAR_LITERAL + // Args are only valid for lexer rules + | TOKEN_REF + ( ARG_ACTION -> ^(TOKEN_REF ARG_ACTION) + | -> TOKEN_REF + ) + | STRING_LITERAL -> STRING_LITERAL + | '.' -> '.' + ) + ( '^' -> ^('^' $terminal) + | '!' -> ^('!' $terminal) + )? + ; + +notTerminal + : CHAR_LITERAL + | TOKEN_REF + | STRING_LITERAL + ; + +ebnfSuffix +@init { + Token op = input.LT(1); +} + : '?' -> OPTIONAL[op] + | '*' -> CLOSURE[op] + | '+' -> POSITIVE_CLOSURE[op] + ; + + + +// R E W R I T E S Y N T A X + +rewrite +@init { + Token firstToken = input.LT(1); +} + : (rew+='->' preds+=SEMPRED predicated+=rewrite_alternative)* + rew2='->' last=rewrite_alternative + -> ^($rew $preds $predicated)* ^($rew2 $last) + | + ; + +rewrite_alternative +options {backtrack=true;} + : rewrite_template + | rewrite_tree_alternative + | /* empty rewrite */ -> ^(ALT["ALT"] EPSILON["EPSILON"] EOA["EOA"]) + ; + +rewrite_tree_block + : lp='(' rewrite_tree_alternative ')' + -> ^(BLOCK[$lp,"BLOCK"] rewrite_tree_alternative EOB[$lp,"EOB"]) + ; + +rewrite_tree_alternative + : rewrite_tree_element+ -> ^(ALT["ALT"] rewrite_tree_element+ EOA["EOA"]) + ; + +rewrite_tree_element + : rewrite_tree_atom + | rewrite_tree_atom ebnfSuffix + -> ^( ebnfSuffix ^(BLOCK["BLOCK"] ^(ALT["ALT"] rewrite_tree_atom EOA["EOA"]) EOB["EOB"])) + | rewrite_tree + ( ebnfSuffix + -> ^(ebnfSuffix ^(BLOCK["BLOCK"] ^(ALT["ALT"] rewrite_tree EOA["EOA"]) EOB["EOB"])) + | -> rewrite_tree + ) + | rewrite_tree_ebnf + ; + +rewrite_tree_atom + : CHAR_LITERAL + | TOKEN_REF ARG_ACTION? -> ^(TOKEN_REF ARG_ACTION?) // for imaginary nodes + | RULE_REF + | STRING_LITERAL + | d='$' id -> LABEL[$d,$id.text] // reference to a label in a rewrite rule + | ACTION + ; + +rewrite_tree_ebnf +@init { + Token firstToken = input.LT(1); +} +@after { + $rewrite_tree_ebnf.tree.getToken().setLine(firstToken.getLine()); + $rewrite_tree_ebnf.tree.getToken().setCharPositionInLine(firstToken.getCharPositionInLine()); +} + : rewrite_tree_block ebnfSuffix -> ^(ebnfSuffix rewrite_tree_block) + ; + +rewrite_tree + : '^(' rewrite_tree_atom rewrite_tree_element* ')' + -> ^(TREE_BEGIN rewrite_tree_atom rewrite_tree_element* ) + ; + +/** Build a tree for a template rewrite: + ^(TEMPLATE (ID|ACTION) ^(ARGLIST ^(ARG ID ACTION) ...) ) + where ARGLIST is always there even if no args exist. + ID can be "template" keyword. If first child is ACTION then it's + an indirect template ref + + -> foo(a={...}, b={...}) + -> ({string-e})(a={...}, b={...}) // e evaluates to template name + -> {%{$ID.text}} // create literal template from string (done in ActionTranslator) + -> {st-expr} // st-expr evaluates to ST + */ +rewrite_template + : // -> template(a={...},...) "..." inline template + id lp='(' rewrite_template_args ')' + ( str=DOUBLE_QUOTE_STRING_LITERAL | str=DOUBLE_ANGLE_STRING_LITERAL ) + -> ^(TEMPLATE[$lp,"TEMPLATE"] id rewrite_template_args $str) + + | // -> foo(a={...}, ...) + rewrite_template_ref + + | // -> ({expr})(a={...}, ...) + rewrite_indirect_template_head + + | // -> {...} + ACTION + ; + +/** -> foo(a={...}, ...) */ +rewrite_template_ref + : id lp='(' rewrite_template_args ')' + -> ^(TEMPLATE[$lp,"TEMPLATE"] id rewrite_template_args) + ; + +/** -> ({expr})(a={...}, ...) */ +rewrite_indirect_template_head + : lp='(' ACTION ')' '(' rewrite_template_args ')' + -> ^(TEMPLATE[$lp,"TEMPLATE"] ACTION rewrite_template_args) + ; + +rewrite_template_args + : rewrite_template_arg (',' rewrite_template_arg)* + -> ^(ARGLIST rewrite_template_arg+) + | -> ARGLIST + ; + +rewrite_template_arg + : id '=' ACTION -> ^(ARG[$id.start] id ACTION) + ; + +id : TOKEN_REF -> ID[$TOKEN_REF] + | RULE_REF -> ID[$RULE_REF] + ; + +// L E X I C A L R U L E S + +SL_COMMENT + : '//' + ( ' $ANTLR ' SRC // src directive + | ~('\r'|'\n')* + ) + '\r'? '\n' + {$channel=HIDDEN;} + ; + +ML_COMMENT + : '/*' {if (input.LA(1)=='*') $type=DOC_COMMENT; else $channel=HIDDEN;} .* '*/' + ; + +CHAR_LITERAL + : '\'' LITERAL_CHAR '\'' + ; + +STRING_LITERAL + : '\'' LITERAL_CHAR LITERAL_CHAR* '\'' + ; + +fragment +LITERAL_CHAR + : ESC + | ~('\''|'\\') + ; + +DOUBLE_QUOTE_STRING_LITERAL + : '"' (ESC | ~('\\'|'"'))* '"' + ; + +DOUBLE_ANGLE_STRING_LITERAL + : '<<' .* '>>' + ; + +fragment +ESC : '\\' + ( 'n' + | 'r' + | 't' + | 'b' + | 'f' + | '"' + | '\'' + | '\\' + | '>' + | 'u' XDIGIT XDIGIT XDIGIT XDIGIT + | . // unknown, leave as it is + ) + ; + +fragment +XDIGIT : + '0' .. '9' + | 'a' .. 'f' + | 'A' .. 'F' + ; + +INT : '0'..'9'+ + ; + +ARG_ACTION + : NESTED_ARG_ACTION + ; + +fragment +NESTED_ARG_ACTION : + '[' + ( options {greedy=false; k=1;} + : NESTED_ARG_ACTION + | ACTION_STRING_LITERAL + | ACTION_CHAR_LITERAL + | . + )* + ']' + {setText(getText().substring(1, getText().length()-1));} + ; + +ACTION + : NESTED_ACTION ( '?' {$type = SEMPRED;} )? + ; + +fragment +NESTED_ACTION : + '{' + ( options {greedy=false; k=2;} + : NESTED_ACTION + | SL_COMMENT + | ML_COMMENT + | ACTION_STRING_LITERAL + | ACTION_CHAR_LITERAL + | . + )* + '}' + ; + +fragment +ACTION_CHAR_LITERAL + : '\'' (ACTION_ESC|~('\\'|'\'')) '\'' + ; + +fragment +ACTION_STRING_LITERAL + : '"' (ACTION_ESC|~('\\'|'"'))* '"' + ; + +fragment +ACTION_ESC + : '\\\'' + | '\\' '"' // ANTLR doesn't like: '\\"' + | '\\' ~('\''|'"') + ; + +TOKEN_REF + : 'A'..'Z' ('a'..'z'|'A'..'Z'|'_'|'0'..'9')* + ; + +RULE_REF + : 'a'..'z' ('a'..'z'|'A'..'Z'|'_'|'0'..'9')* + ; + +/** Match the start of an options section. Don't allow normal + * action processing on the {...} as it's not a action. + */ +OPTIONS + : 'options' WS_LOOP '{' + ; + +TOKENS + : 'tokens' WS_LOOP '{' + ; + +/** Reset the file and line information; useful when the grammar + * has been generated so that errors are shown relative to the + * original file like the old C preprocessor used to do. + */ +fragment +SRC : 'src' ' ' file=ACTION_STRING_LITERAL ' ' line=INT + ; + +WS : ( ' ' + | '\t' + | '\r'? '\n' + )+ + {$channel=HIDDEN;} + ; + +fragment +WS_LOOP + : ( WS + | SL_COMMENT + | ML_COMMENT + )* + ; + + diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/ANTLRv3.tokens b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/ANTLRv3.tokens new file mode 100644 index 0000000..81466b2 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/ANTLRv3.tokens @@ -0,0 +1,126 @@ +CLOSURE=10 +DOUBLE_QUOTE_STRING_LITERAL=50 +TEMPLATE=30 +ARGLIST=22 +PARSER_GRAMMAR=25 +BANG=39 +T__73=73 +GATED_SEMPRED=33 +T__72=72 +T__70=70 +ACTION_ESC=62 +LEXER=6 +STRING_LITERAL=43 +OPTIONAL=9 +ACTION_CHAR_LITERAL=60 +RANGE=13 +DOUBLE_ANGLE_STRING_LITERAL=51 +T__89=89 +WS=64 +T__79=79 +T__66=66 +ARG_ACTION=48 +TOKEN_REF=42 +WS_LOOP=63 +T__92=92 +T__88=88 +XDIGIT=57 +TREE_BEGIN=37 +T__90=90 +INITACTION=28 +POSITIVE_CLOSURE=11 +T__91=91 +T__85=85 +CHAR_RANGE=14 +RET=23 +LITERAL_CHAR=55 +DOC_COMMENT=4 +T__93=93 +T__86=86 +NESTED_ACTION=61 +T__80=80 +T__69=69 +RULE=7 +T__65=65 +LABEL=29 +SYN_SEMPRED=34 +BACKTRACK_SEMPRED=35 +REWRITE=40 +T__67=67 +TREE_GRAMMAR=26 +T__87=87 +BLOCK=8 +T__74=74 +ALT=16 +T__68=68 +CHAR_LITERAL=44 +FRAGMENT=36 +INT=47 +PARSER=5 +EPSILON=15 +SCOPE=31 +TOKENS=41 +OPTIONS=46 +EOR=17 +ML_COMMENT=54 +SRC=52 +SL_COMMENT=53 +ID=20 +COMBINED_GRAMMAR=27 +EOB=18 +T__78=78 +SYNPRED=12 +EOA=19 +ACTION=45 +T__77=77 +ESC=56 +RULE_REF=49 +T__84=84 +SEMPRED=32 +NESTED_ARG_ACTION=58 +ROOT=38 +T__75=75 +ACTION_STRING_LITERAL=59 +ARG=21 +T__76=76 +T__82=82 +T__81=81 +T__83=83 +T__71=71 +LEXER_GRAMMAR=24 +'finally'=86 +'->'=40 +'returns'=78 +'..'=13 +'=>'=88 +'$'=93 +';'=69 +'?'=90 +'tree'=67 +','=81 +'fragment'=36 +'='=71 +'^'=38 +'scope'=31 +'::'=73 +'private'=77 +'|'=83 +'lexer'=65 +')'=84 +'grammar'=68 +'@'=72 +'protected'=75 +'!'=39 +'+='=87 +'('=82 +'catch'=85 +'^('=37 +'throws'=80 +':'=79 +'.'=92 +'*'=74 +'parser'=66 +'+'=91 +'}'=70 +'~'=89 +'public'=76 diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/ANTLRv3Lexer.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/ANTLRv3Lexer.java new file mode 100644 index 0000000..8c443b0 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/ANTLRv3Lexer.java @@ -0,0 +1,3638 @@ +// $ANTLR 3.1.1 /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g 2009-01-26 16:22:51 + +package org.antlr.gunit.swingui.parsers; + + +import org.antlr.runtime.*; +import java.util.Stack; +import java.util.List; +import java.util.ArrayList; + +public class ANTLRv3Lexer extends Lexer { + public static final int CLOSURE=10; + public static final int DOUBLE_QUOTE_STRING_LITERAL=50; + public static final int TEMPLATE=30; + public static final int ARGLIST=22; + public static final int PARSER_GRAMMAR=25; + public static final int BANG=39; + public static final int T__73=73; + public static final int GATED_SEMPRED=33; + public static final int T__72=72; + public static final int T__70=70; + public static final int ACTION_ESC=62; + public static final int LEXER=6; + public static final int STRING_LITERAL=43; + public static final int OPTIONAL=9; + public static final int ACTION_CHAR_LITERAL=60; + public static final int DOUBLE_ANGLE_STRING_LITERAL=51; + public static final int RANGE=13; + public static final int T__89=89; + public static final int WS=64; + public static final int T__79=79; + public static final int ARG_ACTION=48; + public static final int T__66=66; + public static final int TOKEN_REF=42; + public static final int T__92=92; + public static final int WS_LOOP=63; + public static final int T__88=88; + public static final int XDIGIT=57; + public static final int TREE_BEGIN=37; + public static final int T__90=90; + public static final int INITACTION=28; + public static final int POSITIVE_CLOSURE=11; + public static final int T__91=91; + public static final int T__85=85; + public static final int RET=23; + public static final int CHAR_RANGE=14; + public static final int LITERAL_CHAR=55; + public static final int DOC_COMMENT=4; + public static final int T__93=93; + public static final int NESTED_ACTION=61; + public static final int T__86=86; + public static final int T__80=80; + public static final int T__69=69; + public static final int RULE=7; + public static final int T__65=65; + public static final int LABEL=29; + public static final int SYN_SEMPRED=34; + public static final int BACKTRACK_SEMPRED=35; + public static final int REWRITE=40; + public static final int T__67=67; + public static final int TREE_GRAMMAR=26; + public static final int T__87=87; + public static final int BLOCK=8; + public static final int T__74=74; + public static final int ALT=16; + public static final int T__68=68; + public static final int CHAR_LITERAL=44; + public static final int FRAGMENT=36; + public static final int INT=47; + public static final int PARSER=5; + public static final int EPSILON=15; + public static final int SCOPE=31; + public static final int TOKENS=41; + public static final int OPTIONS=46; + public static final int EOR=17; + public static final int ML_COMMENT=54; + public static final int SRC=52; + public static final int SL_COMMENT=53; + public static final int ID=20; + public static final int COMBINED_GRAMMAR=27; + public static final int EOB=18; + public static final int T__78=78; + public static final int SYNPRED=12; + public static final int EOA=19; + public static final int ACTION=45; + public static final int T__77=77; + public static final int ESC=56; + public static final int RULE_REF=49; + public static final int T__84=84; + public static final int SEMPRED=32; + public static final int NESTED_ARG_ACTION=58; + public static final int ROOT=38; + public static final int T__75=75; + public static final int ACTION_STRING_LITERAL=59; + public static final int ARG=21; + public static final int EOF=-1; + public static final int T__76=76; + public static final int T__82=82; + public static final int T__81=81; + public static final int T__83=83; + public static final int T__71=71; + public static final int LEXER_GRAMMAR=24; + + // delegates + // delegators + + public ANTLRv3Lexer() {;} + public ANTLRv3Lexer(CharStream input) { + this(input, new RecognizerSharedState()); + } + public ANTLRv3Lexer(CharStream input, RecognizerSharedState state) { + super(input,state); + + } + public String getGrammarFileName() { return "/Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g"; } + + // $ANTLR start "SCOPE" + public final void mSCOPE() throws RecognitionException { + try { + int _type = SCOPE; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:7:7: ( 'scope' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:7:9: 'scope' + { + match("scope"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "SCOPE" + + // $ANTLR start "FRAGMENT" + public final void mFRAGMENT() throws RecognitionException { + try { + int _type = FRAGMENT; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:8:10: ( 'fragment' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:8:12: 'fragment' + { + match("fragment"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "FRAGMENT" + + // $ANTLR start "TREE_BEGIN" + public final void mTREE_BEGIN() throws RecognitionException { + try { + int _type = TREE_BEGIN; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:9:12: ( '^(' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:9:14: '^(' + { + match("^("); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "TREE_BEGIN" + + // $ANTLR start "ROOT" + public final void mROOT() throws RecognitionException { + try { + int _type = ROOT; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:10:6: ( '^' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:10:8: '^' + { + match('^'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "ROOT" + + // $ANTLR start "BANG" + public final void mBANG() throws RecognitionException { + try { + int _type = BANG; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:11:6: ( '!' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:11:8: '!' + { + match('!'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "BANG" + + // $ANTLR start "RANGE" + public final void mRANGE() throws RecognitionException { + try { + int _type = RANGE; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:12:7: ( '..' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:12:9: '..' + { + match(".."); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "RANGE" + + // $ANTLR start "REWRITE" + public final void mREWRITE() throws RecognitionException { + try { + int _type = REWRITE; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:13:9: ( '->' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:13:11: '->' + { + match("->"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "REWRITE" + + // $ANTLR start "T__65" + public final void mT__65() throws RecognitionException { + try { + int _type = T__65; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:14:7: ( 'lexer' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:14:9: 'lexer' + { + match("lexer"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__65" + + // $ANTLR start "T__66" + public final void mT__66() throws RecognitionException { + try { + int _type = T__66; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:15:7: ( 'parser' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:15:9: 'parser' + { + match("parser"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__66" + + // $ANTLR start "T__67" + public final void mT__67() throws RecognitionException { + try { + int _type = T__67; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:16:7: ( 'tree' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:16:9: 'tree' + { + match("tree"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__67" + + // $ANTLR start "T__68" + public final void mT__68() throws RecognitionException { + try { + int _type = T__68; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:17:7: ( 'grammar' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:17:9: 'grammar' + { + match("grammar"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__68" + + // $ANTLR start "T__69" + public final void mT__69() throws RecognitionException { + try { + int _type = T__69; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:18:7: ( ';' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:18:9: ';' + { + match(';'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__69" + + // $ANTLR start "T__70" + public final void mT__70() throws RecognitionException { + try { + int _type = T__70; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:19:7: ( '}' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:19:9: '}' + { + match('}'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__70" + + // $ANTLR start "T__71" + public final void mT__71() throws RecognitionException { + try { + int _type = T__71; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:20:7: ( '=' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:20:9: '=' + { + match('='); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__71" + + // $ANTLR start "T__72" + public final void mT__72() throws RecognitionException { + try { + int _type = T__72; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:21:7: ( '@' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:21:9: '@' + { + match('@'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__72" + + // $ANTLR start "T__73" + public final void mT__73() throws RecognitionException { + try { + int _type = T__73; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:22:7: ( '::' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:22:9: '::' + { + match("::"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__73" + + // $ANTLR start "T__74" + public final void mT__74() throws RecognitionException { + try { + int _type = T__74; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:23:7: ( '*' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:23:9: '*' + { + match('*'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__74" + + // $ANTLR start "T__75" + public final void mT__75() throws RecognitionException { + try { + int _type = T__75; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:24:7: ( 'protected' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:24:9: 'protected' + { + match("protected"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__75" + + // $ANTLR start "T__76" + public final void mT__76() throws RecognitionException { + try { + int _type = T__76; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:25:7: ( 'public' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:25:9: 'public' + { + match("public"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__76" + + // $ANTLR start "T__77" + public final void mT__77() throws RecognitionException { + try { + int _type = T__77; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:26:7: ( 'private' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:26:9: 'private' + { + match("private"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__77" + + // $ANTLR start "T__78" + public final void mT__78() throws RecognitionException { + try { + int _type = T__78; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:27:7: ( 'returns' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:27:9: 'returns' + { + match("returns"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__78" + + // $ANTLR start "T__79" + public final void mT__79() throws RecognitionException { + try { + int _type = T__79; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:28:7: ( ':' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:28:9: ':' + { + match(':'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__79" + + // $ANTLR start "T__80" + public final void mT__80() throws RecognitionException { + try { + int _type = T__80; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:29:7: ( 'throws' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:29:9: 'throws' + { + match("throws"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__80" + + // $ANTLR start "T__81" + public final void mT__81() throws RecognitionException { + try { + int _type = T__81; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:30:7: ( ',' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:30:9: ',' + { + match(','); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__81" + + // $ANTLR start "T__82" + public final void mT__82() throws RecognitionException { + try { + int _type = T__82; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:31:7: ( '(' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:31:9: '(' + { + match('('); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__82" + + // $ANTLR start "T__83" + public final void mT__83() throws RecognitionException { + try { + int _type = T__83; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:32:7: ( '|' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:32:9: '|' + { + match('|'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__83" + + // $ANTLR start "T__84" + public final void mT__84() throws RecognitionException { + try { + int _type = T__84; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:33:7: ( ')' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:33:9: ')' + { + match(')'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__84" + + // $ANTLR start "T__85" + public final void mT__85() throws RecognitionException { + try { + int _type = T__85; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:34:7: ( 'catch' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:34:9: 'catch' + { + match("catch"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__85" + + // $ANTLR start "T__86" + public final void mT__86() throws RecognitionException { + try { + int _type = T__86; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:35:7: ( 'finally' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:35:9: 'finally' + { + match("finally"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__86" + + // $ANTLR start "T__87" + public final void mT__87() throws RecognitionException { + try { + int _type = T__87; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:36:7: ( '+=' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:36:9: '+=' + { + match("+="); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__87" + + // $ANTLR start "T__88" + public final void mT__88() throws RecognitionException { + try { + int _type = T__88; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:37:7: ( '=>' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:37:9: '=>' + { + match("=>"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__88" + + // $ANTLR start "T__89" + public final void mT__89() throws RecognitionException { + try { + int _type = T__89; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:38:7: ( '~' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:38:9: '~' + { + match('~'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__89" + + // $ANTLR start "T__90" + public final void mT__90() throws RecognitionException { + try { + int _type = T__90; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:39:7: ( '?' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:39:9: '?' + { + match('?'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__90" + + // $ANTLR start "T__91" + public final void mT__91() throws RecognitionException { + try { + int _type = T__91; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:40:7: ( '+' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:40:9: '+' + { + match('+'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__91" + + // $ANTLR start "T__92" + public final void mT__92() throws RecognitionException { + try { + int _type = T__92; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:41:7: ( '.' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:41:9: '.' + { + match('.'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__92" + + // $ANTLR start "T__93" + public final void mT__93() throws RecognitionException { + try { + int _type = T__93; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:42:7: ( '$' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:42:9: '$' + { + match('$'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__93" + + // $ANTLR start "SL_COMMENT" + public final void mSL_COMMENT() throws RecognitionException { + try { + int _type = SL_COMMENT; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:465:3: ( '//' ( ' $ANTLR ' SRC | (~ ( '\\r' | '\\n' ) )* ) ( '\\r' )? '\\n' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:465:5: '//' ( ' $ANTLR ' SRC | (~ ( '\\r' | '\\n' ) )* ) ( '\\r' )? '\\n' + { + match("//"); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:466:5: ( ' $ANTLR ' SRC | (~ ( '\\r' | '\\n' ) )* ) + int alt2=2; + alt2 = dfa2.predict(input); + switch (alt2) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:466:7: ' $ANTLR ' SRC + { + match(" $ANTLR "); + + mSRC(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:467:6: (~ ( '\\r' | '\\n' ) )* + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:467:6: (~ ( '\\r' | '\\n' ) )* + loop1: + do { + int alt1=2; + int LA1_0 = input.LA(1); + + if ( ((LA1_0>='\u0000' && LA1_0<='\t')||(LA1_0>='\u000B' && LA1_0<='\f')||(LA1_0>='\u000E' && LA1_0<='\uFFFF')) ) { + alt1=1; + } + + + switch (alt1) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:467:6: ~ ( '\\r' | '\\n' ) + { + if ( (input.LA(1)>='\u0000' && input.LA(1)<='\t')||(input.LA(1)>='\u000B' && input.LA(1)<='\f')||(input.LA(1)>='\u000E' && input.LA(1)<='\uFFFF') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + break; + + default : + break loop1; + } + } while (true); + + + } + break; + + } + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:469:3: ( '\\r' )? + int alt3=2; + int LA3_0 = input.LA(1); + + if ( (LA3_0=='\r') ) { + alt3=1; + } + switch (alt3) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:469:3: '\\r' + { + match('\r'); + + } + break; + + } + + match('\n'); + _channel=HIDDEN; + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "SL_COMMENT" + + // $ANTLR start "ML_COMMENT" + public final void mML_COMMENT() throws RecognitionException { + try { + int _type = ML_COMMENT; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:474:2: ( '/*' ( . )* '*/' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:474:4: '/*' ( . )* '*/' + { + match("/*"); + + if (input.LA(1)=='*') _type=DOC_COMMENT; else _channel=HIDDEN; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:474:74: ( . )* + loop4: + do { + int alt4=2; + int LA4_0 = input.LA(1); + + if ( (LA4_0=='*') ) { + int LA4_1 = input.LA(2); + + if ( (LA4_1=='/') ) { + alt4=2; + } + else if ( ((LA4_1>='\u0000' && LA4_1<='.')||(LA4_1>='0' && LA4_1<='\uFFFF')) ) { + alt4=1; + } + + + } + else if ( ((LA4_0>='\u0000' && LA4_0<=')')||(LA4_0>='+' && LA4_0<='\uFFFF')) ) { + alt4=1; + } + + + switch (alt4) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:474:74: . + { + matchAny(); + + } + break; + + default : + break loop4; + } + } while (true); + + match("*/"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "ML_COMMENT" + + // $ANTLR start "CHAR_LITERAL" + public final void mCHAR_LITERAL() throws RecognitionException { + try { + int _type = CHAR_LITERAL; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:478:2: ( '\\'' LITERAL_CHAR '\\'' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:478:4: '\\'' LITERAL_CHAR '\\'' + { + match('\''); + mLITERAL_CHAR(); + match('\''); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "CHAR_LITERAL" + + // $ANTLR start "STRING_LITERAL" + public final void mSTRING_LITERAL() throws RecognitionException { + try { + int _type = STRING_LITERAL; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:482:2: ( '\\'' LITERAL_CHAR ( LITERAL_CHAR )* '\\'' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:482:4: '\\'' LITERAL_CHAR ( LITERAL_CHAR )* '\\'' + { + match('\''); + mLITERAL_CHAR(); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:482:22: ( LITERAL_CHAR )* + loop5: + do { + int alt5=2; + int LA5_0 = input.LA(1); + + if ( ((LA5_0>='\u0000' && LA5_0<='&')||(LA5_0>='(' && LA5_0<='\uFFFF')) ) { + alt5=1; + } + + + switch (alt5) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:482:22: LITERAL_CHAR + { + mLITERAL_CHAR(); + + } + break; + + default : + break loop5; + } + } while (true); + + match('\''); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "STRING_LITERAL" + + // $ANTLR start "LITERAL_CHAR" + public final void mLITERAL_CHAR() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:487:2: ( ESC | ~ ( '\\'' | '\\\\' ) ) + int alt6=2; + int LA6_0 = input.LA(1); + + if ( (LA6_0=='\\') ) { + alt6=1; + } + else if ( ((LA6_0>='\u0000' && LA6_0<='&')||(LA6_0>='(' && LA6_0<='[')||(LA6_0>=']' && LA6_0<='\uFFFF')) ) { + alt6=2; + } + else { + NoViableAltException nvae = + new NoViableAltException("", 6, 0, input); + + throw nvae; + } + switch (alt6) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:487:4: ESC + { + mESC(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:488:4: ~ ( '\\'' | '\\\\' ) + { + if ( (input.LA(1)>='\u0000' && input.LA(1)<='&')||(input.LA(1)>='(' && input.LA(1)<='[')||(input.LA(1)>=']' && input.LA(1)<='\uFFFF') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + break; + + } + } + finally { + } + } + // $ANTLR end "LITERAL_CHAR" + + // $ANTLR start "DOUBLE_QUOTE_STRING_LITERAL" + public final void mDOUBLE_QUOTE_STRING_LITERAL() throws RecognitionException { + try { + int _type = DOUBLE_QUOTE_STRING_LITERAL; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:492:2: ( '\"' ( ESC | ~ ( '\\\\' | '\"' ) )* '\"' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:492:4: '\"' ( ESC | ~ ( '\\\\' | '\"' ) )* '\"' + { + match('\"'); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:492:8: ( ESC | ~ ( '\\\\' | '\"' ) )* + loop7: + do { + int alt7=3; + int LA7_0 = input.LA(1); + + if ( (LA7_0=='\\') ) { + alt7=1; + } + else if ( ((LA7_0>='\u0000' && LA7_0<='!')||(LA7_0>='#' && LA7_0<='[')||(LA7_0>=']' && LA7_0<='\uFFFF')) ) { + alt7=2; + } + + + switch (alt7) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:492:9: ESC + { + mESC(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:492:15: ~ ( '\\\\' | '\"' ) + { + if ( (input.LA(1)>='\u0000' && input.LA(1)<='!')||(input.LA(1)>='#' && input.LA(1)<='[')||(input.LA(1)>=']' && input.LA(1)<='\uFFFF') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + break; + + default : + break loop7; + } + } while (true); + + match('\"'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "DOUBLE_QUOTE_STRING_LITERAL" + + // $ANTLR start "DOUBLE_ANGLE_STRING_LITERAL" + public final void mDOUBLE_ANGLE_STRING_LITERAL() throws RecognitionException { + try { + int _type = DOUBLE_ANGLE_STRING_LITERAL; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:496:2: ( '<<' ( . )* '>>' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:496:4: '<<' ( . )* '>>' + { + match("<<"); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:496:9: ( . )* + loop8: + do { + int alt8=2; + int LA8_0 = input.LA(1); + + if ( (LA8_0=='>') ) { + int LA8_1 = input.LA(2); + + if ( (LA8_1=='>') ) { + alt8=2; + } + else if ( ((LA8_1>='\u0000' && LA8_1<='=')||(LA8_1>='?' && LA8_1<='\uFFFF')) ) { + alt8=1; + } + + + } + else if ( ((LA8_0>='\u0000' && LA8_0<='=')||(LA8_0>='?' && LA8_0<='\uFFFF')) ) { + alt8=1; + } + + + switch (alt8) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:496:9: . + { + matchAny(); + + } + break; + + default : + break loop8; + } + } while (true); + + match(">>"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "DOUBLE_ANGLE_STRING_LITERAL" + + // $ANTLR start "ESC" + public final void mESC() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:500:5: ( '\\\\' ( 'n' | 'r' | 't' | 'b' | 'f' | '\"' | '\\'' | '\\\\' | '>' | 'u' XDIGIT XDIGIT XDIGIT XDIGIT | . ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:500:7: '\\\\' ( 'n' | 'r' | 't' | 'b' | 'f' | '\"' | '\\'' | '\\\\' | '>' | 'u' XDIGIT XDIGIT XDIGIT XDIGIT | . ) + { + match('\\'); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:501:3: ( 'n' | 'r' | 't' | 'b' | 'f' | '\"' | '\\'' | '\\\\' | '>' | 'u' XDIGIT XDIGIT XDIGIT XDIGIT | . ) + int alt9=11; + alt9 = dfa9.predict(input); + switch (alt9) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:501:5: 'n' + { + match('n'); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:502:5: 'r' + { + match('r'); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:503:5: 't' + { + match('t'); + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:504:5: 'b' + { + match('b'); + + } + break; + case 5 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:505:5: 'f' + { + match('f'); + + } + break; + case 6 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:506:5: '\"' + { + match('\"'); + + } + break; + case 7 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:507:5: '\\'' + { + match('\''); + + } + break; + case 8 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:508:5: '\\\\' + { + match('\\'); + + } + break; + case 9 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:509:5: '>' + { + match('>'); + + } + break; + case 10 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:510:5: 'u' XDIGIT XDIGIT XDIGIT XDIGIT + { + match('u'); + mXDIGIT(); + mXDIGIT(); + mXDIGIT(); + mXDIGIT(); + + } + break; + case 11 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:511:5: . + { + matchAny(); + + } + break; + + } + + + } + + } + finally { + } + } + // $ANTLR end "ESC" + + // $ANTLR start "XDIGIT" + public final void mXDIGIT() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:516:8: ( '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g: + { + if ( (input.LA(1)>='0' && input.LA(1)<='9')||(input.LA(1)>='A' && input.LA(1)<='F')||(input.LA(1)>='a' && input.LA(1)<='f') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + + } + finally { + } + } + // $ANTLR end "XDIGIT" + + // $ANTLR start "INT" + public final void mINT() throws RecognitionException { + try { + int _type = INT; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:522:5: ( ( '0' .. '9' )+ ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:522:7: ( '0' .. '9' )+ + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:522:7: ( '0' .. '9' )+ + int cnt10=0; + loop10: + do { + int alt10=2; + int LA10_0 = input.LA(1); + + if ( ((LA10_0>='0' && LA10_0<='9')) ) { + alt10=1; + } + + + switch (alt10) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:522:7: '0' .. '9' + { + matchRange('0','9'); + + } + break; + + default : + if ( cnt10 >= 1 ) break loop10; + EarlyExitException eee = + new EarlyExitException(10, input); + throw eee; + } + cnt10++; + } while (true); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "INT" + + // $ANTLR start "ARG_ACTION" + public final void mARG_ACTION() throws RecognitionException { + try { + int _type = ARG_ACTION; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:526:2: ( NESTED_ARG_ACTION ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:526:4: NESTED_ARG_ACTION + { + mNESTED_ARG_ACTION(); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "ARG_ACTION" + + // $ANTLR start "NESTED_ARG_ACTION" + public final void mNESTED_ARG_ACTION() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:530:19: ( '[' ( options {greedy=false; k=1; } : NESTED_ARG_ACTION | ACTION_STRING_LITERAL | ACTION_CHAR_LITERAL | . )* ']' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:531:2: '[' ( options {greedy=false; k=1; } : NESTED_ARG_ACTION | ACTION_STRING_LITERAL | ACTION_CHAR_LITERAL | . )* ']' + { + match('['); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:532:2: ( options {greedy=false; k=1; } : NESTED_ARG_ACTION | ACTION_STRING_LITERAL | ACTION_CHAR_LITERAL | . )* + loop11: + do { + int alt11=5; + int LA11_0 = input.LA(1); + + if ( (LA11_0==']') ) { + alt11=5; + } + else if ( (LA11_0=='[') ) { + alt11=1; + } + else if ( (LA11_0=='\"') ) { + alt11=2; + } + else if ( (LA11_0=='\'') ) { + alt11=3; + } + else if ( ((LA11_0>='\u0000' && LA11_0<='!')||(LA11_0>='#' && LA11_0<='&')||(LA11_0>='(' && LA11_0<='Z')||LA11_0=='\\'||(LA11_0>='^' && LA11_0<='\uFFFF')) ) { + alt11=4; + } + + + switch (alt11) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:533:4: NESTED_ARG_ACTION + { + mNESTED_ARG_ACTION(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:534:4: ACTION_STRING_LITERAL + { + mACTION_STRING_LITERAL(); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:535:4: ACTION_CHAR_LITERAL + { + mACTION_CHAR_LITERAL(); + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:536:4: . + { + matchAny(); + + } + break; + + default : + break loop11; + } + } while (true); + + match(']'); + setText(getText().substring(1, getText().length()-1)); + + } + + } + finally { + } + } + // $ANTLR end "NESTED_ARG_ACTION" + + // $ANTLR start "ACTION" + public final void mACTION() throws RecognitionException { + try { + int _type = ACTION; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:543:2: ( NESTED_ACTION ( '?' )? ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:543:4: NESTED_ACTION ( '?' )? + { + mNESTED_ACTION(); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:543:18: ( '?' )? + int alt12=2; + int LA12_0 = input.LA(1); + + if ( (LA12_0=='?') ) { + alt12=1; + } + switch (alt12) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:543:20: '?' + { + match('?'); + _type = SEMPRED; + + } + break; + + } + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "ACTION" + + // $ANTLR start "NESTED_ACTION" + public final void mNESTED_ACTION() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:547:15: ( '{' ( options {greedy=false; k=2; } : NESTED_ACTION | SL_COMMENT | ML_COMMENT | ACTION_STRING_LITERAL | ACTION_CHAR_LITERAL | . )* '}' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:548:2: '{' ( options {greedy=false; k=2; } : NESTED_ACTION | SL_COMMENT | ML_COMMENT | ACTION_STRING_LITERAL | ACTION_CHAR_LITERAL | . )* '}' + { + match('{'); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:549:2: ( options {greedy=false; k=2; } : NESTED_ACTION | SL_COMMENT | ML_COMMENT | ACTION_STRING_LITERAL | ACTION_CHAR_LITERAL | . )* + loop13: + do { + int alt13=7; + alt13 = dfa13.predict(input); + switch (alt13) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:550:4: NESTED_ACTION + { + mNESTED_ACTION(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:551:4: SL_COMMENT + { + mSL_COMMENT(); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:552:4: ML_COMMENT + { + mML_COMMENT(); + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:553:4: ACTION_STRING_LITERAL + { + mACTION_STRING_LITERAL(); + + } + break; + case 5 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:554:4: ACTION_CHAR_LITERAL + { + mACTION_CHAR_LITERAL(); + + } + break; + case 6 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:555:4: . + { + matchAny(); + + } + break; + + default : + break loop13; + } + } while (true); + + match('}'); + + } + + } + finally { + } + } + // $ANTLR end "NESTED_ACTION" + + // $ANTLR start "ACTION_CHAR_LITERAL" + public final void mACTION_CHAR_LITERAL() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:562:2: ( '\\'' ( ACTION_ESC | ~ ( '\\\\' | '\\'' ) ) '\\'' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:562:4: '\\'' ( ACTION_ESC | ~ ( '\\\\' | '\\'' ) ) '\\'' + { + match('\''); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:562:9: ( ACTION_ESC | ~ ( '\\\\' | '\\'' ) ) + int alt14=2; + int LA14_0 = input.LA(1); + + if ( (LA14_0=='\\') ) { + alt14=1; + } + else if ( ((LA14_0>='\u0000' && LA14_0<='&')||(LA14_0>='(' && LA14_0<='[')||(LA14_0>=']' && LA14_0<='\uFFFF')) ) { + alt14=2; + } + else { + NoViableAltException nvae = + new NoViableAltException("", 14, 0, input); + + throw nvae; + } + switch (alt14) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:562:10: ACTION_ESC + { + mACTION_ESC(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:562:21: ~ ( '\\\\' | '\\'' ) + { + if ( (input.LA(1)>='\u0000' && input.LA(1)<='&')||(input.LA(1)>='(' && input.LA(1)<='[')||(input.LA(1)>=']' && input.LA(1)<='\uFFFF') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + break; + + } + + match('\''); + + } + + } + finally { + } + } + // $ANTLR end "ACTION_CHAR_LITERAL" + + // $ANTLR start "ACTION_STRING_LITERAL" + public final void mACTION_STRING_LITERAL() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:567:2: ( '\"' ( ACTION_ESC | ~ ( '\\\\' | '\"' ) )* '\"' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:567:4: '\"' ( ACTION_ESC | ~ ( '\\\\' | '\"' ) )* '\"' + { + match('\"'); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:567:8: ( ACTION_ESC | ~ ( '\\\\' | '\"' ) )* + loop15: + do { + int alt15=3; + int LA15_0 = input.LA(1); + + if ( (LA15_0=='\\') ) { + alt15=1; + } + else if ( ((LA15_0>='\u0000' && LA15_0<='!')||(LA15_0>='#' && LA15_0<='[')||(LA15_0>=']' && LA15_0<='\uFFFF')) ) { + alt15=2; + } + + + switch (alt15) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:567:9: ACTION_ESC + { + mACTION_ESC(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:567:20: ~ ( '\\\\' | '\"' ) + { + if ( (input.LA(1)>='\u0000' && input.LA(1)<='!')||(input.LA(1)>='#' && input.LA(1)<='[')||(input.LA(1)>=']' && input.LA(1)<='\uFFFF') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + break; + + default : + break loop15; + } + } while (true); + + match('\"'); + + } + + } + finally { + } + } + // $ANTLR end "ACTION_STRING_LITERAL" + + // $ANTLR start "ACTION_ESC" + public final void mACTION_ESC() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:572:2: ( '\\\\\\'' | '\\\\' '\"' | '\\\\' ~ ( '\\'' | '\"' ) ) + int alt16=3; + int LA16_0 = input.LA(1); + + if ( (LA16_0=='\\') ) { + int LA16_1 = input.LA(2); + + if ( (LA16_1=='\'') ) { + alt16=1; + } + else if ( (LA16_1=='\"') ) { + alt16=2; + } + else if ( ((LA16_1>='\u0000' && LA16_1<='!')||(LA16_1>='#' && LA16_1<='&')||(LA16_1>='(' && LA16_1<='\uFFFF')) ) { + alt16=3; + } + else { + NoViableAltException nvae = + new NoViableAltException("", 16, 1, input); + + throw nvae; + } + } + else { + NoViableAltException nvae = + new NoViableAltException("", 16, 0, input); + + throw nvae; + } + switch (alt16) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:572:4: '\\\\\\'' + { + match("\\\'"); + + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:573:4: '\\\\' '\"' + { + match('\\'); + match('\"'); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:574:4: '\\\\' ~ ( '\\'' | '\"' ) + { + match('\\'); + if ( (input.LA(1)>='\u0000' && input.LA(1)<='!')||(input.LA(1)>='#' && input.LA(1)<='&')||(input.LA(1)>='(' && input.LA(1)<='\uFFFF') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + break; + + } + } + finally { + } + } + // $ANTLR end "ACTION_ESC" + + // $ANTLR start "TOKEN_REF" + public final void mTOKEN_REF() throws RecognitionException { + try { + int _type = TOKEN_REF; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:578:2: ( 'A' .. 'Z' ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )* ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:578:4: 'A' .. 'Z' ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )* + { + matchRange('A','Z'); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:578:13: ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )* + loop17: + do { + int alt17=2; + int LA17_0 = input.LA(1); + + if ( ((LA17_0>='0' && LA17_0<='9')||(LA17_0>='A' && LA17_0<='Z')||LA17_0=='_'||(LA17_0>='a' && LA17_0<='z')) ) { + alt17=1; + } + + + switch (alt17) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g: + { + if ( (input.LA(1)>='0' && input.LA(1)<='9')||(input.LA(1)>='A' && input.LA(1)<='Z')||input.LA(1)=='_'||(input.LA(1)>='a' && input.LA(1)<='z') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + break; + + default : + break loop17; + } + } while (true); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "TOKEN_REF" + + // $ANTLR start "RULE_REF" + public final void mRULE_REF() throws RecognitionException { + try { + int _type = RULE_REF; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:582:2: ( 'a' .. 'z' ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )* ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:582:4: 'a' .. 'z' ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )* + { + matchRange('a','z'); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:582:13: ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )* + loop18: + do { + int alt18=2; + int LA18_0 = input.LA(1); + + if ( ((LA18_0>='0' && LA18_0<='9')||(LA18_0>='A' && LA18_0<='Z')||LA18_0=='_'||(LA18_0>='a' && LA18_0<='z')) ) { + alt18=1; + } + + + switch (alt18) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g: + { + if ( (input.LA(1)>='0' && input.LA(1)<='9')||(input.LA(1)>='A' && input.LA(1)<='Z')||input.LA(1)=='_'||(input.LA(1)>='a' && input.LA(1)<='z') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + break; + + default : + break loop18; + } + } while (true); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "RULE_REF" + + // $ANTLR start "OPTIONS" + public final void mOPTIONS() throws RecognitionException { + try { + int _type = OPTIONS; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:589:2: ( 'options' WS_LOOP '{' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:589:4: 'options' WS_LOOP '{' + { + match("options"); + + mWS_LOOP(); + match('{'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "OPTIONS" + + // $ANTLR start "TOKENS" + public final void mTOKENS() throws RecognitionException { + try { + int _type = TOKENS; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:593:2: ( 'tokens' WS_LOOP '{' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:593:4: 'tokens' WS_LOOP '{' + { + match("tokens"); + + mWS_LOOP(); + match('{'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "TOKENS" + + // $ANTLR start "SRC" + public final void mSRC() throws RecognitionException { + try { + Token file=null; + Token line=null; + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:601:5: ( 'src' ' ' file= ACTION_STRING_LITERAL ' ' line= INT ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:601:7: 'src' ' ' file= ACTION_STRING_LITERAL ' ' line= INT + { + match("src"); + + match(' '); + int fileStart982 = getCharIndex(); + mACTION_STRING_LITERAL(); + file = new CommonToken(input, Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, fileStart982, getCharIndex()-1); + match(' '); + int lineStart988 = getCharIndex(); + mINT(); + line = new CommonToken(input, Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, lineStart988, getCharIndex()-1); + + } + + } + finally { + } + } + // $ANTLR end "SRC" + + // $ANTLR start "WS" + public final void mWS() throws RecognitionException { + try { + int _type = WS; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:604:4: ( ( ' ' | '\\t' | ( '\\r' )? '\\n' )+ ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:604:6: ( ' ' | '\\t' | ( '\\r' )? '\\n' )+ + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:604:6: ( ' ' | '\\t' | ( '\\r' )? '\\n' )+ + int cnt20=0; + loop20: + do { + int alt20=4; + switch ( input.LA(1) ) { + case ' ': + { + alt20=1; + } + break; + case '\t': + { + alt20=2; + } + break; + case '\n': + case '\r': + { + alt20=3; + } + break; + + } + + switch (alt20) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:604:8: ' ' + { + match(' '); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:605:5: '\\t' + { + match('\t'); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:606:5: ( '\\r' )? '\\n' + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:606:5: ( '\\r' )? + int alt19=2; + int LA19_0 = input.LA(1); + + if ( (LA19_0=='\r') ) { + alt19=1; + } + switch (alt19) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:606:5: '\\r' + { + match('\r'); + + } + break; + + } + + match('\n'); + + } + break; + + default : + if ( cnt20 >= 1 ) break loop20; + EarlyExitException eee = + new EarlyExitException(20, input); + throw eee; + } + cnt20++; + } while (true); + + _channel=HIDDEN; + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "WS" + + // $ANTLR start "WS_LOOP" + public final void mWS_LOOP() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:613:2: ( ( WS | SL_COMMENT | ML_COMMENT )* ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:613:4: ( WS | SL_COMMENT | ML_COMMENT )* + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:613:4: ( WS | SL_COMMENT | ML_COMMENT )* + loop21: + do { + int alt21=4; + int LA21_0 = input.LA(1); + + if ( ((LA21_0>='\t' && LA21_0<='\n')||LA21_0=='\r'||LA21_0==' ') ) { + alt21=1; + } + else if ( (LA21_0=='/') ) { + int LA21_3 = input.LA(2); + + if ( (LA21_3=='/') ) { + alt21=2; + } + else if ( (LA21_3=='*') ) { + alt21=3; + } + + + } + + + switch (alt21) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:613:6: WS + { + mWS(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:614:5: SL_COMMENT + { + mSL_COMMENT(); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:615:5: ML_COMMENT + { + mML_COMMENT(); + + } + break; + + default : + break loop21; + } + } while (true); + + + } + + } + finally { + } + } + // $ANTLR end "WS_LOOP" + + public void mTokens() throws RecognitionException { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:8: ( SCOPE | FRAGMENT | TREE_BEGIN | ROOT | BANG | RANGE | REWRITE | T__65 | T__66 | T__67 | T__68 | T__69 | T__70 | T__71 | T__72 | T__73 | T__74 | T__75 | T__76 | T__77 | T__78 | T__79 | T__80 | T__81 | T__82 | T__83 | T__84 | T__85 | T__86 | T__87 | T__88 | T__89 | T__90 | T__91 | T__92 | T__93 | SL_COMMENT | ML_COMMENT | CHAR_LITERAL | STRING_LITERAL | DOUBLE_QUOTE_STRING_LITERAL | DOUBLE_ANGLE_STRING_LITERAL | INT | ARG_ACTION | ACTION | TOKEN_REF | RULE_REF | OPTIONS | TOKENS | WS ) + int alt22=50; + alt22 = dfa22.predict(input); + switch (alt22) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:10: SCOPE + { + mSCOPE(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:16: FRAGMENT + { + mFRAGMENT(); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:25: TREE_BEGIN + { + mTREE_BEGIN(); + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:36: ROOT + { + mROOT(); + + } + break; + case 5 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:41: BANG + { + mBANG(); + + } + break; + case 6 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:46: RANGE + { + mRANGE(); + + } + break; + case 7 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:52: REWRITE + { + mREWRITE(); + + } + break; + case 8 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:60: T__65 + { + mT__65(); + + } + break; + case 9 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:66: T__66 + { + mT__66(); + + } + break; + case 10 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:72: T__67 + { + mT__67(); + + } + break; + case 11 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:78: T__68 + { + mT__68(); + + } + break; + case 12 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:84: T__69 + { + mT__69(); + + } + break; + case 13 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:90: T__70 + { + mT__70(); + + } + break; + case 14 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:96: T__71 + { + mT__71(); + + } + break; + case 15 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:102: T__72 + { + mT__72(); + + } + break; + case 16 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:108: T__73 + { + mT__73(); + + } + break; + case 17 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:114: T__74 + { + mT__74(); + + } + break; + case 18 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:120: T__75 + { + mT__75(); + + } + break; + case 19 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:126: T__76 + { + mT__76(); + + } + break; + case 20 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:132: T__77 + { + mT__77(); + + } + break; + case 21 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:138: T__78 + { + mT__78(); + + } + break; + case 22 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:144: T__79 + { + mT__79(); + + } + break; + case 23 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:150: T__80 + { + mT__80(); + + } + break; + case 24 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:156: T__81 + { + mT__81(); + + } + break; + case 25 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:162: T__82 + { + mT__82(); + + } + break; + case 26 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:168: T__83 + { + mT__83(); + + } + break; + case 27 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:174: T__84 + { + mT__84(); + + } + break; + case 28 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:180: T__85 + { + mT__85(); + + } + break; + case 29 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:186: T__86 + { + mT__86(); + + } + break; + case 30 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:192: T__87 + { + mT__87(); + + } + break; + case 31 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:198: T__88 + { + mT__88(); + + } + break; + case 32 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:204: T__89 + { + mT__89(); + + } + break; + case 33 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:210: T__90 + { + mT__90(); + + } + break; + case 34 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:216: T__91 + { + mT__91(); + + } + break; + case 35 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:222: T__92 + { + mT__92(); + + } + break; + case 36 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:228: T__93 + { + mT__93(); + + } + break; + case 37 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:234: SL_COMMENT + { + mSL_COMMENT(); + + } + break; + case 38 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:245: ML_COMMENT + { + mML_COMMENT(); + + } + break; + case 39 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:256: CHAR_LITERAL + { + mCHAR_LITERAL(); + + } + break; + case 40 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:269: STRING_LITERAL + { + mSTRING_LITERAL(); + + } + break; + case 41 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:284: DOUBLE_QUOTE_STRING_LITERAL + { + mDOUBLE_QUOTE_STRING_LITERAL(); + + } + break; + case 42 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:312: DOUBLE_ANGLE_STRING_LITERAL + { + mDOUBLE_ANGLE_STRING_LITERAL(); + + } + break; + case 43 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:340: INT + { + mINT(); + + } + break; + case 44 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:344: ARG_ACTION + { + mARG_ACTION(); + + } + break; + case 45 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:355: ACTION + { + mACTION(); + + } + break; + case 46 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:362: TOKEN_REF + { + mTOKEN_REF(); + + } + break; + case 47 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:372: RULE_REF + { + mRULE_REF(); + + } + break; + case 48 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:381: OPTIONS + { + mOPTIONS(); + + } + break; + case 49 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:389: TOKENS + { + mTOKENS(); + + } + break; + case 50 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:1:396: WS + { + mWS(); + + } + break; + + } + + } + + + protected DFA2 dfa2 = new DFA2(this); + protected DFA9 dfa9 = new DFA9(this); + protected DFA13 dfa13 = new DFA13(this); + protected DFA22 dfa22 = new DFA22(this); + static final String DFA2_eotS = + "\22\uffff\1\2\4\uffff\1\2\4\uffff"; + static final String DFA2_eofS = + "\34\uffff"; + static final String DFA2_minS = + "\2\0\1\uffff\26\0\1\uffff\1\0\1\uffff"; + static final String DFA2_maxS = + "\2\uffff\1\uffff\26\uffff\1\uffff\1\uffff\1\uffff"; + static final String DFA2_acceptS = + "\2\uffff\1\2\26\uffff\1\1\1\uffff\1\1"; + static final String DFA2_specialS = + "\1\5\1\13\1\uffff\1\0\1\2\1\6\1\26\1\12\1\24\1\15\1\16\1\25\1\17"+ + "\1\4\1\20\1\22\1\3\1\1\1\10\1\21\1\7\1\27\1\30\1\14\1\11\1\uffff"+ + "\1\23\1\uffff}>"; + static final String[] DFA2_transitionS = { + "\40\2\1\1\uffdf\2", + "\44\2\1\3\uffdb\2", + "", + "\101\2\1\4\uffbe\2", + "\116\2\1\5\uffb1\2", + "\124\2\1\6\uffab\2", + "\114\2\1\7\uffb3\2", + "\122\2\1\10\uffad\2", + "\40\2\1\11\uffdf\2", + "\163\2\1\12\uff8c\2", + "\162\2\1\13\uff8d\2", + "\143\2\1\14\uff9c\2", + "\40\2\1\15\uffdf\2", + "\42\2\1\16\uffdd\2", + "\12\23\1\22\2\23\1\20\24\23\1\21\71\23\1\17\uffa3\23", + "\12\30\1\27\2\30\1\26\24\30\1\25\4\30\1\24\uffd8\30", + "\12\31\1\22\ufff5\31", + "\40\2\1\32\uffdf\2", + "\0\31", + "\12\23\1\22\2\23\1\20\24\23\1\21\71\23\1\17\uffa3\23", + "\12\23\1\22\2\23\1\20\24\23\1\21\71\23\1\17\uffa3\23", + "\12\23\1\22\2\23\1\20\24\23\1\21\71\23\1\17\uffa3\23", + "\12\31\1\22\ufff5\31", + "\0\31", + "\12\23\1\22\2\23\1\20\24\23\1\21\71\23\1\17\uffa3\23", + "", + "\60\2\12\33\uffc6\2", + "" + }; + + static final short[] DFA2_eot = DFA.unpackEncodedString(DFA2_eotS); + static final short[] DFA2_eof = DFA.unpackEncodedString(DFA2_eofS); + static final char[] DFA2_min = DFA.unpackEncodedStringToUnsignedChars(DFA2_minS); + static final char[] DFA2_max = DFA.unpackEncodedStringToUnsignedChars(DFA2_maxS); + static final short[] DFA2_accept = DFA.unpackEncodedString(DFA2_acceptS); + static final short[] DFA2_special = DFA.unpackEncodedString(DFA2_specialS); + static final short[][] DFA2_transition; + + static { + int numStates = DFA2_transitionS.length; + DFA2_transition = new short[numStates][]; + for (int i=0; i='\u0000' && LA2_3<='@')||(LA2_3>='B' && LA2_3<='\uFFFF')) ) {s = 2;} + + if ( s>=0 ) return s; + break; + case 1 : + int LA2_17 = input.LA(1); + + s = -1; + if ( ((LA2_17>='\u0000' && LA2_17<='\u001F')||(LA2_17>='!' && LA2_17<='\uFFFF')) ) {s = 2;} + + else if ( (LA2_17==' ') ) {s = 26;} + + if ( s>=0 ) return s; + break; + case 2 : + int LA2_4 = input.LA(1); + + s = -1; + if ( (LA2_4=='N') ) {s = 5;} + + else if ( ((LA2_4>='\u0000' && LA2_4<='M')||(LA2_4>='O' && LA2_4<='\uFFFF')) ) {s = 2;} + + if ( s>=0 ) return s; + break; + case 3 : + int LA2_16 = input.LA(1); + + s = -1; + if ( ((LA2_16>='\u0000' && LA2_16<='\t')||(LA2_16>='\u000B' && LA2_16<='\uFFFF')) ) {s = 25;} + + else if ( (LA2_16=='\n') ) {s = 18;} + + if ( s>=0 ) return s; + break; + case 4 : + int LA2_13 = input.LA(1); + + s = -1; + if ( ((LA2_13>='\u0000' && LA2_13<='!')||(LA2_13>='#' && LA2_13<='\uFFFF')) ) {s = 2;} + + else if ( (LA2_13=='\"') ) {s = 14;} + + if ( s>=0 ) return s; + break; + case 5 : + int LA2_0 = input.LA(1); + + s = -1; + if ( (LA2_0==' ') ) {s = 1;} + + else if ( ((LA2_0>='\u0000' && LA2_0<='\u001F')||(LA2_0>='!' && LA2_0<='\uFFFF')) ) {s = 2;} + + if ( s>=0 ) return s; + break; + case 6 : + int LA2_5 = input.LA(1); + + s = -1; + if ( (LA2_5=='T') ) {s = 6;} + + else if ( ((LA2_5>='\u0000' && LA2_5<='S')||(LA2_5>='U' && LA2_5<='\uFFFF')) ) {s = 2;} + + if ( s>=0 ) return s; + break; + case 7 : + int LA2_20 = input.LA(1); + + s = -1; + if ( (LA2_20=='\r') ) {s = 16;} + + else if ( (LA2_20=='\n') ) {s = 18;} + + else if ( (LA2_20=='\"') ) {s = 17;} + + else if ( (LA2_20=='\\') ) {s = 15;} + + else if ( ((LA2_20>='\u0000' && LA2_20<='\t')||(LA2_20>='\u000B' && LA2_20<='\f')||(LA2_20>='\u000E' && LA2_20<='!')||(LA2_20>='#' && LA2_20<='[')||(LA2_20>=']' && LA2_20<='\uFFFF')) ) {s = 19;} + + if ( s>=0 ) return s; + break; + case 8 : + int LA2_18 = input.LA(1); + + s = -1; + if ( ((LA2_18>='\u0000' && LA2_18<='\uFFFF')) ) {s = 25;} + + else s = 2; + + if ( s>=0 ) return s; + break; + case 9 : + int LA2_24 = input.LA(1); + + s = -1; + if ( (LA2_24=='\r') ) {s = 16;} + + else if ( (LA2_24=='\n') ) {s = 18;} + + else if ( (LA2_24=='\"') ) {s = 17;} + + else if ( (LA2_24=='\\') ) {s = 15;} + + else if ( ((LA2_24>='\u0000' && LA2_24<='\t')||(LA2_24>='\u000B' && LA2_24<='\f')||(LA2_24>='\u000E' && LA2_24<='!')||(LA2_24>='#' && LA2_24<='[')||(LA2_24>=']' && LA2_24<='\uFFFF')) ) {s = 19;} + + if ( s>=0 ) return s; + break; + case 10 : + int LA2_7 = input.LA(1); + + s = -1; + if ( (LA2_7=='R') ) {s = 8;} + + else if ( ((LA2_7>='\u0000' && LA2_7<='Q')||(LA2_7>='S' && LA2_7<='\uFFFF')) ) {s = 2;} + + if ( s>=0 ) return s; + break; + case 11 : + int LA2_1 = input.LA(1); + + s = -1; + if ( (LA2_1=='$') ) {s = 3;} + + else if ( ((LA2_1>='\u0000' && LA2_1<='#')||(LA2_1>='%' && LA2_1<='\uFFFF')) ) {s = 2;} + + if ( s>=0 ) return s; + break; + case 12 : + int LA2_23 = input.LA(1); + + s = -1; + if ( ((LA2_23>='\u0000' && LA2_23<='\uFFFF')) ) {s = 25;} + + else s = 2; + + if ( s>=0 ) return s; + break; + case 13 : + int LA2_9 = input.LA(1); + + s = -1; + if ( (LA2_9=='s') ) {s = 10;} + + else if ( ((LA2_9>='\u0000' && LA2_9<='r')||(LA2_9>='t' && LA2_9<='\uFFFF')) ) {s = 2;} + + if ( s>=0 ) return s; + break; + case 14 : + int LA2_10 = input.LA(1); + + s = -1; + if ( (LA2_10=='r') ) {s = 11;} + + else if ( ((LA2_10>='\u0000' && LA2_10<='q')||(LA2_10>='s' && LA2_10<='\uFFFF')) ) {s = 2;} + + if ( s>=0 ) return s; + break; + case 15 : + int LA2_12 = input.LA(1); + + s = -1; + if ( (LA2_12==' ') ) {s = 13;} + + else if ( ((LA2_12>='\u0000' && LA2_12<='\u001F')||(LA2_12>='!' && LA2_12<='\uFFFF')) ) {s = 2;} + + if ( s>=0 ) return s; + break; + case 16 : + int LA2_14 = input.LA(1); + + s = -1; + if ( (LA2_14=='\\') ) {s = 15;} + + else if ( (LA2_14=='\r') ) {s = 16;} + + else if ( (LA2_14=='\"') ) {s = 17;} + + else if ( (LA2_14=='\n') ) {s = 18;} + + else if ( ((LA2_14>='\u0000' && LA2_14<='\t')||(LA2_14>='\u000B' && LA2_14<='\f')||(LA2_14>='\u000E' && LA2_14<='!')||(LA2_14>='#' && LA2_14<='[')||(LA2_14>=']' && LA2_14<='\uFFFF')) ) {s = 19;} + + if ( s>=0 ) return s; + break; + case 17 : + int LA2_19 = input.LA(1); + + s = -1; + if ( (LA2_19=='\r') ) {s = 16;} + + else if ( (LA2_19=='\n') ) {s = 18;} + + else if ( (LA2_19=='\"') ) {s = 17;} + + else if ( (LA2_19=='\\') ) {s = 15;} + + else if ( ((LA2_19>='\u0000' && LA2_19<='\t')||(LA2_19>='\u000B' && LA2_19<='\f')||(LA2_19>='\u000E' && LA2_19<='!')||(LA2_19>='#' && LA2_19<='[')||(LA2_19>=']' && LA2_19<='\uFFFF')) ) {s = 19;} + + if ( s>=0 ) return s; + break; + case 18 : + int LA2_15 = input.LA(1); + + s = -1; + if ( (LA2_15=='\'') ) {s = 20;} + + else if ( (LA2_15=='\"') ) {s = 21;} + + else if ( (LA2_15=='\r') ) {s = 22;} + + else if ( (LA2_15=='\n') ) {s = 23;} + + else if ( ((LA2_15>='\u0000' && LA2_15<='\t')||(LA2_15>='\u000B' && LA2_15<='\f')||(LA2_15>='\u000E' && LA2_15<='!')||(LA2_15>='#' && LA2_15<='&')||(LA2_15>='(' && LA2_15<='\uFFFF')) ) {s = 24;} + + if ( s>=0 ) return s; + break; + case 19 : + int LA2_26 = input.LA(1); + + s = -1; + if ( ((LA2_26>='0' && LA2_26<='9')) ) {s = 27;} + + else if ( ((LA2_26>='\u0000' && LA2_26<='/')||(LA2_26>=':' && LA2_26<='\uFFFF')) ) {s = 2;} + + if ( s>=0 ) return s; + break; + case 20 : + int LA2_8 = input.LA(1); + + s = -1; + if ( (LA2_8==' ') ) {s = 9;} + + else if ( ((LA2_8>='\u0000' && LA2_8<='\u001F')||(LA2_8>='!' && LA2_8<='\uFFFF')) ) {s = 2;} + + if ( s>=0 ) return s; + break; + case 21 : + int LA2_11 = input.LA(1); + + s = -1; + if ( (LA2_11=='c') ) {s = 12;} + + else if ( ((LA2_11>='\u0000' && LA2_11<='b')||(LA2_11>='d' && LA2_11<='\uFFFF')) ) {s = 2;} + + if ( s>=0 ) return s; + break; + case 22 : + int LA2_6 = input.LA(1); + + s = -1; + if ( (LA2_6=='L') ) {s = 7;} + + else if ( ((LA2_6>='\u0000' && LA2_6<='K')||(LA2_6>='M' && LA2_6<='\uFFFF')) ) {s = 2;} + + if ( s>=0 ) return s; + break; + case 23 : + int LA2_21 = input.LA(1); + + s = -1; + if ( (LA2_21=='\"') ) {s = 17;} + + else if ( (LA2_21=='\\') ) {s = 15;} + + else if ( (LA2_21=='\r') ) {s = 16;} + + else if ( (LA2_21=='\n') ) {s = 18;} + + else if ( ((LA2_21>='\u0000' && LA2_21<='\t')||(LA2_21>='\u000B' && LA2_21<='\f')||(LA2_21>='\u000E' && LA2_21<='!')||(LA2_21>='#' && LA2_21<='[')||(LA2_21>=']' && LA2_21<='\uFFFF')) ) {s = 19;} + + if ( s>=0 ) return s; + break; + case 24 : + int LA2_22 = input.LA(1); + + s = -1; + if ( ((LA2_22>='\u0000' && LA2_22<='\t')||(LA2_22>='\u000B' && LA2_22<='\uFFFF')) ) {s = 25;} + + else if ( (LA2_22=='\n') ) {s = 18;} + + if ( s>=0 ) return s; + break; + } + NoViableAltException nvae = + new NoViableAltException(getDescription(), 2, _s, input); + error(nvae); + throw nvae; + } + } + static final String DFA9_eotS = + "\12\uffff\1\13\2\uffff"; + static final String DFA9_eofS = + "\15\uffff"; + static final String DFA9_minS = + "\1\0\11\uffff\1\60\2\uffff"; + static final String DFA9_maxS = + "\1\uffff\11\uffff\1\146\2\uffff"; + static final String DFA9_acceptS = + "\1\uffff\1\1\1\2\1\3\1\4\1\5\1\6\1\7\1\10\1\11\1\uffff\1\13\1\12"; + static final String DFA9_specialS = + "\1\0\14\uffff}>"; + static final String[] DFA9_transitionS = { + "\42\13\1\6\4\13\1\7\26\13\1\11\35\13\1\10\5\13\1\4\3\13\1\5"+ + "\7\13\1\1\3\13\1\2\1\13\1\3\1\12\uff8a\13", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "\12\14\7\uffff\6\14\32\uffff\6\14", + "", + "" + }; + + static final short[] DFA9_eot = DFA.unpackEncodedString(DFA9_eotS); + static final short[] DFA9_eof = DFA.unpackEncodedString(DFA9_eofS); + static final char[] DFA9_min = DFA.unpackEncodedStringToUnsignedChars(DFA9_minS); + static final char[] DFA9_max = DFA.unpackEncodedStringToUnsignedChars(DFA9_maxS); + static final short[] DFA9_accept = DFA.unpackEncodedString(DFA9_acceptS); + static final short[] DFA9_special = DFA.unpackEncodedString(DFA9_specialS); + static final short[][] DFA9_transition; + + static { + int numStates = DFA9_transitionS.length; + DFA9_transition = new short[numStates][]; + for (int i=0; i' | 'u' XDIGIT XDIGIT XDIGIT XDIGIT | . )"; + } + public int specialStateTransition(int s, IntStream _input) throws NoViableAltException { + IntStream input = _input; + int _s = s; + switch ( s ) { + case 0 : + int LA9_0 = input.LA(1); + + s = -1; + if ( (LA9_0=='n') ) {s = 1;} + + else if ( (LA9_0=='r') ) {s = 2;} + + else if ( (LA9_0=='t') ) {s = 3;} + + else if ( (LA9_0=='b') ) {s = 4;} + + else if ( (LA9_0=='f') ) {s = 5;} + + else if ( (LA9_0=='\"') ) {s = 6;} + + else if ( (LA9_0=='\'') ) {s = 7;} + + else if ( (LA9_0=='\\') ) {s = 8;} + + else if ( (LA9_0=='>') ) {s = 9;} + + else if ( (LA9_0=='u') ) {s = 10;} + + else if ( ((LA9_0>='\u0000' && LA9_0<='!')||(LA9_0>='#' && LA9_0<='&')||(LA9_0>='(' && LA9_0<='=')||(LA9_0>='?' && LA9_0<='[')||(LA9_0>=']' && LA9_0<='a')||(LA9_0>='c' && LA9_0<='e')||(LA9_0>='g' && LA9_0<='m')||(LA9_0>='o' && LA9_0<='q')||LA9_0=='s'||(LA9_0>='v' && LA9_0<='\uFFFF')) ) {s = 11;} + + if ( s>=0 ) return s; + break; + } + NoViableAltException nvae = + new NoViableAltException(getDescription(), 9, _s, input); + error(nvae); + throw nvae; + } + } + static final String DFA13_eotS = + "\34\uffff"; + static final String DFA13_eofS = + "\34\uffff"; + static final String DFA13_minS = + "\1\0\2\uffff\3\0\26\uffff"; + static final String DFA13_maxS = + "\1\uffff\2\uffff\3\uffff\26\uffff"; + static final String DFA13_acceptS = + "\1\uffff\1\7\1\1\3\uffff\1\6\1\2\1\3\5\uffff\7\4\6\5\1\uffff"; + static final String DFA13_specialS = + "\1\0\2\uffff\1\1\1\2\1\3\26\uffff}>"; + static final String[] DFA13_transitionS = { + "\42\6\1\4\4\6\1\5\7\6\1\3\113\6\1\2\1\6\1\1\uff82\6", + "", + "", + "\52\6\1\10\4\6\1\7\uffd0\6", + "\42\24\1\20\4\24\1\23\7\24\1\22\54\24\1\16\36\24\1\21\1\24"+ + "\1\17\uff82\24", + "\42\32\1\31\4\32\1\6\7\32\1\30\54\32\1\25\36\32\1\27\1\32\1"+ + "\26\uff82\32", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "" + }; + + static final short[] DFA13_eot = DFA.unpackEncodedString(DFA13_eotS); + static final short[] DFA13_eof = DFA.unpackEncodedString(DFA13_eofS); + static final char[] DFA13_min = DFA.unpackEncodedStringToUnsignedChars(DFA13_minS); + static final char[] DFA13_max = DFA.unpackEncodedStringToUnsignedChars(DFA13_maxS); + static final short[] DFA13_accept = DFA.unpackEncodedString(DFA13_acceptS); + static final short[] DFA13_special = DFA.unpackEncodedString(DFA13_specialS); + static final short[][] DFA13_transition; + + static { + int numStates = DFA13_transitionS.length; + DFA13_transition = new short[numStates][]; + for (int i=0; i='\u0000' && LA13_0<='!')||(LA13_0>='#' && LA13_0<='&')||(LA13_0>='(' && LA13_0<='.')||(LA13_0>='0' && LA13_0<='z')||LA13_0=='|'||(LA13_0>='~' && LA13_0<='\uFFFF')) ) {s = 6;} + + if ( s>=0 ) return s; + break; + case 1 : + int LA13_3 = input.LA(1); + + s = -1; + if ( (LA13_3=='/') ) {s = 7;} + + else if ( (LA13_3=='*') ) {s = 8;} + + else if ( ((LA13_3>='\u0000' && LA13_3<=')')||(LA13_3>='+' && LA13_3<='.')||(LA13_3>='0' && LA13_3<='\uFFFF')) ) {s = 6;} + + if ( s>=0 ) return s; + break; + case 2 : + int LA13_4 = input.LA(1); + + s = -1; + if ( (LA13_4=='\\') ) {s = 14;} + + else if ( (LA13_4=='}') ) {s = 15;} + + else if ( (LA13_4=='\"') ) {s = 16;} + + else if ( (LA13_4=='{') ) {s = 17;} + + else if ( (LA13_4=='/') ) {s = 18;} + + else if ( (LA13_4=='\'') ) {s = 19;} + + else if ( ((LA13_4>='\u0000' && LA13_4<='!')||(LA13_4>='#' && LA13_4<='&')||(LA13_4>='(' && LA13_4<='.')||(LA13_4>='0' && LA13_4<='[')||(LA13_4>=']' && LA13_4<='z')||LA13_4=='|'||(LA13_4>='~' && LA13_4<='\uFFFF')) ) {s = 20;} + + if ( s>=0 ) return s; + break; + case 3 : + int LA13_5 = input.LA(1); + + s = -1; + if ( (LA13_5=='\\') ) {s = 21;} + + else if ( (LA13_5=='}') ) {s = 22;} + + else if ( (LA13_5=='{') ) {s = 23;} + + else if ( (LA13_5=='/') ) {s = 24;} + + else if ( (LA13_5=='\"') ) {s = 25;} + + else if ( ((LA13_5>='\u0000' && LA13_5<='!')||(LA13_5>='#' && LA13_5<='&')||(LA13_5>='(' && LA13_5<='.')||(LA13_5>='0' && LA13_5<='[')||(LA13_5>=']' && LA13_5<='z')||LA13_5=='|'||(LA13_5>='~' && LA13_5<='\uFFFF')) ) {s = 26;} + + else if ( (LA13_5=='\'') ) {s = 6;} + + if ( s>=0 ) return s; + break; + } + NoViableAltException nvae = + new NoViableAltException(getDescription(), 13, _s, input); + error(nvae); + throw nvae; + } + } + static final String DFA22_eotS = + "\1\uffff\2\44\1\52\1\uffff\1\54\1\uffff\4\44\2\uffff\1\66\1\uffff"+ + "\1\70\1\uffff\1\44\4\uffff\1\44\1\74\13\uffff\1\44\2\uffff\3\44"+ + "\4\uffff\10\44\4\uffff\2\44\6\uffff\17\44\15\uffff\11\44\1\167\5"+ + "\44\2\uffff\1\44\1\177\2\44\1\u0082\4\44\1\uffff\4\44\1\u008b\1"+ + "\uffff\1\44\1\uffff\2\44\1\uffff\1\u0090\2\44\1\u0093\1\u0094\3"+ + "\44\2\uffff\2\44\1\u009b\1\uffff\1\44\1\u009d\3\uffff\1\u009e\1"+ + "\u009f\1\uffff\1\44\1\u00a1\1\uffff\1\44\5\uffff\1\u00a3\1\uffff"; + static final String DFA22_eofS = + "\u00a4\uffff"; + static final String DFA22_minS = + "\1\11\1\143\1\151\1\50\1\uffff\1\56\1\uffff\1\145\1\141\1\150\1"+ + "\162\2\uffff\1\76\1\uffff\1\72\1\uffff\1\145\4\uffff\1\141\1\75"+ + "\3\uffff\1\52\1\0\6\uffff\1\160\2\uffff\1\157\1\141\1\156\4\uffff"+ + "\1\170\1\162\1\151\1\142\1\145\1\162\1\153\1\141\4\uffff\2\164\4"+ + "\uffff\2\0\1\164\1\160\1\147\1\141\1\145\1\163\1\164\1\166\1\154"+ + "\1\145\1\157\1\145\1\155\1\165\1\143\13\0\2\uffff\1\151\1\145\1"+ + "\155\1\154\1\162\2\145\1\141\1\151\1\60\1\167\1\156\1\155\1\162"+ + "\1\150\1\0\1\uffff\1\157\1\60\1\145\1\154\1\60\1\162\1\143\1\164"+ + "\1\143\1\uffff\2\163\1\141\1\156\1\60\1\0\1\156\1\uffff\1\156\1"+ + "\171\1\uffff\1\60\1\164\1\145\2\60\1\11\1\162\1\163\1\uffff\1\0"+ + "\1\163\1\164\1\60\1\uffff\1\145\1\60\3\uffff\2\60\1\0\1\11\1\60"+ + "\1\uffff\1\144\5\uffff\1\60\1\uffff"; + static final String DFA22_maxS = + "\1\176\1\143\1\162\1\50\1\uffff\1\56\1\uffff\1\145\1\165\2\162\2"+ + "\uffff\1\76\1\uffff\1\72\1\uffff\1\145\4\uffff\1\141\1\75\3\uffff"+ + "\1\57\1\uffff\6\uffff\1\160\2\uffff\1\157\1\141\1\156\4\uffff\1"+ + "\170\1\162\1\157\1\142\1\145\1\162\1\153\1\141\4\uffff\2\164\4\uffff"+ + "\2\uffff\1\164\1\160\1\147\1\141\1\145\1\163\1\164\1\166\1\154\1"+ + "\145\1\157\1\145\1\155\1\165\1\143\13\uffff\2\uffff\1\151\1\145"+ + "\1\155\1\154\1\162\2\145\1\141\1\151\1\172\1\167\1\156\1\155\1\162"+ + "\1\150\1\uffff\1\uffff\1\157\1\172\1\145\1\154\1\172\1\162\1\143"+ + "\1\164\1\143\1\uffff\2\163\1\141\1\156\1\172\1\uffff\1\156\1\uffff"+ + "\1\156\1\171\1\uffff\1\172\1\164\1\145\2\172\1\173\1\162\1\163\1"+ + "\uffff\1\uffff\1\163\1\164\1\172\1\uffff\1\145\1\172\3\uffff\2\172"+ + "\1\uffff\1\173\1\172\1\uffff\1\144\5\uffff\1\172\1\uffff"; + static final String DFA22_acceptS = + "\4\uffff\1\5\1\uffff\1\7\4\uffff\1\14\1\15\1\uffff\1\17\1\uffff"+ + "\1\21\1\uffff\1\30\1\31\1\32\1\33\2\uffff\1\40\1\41\1\44\2\uffff"+ + "\1\51\1\52\1\53\1\54\1\55\1\56\1\uffff\1\57\1\62\3\uffff\1\3\1\4"+ + "\1\6\1\43\10\uffff\1\37\1\16\1\20\1\26\2\uffff\1\36\1\42\1\45\1"+ + "\46\34\uffff\1\50\1\47\20\uffff\1\47\11\uffff\1\12\7\uffff\1\1\2"+ + "\uffff\1\10\10\uffff\1\34\4\uffff\1\11\2\uffff\1\23\1\27\1\61\5"+ + "\uffff\1\35\1\uffff\1\24\1\13\1\25\1\60\1\2\1\uffff\1\22"; + static final String DFA22_specialS = + "\34\uffff\1\5\42\uffff\1\12\1\20\17\uffff\1\13\1\3\1\14\1\6\1\11"+ + "\1\2\1\21\1\16\1\15\1\4\1\7\21\uffff\1\17\20\uffff\1\0\16\uffff"+ + "\1\10\13\uffff\1\1\13\uffff}>"; + static final String[] DFA22_transitionS = { + "\2\45\2\uffff\1\45\22\uffff\1\45\1\4\1\35\1\uffff\1\32\2\uffff"+ + "\1\34\1\23\1\25\1\20\1\27\1\22\1\6\1\5\1\33\12\37\1\17\1\13"+ + "\1\36\1\15\1\uffff\1\31\1\16\32\42\1\40\2\uffff\1\3\2\uffff"+ + "\2\44\1\26\2\44\1\2\1\12\4\44\1\7\2\44\1\43\1\10\1\44\1\21\1"+ + "\1\1\11\6\44\1\41\1\24\1\14\1\30", + "\1\46", + "\1\50\10\uffff\1\47", + "\1\51", + "", + "\1\53", + "", + "\1\55", + "\1\56\20\uffff\1\57\2\uffff\1\60", + "\1\62\6\uffff\1\63\2\uffff\1\61", + "\1\64", + "", + "", + "\1\65", + "", + "\1\67", + "", + "\1\71", + "", + "", + "", + "", + "\1\72", + "\1\73", + "", + "", + "", + "\1\76\4\uffff\1\75", + "\47\100\1\uffff\64\100\1\77\uffa3\100", + "", + "", + "", + "", + "", + "", + "\1\101", + "", + "", + "\1\102", + "\1\103", + "\1\104", + "", + "", + "", + "", + "\1\105", + "\1\106", + "\1\110\5\uffff\1\107", + "\1\111", + "\1\112", + "\1\113", + "\1\114", + "\1\115", + "", + "", + "", + "", + "\1\116", + "\1\117", + "", + "", + "", + "", + "\42\132\1\125\4\132\1\126\26\132\1\130\35\132\1\127\5\132\1"+ + "\123\3\132\1\124\7\132\1\120\3\132\1\121\1\132\1\122\1\131\uff8a"+ + "\132", + "\47\133\1\134\uffd8\133", + "\1\135", + "\1\136", + "\1\137", + "\1\140", + "\1\141", + "\1\142", + "\1\143", + "\1\144", + "\1\145", + "\1\146", + "\1\147", + "\1\150", + "\1\151", + "\1\152", + "\1\153", + "\47\133\1\134\uffd8\133", + "\47\133\1\134\uffd8\133", + "\47\133\1\134\uffd8\133", + "\47\133\1\134\uffd8\133", + "\47\133\1\134\uffd8\133", + "\47\133\1\134\uffd8\133", + "\47\133\1\134\uffd8\133", + "\47\133\1\134\uffd8\133", + "\47\133\1\134\uffd8\133", + "\47\133\1\134\10\133\12\154\7\133\6\154\32\133\6\154\uff99"+ + "\133", + "\47\133\1\134\uffd8\133", + "", + "", + "\1\156", + "\1\157", + "\1\160", + "\1\161", + "\1\162", + "\1\163", + "\1\164", + "\1\165", + "\1\166", + "\12\44\7\uffff\32\44\4\uffff\1\44\1\uffff\32\44", + "\1\170", + "\1\171", + "\1\172", + "\1\173", + "\1\174", + "\60\133\12\175\7\133\6\175\32\133\6\175\uff99\133", + "", + "\1\176", + "\12\44\7\uffff\32\44\4\uffff\1\44\1\uffff\32\44", + "\1\u0080", + "\1\u0081", + "\12\44\7\uffff\32\44\4\uffff\1\44\1\uffff\32\44", + "\1\u0083", + "\1\u0084", + "\1\u0085", + "\1\u0086", + "", + "\1\u0087", + "\1\u0088", + "\1\u0089", + "\1\u008a", + "\12\44\7\uffff\32\44\4\uffff\1\44\1\uffff\32\44", + "\60\133\12\u008c\7\133\6\u008c\32\133\6\u008c\uff99\133", + "\1\u008d", + "", + "\1\u008e", + "\1\u008f", + "", + "\12\44\7\uffff\32\44\4\uffff\1\44\1\uffff\32\44", + "\1\u0091", + "\1\u0092", + "\12\44\7\uffff\32\44\4\uffff\1\44\1\uffff\32\44", + "\12\44\7\uffff\32\44\4\uffff\1\44\1\uffff\32\44", + "\2\u0095\2\uffff\1\u0095\22\uffff\1\u0095\16\uffff\1\u0095"+ + "\113\uffff\1\u0095", + "\1\u0096", + "\1\u0097", + "", + "\60\133\12\u0098\7\133\6\u0098\32\133\6\u0098\uff99\133", + "\1\u0099", + "\1\u009a", + "\12\44\7\uffff\32\44\4\uffff\1\44\1\uffff\32\44", + "", + "\1\u009c", + "\12\44\7\uffff\32\44\4\uffff\1\44\1\uffff\32\44", + "", + "", + "", + "\12\44\7\uffff\32\44\4\uffff\1\44\1\uffff\32\44", + "\12\44\7\uffff\32\44\4\uffff\1\44\1\uffff\32\44", + "\47\133\1\134\uffd8\133", + "\2\u00a0\2\uffff\1\u00a0\22\uffff\1\u00a0\16\uffff\1\u00a0"+ + "\113\uffff\1\u00a0", + "\12\44\7\uffff\32\44\4\uffff\1\44\1\uffff\32\44", + "", + "\1\u00a2", + "", + "", + "", + "", + "", + "\12\44\7\uffff\32\44\4\uffff\1\44\1\uffff\32\44", + "" + }; + + static final short[] DFA22_eot = DFA.unpackEncodedString(DFA22_eotS); + static final short[] DFA22_eof = DFA.unpackEncodedString(DFA22_eofS); + static final char[] DFA22_min = DFA.unpackEncodedStringToUnsignedChars(DFA22_minS); + static final char[] DFA22_max = DFA.unpackEncodedStringToUnsignedChars(DFA22_maxS); + static final short[] DFA22_accept = DFA.unpackEncodedString(DFA22_acceptS); + static final short[] DFA22_special = DFA.unpackEncodedString(DFA22_specialS); + static final short[][] DFA22_transition; + + static { + int numStates = DFA22_transitionS.length; + DFA22_transition = new short[numStates][]; + for (int i=0; i='0' && LA22_125<='9')||(LA22_125>='A' && LA22_125<='F')||(LA22_125>='a' && LA22_125<='f')) ) {s = 140;} + + else if ( ((LA22_125>='\u0000' && LA22_125<='/')||(LA22_125>=':' && LA22_125<='@')||(LA22_125>='G' && LA22_125<='`')||(LA22_125>='g' && LA22_125<='\uFFFF')) ) {s = 91;} + + if ( s>=0 ) return s; + break; + case 1 : + int LA22_152 = input.LA(1); + + s = -1; + if ( (LA22_152=='\'') ) {s = 92;} + + else if ( ((LA22_152>='\u0000' && LA22_152<='&')||(LA22_152>='(' && LA22_152<='\uFFFF')) ) {s = 91;} + + if ( s>=0 ) return s; + break; + case 2 : + int LA22_85 = input.LA(1); + + s = -1; + if ( (LA22_85=='\'') ) {s = 92;} + + else if ( ((LA22_85>='\u0000' && LA22_85<='&')||(LA22_85>='(' && LA22_85<='\uFFFF')) ) {s = 91;} + + if ( s>=0 ) return s; + break; + case 3 : + int LA22_81 = input.LA(1); + + s = -1; + if ( ((LA22_81>='\u0000' && LA22_81<='&')||(LA22_81>='(' && LA22_81<='\uFFFF')) ) {s = 91;} + + else if ( (LA22_81=='\'') ) {s = 92;} + + if ( s>=0 ) return s; + break; + case 4 : + int LA22_89 = input.LA(1); + + s = -1; + if ( ((LA22_89>='\u0000' && LA22_89<='&')||(LA22_89>='(' && LA22_89<='/')||(LA22_89>=':' && LA22_89<='@')||(LA22_89>='G' && LA22_89<='`')||(LA22_89>='g' && LA22_89<='\uFFFF')) ) {s = 91;} + + else if ( ((LA22_89>='0' && LA22_89<='9')||(LA22_89>='A' && LA22_89<='F')||(LA22_89>='a' && LA22_89<='f')) ) {s = 108;} + + else if ( (LA22_89=='\'') ) {s = 92;} + + if ( s>=0 ) return s; + break; + case 5 : + int LA22_28 = input.LA(1); + + s = -1; + if ( (LA22_28=='\\') ) {s = 63;} + + else if ( ((LA22_28>='\u0000' && LA22_28<='&')||(LA22_28>='(' && LA22_28<='[')||(LA22_28>=']' && LA22_28<='\uFFFF')) ) {s = 64;} + + if ( s>=0 ) return s; + break; + case 6 : + int LA22_83 = input.LA(1); + + s = -1; + if ( ((LA22_83>='\u0000' && LA22_83<='&')||(LA22_83>='(' && LA22_83<='\uFFFF')) ) {s = 91;} + + else if ( (LA22_83=='\'') ) {s = 92;} + + if ( s>=0 ) return s; + break; + case 7 : + int LA22_90 = input.LA(1); + + s = -1; + if ( ((LA22_90>='\u0000' && LA22_90<='&')||(LA22_90>='(' && LA22_90<='\uFFFF')) ) {s = 91;} + + else if ( (LA22_90=='\'') ) {s = 92;} + + if ( s>=0 ) return s; + break; + case 8 : + int LA22_140 = input.LA(1); + + s = -1; + if ( ((LA22_140>='0' && LA22_140<='9')||(LA22_140>='A' && LA22_140<='F')||(LA22_140>='a' && LA22_140<='f')) ) {s = 152;} + + else if ( ((LA22_140>='\u0000' && LA22_140<='/')||(LA22_140>=':' && LA22_140<='@')||(LA22_140>='G' && LA22_140<='`')||(LA22_140>='g' && LA22_140<='\uFFFF')) ) {s = 91;} + + if ( s>=0 ) return s; + break; + case 9 : + int LA22_84 = input.LA(1); + + s = -1; + if ( ((LA22_84>='\u0000' && LA22_84<='&')||(LA22_84>='(' && LA22_84<='\uFFFF')) ) {s = 91;} + + else if ( (LA22_84=='\'') ) {s = 92;} + + if ( s>=0 ) return s; + break; + case 10 : + int LA22_63 = input.LA(1); + + s = -1; + if ( (LA22_63=='n') ) {s = 80;} + + else if ( (LA22_63=='r') ) {s = 81;} + + else if ( (LA22_63=='t') ) {s = 82;} + + else if ( (LA22_63=='b') ) {s = 83;} + + else if ( (LA22_63=='f') ) {s = 84;} + + else if ( (LA22_63=='\"') ) {s = 85;} + + else if ( (LA22_63=='\'') ) {s = 86;} + + else if ( (LA22_63=='\\') ) {s = 87;} + + else if ( (LA22_63=='>') ) {s = 88;} + + else if ( (LA22_63=='u') ) {s = 89;} + + else if ( ((LA22_63>='\u0000' && LA22_63<='!')||(LA22_63>='#' && LA22_63<='&')||(LA22_63>='(' && LA22_63<='=')||(LA22_63>='?' && LA22_63<='[')||(LA22_63>=']' && LA22_63<='a')||(LA22_63>='c' && LA22_63<='e')||(LA22_63>='g' && LA22_63<='m')||(LA22_63>='o' && LA22_63<='q')||LA22_63=='s'||(LA22_63>='v' && LA22_63<='\uFFFF')) ) {s = 90;} + + if ( s>=0 ) return s; + break; + case 11 : + int LA22_80 = input.LA(1); + + s = -1; + if ( (LA22_80=='\'') ) {s = 92;} + + else if ( ((LA22_80>='\u0000' && LA22_80<='&')||(LA22_80>='(' && LA22_80<='\uFFFF')) ) {s = 91;} + + if ( s>=0 ) return s; + break; + case 12 : + int LA22_82 = input.LA(1); + + s = -1; + if ( ((LA22_82>='\u0000' && LA22_82<='&')||(LA22_82>='(' && LA22_82<='\uFFFF')) ) {s = 91;} + + else if ( (LA22_82=='\'') ) {s = 92;} + + if ( s>=0 ) return s; + break; + case 13 : + int LA22_88 = input.LA(1); + + s = -1; + if ( ((LA22_88>='\u0000' && LA22_88<='&')||(LA22_88>='(' && LA22_88<='\uFFFF')) ) {s = 91;} + + else if ( (LA22_88=='\'') ) {s = 92;} + + if ( s>=0 ) return s; + break; + case 14 : + int LA22_87 = input.LA(1); + + s = -1; + if ( ((LA22_87>='\u0000' && LA22_87<='&')||(LA22_87>='(' && LA22_87<='\uFFFF')) ) {s = 91;} + + else if ( (LA22_87=='\'') ) {s = 92;} + + if ( s>=0 ) return s; + break; + case 15 : + int LA22_108 = input.LA(1); + + s = -1; + if ( ((LA22_108>='0' && LA22_108<='9')||(LA22_108>='A' && LA22_108<='F')||(LA22_108>='a' && LA22_108<='f')) ) {s = 125;} + + else if ( ((LA22_108>='\u0000' && LA22_108<='/')||(LA22_108>=':' && LA22_108<='@')||(LA22_108>='G' && LA22_108<='`')||(LA22_108>='g' && LA22_108<='\uFFFF')) ) {s = 91;} + + if ( s>=0 ) return s; + break; + case 16 : + int LA22_64 = input.LA(1); + + s = -1; + if ( ((LA22_64>='\u0000' && LA22_64<='&')||(LA22_64>='(' && LA22_64<='\uFFFF')) ) {s = 91;} + + else if ( (LA22_64=='\'') ) {s = 92;} + + if ( s>=0 ) return s; + break; + case 17 : + int LA22_86 = input.LA(1); + + s = -1; + if ( (LA22_86=='\'') ) {s = 92;} + + else if ( ((LA22_86>='\u0000' && LA22_86<='&')||(LA22_86>='(' && LA22_86<='\uFFFF')) ) {s = 91;} + + if ( s>=0 ) return s; + break; + } + NoViableAltException nvae = + new NoViableAltException(getDescription(), 22, _s, input); + error(nvae); + throw nvae; + } + } + + +} \ No newline at end of file diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/ANTLRv3Parser.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/ANTLRv3Parser.java new file mode 100644 index 0000000..dabf4d3 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/ANTLRv3Parser.java @@ -0,0 +1,8727 @@ +// $ANTLR 3.1.1 /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g 2009-01-26 16:22:51 + +package org.antlr.gunit.swingui.parsers; + +import java.util.List; + + +import org.antlr.runtime.*; +import java.util.Stack; +import java.util.List; +import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; + +import org.antlr.runtime.tree.*; + +/** ANTLR v3 grammar written in ANTLR v3 with AST construction */ +public class ANTLRv3Parser extends Parser { + public static final String[] tokenNames = new String[] { + "", "", "", "", "DOC_COMMENT", "PARSER", "LEXER", "RULE", "BLOCK", "OPTIONAL", "CLOSURE", "POSITIVE_CLOSURE", "SYNPRED", "RANGE", "CHAR_RANGE", "EPSILON", "ALT", "EOR", "EOB", "EOA", "ID", "ARG", "ARGLIST", "RET", "LEXER_GRAMMAR", "PARSER_GRAMMAR", "TREE_GRAMMAR", "COMBINED_GRAMMAR", "INITACTION", "LABEL", "TEMPLATE", "SCOPE", "SEMPRED", "GATED_SEMPRED", "SYN_SEMPRED", "BACKTRACK_SEMPRED", "FRAGMENT", "TREE_BEGIN", "ROOT", "BANG", "REWRITE", "TOKENS", "TOKEN_REF", "STRING_LITERAL", "CHAR_LITERAL", "ACTION", "OPTIONS", "INT", "ARG_ACTION", "RULE_REF", "DOUBLE_QUOTE_STRING_LITERAL", "DOUBLE_ANGLE_STRING_LITERAL", "SRC", "SL_COMMENT", "ML_COMMENT", "LITERAL_CHAR", "ESC", "XDIGIT", "NESTED_ARG_ACTION", "ACTION_STRING_LITERAL", "ACTION_CHAR_LITERAL", "NESTED_ACTION", "ACTION_ESC", "WS_LOOP", "WS", "'lexer'", "'parser'", "'tree'", "'grammar'", "';'", "'}'", "'='", "'@'", "'::'", "'*'", "'protected'", "'public'", "'private'", "'returns'", "':'", "'throws'", "','", "'('", "'|'", "')'", "'catch'", "'finally'", "'+='", "'=>'", "'~'", "'?'", "'+'", "'.'", "'$'" + }; + public static final int CLOSURE=10; + public static final int DOUBLE_QUOTE_STRING_LITERAL=50; + public static final int TEMPLATE=30; + public static final int ARGLIST=22; + public static final int PARSER_GRAMMAR=25; + public static final int BANG=39; + public static final int T__73=73; + public static final int GATED_SEMPRED=33; + public static final int T__72=72; + public static final int T__70=70; + public static final int ACTION_ESC=62; + public static final int LEXER=6; + public static final int STRING_LITERAL=43; + public static final int OPTIONAL=9; + public static final int ACTION_CHAR_LITERAL=60; + public static final int RANGE=13; + public static final int DOUBLE_ANGLE_STRING_LITERAL=51; + public static final int T__89=89; + public static final int WS=64; + public static final int T__79=79; + public static final int T__66=66; + public static final int ARG_ACTION=48; + public static final int TOKEN_REF=42; + public static final int WS_LOOP=63; + public static final int T__92=92; + public static final int T__88=88; + public static final int XDIGIT=57; + public static final int TREE_BEGIN=37; + public static final int T__90=90; + public static final int INITACTION=28; + public static final int POSITIVE_CLOSURE=11; + public static final int T__91=91; + public static final int T__85=85; + public static final int CHAR_RANGE=14; + public static final int RET=23; + public static final int LITERAL_CHAR=55; + public static final int DOC_COMMENT=4; + public static final int T__93=93; + public static final int T__86=86; + public static final int NESTED_ACTION=61; + public static final int T__80=80; + public static final int T__69=69; + public static final int RULE=7; + public static final int T__65=65; + public static final int LABEL=29; + public static final int SYN_SEMPRED=34; + public static final int BACKTRACK_SEMPRED=35; + public static final int REWRITE=40; + public static final int T__67=67; + public static final int TREE_GRAMMAR=26; + public static final int T__87=87; + public static final int BLOCK=8; + public static final int T__74=74; + public static final int ALT=16; + public static final int T__68=68; + public static final int CHAR_LITERAL=44; + public static final int FRAGMENT=36; + public static final int INT=47; + public static final int PARSER=5; + public static final int EPSILON=15; + public static final int SCOPE=31; + public static final int TOKENS=41; + public static final int OPTIONS=46; + public static final int EOR=17; + public static final int ML_COMMENT=54; + public static final int SRC=52; + public static final int SL_COMMENT=53; + public static final int ID=20; + public static final int COMBINED_GRAMMAR=27; + public static final int EOB=18; + public static final int T__78=78; + public static final int SYNPRED=12; + public static final int EOA=19; + public static final int ACTION=45; + public static final int T__77=77; + public static final int ESC=56; + public static final int RULE_REF=49; + public static final int T__84=84; + public static final int SEMPRED=32; + public static final int NESTED_ARG_ACTION=58; + public static final int ROOT=38; + public static final int T__75=75; + public static final int ACTION_STRING_LITERAL=59; + public static final int ARG=21; + public static final int EOF=-1; + public static final int T__76=76; + public static final int T__82=82; + public static final int T__81=81; + public static final int T__83=83; + public static final int T__71=71; + public static final int LEXER_GRAMMAR=24; + + // delegates + // delegators + + + public ANTLRv3Parser(TokenStream input) { + this(input, new RecognizerSharedState()); + } + public ANTLRv3Parser(TokenStream input, RecognizerSharedState state) { + super(input, state); + + } + + protected TreeAdaptor adaptor = new CommonTreeAdaptor(); + + public void setTreeAdaptor(TreeAdaptor adaptor) { + this.adaptor = adaptor; + } + public TreeAdaptor getTreeAdaptor() { + return adaptor; + } + + public String[] getTokenNames() { return ANTLRv3Parser.tokenNames; } + public String getGrammarFileName() { return "/Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g"; } + + + int gtype; + public List rules; + + + public static class grammarDef_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "grammarDef" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:94:1: grammarDef : ( DOC_COMMENT )? ( 'lexer' | 'parser' | 'tree' | ) g= 'grammar' id ';' ( optionsSpec )? ( tokensSpec )? ( attrScope )* ( action )* ( rule )+ EOF -> ^( id ( DOC_COMMENT )? ( optionsSpec )? ( tokensSpec )? ( attrScope )* ( action )* ( rule )+ ) ; + public final ANTLRv3Parser.grammarDef_return grammarDef() throws RecognitionException { + ANTLRv3Parser.grammarDef_return retval = new ANTLRv3Parser.grammarDef_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token g=null; + Token DOC_COMMENT1=null; + Token string_literal2=null; + Token string_literal3=null; + Token string_literal4=null; + Token char_literal6=null; + Token EOF12=null; + ANTLRv3Parser.id_return id5 = null; + + ANTLRv3Parser.optionsSpec_return optionsSpec7 = null; + + ANTLRv3Parser.tokensSpec_return tokensSpec8 = null; + + ANTLRv3Parser.attrScope_return attrScope9 = null; + + ANTLRv3Parser.action_return action10 = null; + + ANTLRv3Parser.rule_return rule11 = null; + + + CommonTree g_tree=null; + CommonTree DOC_COMMENT1_tree=null; + CommonTree string_literal2_tree=null; + CommonTree string_literal3_tree=null; + CommonTree string_literal4_tree=null; + CommonTree char_literal6_tree=null; + CommonTree EOF12_tree=null; + RewriteRuleTokenStream stream_65=new RewriteRuleTokenStream(adaptor,"token 65"); + RewriteRuleTokenStream stream_68=new RewriteRuleTokenStream(adaptor,"token 68"); + RewriteRuleTokenStream stream_69=new RewriteRuleTokenStream(adaptor,"token 69"); + RewriteRuleTokenStream stream_DOC_COMMENT=new RewriteRuleTokenStream(adaptor,"token DOC_COMMENT"); + RewriteRuleTokenStream stream_EOF=new RewriteRuleTokenStream(adaptor,"token EOF"); + RewriteRuleTokenStream stream_66=new RewriteRuleTokenStream(adaptor,"token 66"); + RewriteRuleTokenStream stream_67=new RewriteRuleTokenStream(adaptor,"token 67"); + RewriteRuleSubtreeStream stream_attrScope=new RewriteRuleSubtreeStream(adaptor,"rule attrScope"); + RewriteRuleSubtreeStream stream_action=new RewriteRuleSubtreeStream(adaptor,"rule action"); + RewriteRuleSubtreeStream stream_rule=new RewriteRuleSubtreeStream(adaptor,"rule rule"); + RewriteRuleSubtreeStream stream_tokensSpec=new RewriteRuleSubtreeStream(adaptor,"rule tokensSpec"); + RewriteRuleSubtreeStream stream_optionsSpec=new RewriteRuleSubtreeStream(adaptor,"rule optionsSpec"); + RewriteRuleSubtreeStream stream_id=new RewriteRuleSubtreeStream(adaptor,"rule id"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:95:5: ( ( DOC_COMMENT )? ( 'lexer' | 'parser' | 'tree' | ) g= 'grammar' id ';' ( optionsSpec )? ( tokensSpec )? ( attrScope )* ( action )* ( rule )+ EOF -> ^( id ( DOC_COMMENT )? ( optionsSpec )? ( tokensSpec )? ( attrScope )* ( action )* ( rule )+ ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:95:9: ( DOC_COMMENT )? ( 'lexer' | 'parser' | 'tree' | ) g= 'grammar' id ';' ( optionsSpec )? ( tokensSpec )? ( attrScope )* ( action )* ( rule )+ EOF + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:95:9: ( DOC_COMMENT )? + int alt1=2; + int LA1_0 = input.LA(1); + + if ( (LA1_0==DOC_COMMENT) ) { + alt1=1; + } + switch (alt1) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:95:9: DOC_COMMENT + { + DOC_COMMENT1=(Token)match(input,DOC_COMMENT,FOLLOW_DOC_COMMENT_in_grammarDef347); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_DOC_COMMENT.add(DOC_COMMENT1); + + + } + break; + + } + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:96:6: ( 'lexer' | 'parser' | 'tree' | ) + int alt2=4; + switch ( input.LA(1) ) { + case 65: + { + alt2=1; + } + break; + case 66: + { + alt2=2; + } + break; + case 67: + { + alt2=3; + } + break; + case 68: + { + alt2=4; + } + break; + default: + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 2, 0, input); + + throw nvae; + } + + switch (alt2) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:96:8: 'lexer' + { + string_literal2=(Token)match(input,65,FOLLOW_65_in_grammarDef357); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_65.add(string_literal2); + + if ( state.backtracking==0 ) { + gtype=LEXER_GRAMMAR; + } + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:97:10: 'parser' + { + string_literal3=(Token)match(input,66,FOLLOW_66_in_grammarDef375); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_66.add(string_literal3); + + if ( state.backtracking==0 ) { + gtype=PARSER_GRAMMAR; + } + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:98:10: 'tree' + { + string_literal4=(Token)match(input,67,FOLLOW_67_in_grammarDef391); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_67.add(string_literal4); + + if ( state.backtracking==0 ) { + gtype=TREE_GRAMMAR; + } + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:99:14: + { + if ( state.backtracking==0 ) { + gtype=COMBINED_GRAMMAR; + } + + } + break; + + } + + g=(Token)match(input,68,FOLLOW_68_in_grammarDef432); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_68.add(g); + + pushFollow(FOLLOW_id_in_grammarDef434); + id5=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id5.getTree()); + char_literal6=(Token)match(input,69,FOLLOW_69_in_grammarDef436); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_69.add(char_literal6); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:101:25: ( optionsSpec )? + int alt3=2; + int LA3_0 = input.LA(1); + + if ( (LA3_0==OPTIONS) ) { + alt3=1; + } + switch (alt3) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:101:25: optionsSpec + { + pushFollow(FOLLOW_optionsSpec_in_grammarDef438); + optionsSpec7=optionsSpec(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_optionsSpec.add(optionsSpec7.getTree()); + + } + break; + + } + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:101:38: ( tokensSpec )? + int alt4=2; + int LA4_0 = input.LA(1); + + if ( (LA4_0==TOKENS) ) { + alt4=1; + } + switch (alt4) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:101:38: tokensSpec + { + pushFollow(FOLLOW_tokensSpec_in_grammarDef441); + tokensSpec8=tokensSpec(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_tokensSpec.add(tokensSpec8.getTree()); + + } + break; + + } + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:101:50: ( attrScope )* + loop5: + do { + int alt5=2; + int LA5_0 = input.LA(1); + + if ( (LA5_0==SCOPE) ) { + alt5=1; + } + + + switch (alt5) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:101:50: attrScope + { + pushFollow(FOLLOW_attrScope_in_grammarDef444); + attrScope9=attrScope(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_attrScope.add(attrScope9.getTree()); + + } + break; + + default : + break loop5; + } + } while (true); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:101:61: ( action )* + loop6: + do { + int alt6=2; + int LA6_0 = input.LA(1); + + if ( (LA6_0==72) ) { + alt6=1; + } + + + switch (alt6) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:101:61: action + { + pushFollow(FOLLOW_action_in_grammarDef447); + action10=action(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_action.add(action10.getTree()); + + } + break; + + default : + break loop6; + } + } while (true); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:102:6: ( rule )+ + int cnt7=0; + loop7: + do { + int alt7=2; + int LA7_0 = input.LA(1); + + if ( (LA7_0==DOC_COMMENT||LA7_0==FRAGMENT||LA7_0==TOKEN_REF||LA7_0==RULE_REF||(LA7_0>=75 && LA7_0<=77)) ) { + alt7=1; + } + + + switch (alt7) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:102:6: rule + { + pushFollow(FOLLOW_rule_in_grammarDef455); + rule11=rule(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rule.add(rule11.getTree()); + + } + break; + + default : + if ( cnt7 >= 1 ) break loop7; + if (state.backtracking>0) {state.failed=true; return retval;} + EarlyExitException eee = + new EarlyExitException(7, input); + throw eee; + } + cnt7++; + } while (true); + + EOF12=(Token)match(input,EOF,FOLLOW_EOF_in_grammarDef463); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_EOF.add(EOF12); + + + + // AST REWRITE + // elements: tokensSpec, id, DOC_COMMENT, action, rule, optionsSpec, attrScope + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 104:6: -> ^( id ( DOC_COMMENT )? ( optionsSpec )? ( tokensSpec )? ( attrScope )* ( action )* ( rule )+ ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:104:9: ^( id ( DOC_COMMENT )? ( optionsSpec )? ( tokensSpec )? ( attrScope )* ( action )* ( rule )+ ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(adaptor.create(gtype,g), root_1); + + adaptor.addChild(root_1, stream_id.nextTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:105:12: ( DOC_COMMENT )? + if ( stream_DOC_COMMENT.hasNext() ) { + adaptor.addChild(root_1, stream_DOC_COMMENT.nextNode()); + + } + stream_DOC_COMMENT.reset(); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:105:25: ( optionsSpec )? + if ( stream_optionsSpec.hasNext() ) { + adaptor.addChild(root_1, stream_optionsSpec.nextTree()); + + } + stream_optionsSpec.reset(); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:105:38: ( tokensSpec )? + if ( stream_tokensSpec.hasNext() ) { + adaptor.addChild(root_1, stream_tokensSpec.nextTree()); + + } + stream_tokensSpec.reset(); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:105:50: ( attrScope )* + while ( stream_attrScope.hasNext() ) { + adaptor.addChild(root_1, stream_attrScope.nextTree()); + + } + stream_attrScope.reset(); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:105:61: ( action )* + while ( stream_action.hasNext() ) { + adaptor.addChild(root_1, stream_action.nextTree()); + + } + stream_action.reset(); + if ( !(stream_rule.hasNext()) ) { + throw new RewriteEarlyExitException(); + } + while ( stream_rule.hasNext() ) { + adaptor.addChild(root_1, stream_rule.nextTree()); + + } + stream_rule.reset(); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "grammarDef" + + public static class tokensSpec_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "tokensSpec" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:109:1: tokensSpec : TOKENS ( tokenSpec )+ '}' -> ^( TOKENS ( tokenSpec )+ ) ; + public final ANTLRv3Parser.tokensSpec_return tokensSpec() throws RecognitionException { + ANTLRv3Parser.tokensSpec_return retval = new ANTLRv3Parser.tokensSpec_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token TOKENS13=null; + Token char_literal15=null; + ANTLRv3Parser.tokenSpec_return tokenSpec14 = null; + + + CommonTree TOKENS13_tree=null; + CommonTree char_literal15_tree=null; + RewriteRuleTokenStream stream_TOKENS=new RewriteRuleTokenStream(adaptor,"token TOKENS"); + RewriteRuleTokenStream stream_70=new RewriteRuleTokenStream(adaptor,"token 70"); + RewriteRuleSubtreeStream stream_tokenSpec=new RewriteRuleSubtreeStream(adaptor,"rule tokenSpec"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:110:2: ( TOKENS ( tokenSpec )+ '}' -> ^( TOKENS ( tokenSpec )+ ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:110:4: TOKENS ( tokenSpec )+ '}' + { + TOKENS13=(Token)match(input,TOKENS,FOLLOW_TOKENS_in_tokensSpec524); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_TOKENS.add(TOKENS13); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:110:11: ( tokenSpec )+ + int cnt8=0; + loop8: + do { + int alt8=2; + int LA8_0 = input.LA(1); + + if ( (LA8_0==TOKEN_REF) ) { + alt8=1; + } + + + switch (alt8) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:110:11: tokenSpec + { + pushFollow(FOLLOW_tokenSpec_in_tokensSpec526); + tokenSpec14=tokenSpec(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_tokenSpec.add(tokenSpec14.getTree()); + + } + break; + + default : + if ( cnt8 >= 1 ) break loop8; + if (state.backtracking>0) {state.failed=true; return retval;} + EarlyExitException eee = + new EarlyExitException(8, input); + throw eee; + } + cnt8++; + } while (true); + + char_literal15=(Token)match(input,70,FOLLOW_70_in_tokensSpec529); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_70.add(char_literal15); + + + + // AST REWRITE + // elements: TOKENS, tokenSpec + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 110:26: -> ^( TOKENS ( tokenSpec )+ ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:110:29: ^( TOKENS ( tokenSpec )+ ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_TOKENS.nextNode(), root_1); + + if ( !(stream_tokenSpec.hasNext()) ) { + throw new RewriteEarlyExitException(); + } + while ( stream_tokenSpec.hasNext() ) { + adaptor.addChild(root_1, stream_tokenSpec.nextTree()); + + } + stream_tokenSpec.reset(); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "tokensSpec" + + public static class tokenSpec_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "tokenSpec" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:113:1: tokenSpec : TOKEN_REF ( '=' (lit= STRING_LITERAL | lit= CHAR_LITERAL ) -> ^( '=' TOKEN_REF $lit) | -> TOKEN_REF ) ';' ; + public final ANTLRv3Parser.tokenSpec_return tokenSpec() throws RecognitionException { + ANTLRv3Parser.tokenSpec_return retval = new ANTLRv3Parser.tokenSpec_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token lit=null; + Token TOKEN_REF16=null; + Token char_literal17=null; + Token char_literal18=null; + + CommonTree lit_tree=null; + CommonTree TOKEN_REF16_tree=null; + CommonTree char_literal17_tree=null; + CommonTree char_literal18_tree=null; + RewriteRuleTokenStream stream_71=new RewriteRuleTokenStream(adaptor,"token 71"); + RewriteRuleTokenStream stream_TOKEN_REF=new RewriteRuleTokenStream(adaptor,"token TOKEN_REF"); + RewriteRuleTokenStream stream_CHAR_LITERAL=new RewriteRuleTokenStream(adaptor,"token CHAR_LITERAL"); + RewriteRuleTokenStream stream_69=new RewriteRuleTokenStream(adaptor,"token 69"); + RewriteRuleTokenStream stream_STRING_LITERAL=new RewriteRuleTokenStream(adaptor,"token STRING_LITERAL"); + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:114:2: ( TOKEN_REF ( '=' (lit= STRING_LITERAL | lit= CHAR_LITERAL ) -> ^( '=' TOKEN_REF $lit) | -> TOKEN_REF ) ';' ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:114:4: TOKEN_REF ( '=' (lit= STRING_LITERAL | lit= CHAR_LITERAL ) -> ^( '=' TOKEN_REF $lit) | -> TOKEN_REF ) ';' + { + TOKEN_REF16=(Token)match(input,TOKEN_REF,FOLLOW_TOKEN_REF_in_tokenSpec549); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_TOKEN_REF.add(TOKEN_REF16); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:115:3: ( '=' (lit= STRING_LITERAL | lit= CHAR_LITERAL ) -> ^( '=' TOKEN_REF $lit) | -> TOKEN_REF ) + int alt10=2; + int LA10_0 = input.LA(1); + + if ( (LA10_0==71) ) { + alt10=1; + } + else if ( (LA10_0==69) ) { + alt10=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 10, 0, input); + + throw nvae; + } + switch (alt10) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:115:5: '=' (lit= STRING_LITERAL | lit= CHAR_LITERAL ) + { + char_literal17=(Token)match(input,71,FOLLOW_71_in_tokenSpec555); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_71.add(char_literal17); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:115:9: (lit= STRING_LITERAL | lit= CHAR_LITERAL ) + int alt9=2; + int LA9_0 = input.LA(1); + + if ( (LA9_0==STRING_LITERAL) ) { + alt9=1; + } + else if ( (LA9_0==CHAR_LITERAL) ) { + alt9=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 9, 0, input); + + throw nvae; + } + switch (alt9) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:115:10: lit= STRING_LITERAL + { + lit=(Token)match(input,STRING_LITERAL,FOLLOW_STRING_LITERAL_in_tokenSpec560); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_STRING_LITERAL.add(lit); + + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:115:29: lit= CHAR_LITERAL + { + lit=(Token)match(input,CHAR_LITERAL,FOLLOW_CHAR_LITERAL_in_tokenSpec564); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_CHAR_LITERAL.add(lit); + + + } + break; + + } + + + + // AST REWRITE + // elements: TOKEN_REF, 71, lit + // token labels: lit + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleTokenStream stream_lit=new RewriteRuleTokenStream(adaptor,"token lit",lit); + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 115:47: -> ^( '=' TOKEN_REF $lit) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:115:50: ^( '=' TOKEN_REF $lit) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_71.nextNode(), root_1); + + adaptor.addChild(root_1, stream_TOKEN_REF.nextNode()); + adaptor.addChild(root_1, stream_lit.nextNode()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:116:16: + { + + // AST REWRITE + // elements: TOKEN_REF + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 116:16: -> TOKEN_REF + { + adaptor.addChild(root_0, stream_TOKEN_REF.nextNode()); + + } + + retval.tree = root_0;} + } + break; + + } + + char_literal18=(Token)match(input,69,FOLLOW_69_in_tokenSpec603); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_69.add(char_literal18); + + + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "tokenSpec" + + public static class attrScope_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "attrScope" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:121:1: attrScope : 'scope' id ACTION -> ^( 'scope' id ACTION ) ; + public final ANTLRv3Parser.attrScope_return attrScope() throws RecognitionException { + ANTLRv3Parser.attrScope_return retval = new ANTLRv3Parser.attrScope_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token string_literal19=null; + Token ACTION21=null; + ANTLRv3Parser.id_return id20 = null; + + + CommonTree string_literal19_tree=null; + CommonTree ACTION21_tree=null; + RewriteRuleTokenStream stream_ACTION=new RewriteRuleTokenStream(adaptor,"token ACTION"); + RewriteRuleTokenStream stream_SCOPE=new RewriteRuleTokenStream(adaptor,"token SCOPE"); + RewriteRuleSubtreeStream stream_id=new RewriteRuleSubtreeStream(adaptor,"rule id"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:122:2: ( 'scope' id ACTION -> ^( 'scope' id ACTION ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:122:4: 'scope' id ACTION + { + string_literal19=(Token)match(input,SCOPE,FOLLOW_SCOPE_in_attrScope614); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_SCOPE.add(string_literal19); + + pushFollow(FOLLOW_id_in_attrScope616); + id20=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id20.getTree()); + ACTION21=(Token)match(input,ACTION,FOLLOW_ACTION_in_attrScope618); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ACTION.add(ACTION21); + + + + // AST REWRITE + // elements: SCOPE, id, ACTION + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 122:22: -> ^( 'scope' id ACTION ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:122:25: ^( 'scope' id ACTION ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_SCOPE.nextNode(), root_1); + + adaptor.addChild(root_1, stream_id.nextTree()); + adaptor.addChild(root_1, stream_ACTION.nextNode()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "attrScope" + + public static class action_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "action" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:125:1: action : '@' ( actionScopeName '::' )? id ACTION -> ^( '@' ( actionScopeName )? id ACTION ) ; + public final ANTLRv3Parser.action_return action() throws RecognitionException { + ANTLRv3Parser.action_return retval = new ANTLRv3Parser.action_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token char_literal22=null; + Token string_literal24=null; + Token ACTION26=null; + ANTLRv3Parser.actionScopeName_return actionScopeName23 = null; + + ANTLRv3Parser.id_return id25 = null; + + + CommonTree char_literal22_tree=null; + CommonTree string_literal24_tree=null; + CommonTree ACTION26_tree=null; + RewriteRuleTokenStream stream_ACTION=new RewriteRuleTokenStream(adaptor,"token ACTION"); + RewriteRuleTokenStream stream_72=new RewriteRuleTokenStream(adaptor,"token 72"); + RewriteRuleTokenStream stream_73=new RewriteRuleTokenStream(adaptor,"token 73"); + RewriteRuleSubtreeStream stream_actionScopeName=new RewriteRuleSubtreeStream(adaptor,"rule actionScopeName"); + RewriteRuleSubtreeStream stream_id=new RewriteRuleSubtreeStream(adaptor,"rule id"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:127:2: ( '@' ( actionScopeName '::' )? id ACTION -> ^( '@' ( actionScopeName )? id ACTION ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:127:4: '@' ( actionScopeName '::' )? id ACTION + { + char_literal22=(Token)match(input,72,FOLLOW_72_in_action641); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_72.add(char_literal22); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:127:8: ( actionScopeName '::' )? + int alt11=2; + switch ( input.LA(1) ) { + case TOKEN_REF: + { + int LA11_1 = input.LA(2); + + if ( (LA11_1==73) ) { + alt11=1; + } + } + break; + case RULE_REF: + { + int LA11_2 = input.LA(2); + + if ( (LA11_2==73) ) { + alt11=1; + } + } + break; + case 65: + case 66: + { + alt11=1; + } + break; + } + + switch (alt11) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:127:9: actionScopeName '::' + { + pushFollow(FOLLOW_actionScopeName_in_action644); + actionScopeName23=actionScopeName(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_actionScopeName.add(actionScopeName23.getTree()); + string_literal24=(Token)match(input,73,FOLLOW_73_in_action646); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_73.add(string_literal24); + + + } + break; + + } + + pushFollow(FOLLOW_id_in_action650); + id25=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id25.getTree()); + ACTION26=(Token)match(input,ACTION,FOLLOW_ACTION_in_action652); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ACTION.add(ACTION26); + + + + // AST REWRITE + // elements: ACTION, actionScopeName, id, 72 + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 127:42: -> ^( '@' ( actionScopeName )? id ACTION ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:127:45: ^( '@' ( actionScopeName )? id ACTION ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_72.nextNode(), root_1); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:127:51: ( actionScopeName )? + if ( stream_actionScopeName.hasNext() ) { + adaptor.addChild(root_1, stream_actionScopeName.nextTree()); + + } + stream_actionScopeName.reset(); + adaptor.addChild(root_1, stream_id.nextTree()); + adaptor.addChild(root_1, stream_ACTION.nextNode()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "action" + + public static class actionScopeName_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "actionScopeName" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:130:1: actionScopeName : ( id | l= 'lexer' -> ID[$l] | p= 'parser' -> ID[$p] ); + public final ANTLRv3Parser.actionScopeName_return actionScopeName() throws RecognitionException { + ANTLRv3Parser.actionScopeName_return retval = new ANTLRv3Parser.actionScopeName_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token l=null; + Token p=null; + ANTLRv3Parser.id_return id27 = null; + + + CommonTree l_tree=null; + CommonTree p_tree=null; + RewriteRuleTokenStream stream_65=new RewriteRuleTokenStream(adaptor,"token 65"); + RewriteRuleTokenStream stream_66=new RewriteRuleTokenStream(adaptor,"token 66"); + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:134:2: ( id | l= 'lexer' -> ID[$l] | p= 'parser' -> ID[$p] ) + int alt12=3; + switch ( input.LA(1) ) { + case TOKEN_REF: + case RULE_REF: + { + alt12=1; + } + break; + case 65: + { + alt12=2; + } + break; + case 66: + { + alt12=3; + } + break; + default: + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 12, 0, input); + + throw nvae; + } + + switch (alt12) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:134:4: id + { + root_0 = (CommonTree)adaptor.nil(); + + pushFollow(FOLLOW_id_in_actionScopeName678); + id27=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) adaptor.addChild(root_0, id27.getTree()); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:135:4: l= 'lexer' + { + l=(Token)match(input,65,FOLLOW_65_in_actionScopeName685); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_65.add(l); + + + + // AST REWRITE + // elements: + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 135:14: -> ID[$l] + { + adaptor.addChild(root_0, (CommonTree)adaptor.create(ID, l)); + + } + + retval.tree = root_0;} + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:136:9: p= 'parser' + { + p=(Token)match(input,66,FOLLOW_66_in_actionScopeName702); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_66.add(p); + + + + // AST REWRITE + // elements: + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 136:20: -> ID[$p] + { + adaptor.addChild(root_0, (CommonTree)adaptor.create(ID, p)); + + } + + retval.tree = root_0;} + } + break; + + } + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "actionScopeName" + + public static class optionsSpec_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "optionsSpec" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:139:1: optionsSpec : OPTIONS ( option ';' )+ '}' -> ^( OPTIONS ( option )+ ) ; + public final ANTLRv3Parser.optionsSpec_return optionsSpec() throws RecognitionException { + ANTLRv3Parser.optionsSpec_return retval = new ANTLRv3Parser.optionsSpec_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token OPTIONS28=null; + Token char_literal30=null; + Token char_literal31=null; + ANTLRv3Parser.option_return option29 = null; + + + CommonTree OPTIONS28_tree=null; + CommonTree char_literal30_tree=null; + CommonTree char_literal31_tree=null; + RewriteRuleTokenStream stream_OPTIONS=new RewriteRuleTokenStream(adaptor,"token OPTIONS"); + RewriteRuleTokenStream stream_70=new RewriteRuleTokenStream(adaptor,"token 70"); + RewriteRuleTokenStream stream_69=new RewriteRuleTokenStream(adaptor,"token 69"); + RewriteRuleSubtreeStream stream_option=new RewriteRuleSubtreeStream(adaptor,"rule option"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:140:2: ( OPTIONS ( option ';' )+ '}' -> ^( OPTIONS ( option )+ ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:140:4: OPTIONS ( option ';' )+ '}' + { + OPTIONS28=(Token)match(input,OPTIONS,FOLLOW_OPTIONS_in_optionsSpec718); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_OPTIONS.add(OPTIONS28); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:140:12: ( option ';' )+ + int cnt13=0; + loop13: + do { + int alt13=2; + int LA13_0 = input.LA(1); + + if ( (LA13_0==TOKEN_REF||LA13_0==RULE_REF) ) { + alt13=1; + } + + + switch (alt13) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:140:13: option ';' + { + pushFollow(FOLLOW_option_in_optionsSpec721); + option29=option(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_option.add(option29.getTree()); + char_literal30=(Token)match(input,69,FOLLOW_69_in_optionsSpec723); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_69.add(char_literal30); + + + } + break; + + default : + if ( cnt13 >= 1 ) break loop13; + if (state.backtracking>0) {state.failed=true; return retval;} + EarlyExitException eee = + new EarlyExitException(13, input); + throw eee; + } + cnt13++; + } while (true); + + char_literal31=(Token)match(input,70,FOLLOW_70_in_optionsSpec727); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_70.add(char_literal31); + + + + // AST REWRITE + // elements: option, OPTIONS + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 140:30: -> ^( OPTIONS ( option )+ ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:140:33: ^( OPTIONS ( option )+ ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_OPTIONS.nextNode(), root_1); + + if ( !(stream_option.hasNext()) ) { + throw new RewriteEarlyExitException(); + } + while ( stream_option.hasNext() ) { + adaptor.addChild(root_1, stream_option.nextTree()); + + } + stream_option.reset(); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "optionsSpec" + + public static class option_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "option" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:143:1: option : id '=' optionValue -> ^( '=' id optionValue ) ; + public final ANTLRv3Parser.option_return option() throws RecognitionException { + ANTLRv3Parser.option_return retval = new ANTLRv3Parser.option_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token char_literal33=null; + ANTLRv3Parser.id_return id32 = null; + + ANTLRv3Parser.optionValue_return optionValue34 = null; + + + CommonTree char_literal33_tree=null; + RewriteRuleTokenStream stream_71=new RewriteRuleTokenStream(adaptor,"token 71"); + RewriteRuleSubtreeStream stream_optionValue=new RewriteRuleSubtreeStream(adaptor,"rule optionValue"); + RewriteRuleSubtreeStream stream_id=new RewriteRuleSubtreeStream(adaptor,"rule id"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:144:5: ( id '=' optionValue -> ^( '=' id optionValue ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:144:9: id '=' optionValue + { + pushFollow(FOLLOW_id_in_option752); + id32=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id32.getTree()); + char_literal33=(Token)match(input,71,FOLLOW_71_in_option754); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_71.add(char_literal33); + + pushFollow(FOLLOW_optionValue_in_option756); + optionValue34=optionValue(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_optionValue.add(optionValue34.getTree()); + + + // AST REWRITE + // elements: id, 71, optionValue + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 144:28: -> ^( '=' id optionValue ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:144:31: ^( '=' id optionValue ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_71.nextNode(), root_1); + + adaptor.addChild(root_1, stream_id.nextTree()); + adaptor.addChild(root_1, stream_optionValue.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "option" + + public static class optionValue_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "optionValue" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:147:1: optionValue : ( id | STRING_LITERAL | CHAR_LITERAL | INT | s= '*' -> STRING_LITERAL[$s] ); + public final ANTLRv3Parser.optionValue_return optionValue() throws RecognitionException { + ANTLRv3Parser.optionValue_return retval = new ANTLRv3Parser.optionValue_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token s=null; + Token STRING_LITERAL36=null; + Token CHAR_LITERAL37=null; + Token INT38=null; + ANTLRv3Parser.id_return id35 = null; + + + CommonTree s_tree=null; + CommonTree STRING_LITERAL36_tree=null; + CommonTree CHAR_LITERAL37_tree=null; + CommonTree INT38_tree=null; + RewriteRuleTokenStream stream_74=new RewriteRuleTokenStream(adaptor,"token 74"); + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:148:5: ( id | STRING_LITERAL | CHAR_LITERAL | INT | s= '*' -> STRING_LITERAL[$s] ) + int alt14=5; + switch ( input.LA(1) ) { + case TOKEN_REF: + case RULE_REF: + { + alt14=1; + } + break; + case STRING_LITERAL: + { + alt14=2; + } + break; + case CHAR_LITERAL: + { + alt14=3; + } + break; + case INT: + { + alt14=4; + } + break; + case 74: + { + alt14=5; + } + break; + default: + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 14, 0, input); + + throw nvae; + } + + switch (alt14) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:148:9: id + { + root_0 = (CommonTree)adaptor.nil(); + + pushFollow(FOLLOW_id_in_optionValue785); + id35=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) adaptor.addChild(root_0, id35.getTree()); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:149:9: STRING_LITERAL + { + root_0 = (CommonTree)adaptor.nil(); + + STRING_LITERAL36=(Token)match(input,STRING_LITERAL,FOLLOW_STRING_LITERAL_in_optionValue795); if (state.failed) return retval; + if ( state.backtracking==0 ) { + STRING_LITERAL36_tree = (CommonTree)adaptor.create(STRING_LITERAL36); + adaptor.addChild(root_0, STRING_LITERAL36_tree); + } + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:150:9: CHAR_LITERAL + { + root_0 = (CommonTree)adaptor.nil(); + + CHAR_LITERAL37=(Token)match(input,CHAR_LITERAL,FOLLOW_CHAR_LITERAL_in_optionValue805); if (state.failed) return retval; + if ( state.backtracking==0 ) { + CHAR_LITERAL37_tree = (CommonTree)adaptor.create(CHAR_LITERAL37); + adaptor.addChild(root_0, CHAR_LITERAL37_tree); + } + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:151:9: INT + { + root_0 = (CommonTree)adaptor.nil(); + + INT38=(Token)match(input,INT,FOLLOW_INT_in_optionValue815); if (state.failed) return retval; + if ( state.backtracking==0 ) { + INT38_tree = (CommonTree)adaptor.create(INT38); + adaptor.addChild(root_0, INT38_tree); + } + + } + break; + case 5 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:152:7: s= '*' + { + s=(Token)match(input,74,FOLLOW_74_in_optionValue825); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_74.add(s); + + + + // AST REWRITE + // elements: + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 152:13: -> STRING_LITERAL[$s] + { + adaptor.addChild(root_0, (CommonTree)adaptor.create(STRING_LITERAL, s)); + + } + + retval.tree = root_0;} + } + break; + + } + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "optionValue" + + protected static class rule_scope { + String name; + } + protected Stack rule_stack = new Stack(); + + public static class rule_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "rule" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:155:1: rule : ( DOC_COMMENT )? (modifier= ( 'protected' | 'public' | 'private' | 'fragment' ) )? id ( '!' )? (arg= ARG_ACTION )? ( 'returns' rt= ARG_ACTION )? ( throwsSpec )? ( optionsSpec )? ( ruleScopeSpec )? ( ruleAction )* ':' altList ';' ( exceptionGroup )? -> ^( RULE id ( ^( ARG $arg) )? ( ^( RET $rt) )? ( optionsSpec )? ( ruleScopeSpec )? ( ruleAction )* altList ( exceptionGroup )? EOR[\"EOR\"] ) ; + public final ANTLRv3Parser.rule_return rule() throws RecognitionException { + rule_stack.push(new rule_scope()); + ANTLRv3Parser.rule_return retval = new ANTLRv3Parser.rule_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token modifier=null; + Token arg=null; + Token rt=null; + Token DOC_COMMENT39=null; + Token string_literal40=null; + Token string_literal41=null; + Token string_literal42=null; + Token string_literal43=null; + Token char_literal45=null; + Token string_literal46=null; + Token char_literal51=null; + Token char_literal53=null; + ANTLRv3Parser.id_return id44 = null; + + ANTLRv3Parser.throwsSpec_return throwsSpec47 = null; + + ANTLRv3Parser.optionsSpec_return optionsSpec48 = null; + + ANTLRv3Parser.ruleScopeSpec_return ruleScopeSpec49 = null; + + ANTLRv3Parser.ruleAction_return ruleAction50 = null; + + ANTLRv3Parser.altList_return altList52 = null; + + ANTLRv3Parser.exceptionGroup_return exceptionGroup54 = null; + + + CommonTree modifier_tree=null; + CommonTree arg_tree=null; + CommonTree rt_tree=null; + CommonTree DOC_COMMENT39_tree=null; + CommonTree string_literal40_tree=null; + CommonTree string_literal41_tree=null; + CommonTree string_literal42_tree=null; + CommonTree string_literal43_tree=null; + CommonTree char_literal45_tree=null; + CommonTree string_literal46_tree=null; + CommonTree char_literal51_tree=null; + CommonTree char_literal53_tree=null; + RewriteRuleTokenStream stream_76=new RewriteRuleTokenStream(adaptor,"token 76"); + RewriteRuleTokenStream stream_ARG_ACTION=new RewriteRuleTokenStream(adaptor,"token ARG_ACTION"); + RewriteRuleTokenStream stream_79=new RewriteRuleTokenStream(adaptor,"token 79"); + RewriteRuleTokenStream stream_FRAGMENT=new RewriteRuleTokenStream(adaptor,"token FRAGMENT"); + RewriteRuleTokenStream stream_75=new RewriteRuleTokenStream(adaptor,"token 75"); + RewriteRuleTokenStream stream_77=new RewriteRuleTokenStream(adaptor,"token 77"); + RewriteRuleTokenStream stream_69=new RewriteRuleTokenStream(adaptor,"token 69"); + RewriteRuleTokenStream stream_DOC_COMMENT=new RewriteRuleTokenStream(adaptor,"token DOC_COMMENT"); + RewriteRuleTokenStream stream_78=new RewriteRuleTokenStream(adaptor,"token 78"); + RewriteRuleTokenStream stream_BANG=new RewriteRuleTokenStream(adaptor,"token BANG"); + RewriteRuleSubtreeStream stream_ruleScopeSpec=new RewriteRuleSubtreeStream(adaptor,"rule ruleScopeSpec"); + RewriteRuleSubtreeStream stream_ruleAction=new RewriteRuleSubtreeStream(adaptor,"rule ruleAction"); + RewriteRuleSubtreeStream stream_optionsSpec=new RewriteRuleSubtreeStream(adaptor,"rule optionsSpec"); + RewriteRuleSubtreeStream stream_altList=new RewriteRuleSubtreeStream(adaptor,"rule altList"); + RewriteRuleSubtreeStream stream_exceptionGroup=new RewriteRuleSubtreeStream(adaptor,"rule exceptionGroup"); + RewriteRuleSubtreeStream stream_throwsSpec=new RewriteRuleSubtreeStream(adaptor,"rule throwsSpec"); + RewriteRuleSubtreeStream stream_id=new RewriteRuleSubtreeStream(adaptor,"rule id"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:162:2: ( ( DOC_COMMENT )? (modifier= ( 'protected' | 'public' | 'private' | 'fragment' ) )? id ( '!' )? (arg= ARG_ACTION )? ( 'returns' rt= ARG_ACTION )? ( throwsSpec )? ( optionsSpec )? ( ruleScopeSpec )? ( ruleAction )* ':' altList ';' ( exceptionGroup )? -> ^( RULE id ( ^( ARG $arg) )? ( ^( RET $rt) )? ( optionsSpec )? ( ruleScopeSpec )? ( ruleAction )* altList ( exceptionGroup )? EOR[\"EOR\"] ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:162:4: ( DOC_COMMENT )? (modifier= ( 'protected' | 'public' | 'private' | 'fragment' ) )? id ( '!' )? (arg= ARG_ACTION )? ( 'returns' rt= ARG_ACTION )? ( throwsSpec )? ( optionsSpec )? ( ruleScopeSpec )? ( ruleAction )* ':' altList ';' ( exceptionGroup )? + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:162:4: ( DOC_COMMENT )? + int alt15=2; + int LA15_0 = input.LA(1); + + if ( (LA15_0==DOC_COMMENT) ) { + alt15=1; + } + switch (alt15) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:162:4: DOC_COMMENT + { + DOC_COMMENT39=(Token)match(input,DOC_COMMENT,FOLLOW_DOC_COMMENT_in_rule854); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_DOC_COMMENT.add(DOC_COMMENT39); + + + } + break; + + } + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:163:3: (modifier= ( 'protected' | 'public' | 'private' | 'fragment' ) )? + int alt17=2; + int LA17_0 = input.LA(1); + + if ( (LA17_0==FRAGMENT||(LA17_0>=75 && LA17_0<=77)) ) { + alt17=1; + } + switch (alt17) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:163:5: modifier= ( 'protected' | 'public' | 'private' | 'fragment' ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:163:14: ( 'protected' | 'public' | 'private' | 'fragment' ) + int alt16=4; + switch ( input.LA(1) ) { + case 75: + { + alt16=1; + } + break; + case 76: + { + alt16=2; + } + break; + case 77: + { + alt16=3; + } + break; + case FRAGMENT: + { + alt16=4; + } + break; + default: + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 16, 0, input); + + throw nvae; + } + + switch (alt16) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:163:15: 'protected' + { + string_literal40=(Token)match(input,75,FOLLOW_75_in_rule864); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_75.add(string_literal40); + + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:163:27: 'public' + { + string_literal41=(Token)match(input,76,FOLLOW_76_in_rule866); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_76.add(string_literal41); + + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:163:36: 'private' + { + string_literal42=(Token)match(input,77,FOLLOW_77_in_rule868); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_77.add(string_literal42); + + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:163:46: 'fragment' + { + string_literal43=(Token)match(input,FRAGMENT,FOLLOW_FRAGMENT_in_rule870); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_FRAGMENT.add(string_literal43); + + + } + break; + + } + + + } + break; + + } + + pushFollow(FOLLOW_id_in_rule878); + id44=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id44.getTree()); + if ( state.backtracking==0 ) { + ((rule_scope)rule_stack.peek()).name = (id44!=null?input.toString(id44.start,id44.stop):null); + } + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:165:3: ( '!' )? + int alt18=2; + int LA18_0 = input.LA(1); + + if ( (LA18_0==BANG) ) { + alt18=1; + } + switch (alt18) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:165:3: '!' + { + char_literal45=(Token)match(input,BANG,FOLLOW_BANG_in_rule884); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_BANG.add(char_literal45); + + + } + break; + + } + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:166:3: (arg= ARG_ACTION )? + int alt19=2; + int LA19_0 = input.LA(1); + + if ( (LA19_0==ARG_ACTION) ) { + alt19=1; + } + switch (alt19) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:166:5: arg= ARG_ACTION + { + arg=(Token)match(input,ARG_ACTION,FOLLOW_ARG_ACTION_in_rule893); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ARG_ACTION.add(arg); + + + } + break; + + } + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:167:3: ( 'returns' rt= ARG_ACTION )? + int alt20=2; + int LA20_0 = input.LA(1); + + if ( (LA20_0==78) ) { + alt20=1; + } + switch (alt20) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:167:5: 'returns' rt= ARG_ACTION + { + string_literal46=(Token)match(input,78,FOLLOW_78_in_rule902); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_78.add(string_literal46); + + rt=(Token)match(input,ARG_ACTION,FOLLOW_ARG_ACTION_in_rule906); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ARG_ACTION.add(rt); + + + } + break; + + } + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:168:3: ( throwsSpec )? + int alt21=2; + int LA21_0 = input.LA(1); + + if ( (LA21_0==80) ) { + alt21=1; + } + switch (alt21) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:168:3: throwsSpec + { + pushFollow(FOLLOW_throwsSpec_in_rule914); + throwsSpec47=throwsSpec(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_throwsSpec.add(throwsSpec47.getTree()); + + } + break; + + } + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:168:15: ( optionsSpec )? + int alt22=2; + int LA22_0 = input.LA(1); + + if ( (LA22_0==OPTIONS) ) { + alt22=1; + } + switch (alt22) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:168:15: optionsSpec + { + pushFollow(FOLLOW_optionsSpec_in_rule917); + optionsSpec48=optionsSpec(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_optionsSpec.add(optionsSpec48.getTree()); + + } + break; + + } + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:168:28: ( ruleScopeSpec )? + int alt23=2; + int LA23_0 = input.LA(1); + + if ( (LA23_0==SCOPE) ) { + alt23=1; + } + switch (alt23) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:168:28: ruleScopeSpec + { + pushFollow(FOLLOW_ruleScopeSpec_in_rule920); + ruleScopeSpec49=ruleScopeSpec(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ruleScopeSpec.add(ruleScopeSpec49.getTree()); + + } + break; + + } + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:168:43: ( ruleAction )* + loop24: + do { + int alt24=2; + int LA24_0 = input.LA(1); + + if ( (LA24_0==72) ) { + alt24=1; + } + + + switch (alt24) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:168:43: ruleAction + { + pushFollow(FOLLOW_ruleAction_in_rule923); + ruleAction50=ruleAction(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ruleAction.add(ruleAction50.getTree()); + + } + break; + + default : + break loop24; + } + } while (true); + + char_literal51=(Token)match(input,79,FOLLOW_79_in_rule928); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_79.add(char_literal51); + + pushFollow(FOLLOW_altList_in_rule930); + altList52=altList(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_altList.add(altList52.getTree()); + char_literal53=(Token)match(input,69,FOLLOW_69_in_rule932); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_69.add(char_literal53); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:170:3: ( exceptionGroup )? + int alt25=2; + int LA25_0 = input.LA(1); + + if ( ((LA25_0>=85 && LA25_0<=86)) ) { + alt25=1; + } + switch (alt25) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:170:3: exceptionGroup + { + pushFollow(FOLLOW_exceptionGroup_in_rule936); + exceptionGroup54=exceptionGroup(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_exceptionGroup.add(exceptionGroup54.getTree()); + + } + break; + + } + + + + // AST REWRITE + // elements: id, altList, optionsSpec, rt, exceptionGroup, ruleAction, arg, ruleScopeSpec + // token labels: arg, rt + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleTokenStream stream_arg=new RewriteRuleTokenStream(adaptor,"token arg",arg); + RewriteRuleTokenStream stream_rt=new RewriteRuleTokenStream(adaptor,"token rt",rt); + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 171:6: -> ^( RULE id ( ^( ARG $arg) )? ( ^( RET $rt) )? ( optionsSpec )? ( ruleScopeSpec )? ( ruleAction )* altList ( exceptionGroup )? EOR[\"EOR\"] ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:171:9: ^( RULE id ( ^( ARG $arg) )? ( ^( RET $rt) )? ( optionsSpec )? ( ruleScopeSpec )? ( ruleAction )* altList ( exceptionGroup )? EOR[\"EOR\"] ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(RULE, "RULE"), root_1); + + adaptor.addChild(root_1, stream_id.nextTree()); + adaptor.addChild(root_1, modifier!=null?adaptor.create(modifier):null); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:171:67: ( ^( ARG $arg) )? + if ( stream_arg.hasNext() ) { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:171:67: ^( ARG $arg) + { + CommonTree root_2 = (CommonTree)adaptor.nil(); + root_2 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(ARG, "ARG"), root_2); + + adaptor.addChild(root_2, stream_arg.nextNode()); + + adaptor.addChild(root_1, root_2); + } + + } + stream_arg.reset(); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:171:80: ( ^( RET $rt) )? + if ( stream_rt.hasNext() ) { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:171:80: ^( RET $rt) + { + CommonTree root_2 = (CommonTree)adaptor.nil(); + root_2 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(RET, "RET"), root_2); + + adaptor.addChild(root_2, stream_rt.nextNode()); + + adaptor.addChild(root_1, root_2); + } + + } + stream_rt.reset(); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:172:9: ( optionsSpec )? + if ( stream_optionsSpec.hasNext() ) { + adaptor.addChild(root_1, stream_optionsSpec.nextTree()); + + } + stream_optionsSpec.reset(); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:172:22: ( ruleScopeSpec )? + if ( stream_ruleScopeSpec.hasNext() ) { + adaptor.addChild(root_1, stream_ruleScopeSpec.nextTree()); + + } + stream_ruleScopeSpec.reset(); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:172:37: ( ruleAction )* + while ( stream_ruleAction.hasNext() ) { + adaptor.addChild(root_1, stream_ruleAction.nextTree()); + + } + stream_ruleAction.reset(); + adaptor.addChild(root_1, stream_altList.nextTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:174:9: ( exceptionGroup )? + if ( stream_exceptionGroup.hasNext() ) { + adaptor.addChild(root_1, stream_exceptionGroup.nextTree()); + + } + stream_exceptionGroup.reset(); + adaptor.addChild(root_1, (CommonTree)adaptor.create(EOR, "EOR")); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + if ( state.backtracking==0 ) { + + this.rules.add(((rule_scope)rule_stack.peek()).name); + + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + rule_stack.pop(); + } + return retval; + } + // $ANTLR end "rule" + + public static class ruleAction_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "ruleAction" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:179:1: ruleAction : '@' id ACTION -> ^( '@' id ACTION ) ; + public final ANTLRv3Parser.ruleAction_return ruleAction() throws RecognitionException { + ANTLRv3Parser.ruleAction_return retval = new ANTLRv3Parser.ruleAction_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token char_literal55=null; + Token ACTION57=null; + ANTLRv3Parser.id_return id56 = null; + + + CommonTree char_literal55_tree=null; + CommonTree ACTION57_tree=null; + RewriteRuleTokenStream stream_ACTION=new RewriteRuleTokenStream(adaptor,"token ACTION"); + RewriteRuleTokenStream stream_72=new RewriteRuleTokenStream(adaptor,"token 72"); + RewriteRuleSubtreeStream stream_id=new RewriteRuleSubtreeStream(adaptor,"rule id"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:181:2: ( '@' id ACTION -> ^( '@' id ACTION ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:181:4: '@' id ACTION + { + char_literal55=(Token)match(input,72,FOLLOW_72_in_ruleAction1038); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_72.add(char_literal55); + + pushFollow(FOLLOW_id_in_ruleAction1040); + id56=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id56.getTree()); + ACTION57=(Token)match(input,ACTION,FOLLOW_ACTION_in_ruleAction1042); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ACTION.add(ACTION57); + + + + // AST REWRITE + // elements: 72, ACTION, id + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 181:18: -> ^( '@' id ACTION ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:181:21: ^( '@' id ACTION ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_72.nextNode(), root_1); + + adaptor.addChild(root_1, stream_id.nextTree()); + adaptor.addChild(root_1, stream_ACTION.nextNode()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "ruleAction" + + public static class throwsSpec_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "throwsSpec" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:184:1: throwsSpec : 'throws' id ( ',' id )* -> ^( 'throws' ( id )+ ) ; + public final ANTLRv3Parser.throwsSpec_return throwsSpec() throws RecognitionException { + ANTLRv3Parser.throwsSpec_return retval = new ANTLRv3Parser.throwsSpec_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token string_literal58=null; + Token char_literal60=null; + ANTLRv3Parser.id_return id59 = null; + + ANTLRv3Parser.id_return id61 = null; + + + CommonTree string_literal58_tree=null; + CommonTree char_literal60_tree=null; + RewriteRuleTokenStream stream_81=new RewriteRuleTokenStream(adaptor,"token 81"); + RewriteRuleTokenStream stream_80=new RewriteRuleTokenStream(adaptor,"token 80"); + RewriteRuleSubtreeStream stream_id=new RewriteRuleSubtreeStream(adaptor,"rule id"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:185:2: ( 'throws' id ( ',' id )* -> ^( 'throws' ( id )+ ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:185:4: 'throws' id ( ',' id )* + { + string_literal58=(Token)match(input,80,FOLLOW_80_in_throwsSpec1063); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_80.add(string_literal58); + + pushFollow(FOLLOW_id_in_throwsSpec1065); + id59=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id59.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:185:16: ( ',' id )* + loop26: + do { + int alt26=2; + int LA26_0 = input.LA(1); + + if ( (LA26_0==81) ) { + alt26=1; + } + + + switch (alt26) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:185:18: ',' id + { + char_literal60=(Token)match(input,81,FOLLOW_81_in_throwsSpec1069); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_81.add(char_literal60); + + pushFollow(FOLLOW_id_in_throwsSpec1071); + id61=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id61.getTree()); + + } + break; + + default : + break loop26; + } + } while (true); + + + + // AST REWRITE + // elements: id, 80 + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 185:28: -> ^( 'throws' ( id )+ ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:185:31: ^( 'throws' ( id )+ ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_80.nextNode(), root_1); + + if ( !(stream_id.hasNext()) ) { + throw new RewriteEarlyExitException(); + } + while ( stream_id.hasNext() ) { + adaptor.addChild(root_1, stream_id.nextTree()); + + } + stream_id.reset(); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "throwsSpec" + + public static class ruleScopeSpec_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "ruleScopeSpec" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:188:1: ruleScopeSpec : ( 'scope' ACTION -> ^( 'scope' ACTION ) | 'scope' id ( ',' id )* ';' -> ^( 'scope' ( id )+ ) | 'scope' ACTION 'scope' id ( ',' id )* ';' -> ^( 'scope' ACTION ( id )+ ) ); + public final ANTLRv3Parser.ruleScopeSpec_return ruleScopeSpec() throws RecognitionException { + ANTLRv3Parser.ruleScopeSpec_return retval = new ANTLRv3Parser.ruleScopeSpec_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token string_literal62=null; + Token ACTION63=null; + Token string_literal64=null; + Token char_literal66=null; + Token char_literal68=null; + Token string_literal69=null; + Token ACTION70=null; + Token string_literal71=null; + Token char_literal73=null; + Token char_literal75=null; + ANTLRv3Parser.id_return id65 = null; + + ANTLRv3Parser.id_return id67 = null; + + ANTLRv3Parser.id_return id72 = null; + + ANTLRv3Parser.id_return id74 = null; + + + CommonTree string_literal62_tree=null; + CommonTree ACTION63_tree=null; + CommonTree string_literal64_tree=null; + CommonTree char_literal66_tree=null; + CommonTree char_literal68_tree=null; + CommonTree string_literal69_tree=null; + CommonTree ACTION70_tree=null; + CommonTree string_literal71_tree=null; + CommonTree char_literal73_tree=null; + CommonTree char_literal75_tree=null; + RewriteRuleTokenStream stream_ACTION=new RewriteRuleTokenStream(adaptor,"token ACTION"); + RewriteRuleTokenStream stream_81=new RewriteRuleTokenStream(adaptor,"token 81"); + RewriteRuleTokenStream stream_69=new RewriteRuleTokenStream(adaptor,"token 69"); + RewriteRuleTokenStream stream_SCOPE=new RewriteRuleTokenStream(adaptor,"token SCOPE"); + RewriteRuleSubtreeStream stream_id=new RewriteRuleSubtreeStream(adaptor,"rule id"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:189:2: ( 'scope' ACTION -> ^( 'scope' ACTION ) | 'scope' id ( ',' id )* ';' -> ^( 'scope' ( id )+ ) | 'scope' ACTION 'scope' id ( ',' id )* ';' -> ^( 'scope' ACTION ( id )+ ) ) + int alt29=3; + int LA29_0 = input.LA(1); + + if ( (LA29_0==SCOPE) ) { + int LA29_1 = input.LA(2); + + if ( (LA29_1==ACTION) ) { + int LA29_2 = input.LA(3); + + if ( (LA29_2==SCOPE) ) { + alt29=3; + } + else if ( (LA29_2==72||LA29_2==79) ) { + alt29=1; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 29, 2, input); + + throw nvae; + } + } + else if ( (LA29_1==TOKEN_REF||LA29_1==RULE_REF) ) { + alt29=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 29, 1, input); + + throw nvae; + } + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 29, 0, input); + + throw nvae; + } + switch (alt29) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:189:4: 'scope' ACTION + { + string_literal62=(Token)match(input,SCOPE,FOLLOW_SCOPE_in_ruleScopeSpec1094); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_SCOPE.add(string_literal62); + + ACTION63=(Token)match(input,ACTION,FOLLOW_ACTION_in_ruleScopeSpec1096); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ACTION.add(ACTION63); + + + + // AST REWRITE + // elements: SCOPE, ACTION + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 189:19: -> ^( 'scope' ACTION ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:189:22: ^( 'scope' ACTION ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_SCOPE.nextNode(), root_1); + + adaptor.addChild(root_1, stream_ACTION.nextNode()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:190:4: 'scope' id ( ',' id )* ';' + { + string_literal64=(Token)match(input,SCOPE,FOLLOW_SCOPE_in_ruleScopeSpec1109); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_SCOPE.add(string_literal64); + + pushFollow(FOLLOW_id_in_ruleScopeSpec1111); + id65=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id65.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:190:15: ( ',' id )* + loop27: + do { + int alt27=2; + int LA27_0 = input.LA(1); + + if ( (LA27_0==81) ) { + alt27=1; + } + + + switch (alt27) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:190:16: ',' id + { + char_literal66=(Token)match(input,81,FOLLOW_81_in_ruleScopeSpec1114); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_81.add(char_literal66); + + pushFollow(FOLLOW_id_in_ruleScopeSpec1116); + id67=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id67.getTree()); + + } + break; + + default : + break loop27; + } + } while (true); + + char_literal68=(Token)match(input,69,FOLLOW_69_in_ruleScopeSpec1120); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_69.add(char_literal68); + + + + // AST REWRITE + // elements: SCOPE, id + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 190:29: -> ^( 'scope' ( id )+ ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:190:32: ^( 'scope' ( id )+ ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_SCOPE.nextNode(), root_1); + + if ( !(stream_id.hasNext()) ) { + throw new RewriteEarlyExitException(); + } + while ( stream_id.hasNext() ) { + adaptor.addChild(root_1, stream_id.nextTree()); + + } + stream_id.reset(); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:191:4: 'scope' ACTION 'scope' id ( ',' id )* ';' + { + string_literal69=(Token)match(input,SCOPE,FOLLOW_SCOPE_in_ruleScopeSpec1134); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_SCOPE.add(string_literal69); + + ACTION70=(Token)match(input,ACTION,FOLLOW_ACTION_in_ruleScopeSpec1136); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ACTION.add(ACTION70); + + string_literal71=(Token)match(input,SCOPE,FOLLOW_SCOPE_in_ruleScopeSpec1140); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_SCOPE.add(string_literal71); + + pushFollow(FOLLOW_id_in_ruleScopeSpec1142); + id72=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id72.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:192:14: ( ',' id )* + loop28: + do { + int alt28=2; + int LA28_0 = input.LA(1); + + if ( (LA28_0==81) ) { + alt28=1; + } + + + switch (alt28) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:192:15: ',' id + { + char_literal73=(Token)match(input,81,FOLLOW_81_in_ruleScopeSpec1145); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_81.add(char_literal73); + + pushFollow(FOLLOW_id_in_ruleScopeSpec1147); + id74=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id74.getTree()); + + } + break; + + default : + break loop28; + } + } while (true); + + char_literal75=(Token)match(input,69,FOLLOW_69_in_ruleScopeSpec1151); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_69.add(char_literal75); + + + + // AST REWRITE + // elements: ACTION, id, SCOPE + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 193:3: -> ^( 'scope' ACTION ( id )+ ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:193:6: ^( 'scope' ACTION ( id )+ ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_SCOPE.nextNode(), root_1); + + adaptor.addChild(root_1, stream_ACTION.nextNode()); + if ( !(stream_id.hasNext()) ) { + throw new RewriteEarlyExitException(); + } + while ( stream_id.hasNext() ) { + adaptor.addChild(root_1, stream_id.nextTree()); + + } + stream_id.reset(); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + + } + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "ruleScopeSpec" + + public static class block_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "block" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:196:1: block : lp= '(' ( (opts= optionsSpec )? ':' )? a1= alternative rewrite ( '|' a2= alternative rewrite )* rp= ')' -> ^( BLOCK[$lp,\"BLOCK\"] ( optionsSpec )? ( alternative ( rewrite )? )+ EOB[$rp,\"EOB\"] ) ; + public final ANTLRv3Parser.block_return block() throws RecognitionException { + ANTLRv3Parser.block_return retval = new ANTLRv3Parser.block_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token lp=null; + Token rp=null; + Token char_literal76=null; + Token char_literal78=null; + ANTLRv3Parser.optionsSpec_return opts = null; + + ANTLRv3Parser.alternative_return a1 = null; + + ANTLRv3Parser.alternative_return a2 = null; + + ANTLRv3Parser.rewrite_return rewrite77 = null; + + ANTLRv3Parser.rewrite_return rewrite79 = null; + + + CommonTree lp_tree=null; + CommonTree rp_tree=null; + CommonTree char_literal76_tree=null; + CommonTree char_literal78_tree=null; + RewriteRuleTokenStream stream_79=new RewriteRuleTokenStream(adaptor,"token 79"); + RewriteRuleTokenStream stream_83=new RewriteRuleTokenStream(adaptor,"token 83"); + RewriteRuleTokenStream stream_84=new RewriteRuleTokenStream(adaptor,"token 84"); + RewriteRuleTokenStream stream_82=new RewriteRuleTokenStream(adaptor,"token 82"); + RewriteRuleSubtreeStream stream_alternative=new RewriteRuleSubtreeStream(adaptor,"rule alternative"); + RewriteRuleSubtreeStream stream_optionsSpec=new RewriteRuleSubtreeStream(adaptor,"rule optionsSpec"); + RewriteRuleSubtreeStream stream_rewrite=new RewriteRuleSubtreeStream(adaptor,"rule rewrite"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:197:5: (lp= '(' ( (opts= optionsSpec )? ':' )? a1= alternative rewrite ( '|' a2= alternative rewrite )* rp= ')' -> ^( BLOCK[$lp,\"BLOCK\"] ( optionsSpec )? ( alternative ( rewrite )? )+ EOB[$rp,\"EOB\"] ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:197:9: lp= '(' ( (opts= optionsSpec )? ':' )? a1= alternative rewrite ( '|' a2= alternative rewrite )* rp= ')' + { + lp=(Token)match(input,82,FOLLOW_82_in_block1183); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_82.add(lp); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:198:3: ( (opts= optionsSpec )? ':' )? + int alt31=2; + int LA31_0 = input.LA(1); + + if ( (LA31_0==OPTIONS||LA31_0==79) ) { + alt31=1; + } + switch (alt31) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:198:5: (opts= optionsSpec )? ':' + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:198:5: (opts= optionsSpec )? + int alt30=2; + int LA30_0 = input.LA(1); + + if ( (LA30_0==OPTIONS) ) { + alt30=1; + } + switch (alt30) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:198:6: opts= optionsSpec + { + pushFollow(FOLLOW_optionsSpec_in_block1192); + opts=optionsSpec(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_optionsSpec.add(opts.getTree()); + + } + break; + + } + + char_literal76=(Token)match(input,79,FOLLOW_79_in_block1196); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_79.add(char_literal76); + + + } + break; + + } + + pushFollow(FOLLOW_alternative_in_block1205); + a1=alternative(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_alternative.add(a1.getTree()); + pushFollow(FOLLOW_rewrite_in_block1207); + rewrite77=rewrite(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite.add(rewrite77.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:199:26: ( '|' a2= alternative rewrite )* + loop32: + do { + int alt32=2; + int LA32_0 = input.LA(1); + + if ( (LA32_0==83) ) { + alt32=1; + } + + + switch (alt32) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:199:28: '|' a2= alternative rewrite + { + char_literal78=(Token)match(input,83,FOLLOW_83_in_block1211); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_83.add(char_literal78); + + pushFollow(FOLLOW_alternative_in_block1215); + a2=alternative(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_alternative.add(a2.getTree()); + pushFollow(FOLLOW_rewrite_in_block1217); + rewrite79=rewrite(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite.add(rewrite79.getTree()); + + } + break; + + default : + break loop32; + } + } while (true); + + rp=(Token)match(input,84,FOLLOW_84_in_block1232); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_84.add(rp); + + + + // AST REWRITE + // elements: optionsSpec, alternative, rewrite + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 201:9: -> ^( BLOCK[$lp,\"BLOCK\"] ( optionsSpec )? ( alternative ( rewrite )? )+ EOB[$rp,\"EOB\"] ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:201:12: ^( BLOCK[$lp,\"BLOCK\"] ( optionsSpec )? ( alternative ( rewrite )? )+ EOB[$rp,\"EOB\"] ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(BLOCK, lp, "BLOCK"), root_1); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:201:34: ( optionsSpec )? + if ( stream_optionsSpec.hasNext() ) { + adaptor.addChild(root_1, stream_optionsSpec.nextTree()); + + } + stream_optionsSpec.reset(); + if ( !(stream_alternative.hasNext()) ) { + throw new RewriteEarlyExitException(); + } + while ( stream_alternative.hasNext() ) { + adaptor.addChild(root_1, stream_alternative.nextTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:201:60: ( rewrite )? + if ( stream_rewrite.hasNext() ) { + adaptor.addChild(root_1, stream_rewrite.nextTree()); + + } + stream_rewrite.reset(); + + } + stream_alternative.reset(); + adaptor.addChild(root_1, (CommonTree)adaptor.create(EOB, rp, "EOB")); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "block" + + public static class altList_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "altList" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:204:1: altList : a1= alternative rewrite ( '|' a2= alternative rewrite )* -> ^( ( alternative ( rewrite )? )+ EOB[\"EOB\"] ) ; + public final ANTLRv3Parser.altList_return altList() throws RecognitionException { + ANTLRv3Parser.altList_return retval = new ANTLRv3Parser.altList_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token char_literal81=null; + ANTLRv3Parser.alternative_return a1 = null; + + ANTLRv3Parser.alternative_return a2 = null; + + ANTLRv3Parser.rewrite_return rewrite80 = null; + + ANTLRv3Parser.rewrite_return rewrite82 = null; + + + CommonTree char_literal81_tree=null; + RewriteRuleTokenStream stream_83=new RewriteRuleTokenStream(adaptor,"token 83"); + RewriteRuleSubtreeStream stream_alternative=new RewriteRuleSubtreeStream(adaptor,"rule alternative"); + RewriteRuleSubtreeStream stream_rewrite=new RewriteRuleSubtreeStream(adaptor,"rule rewrite"); + + // must create root manually as it's used by invoked rules in real antlr tool. + // leave here to demonstrate use of {...} in rewrite rule + // it's really BLOCK[firstToken,"BLOCK"]; set line/col to previous ( or : token. + CommonTree blkRoot = (CommonTree)adaptor.create(BLOCK,input.LT(-1),"BLOCK"); + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:211:5: (a1= alternative rewrite ( '|' a2= alternative rewrite )* -> ^( ( alternative ( rewrite )? )+ EOB[\"EOB\"] ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:211:9: a1= alternative rewrite ( '|' a2= alternative rewrite )* + { + pushFollow(FOLLOW_alternative_in_altList1289); + a1=alternative(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_alternative.add(a1.getTree()); + pushFollow(FOLLOW_rewrite_in_altList1291); + rewrite80=rewrite(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite.add(rewrite80.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:211:32: ( '|' a2= alternative rewrite )* + loop33: + do { + int alt33=2; + int LA33_0 = input.LA(1); + + if ( (LA33_0==83) ) { + alt33=1; + } + + + switch (alt33) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:211:34: '|' a2= alternative rewrite + { + char_literal81=(Token)match(input,83,FOLLOW_83_in_altList1295); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_83.add(char_literal81); + + pushFollow(FOLLOW_alternative_in_altList1299); + a2=alternative(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_alternative.add(a2.getTree()); + pushFollow(FOLLOW_rewrite_in_altList1301); + rewrite82=rewrite(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite.add(rewrite82.getTree()); + + } + break; + + default : + break loop33; + } + } while (true); + + + + // AST REWRITE + // elements: alternative, rewrite + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 212:3: -> ^( ( alternative ( rewrite )? )+ EOB[\"EOB\"] ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:212:6: ^( ( alternative ( rewrite )? )+ EOB[\"EOB\"] ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(blkRoot, root_1); + + if ( !(stream_alternative.hasNext()) ) { + throw new RewriteEarlyExitException(); + } + while ( stream_alternative.hasNext() ) { + adaptor.addChild(root_1, stream_alternative.nextTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:212:32: ( rewrite )? + if ( stream_rewrite.hasNext() ) { + adaptor.addChild(root_1, stream_rewrite.nextTree()); + + } + stream_rewrite.reset(); + + } + stream_alternative.reset(); + adaptor.addChild(root_1, (CommonTree)adaptor.create(EOB, "EOB")); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "altList" + + public static class alternative_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "alternative" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:215:1: alternative : ( ( element )+ -> ^( ALT[firstToken,\"ALT\"] ( element )+ EOA[\"EOA\"] ) | -> ^( ALT[prevToken,\"ALT\"] EPSILON[prevToken,\"EPSILON\"] EOA[\"EOA\"] ) ); + public final ANTLRv3Parser.alternative_return alternative() throws RecognitionException { + ANTLRv3Parser.alternative_return retval = new ANTLRv3Parser.alternative_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + ANTLRv3Parser.element_return element83 = null; + + + RewriteRuleSubtreeStream stream_element=new RewriteRuleSubtreeStream(adaptor,"rule element"); + + Token firstToken = input.LT(1); + Token prevToken = input.LT(-1); // either : or | I think + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:220:5: ( ( element )+ -> ^( ALT[firstToken,\"ALT\"] ( element )+ EOA[\"EOA\"] ) | -> ^( ALT[prevToken,\"ALT\"] EPSILON[prevToken,\"EPSILON\"] EOA[\"EOA\"] ) ) + int alt35=2; + int LA35_0 = input.LA(1); + + if ( (LA35_0==SEMPRED||LA35_0==TREE_BEGIN||(LA35_0>=TOKEN_REF && LA35_0<=ACTION)||LA35_0==RULE_REF||LA35_0==82||LA35_0==89||LA35_0==92) ) { + alt35=1; + } + else if ( (LA35_0==REWRITE||LA35_0==69||(LA35_0>=83 && LA35_0<=84)) ) { + alt35=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 35, 0, input); + + throw nvae; + } + switch (alt35) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:220:9: ( element )+ + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:220:9: ( element )+ + int cnt34=0; + loop34: + do { + int alt34=2; + int LA34_0 = input.LA(1); + + if ( (LA34_0==SEMPRED||LA34_0==TREE_BEGIN||(LA34_0>=TOKEN_REF && LA34_0<=ACTION)||LA34_0==RULE_REF||LA34_0==82||LA34_0==89||LA34_0==92) ) { + alt34=1; + } + + + switch (alt34) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:220:9: element + { + pushFollow(FOLLOW_element_in_alternative1349); + element83=element(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_element.add(element83.getTree()); + + } + break; + + default : + if ( cnt34 >= 1 ) break loop34; + if (state.backtracking>0) {state.failed=true; return retval;} + EarlyExitException eee = + new EarlyExitException(34, input); + throw eee; + } + cnt34++; + } while (true); + + + + // AST REWRITE + // elements: element + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 220:18: -> ^( ALT[firstToken,\"ALT\"] ( element )+ EOA[\"EOA\"] ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:220:21: ^( ALT[firstToken,\"ALT\"] ( element )+ EOA[\"EOA\"] ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(ALT, firstToken, "ALT"), root_1); + + if ( !(stream_element.hasNext()) ) { + throw new RewriteEarlyExitException(); + } + while ( stream_element.hasNext() ) { + adaptor.addChild(root_1, stream_element.nextTree()); + + } + stream_element.reset(); + adaptor.addChild(root_1, (CommonTree)adaptor.create(EOA, "EOA")); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:221:9: + { + + // AST REWRITE + // elements: + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 221:9: -> ^( ALT[prevToken,\"ALT\"] EPSILON[prevToken,\"EPSILON\"] EOA[\"EOA\"] ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:221:12: ^( ALT[prevToken,\"ALT\"] EPSILON[prevToken,\"EPSILON\"] EOA[\"EOA\"] ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(ALT, prevToken, "ALT"), root_1); + + adaptor.addChild(root_1, (CommonTree)adaptor.create(EPSILON, prevToken, "EPSILON")); + adaptor.addChild(root_1, (CommonTree)adaptor.create(EOA, "EOA")); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + + } + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "alternative" + + public static class exceptionGroup_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "exceptionGroup" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:224:1: exceptionGroup : ( ( exceptionHandler )+ ( finallyClause )? | finallyClause ); + public final ANTLRv3Parser.exceptionGroup_return exceptionGroup() throws RecognitionException { + ANTLRv3Parser.exceptionGroup_return retval = new ANTLRv3Parser.exceptionGroup_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + ANTLRv3Parser.exceptionHandler_return exceptionHandler84 = null; + + ANTLRv3Parser.finallyClause_return finallyClause85 = null; + + ANTLRv3Parser.finallyClause_return finallyClause86 = null; + + + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:225:2: ( ( exceptionHandler )+ ( finallyClause )? | finallyClause ) + int alt38=2; + int LA38_0 = input.LA(1); + + if ( (LA38_0==85) ) { + alt38=1; + } + else if ( (LA38_0==86) ) { + alt38=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 38, 0, input); + + throw nvae; + } + switch (alt38) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:225:4: ( exceptionHandler )+ ( finallyClause )? + { + root_0 = (CommonTree)adaptor.nil(); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:225:4: ( exceptionHandler )+ + int cnt36=0; + loop36: + do { + int alt36=2; + int LA36_0 = input.LA(1); + + if ( (LA36_0==85) ) { + alt36=1; + } + + + switch (alt36) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:225:6: exceptionHandler + { + pushFollow(FOLLOW_exceptionHandler_in_exceptionGroup1400); + exceptionHandler84=exceptionHandler(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) adaptor.addChild(root_0, exceptionHandler84.getTree()); + + } + break; + + default : + if ( cnt36 >= 1 ) break loop36; + if (state.backtracking>0) {state.failed=true; return retval;} + EarlyExitException eee = + new EarlyExitException(36, input); + throw eee; + } + cnt36++; + } while (true); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:225:26: ( finallyClause )? + int alt37=2; + int LA37_0 = input.LA(1); + + if ( (LA37_0==86) ) { + alt37=1; + } + switch (alt37) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:225:28: finallyClause + { + pushFollow(FOLLOW_finallyClause_in_exceptionGroup1407); + finallyClause85=finallyClause(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) adaptor.addChild(root_0, finallyClause85.getTree()); + + } + break; + + } + + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:226:4: finallyClause + { + root_0 = (CommonTree)adaptor.nil(); + + pushFollow(FOLLOW_finallyClause_in_exceptionGroup1415); + finallyClause86=finallyClause(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) adaptor.addChild(root_0, finallyClause86.getTree()); + + } + break; + + } + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "exceptionGroup" + + public static class exceptionHandler_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "exceptionHandler" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:229:1: exceptionHandler : 'catch' ARG_ACTION ACTION -> ^( 'catch' ARG_ACTION ACTION ) ; + public final ANTLRv3Parser.exceptionHandler_return exceptionHandler() throws RecognitionException { + ANTLRv3Parser.exceptionHandler_return retval = new ANTLRv3Parser.exceptionHandler_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token string_literal87=null; + Token ARG_ACTION88=null; + Token ACTION89=null; + + CommonTree string_literal87_tree=null; + CommonTree ARG_ACTION88_tree=null; + CommonTree ACTION89_tree=null; + RewriteRuleTokenStream stream_85=new RewriteRuleTokenStream(adaptor,"token 85"); + RewriteRuleTokenStream stream_ARG_ACTION=new RewriteRuleTokenStream(adaptor,"token ARG_ACTION"); + RewriteRuleTokenStream stream_ACTION=new RewriteRuleTokenStream(adaptor,"token ACTION"); + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:230:5: ( 'catch' ARG_ACTION ACTION -> ^( 'catch' ARG_ACTION ACTION ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:230:10: 'catch' ARG_ACTION ACTION + { + string_literal87=(Token)match(input,85,FOLLOW_85_in_exceptionHandler1435); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_85.add(string_literal87); + + ARG_ACTION88=(Token)match(input,ARG_ACTION,FOLLOW_ARG_ACTION_in_exceptionHandler1437); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ARG_ACTION.add(ARG_ACTION88); + + ACTION89=(Token)match(input,ACTION,FOLLOW_ACTION_in_exceptionHandler1439); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ACTION.add(ACTION89); + + + + // AST REWRITE + // elements: ACTION, ARG_ACTION, 85 + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 230:36: -> ^( 'catch' ARG_ACTION ACTION ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:230:39: ^( 'catch' ARG_ACTION ACTION ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_85.nextNode(), root_1); + + adaptor.addChild(root_1, stream_ARG_ACTION.nextNode()); + adaptor.addChild(root_1, stream_ACTION.nextNode()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "exceptionHandler" + + public static class finallyClause_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "finallyClause" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:233:1: finallyClause : 'finally' ACTION -> ^( 'finally' ACTION ) ; + public final ANTLRv3Parser.finallyClause_return finallyClause() throws RecognitionException { + ANTLRv3Parser.finallyClause_return retval = new ANTLRv3Parser.finallyClause_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token string_literal90=null; + Token ACTION91=null; + + CommonTree string_literal90_tree=null; + CommonTree ACTION91_tree=null; + RewriteRuleTokenStream stream_ACTION=new RewriteRuleTokenStream(adaptor,"token ACTION"); + RewriteRuleTokenStream stream_86=new RewriteRuleTokenStream(adaptor,"token 86"); + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:234:5: ( 'finally' ACTION -> ^( 'finally' ACTION ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:234:10: 'finally' ACTION + { + string_literal90=(Token)match(input,86,FOLLOW_86_in_finallyClause1469); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_86.add(string_literal90); + + ACTION91=(Token)match(input,ACTION,FOLLOW_ACTION_in_finallyClause1471); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ACTION.add(ACTION91); + + + + // AST REWRITE + // elements: 86, ACTION + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 234:27: -> ^( 'finally' ACTION ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:234:30: ^( 'finally' ACTION ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_86.nextNode(), root_1); + + adaptor.addChild(root_1, stream_ACTION.nextNode()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "finallyClause" + + public static class element_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "element" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:237:1: element : elementNoOptionSpec ; + public final ANTLRv3Parser.element_return element() throws RecognitionException { + ANTLRv3Parser.element_return retval = new ANTLRv3Parser.element_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + ANTLRv3Parser.elementNoOptionSpec_return elementNoOptionSpec92 = null; + + + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:238:2: ( elementNoOptionSpec ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:238:4: elementNoOptionSpec + { + root_0 = (CommonTree)adaptor.nil(); + + pushFollow(FOLLOW_elementNoOptionSpec_in_element1493); + elementNoOptionSpec92=elementNoOptionSpec(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) adaptor.addChild(root_0, elementNoOptionSpec92.getTree()); + + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "element" + + public static class elementNoOptionSpec_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "elementNoOptionSpec" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:241:1: elementNoOptionSpec : ( id (labelOp= '=' | labelOp= '+=' ) atom ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id atom ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> ^( $labelOp id atom ) ) | id (labelOp= '=' | labelOp= '+=' ) block ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id block ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> ^( $labelOp id block ) ) | atom ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] atom EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> atom ) | ebnf | ACTION | SEMPRED ( '=>' -> GATED_SEMPRED | -> SEMPRED ) | treeSpec ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] treeSpec EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> treeSpec ) ); + public final ANTLRv3Parser.elementNoOptionSpec_return elementNoOptionSpec() throws RecognitionException { + ANTLRv3Parser.elementNoOptionSpec_return retval = new ANTLRv3Parser.elementNoOptionSpec_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token labelOp=null; + Token ACTION102=null; + Token SEMPRED103=null; + Token string_literal104=null; + ANTLRv3Parser.id_return id93 = null; + + ANTLRv3Parser.atom_return atom94 = null; + + ANTLRv3Parser.ebnfSuffix_return ebnfSuffix95 = null; + + ANTLRv3Parser.id_return id96 = null; + + ANTLRv3Parser.block_return block97 = null; + + ANTLRv3Parser.ebnfSuffix_return ebnfSuffix98 = null; + + ANTLRv3Parser.atom_return atom99 = null; + + ANTLRv3Parser.ebnfSuffix_return ebnfSuffix100 = null; + + ANTLRv3Parser.ebnf_return ebnf101 = null; + + ANTLRv3Parser.treeSpec_return treeSpec105 = null; + + ANTLRv3Parser.ebnfSuffix_return ebnfSuffix106 = null; + + + CommonTree labelOp_tree=null; + CommonTree ACTION102_tree=null; + CommonTree SEMPRED103_tree=null; + CommonTree string_literal104_tree=null; + RewriteRuleTokenStream stream_71=new RewriteRuleTokenStream(adaptor,"token 71"); + RewriteRuleTokenStream stream_SEMPRED=new RewriteRuleTokenStream(adaptor,"token SEMPRED"); + RewriteRuleTokenStream stream_87=new RewriteRuleTokenStream(adaptor,"token 87"); + RewriteRuleTokenStream stream_88=new RewriteRuleTokenStream(adaptor,"token 88"); + RewriteRuleSubtreeStream stream_treeSpec=new RewriteRuleSubtreeStream(adaptor,"rule treeSpec"); + RewriteRuleSubtreeStream stream_atom=new RewriteRuleSubtreeStream(adaptor,"rule atom"); + RewriteRuleSubtreeStream stream_ebnfSuffix=new RewriteRuleSubtreeStream(adaptor,"rule ebnfSuffix"); + RewriteRuleSubtreeStream stream_block=new RewriteRuleSubtreeStream(adaptor,"rule block"); + RewriteRuleSubtreeStream stream_id=new RewriteRuleSubtreeStream(adaptor,"rule id"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:242:2: ( id (labelOp= '=' | labelOp= '+=' ) atom ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id atom ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> ^( $labelOp id atom ) ) | id (labelOp= '=' | labelOp= '+=' ) block ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id block ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> ^( $labelOp id block ) ) | atom ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] atom EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> atom ) | ebnf | ACTION | SEMPRED ( '=>' -> GATED_SEMPRED | -> SEMPRED ) | treeSpec ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] treeSpec EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> treeSpec ) ) + int alt46=7; + alt46 = dfa46.predict(input); + switch (alt46) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:242:4: id (labelOp= '=' | labelOp= '+=' ) atom ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id atom ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> ^( $labelOp id atom ) ) + { + pushFollow(FOLLOW_id_in_elementNoOptionSpec1504); + id93=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id93.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:242:7: (labelOp= '=' | labelOp= '+=' ) + int alt39=2; + int LA39_0 = input.LA(1); + + if ( (LA39_0==71) ) { + alt39=1; + } + else if ( (LA39_0==87) ) { + alt39=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 39, 0, input); + + throw nvae; + } + switch (alt39) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:242:8: labelOp= '=' + { + labelOp=(Token)match(input,71,FOLLOW_71_in_elementNoOptionSpec1509); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_71.add(labelOp); + + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:242:20: labelOp= '+=' + { + labelOp=(Token)match(input,87,FOLLOW_87_in_elementNoOptionSpec1513); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_87.add(labelOp); + + + } + break; + + } + + pushFollow(FOLLOW_atom_in_elementNoOptionSpec1516); + atom94=atom(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_atom.add(atom94.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:243:3: ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id atom ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> ^( $labelOp id atom ) ) + int alt40=2; + int LA40_0 = input.LA(1); + + if ( (LA40_0==74||(LA40_0>=90 && LA40_0<=91)) ) { + alt40=1; + } + else if ( (LA40_0==SEMPRED||LA40_0==TREE_BEGIN||LA40_0==REWRITE||(LA40_0>=TOKEN_REF && LA40_0<=ACTION)||LA40_0==RULE_REF||LA40_0==69||(LA40_0>=82 && LA40_0<=84)||LA40_0==89||LA40_0==92) ) { + alt40=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 40, 0, input); + + throw nvae; + } + switch (alt40) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:243:5: ebnfSuffix + { + pushFollow(FOLLOW_ebnfSuffix_in_elementNoOptionSpec1522); + ebnfSuffix95=ebnfSuffix(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ebnfSuffix.add(ebnfSuffix95.getTree()); + + + // AST REWRITE + // elements: atom, labelOp, ebnfSuffix, id + // token labels: labelOp + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleTokenStream stream_labelOp=new RewriteRuleTokenStream(adaptor,"token labelOp",labelOp); + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 243:16: -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id atom ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:243:19: ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id atom ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_ebnfSuffix.nextNode(), root_1); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:243:33: ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id atom ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) + { + CommonTree root_2 = (CommonTree)adaptor.nil(); + root_2 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(BLOCK, "BLOCK"), root_2); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:243:50: ^( ALT[\"ALT\"] ^( $labelOp id atom ) EOA[\"EOA\"] ) + { + CommonTree root_3 = (CommonTree)adaptor.nil(); + root_3 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(ALT, "ALT"), root_3); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:243:63: ^( $labelOp id atom ) + { + CommonTree root_4 = (CommonTree)adaptor.nil(); + root_4 = (CommonTree)adaptor.becomeRoot(stream_labelOp.nextNode(), root_4); + + adaptor.addChild(root_4, stream_id.nextTree()); + adaptor.addChild(root_4, stream_atom.nextTree()); + + adaptor.addChild(root_3, root_4); + } + adaptor.addChild(root_3, (CommonTree)adaptor.create(EOA, "EOA")); + + adaptor.addChild(root_2, root_3); + } + adaptor.addChild(root_2, (CommonTree)adaptor.create(EOB, "EOB")); + + adaptor.addChild(root_1, root_2); + } + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:244:8: + { + + // AST REWRITE + // elements: id, atom, labelOp + // token labels: labelOp + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleTokenStream stream_labelOp=new RewriteRuleTokenStream(adaptor,"token labelOp",labelOp); + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 244:8: -> ^( $labelOp id atom ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:244:11: ^( $labelOp id atom ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_labelOp.nextNode(), root_1); + + adaptor.addChild(root_1, stream_id.nextTree()); + adaptor.addChild(root_1, stream_atom.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + + } + + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:246:4: id (labelOp= '=' | labelOp= '+=' ) block ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id block ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> ^( $labelOp id block ) ) + { + pushFollow(FOLLOW_id_in_elementNoOptionSpec1581); + id96=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id96.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:246:7: (labelOp= '=' | labelOp= '+=' ) + int alt41=2; + int LA41_0 = input.LA(1); + + if ( (LA41_0==71) ) { + alt41=1; + } + else if ( (LA41_0==87) ) { + alt41=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 41, 0, input); + + throw nvae; + } + switch (alt41) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:246:8: labelOp= '=' + { + labelOp=(Token)match(input,71,FOLLOW_71_in_elementNoOptionSpec1586); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_71.add(labelOp); + + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:246:20: labelOp= '+=' + { + labelOp=(Token)match(input,87,FOLLOW_87_in_elementNoOptionSpec1590); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_87.add(labelOp); + + + } + break; + + } + + pushFollow(FOLLOW_block_in_elementNoOptionSpec1593); + block97=block(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_block.add(block97.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:247:3: ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id block ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> ^( $labelOp id block ) ) + int alt42=2; + int LA42_0 = input.LA(1); + + if ( (LA42_0==74||(LA42_0>=90 && LA42_0<=91)) ) { + alt42=1; + } + else if ( (LA42_0==SEMPRED||LA42_0==TREE_BEGIN||LA42_0==REWRITE||(LA42_0>=TOKEN_REF && LA42_0<=ACTION)||LA42_0==RULE_REF||LA42_0==69||(LA42_0>=82 && LA42_0<=84)||LA42_0==89||LA42_0==92) ) { + alt42=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 42, 0, input); + + throw nvae; + } + switch (alt42) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:247:5: ebnfSuffix + { + pushFollow(FOLLOW_ebnfSuffix_in_elementNoOptionSpec1599); + ebnfSuffix98=ebnfSuffix(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ebnfSuffix.add(ebnfSuffix98.getTree()); + + + // AST REWRITE + // elements: labelOp, ebnfSuffix, id, block + // token labels: labelOp + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleTokenStream stream_labelOp=new RewriteRuleTokenStream(adaptor,"token labelOp",labelOp); + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 247:16: -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id block ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:247:19: ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id block ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_ebnfSuffix.nextNode(), root_1); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:247:33: ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id block ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) + { + CommonTree root_2 = (CommonTree)adaptor.nil(); + root_2 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(BLOCK, "BLOCK"), root_2); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:247:50: ^( ALT[\"ALT\"] ^( $labelOp id block ) EOA[\"EOA\"] ) + { + CommonTree root_3 = (CommonTree)adaptor.nil(); + root_3 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(ALT, "ALT"), root_3); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:247:63: ^( $labelOp id block ) + { + CommonTree root_4 = (CommonTree)adaptor.nil(); + root_4 = (CommonTree)adaptor.becomeRoot(stream_labelOp.nextNode(), root_4); + + adaptor.addChild(root_4, stream_id.nextTree()); + adaptor.addChild(root_4, stream_block.nextTree()); + + adaptor.addChild(root_3, root_4); + } + adaptor.addChild(root_3, (CommonTree)adaptor.create(EOA, "EOA")); + + adaptor.addChild(root_2, root_3); + } + adaptor.addChild(root_2, (CommonTree)adaptor.create(EOB, "EOB")); + + adaptor.addChild(root_1, root_2); + } + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:248:8: + { + + // AST REWRITE + // elements: id, labelOp, block + // token labels: labelOp + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleTokenStream stream_labelOp=new RewriteRuleTokenStream(adaptor,"token labelOp",labelOp); + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 248:8: -> ^( $labelOp id block ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:248:11: ^( $labelOp id block ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_labelOp.nextNode(), root_1); + + adaptor.addChild(root_1, stream_id.nextTree()); + adaptor.addChild(root_1, stream_block.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + + } + + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:250:4: atom ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] atom EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> atom ) + { + pushFollow(FOLLOW_atom_in_elementNoOptionSpec1658); + atom99=atom(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_atom.add(atom99.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:251:3: ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] atom EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> atom ) + int alt43=2; + int LA43_0 = input.LA(1); + + if ( (LA43_0==74||(LA43_0>=90 && LA43_0<=91)) ) { + alt43=1; + } + else if ( (LA43_0==SEMPRED||LA43_0==TREE_BEGIN||LA43_0==REWRITE||(LA43_0>=TOKEN_REF && LA43_0<=ACTION)||LA43_0==RULE_REF||LA43_0==69||(LA43_0>=82 && LA43_0<=84)||LA43_0==89||LA43_0==92) ) { + alt43=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 43, 0, input); + + throw nvae; + } + switch (alt43) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:251:5: ebnfSuffix + { + pushFollow(FOLLOW_ebnfSuffix_in_elementNoOptionSpec1664); + ebnfSuffix100=ebnfSuffix(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ebnfSuffix.add(ebnfSuffix100.getTree()); + + + // AST REWRITE + // elements: atom, ebnfSuffix + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 251:16: -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] atom EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:251:19: ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] atom EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_ebnfSuffix.nextNode(), root_1); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:251:33: ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] atom EOA[\"EOA\"] ) EOB[\"EOB\"] ) + { + CommonTree root_2 = (CommonTree)adaptor.nil(); + root_2 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(BLOCK, "BLOCK"), root_2); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:251:50: ^( ALT[\"ALT\"] atom EOA[\"EOA\"] ) + { + CommonTree root_3 = (CommonTree)adaptor.nil(); + root_3 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(ALT, "ALT"), root_3); + + adaptor.addChild(root_3, stream_atom.nextTree()); + adaptor.addChild(root_3, (CommonTree)adaptor.create(EOA, "EOA")); + + adaptor.addChild(root_2, root_3); + } + adaptor.addChild(root_2, (CommonTree)adaptor.create(EOB, "EOB")); + + adaptor.addChild(root_1, root_2); + } + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:252:8: + { + + // AST REWRITE + // elements: atom + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 252:8: -> atom + { + adaptor.addChild(root_0, stream_atom.nextTree()); + + } + + retval.tree = root_0;} + } + break; + + } + + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:254:4: ebnf + { + root_0 = (CommonTree)adaptor.nil(); + + pushFollow(FOLLOW_ebnf_in_elementNoOptionSpec1710); + ebnf101=ebnf(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) adaptor.addChild(root_0, ebnf101.getTree()); + + } + break; + case 5 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:255:6: ACTION + { + root_0 = (CommonTree)adaptor.nil(); + + ACTION102=(Token)match(input,ACTION,FOLLOW_ACTION_in_elementNoOptionSpec1717); if (state.failed) return retval; + if ( state.backtracking==0 ) { + ACTION102_tree = (CommonTree)adaptor.create(ACTION102); + adaptor.addChild(root_0, ACTION102_tree); + } + + } + break; + case 6 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:256:6: SEMPRED ( '=>' -> GATED_SEMPRED | -> SEMPRED ) + { + SEMPRED103=(Token)match(input,SEMPRED,FOLLOW_SEMPRED_in_elementNoOptionSpec1724); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_SEMPRED.add(SEMPRED103); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:256:14: ( '=>' -> GATED_SEMPRED | -> SEMPRED ) + int alt44=2; + int LA44_0 = input.LA(1); + + if ( (LA44_0==88) ) { + alt44=1; + } + else if ( (LA44_0==SEMPRED||LA44_0==TREE_BEGIN||LA44_0==REWRITE||(LA44_0>=TOKEN_REF && LA44_0<=ACTION)||LA44_0==RULE_REF||LA44_0==69||(LA44_0>=82 && LA44_0<=84)||LA44_0==89||LA44_0==92) ) { + alt44=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 44, 0, input); + + throw nvae; + } + switch (alt44) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:256:16: '=>' + { + string_literal104=(Token)match(input,88,FOLLOW_88_in_elementNoOptionSpec1728); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_88.add(string_literal104); + + + + // AST REWRITE + // elements: + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 256:21: -> GATED_SEMPRED + { + adaptor.addChild(root_0, (CommonTree)adaptor.create(GATED_SEMPRED, "GATED_SEMPRED")); + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:256:40: + { + + // AST REWRITE + // elements: SEMPRED + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 256:40: -> SEMPRED + { + adaptor.addChild(root_0, stream_SEMPRED.nextNode()); + + } + + retval.tree = root_0;} + } + break; + + } + + + } + break; + case 7 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:257:6: treeSpec ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] treeSpec EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> treeSpec ) + { + pushFollow(FOLLOW_treeSpec_in_elementNoOptionSpec1747); + treeSpec105=treeSpec(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_treeSpec.add(treeSpec105.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:258:3: ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] treeSpec EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> treeSpec ) + int alt45=2; + int LA45_0 = input.LA(1); + + if ( (LA45_0==74||(LA45_0>=90 && LA45_0<=91)) ) { + alt45=1; + } + else if ( (LA45_0==SEMPRED||LA45_0==TREE_BEGIN||LA45_0==REWRITE||(LA45_0>=TOKEN_REF && LA45_0<=ACTION)||LA45_0==RULE_REF||LA45_0==69||(LA45_0>=82 && LA45_0<=84)||LA45_0==89||LA45_0==92) ) { + alt45=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 45, 0, input); + + throw nvae; + } + switch (alt45) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:258:5: ebnfSuffix + { + pushFollow(FOLLOW_ebnfSuffix_in_elementNoOptionSpec1753); + ebnfSuffix106=ebnfSuffix(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ebnfSuffix.add(ebnfSuffix106.getTree()); + + + // AST REWRITE + // elements: ebnfSuffix, treeSpec + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 258:16: -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] treeSpec EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:258:19: ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] treeSpec EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_ebnfSuffix.nextNode(), root_1); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:258:33: ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] treeSpec EOA[\"EOA\"] ) EOB[\"EOB\"] ) + { + CommonTree root_2 = (CommonTree)adaptor.nil(); + root_2 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(BLOCK, "BLOCK"), root_2); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:258:50: ^( ALT[\"ALT\"] treeSpec EOA[\"EOA\"] ) + { + CommonTree root_3 = (CommonTree)adaptor.nil(); + root_3 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(ALT, "ALT"), root_3); + + adaptor.addChild(root_3, stream_treeSpec.nextTree()); + adaptor.addChild(root_3, (CommonTree)adaptor.create(EOA, "EOA")); + + adaptor.addChild(root_2, root_3); + } + adaptor.addChild(root_2, (CommonTree)adaptor.create(EOB, "EOB")); + + adaptor.addChild(root_1, root_2); + } + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:259:8: + { + + // AST REWRITE + // elements: treeSpec + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 259:8: -> treeSpec + { + adaptor.addChild(root_0, stream_treeSpec.nextTree()); + + } + + retval.tree = root_0;} + } + break; + + } + + + } + break; + + } + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "elementNoOptionSpec" + + public static class atom_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "atom" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:263:1: atom : ( range ( (op= '^' | op= '!' ) -> ^( $op range ) | -> range ) | terminal | notSet ( (op= '^' | op= '!' ) -> ^( $op notSet ) | -> notSet ) | RULE_REF (arg= ARG_ACTION )? ( (op= '^' | op= '!' ) )? -> {$arg!=null&&op!=null}? ^( $op RULE_REF $arg) -> {$arg!=null}? ^( RULE_REF $arg) -> {$op!=null}? ^( $op RULE_REF ) -> RULE_REF ); + public final ANTLRv3Parser.atom_return atom() throws RecognitionException { + ANTLRv3Parser.atom_return retval = new ANTLRv3Parser.atom_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token op=null; + Token arg=null; + Token RULE_REF110=null; + ANTLRv3Parser.range_return range107 = null; + + ANTLRv3Parser.terminal_return terminal108 = null; + + ANTLRv3Parser.notSet_return notSet109 = null; + + + CommonTree op_tree=null; + CommonTree arg_tree=null; + CommonTree RULE_REF110_tree=null; + RewriteRuleTokenStream stream_ARG_ACTION=new RewriteRuleTokenStream(adaptor,"token ARG_ACTION"); + RewriteRuleTokenStream stream_ROOT=new RewriteRuleTokenStream(adaptor,"token ROOT"); + RewriteRuleTokenStream stream_RULE_REF=new RewriteRuleTokenStream(adaptor,"token RULE_REF"); + RewriteRuleTokenStream stream_BANG=new RewriteRuleTokenStream(adaptor,"token BANG"); + RewriteRuleSubtreeStream stream_notSet=new RewriteRuleSubtreeStream(adaptor,"rule notSet"); + RewriteRuleSubtreeStream stream_range=new RewriteRuleSubtreeStream(adaptor,"rule range"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:263:5: ( range ( (op= '^' | op= '!' ) -> ^( $op range ) | -> range ) | terminal | notSet ( (op= '^' | op= '!' ) -> ^( $op notSet ) | -> notSet ) | RULE_REF (arg= ARG_ACTION )? ( (op= '^' | op= '!' ) )? -> {$arg!=null&&op!=null}? ^( $op RULE_REF $arg) -> {$arg!=null}? ^( RULE_REF $arg) -> {$op!=null}? ^( $op RULE_REF ) -> RULE_REF ) + int alt54=4; + switch ( input.LA(1) ) { + case CHAR_LITERAL: + { + int LA54_1 = input.LA(2); + + if ( (LA54_1==RANGE) ) { + alt54=1; + } + else if ( (LA54_1==SEMPRED||(LA54_1>=TREE_BEGIN && LA54_1<=REWRITE)||(LA54_1>=TOKEN_REF && LA54_1<=ACTION)||LA54_1==RULE_REF||LA54_1==69||LA54_1==74||(LA54_1>=82 && LA54_1<=84)||(LA54_1>=89 && LA54_1<=92)) ) { + alt54=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 54, 1, input); + + throw nvae; + } + } + break; + case TOKEN_REF: + case STRING_LITERAL: + case 92: + { + alt54=2; + } + break; + case 89: + { + alt54=3; + } + break; + case RULE_REF: + { + alt54=4; + } + break; + default: + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 54, 0, input); + + throw nvae; + } + + switch (alt54) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:263:9: range ( (op= '^' | op= '!' ) -> ^( $op range ) | -> range ) + { + pushFollow(FOLLOW_range_in_atom1805); + range107=range(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_range.add(range107.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:263:15: ( (op= '^' | op= '!' ) -> ^( $op range ) | -> range ) + int alt48=2; + int LA48_0 = input.LA(1); + + if ( ((LA48_0>=ROOT && LA48_0<=BANG)) ) { + alt48=1; + } + else if ( (LA48_0==SEMPRED||LA48_0==TREE_BEGIN||LA48_0==REWRITE||(LA48_0>=TOKEN_REF && LA48_0<=ACTION)||LA48_0==RULE_REF||LA48_0==69||LA48_0==74||(LA48_0>=82 && LA48_0<=84)||(LA48_0>=89 && LA48_0<=92)) ) { + alt48=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 48, 0, input); + + throw nvae; + } + switch (alt48) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:263:17: (op= '^' | op= '!' ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:263:17: (op= '^' | op= '!' ) + int alt47=2; + int LA47_0 = input.LA(1); + + if ( (LA47_0==ROOT) ) { + alt47=1; + } + else if ( (LA47_0==BANG) ) { + alt47=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 47, 0, input); + + throw nvae; + } + switch (alt47) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:263:18: op= '^' + { + op=(Token)match(input,ROOT,FOLLOW_ROOT_in_atom1812); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ROOT.add(op); + + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:263:25: op= '!' + { + op=(Token)match(input,BANG,FOLLOW_BANG_in_atom1816); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_BANG.add(op); + + + } + break; + + } + + + + // AST REWRITE + // elements: range, op + // token labels: op + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleTokenStream stream_op=new RewriteRuleTokenStream(adaptor,"token op",op); + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 263:33: -> ^( $op range ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:263:36: ^( $op range ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_op.nextNode(), root_1); + + adaptor.addChild(root_1, stream_range.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:263:51: + { + + // AST REWRITE + // elements: range + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 263:51: -> range + { + adaptor.addChild(root_0, stream_range.nextTree()); + + } + + retval.tree = root_0;} + } + break; + + } + + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:264:9: terminal + { + root_0 = (CommonTree)adaptor.nil(); + + pushFollow(FOLLOW_terminal_in_atom1844); + terminal108=terminal(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) adaptor.addChild(root_0, terminal108.getTree()); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:265:7: notSet ( (op= '^' | op= '!' ) -> ^( $op notSet ) | -> notSet ) + { + pushFollow(FOLLOW_notSet_in_atom1852); + notSet109=notSet(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_notSet.add(notSet109.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:265:14: ( (op= '^' | op= '!' ) -> ^( $op notSet ) | -> notSet ) + int alt50=2; + int LA50_0 = input.LA(1); + + if ( ((LA50_0>=ROOT && LA50_0<=BANG)) ) { + alt50=1; + } + else if ( (LA50_0==SEMPRED||LA50_0==TREE_BEGIN||LA50_0==REWRITE||(LA50_0>=TOKEN_REF && LA50_0<=ACTION)||LA50_0==RULE_REF||LA50_0==69||LA50_0==74||(LA50_0>=82 && LA50_0<=84)||(LA50_0>=89 && LA50_0<=92)) ) { + alt50=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 50, 0, input); + + throw nvae; + } + switch (alt50) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:265:16: (op= '^' | op= '!' ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:265:16: (op= '^' | op= '!' ) + int alt49=2; + int LA49_0 = input.LA(1); + + if ( (LA49_0==ROOT) ) { + alt49=1; + } + else if ( (LA49_0==BANG) ) { + alt49=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 49, 0, input); + + throw nvae; + } + switch (alt49) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:265:17: op= '^' + { + op=(Token)match(input,ROOT,FOLLOW_ROOT_in_atom1859); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ROOT.add(op); + + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:265:24: op= '!' + { + op=(Token)match(input,BANG,FOLLOW_BANG_in_atom1863); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_BANG.add(op); + + + } + break; + + } + + + + // AST REWRITE + // elements: op, notSet + // token labels: op + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleTokenStream stream_op=new RewriteRuleTokenStream(adaptor,"token op",op); + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 265:32: -> ^( $op notSet ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:265:35: ^( $op notSet ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_op.nextNode(), root_1); + + adaptor.addChild(root_1, stream_notSet.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:265:51: + { + + // AST REWRITE + // elements: notSet + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 265:51: -> notSet + { + adaptor.addChild(root_0, stream_notSet.nextTree()); + + } + + retval.tree = root_0;} + } + break; + + } + + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:266:9: RULE_REF (arg= ARG_ACTION )? ( (op= '^' | op= '!' ) )? + { + RULE_REF110=(Token)match(input,RULE_REF,FOLLOW_RULE_REF_in_atom1891); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_RULE_REF.add(RULE_REF110); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:266:18: (arg= ARG_ACTION )? + int alt51=2; + int LA51_0 = input.LA(1); + + if ( (LA51_0==ARG_ACTION) ) { + alt51=1; + } + switch (alt51) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:266:20: arg= ARG_ACTION + { + arg=(Token)match(input,ARG_ACTION,FOLLOW_ARG_ACTION_in_atom1897); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ARG_ACTION.add(arg); + + + } + break; + + } + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:266:38: ( (op= '^' | op= '!' ) )? + int alt53=2; + int LA53_0 = input.LA(1); + + if ( ((LA53_0>=ROOT && LA53_0<=BANG)) ) { + alt53=1; + } + switch (alt53) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:266:40: (op= '^' | op= '!' ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:266:40: (op= '^' | op= '!' ) + int alt52=2; + int LA52_0 = input.LA(1); + + if ( (LA52_0==ROOT) ) { + alt52=1; + } + else if ( (LA52_0==BANG) ) { + alt52=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 52, 0, input); + + throw nvae; + } + switch (alt52) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:266:41: op= '^' + { + op=(Token)match(input,ROOT,FOLLOW_ROOT_in_atom1907); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ROOT.add(op); + + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:266:48: op= '!' + { + op=(Token)match(input,BANG,FOLLOW_BANG_in_atom1911); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_BANG.add(op); + + + } + break; + + } + + + } + break; + + } + + + + // AST REWRITE + // elements: arg, op, op, RULE_REF, RULE_REF, RULE_REF, arg, RULE_REF + // token labels: op, arg + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleTokenStream stream_op=new RewriteRuleTokenStream(adaptor,"token op",op); + RewriteRuleTokenStream stream_arg=new RewriteRuleTokenStream(adaptor,"token arg",arg); + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 267:6: -> {$arg!=null&&op!=null}? ^( $op RULE_REF $arg) + if (arg!=null&&op!=null) { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:267:33: ^( $op RULE_REF $arg) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_op.nextNode(), root_1); + + adaptor.addChild(root_1, stream_RULE_REF.nextNode()); + adaptor.addChild(root_1, stream_arg.nextNode()); + + adaptor.addChild(root_0, root_1); + } + + } + else // 268:6: -> {$arg!=null}? ^( RULE_REF $arg) + if (arg!=null) { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:268:25: ^( RULE_REF $arg) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_RULE_REF.nextNode(), root_1); + + adaptor.addChild(root_1, stream_arg.nextNode()); + + adaptor.addChild(root_0, root_1); + } + + } + else // 269:6: -> {$op!=null}? ^( $op RULE_REF ) + if (op!=null) { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:269:25: ^( $op RULE_REF ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_op.nextNode(), root_1); + + adaptor.addChild(root_1, stream_RULE_REF.nextNode()); + + adaptor.addChild(root_0, root_1); + } + + } + else // 270:6: -> RULE_REF + { + adaptor.addChild(root_0, stream_RULE_REF.nextNode()); + + } + + retval.tree = root_0;} + } + break; + + } + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "atom" + + public static class notSet_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "notSet" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:273:1: notSet : '~' ( notTerminal -> ^( '~' notTerminal ) | block -> ^( '~' block ) ) ; + public final ANTLRv3Parser.notSet_return notSet() throws RecognitionException { + ANTLRv3Parser.notSet_return retval = new ANTLRv3Parser.notSet_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token char_literal111=null; + ANTLRv3Parser.notTerminal_return notTerminal112 = null; + + ANTLRv3Parser.block_return block113 = null; + + + CommonTree char_literal111_tree=null; + RewriteRuleTokenStream stream_89=new RewriteRuleTokenStream(adaptor,"token 89"); + RewriteRuleSubtreeStream stream_notTerminal=new RewriteRuleSubtreeStream(adaptor,"rule notTerminal"); + RewriteRuleSubtreeStream stream_block=new RewriteRuleSubtreeStream(adaptor,"rule block"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:274:2: ( '~' ( notTerminal -> ^( '~' notTerminal ) | block -> ^( '~' block ) ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:274:4: '~' ( notTerminal -> ^( '~' notTerminal ) | block -> ^( '~' block ) ) + { + char_literal111=(Token)match(input,89,FOLLOW_89_in_notSet1994); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_89.add(char_literal111); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:275:3: ( notTerminal -> ^( '~' notTerminal ) | block -> ^( '~' block ) ) + int alt55=2; + int LA55_0 = input.LA(1); + + if ( ((LA55_0>=TOKEN_REF && LA55_0<=CHAR_LITERAL)) ) { + alt55=1; + } + else if ( (LA55_0==82) ) { + alt55=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 55, 0, input); + + throw nvae; + } + switch (alt55) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:275:5: notTerminal + { + pushFollow(FOLLOW_notTerminal_in_notSet2000); + notTerminal112=notTerminal(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_notTerminal.add(notTerminal112.getTree()); + + + // AST REWRITE + // elements: notTerminal, 89 + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 275:17: -> ^( '~' notTerminal ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:275:20: ^( '~' notTerminal ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_89.nextNode(), root_1); + + adaptor.addChild(root_1, stream_notTerminal.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:276:5: block + { + pushFollow(FOLLOW_block_in_notSet2014); + block113=block(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_block.add(block113.getTree()); + + + // AST REWRITE + // elements: 89, block + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 276:12: -> ^( '~' block ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:276:15: ^( '~' block ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_89.nextNode(), root_1); + + adaptor.addChild(root_1, stream_block.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + + } + + + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "notSet" + + public static class treeSpec_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "treeSpec" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:280:1: treeSpec : '^(' element ( element )+ ')' -> ^( TREE_BEGIN ( element )+ ) ; + public final ANTLRv3Parser.treeSpec_return treeSpec() throws RecognitionException { + ANTLRv3Parser.treeSpec_return retval = new ANTLRv3Parser.treeSpec_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token string_literal114=null; + Token char_literal117=null; + ANTLRv3Parser.element_return element115 = null; + + ANTLRv3Parser.element_return element116 = null; + + + CommonTree string_literal114_tree=null; + CommonTree char_literal117_tree=null; + RewriteRuleTokenStream stream_84=new RewriteRuleTokenStream(adaptor,"token 84"); + RewriteRuleTokenStream stream_TREE_BEGIN=new RewriteRuleTokenStream(adaptor,"token TREE_BEGIN"); + RewriteRuleSubtreeStream stream_element=new RewriteRuleSubtreeStream(adaptor,"rule element"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:281:2: ( '^(' element ( element )+ ')' -> ^( TREE_BEGIN ( element )+ ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:281:4: '^(' element ( element )+ ')' + { + string_literal114=(Token)match(input,TREE_BEGIN,FOLLOW_TREE_BEGIN_in_treeSpec2038); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_TREE_BEGIN.add(string_literal114); + + pushFollow(FOLLOW_element_in_treeSpec2040); + element115=element(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_element.add(element115.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:281:17: ( element )+ + int cnt56=0; + loop56: + do { + int alt56=2; + int LA56_0 = input.LA(1); + + if ( (LA56_0==SEMPRED||LA56_0==TREE_BEGIN||(LA56_0>=TOKEN_REF && LA56_0<=ACTION)||LA56_0==RULE_REF||LA56_0==82||LA56_0==89||LA56_0==92) ) { + alt56=1; + } + + + switch (alt56) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:281:19: element + { + pushFollow(FOLLOW_element_in_treeSpec2044); + element116=element(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_element.add(element116.getTree()); + + } + break; + + default : + if ( cnt56 >= 1 ) break loop56; + if (state.backtracking>0) {state.failed=true; return retval;} + EarlyExitException eee = + new EarlyExitException(56, input); + throw eee; + } + cnt56++; + } while (true); + + char_literal117=(Token)match(input,84,FOLLOW_84_in_treeSpec2049); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_84.add(char_literal117); + + + + // AST REWRITE + // elements: element + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 281:34: -> ^( TREE_BEGIN ( element )+ ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:281:37: ^( TREE_BEGIN ( element )+ ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(TREE_BEGIN, "TREE_BEGIN"), root_1); + + if ( !(stream_element.hasNext()) ) { + throw new RewriteEarlyExitException(); + } + while ( stream_element.hasNext() ) { + adaptor.addChild(root_1, stream_element.nextTree()); + + } + stream_element.reset(); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "treeSpec" + + public static class ebnf_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "ebnf" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:284:1: ebnf : block (op= '?' -> ^( OPTIONAL[op] block ) | op= '*' -> ^( CLOSURE[op] block ) | op= '+' -> ^( POSITIVE_CLOSURE[op] block ) | '=>' -> {gtype==COMBINED_GRAMMAR &&\n\t\t\t\t\t Character.isUpperCase($rule::name.charAt(0))}? ^( SYNPRED[\"=>\"] block ) -> SYN_SEMPRED | -> block ) ; + public final ANTLRv3Parser.ebnf_return ebnf() throws RecognitionException { + ANTLRv3Parser.ebnf_return retval = new ANTLRv3Parser.ebnf_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token op=null; + Token string_literal119=null; + ANTLRv3Parser.block_return block118 = null; + + + CommonTree op_tree=null; + CommonTree string_literal119_tree=null; + RewriteRuleTokenStream stream_91=new RewriteRuleTokenStream(adaptor,"token 91"); + RewriteRuleTokenStream stream_74=new RewriteRuleTokenStream(adaptor,"token 74"); + RewriteRuleTokenStream stream_90=new RewriteRuleTokenStream(adaptor,"token 90"); + RewriteRuleTokenStream stream_88=new RewriteRuleTokenStream(adaptor,"token 88"); + RewriteRuleSubtreeStream stream_block=new RewriteRuleSubtreeStream(adaptor,"rule block"); + + Token firstToken = input.LT(1); + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:293:2: ( block (op= '?' -> ^( OPTIONAL[op] block ) | op= '*' -> ^( CLOSURE[op] block ) | op= '+' -> ^( POSITIVE_CLOSURE[op] block ) | '=>' -> {gtype==COMBINED_GRAMMAR &&\n\t\t\t\t\t Character.isUpperCase($rule::name.charAt(0))}? ^( SYNPRED[\"=>\"] block ) -> SYN_SEMPRED | -> block ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:293:4: block (op= '?' -> ^( OPTIONAL[op] block ) | op= '*' -> ^( CLOSURE[op] block ) | op= '+' -> ^( POSITIVE_CLOSURE[op] block ) | '=>' -> {gtype==COMBINED_GRAMMAR &&\n\t\t\t\t\t Character.isUpperCase($rule::name.charAt(0))}? ^( SYNPRED[\"=>\"] block ) -> SYN_SEMPRED | -> block ) + { + pushFollow(FOLLOW_block_in_ebnf2081); + block118=block(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_block.add(block118.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:294:3: (op= '?' -> ^( OPTIONAL[op] block ) | op= '*' -> ^( CLOSURE[op] block ) | op= '+' -> ^( POSITIVE_CLOSURE[op] block ) | '=>' -> {gtype==COMBINED_GRAMMAR &&\n\t\t\t\t\t Character.isUpperCase($rule::name.charAt(0))}? ^( SYNPRED[\"=>\"] block ) -> SYN_SEMPRED | -> block ) + int alt57=5; + switch ( input.LA(1) ) { + case 90: + { + alt57=1; + } + break; + case 74: + { + alt57=2; + } + break; + case 91: + { + alt57=3; + } + break; + case 88: + { + alt57=4; + } + break; + case SEMPRED: + case TREE_BEGIN: + case REWRITE: + case TOKEN_REF: + case STRING_LITERAL: + case CHAR_LITERAL: + case ACTION: + case RULE_REF: + case 69: + case 82: + case 83: + case 84: + case 89: + case 92: + { + alt57=5; + } + break; + default: + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 57, 0, input); + + throw nvae; + } + + switch (alt57) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:294:5: op= '?' + { + op=(Token)match(input,90,FOLLOW_90_in_ebnf2089); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_90.add(op); + + + + // AST REWRITE + // elements: block + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 294:12: -> ^( OPTIONAL[op] block ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:294:15: ^( OPTIONAL[op] block ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(OPTIONAL, op), root_1); + + adaptor.addChild(root_1, stream_block.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:295:5: op= '*' + { + op=(Token)match(input,74,FOLLOW_74_in_ebnf2106); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_74.add(op); + + + + // AST REWRITE + // elements: block + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 295:12: -> ^( CLOSURE[op] block ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:295:15: ^( CLOSURE[op] block ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(CLOSURE, op), root_1); + + adaptor.addChild(root_1, stream_block.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:296:5: op= '+' + { + op=(Token)match(input,91,FOLLOW_91_in_ebnf2123); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_91.add(op); + + + + // AST REWRITE + // elements: block + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 296:12: -> ^( POSITIVE_CLOSURE[op] block ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:296:15: ^( POSITIVE_CLOSURE[op] block ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(POSITIVE_CLOSURE, op), root_1); + + adaptor.addChild(root_1, stream_block.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:297:7: '=>' + { + string_literal119=(Token)match(input,88,FOLLOW_88_in_ebnf2140); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_88.add(string_literal119); + + + + // AST REWRITE + // elements: block + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 298:6: -> {gtype==COMBINED_GRAMMAR &&\n\t\t\t\t\t Character.isUpperCase($rule::name.charAt(0))}? ^( SYNPRED[\"=>\"] block ) + if (gtype==COMBINED_GRAMMAR && + Character.isUpperCase(((rule_scope)rule_stack.peek()).name.charAt(0))) { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:301:9: ^( SYNPRED[\"=>\"] block ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(SYNPRED, "=>"), root_1); + + adaptor.addChild(root_1, stream_block.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + else // 303:6: -> SYN_SEMPRED + { + adaptor.addChild(root_0, (CommonTree)adaptor.create(SYN_SEMPRED, "SYN_SEMPRED")); + + } + + retval.tree = root_0;} + } + break; + case 5 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:304:13: + { + + // AST REWRITE + // elements: block + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 304:13: -> block + { + adaptor.addChild(root_0, stream_block.nextTree()); + + } + + retval.tree = root_0;} + } + break; + + } + + + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + if ( state.backtracking==0 ) { + + ((CommonTree)retval.tree).getToken().setLine(firstToken.getLine()); + ((CommonTree)retval.tree).getToken().setCharPositionInLine(firstToken.getCharPositionInLine()); + + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "ebnf" + + public static class range_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "range" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:308:1: range : c1= CHAR_LITERAL RANGE c2= CHAR_LITERAL -> ^( CHAR_RANGE[$c1,\"..\"] $c1 $c2) ; + public final ANTLRv3Parser.range_return range() throws RecognitionException { + ANTLRv3Parser.range_return retval = new ANTLRv3Parser.range_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token c1=null; + Token c2=null; + Token RANGE120=null; + + CommonTree c1_tree=null; + CommonTree c2_tree=null; + CommonTree RANGE120_tree=null; + RewriteRuleTokenStream stream_RANGE=new RewriteRuleTokenStream(adaptor,"token RANGE"); + RewriteRuleTokenStream stream_CHAR_LITERAL=new RewriteRuleTokenStream(adaptor,"token CHAR_LITERAL"); + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:309:2: (c1= CHAR_LITERAL RANGE c2= CHAR_LITERAL -> ^( CHAR_RANGE[$c1,\"..\"] $c1 $c2) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:309:4: c1= CHAR_LITERAL RANGE c2= CHAR_LITERAL + { + c1=(Token)match(input,CHAR_LITERAL,FOLLOW_CHAR_LITERAL_in_range2223); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_CHAR_LITERAL.add(c1); + + RANGE120=(Token)match(input,RANGE,FOLLOW_RANGE_in_range2225); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_RANGE.add(RANGE120); + + c2=(Token)match(input,CHAR_LITERAL,FOLLOW_CHAR_LITERAL_in_range2229); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_CHAR_LITERAL.add(c2); + + + + // AST REWRITE + // elements: c2, c1 + // token labels: c2, c1 + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleTokenStream stream_c2=new RewriteRuleTokenStream(adaptor,"token c2",c2); + RewriteRuleTokenStream stream_c1=new RewriteRuleTokenStream(adaptor,"token c1",c1); + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 309:42: -> ^( CHAR_RANGE[$c1,\"..\"] $c1 $c2) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:309:45: ^( CHAR_RANGE[$c1,\"..\"] $c1 $c2) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(CHAR_RANGE, c1, ".."), root_1); + + adaptor.addChild(root_1, stream_c1.nextNode()); + adaptor.addChild(root_1, stream_c2.nextNode()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "range" + + public static class terminal_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "terminal" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:312:1: terminal : ( CHAR_LITERAL -> CHAR_LITERAL | TOKEN_REF ( ARG_ACTION -> ^( TOKEN_REF ARG_ACTION ) | -> TOKEN_REF ) | STRING_LITERAL -> STRING_LITERAL | '.' -> '.' ) ( '^' -> ^( '^' $terminal) | '!' -> ^( '!' $terminal) )? ; + public final ANTLRv3Parser.terminal_return terminal() throws RecognitionException { + ANTLRv3Parser.terminal_return retval = new ANTLRv3Parser.terminal_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token CHAR_LITERAL121=null; + Token TOKEN_REF122=null; + Token ARG_ACTION123=null; + Token STRING_LITERAL124=null; + Token char_literal125=null; + Token char_literal126=null; + Token char_literal127=null; + + CommonTree CHAR_LITERAL121_tree=null; + CommonTree TOKEN_REF122_tree=null; + CommonTree ARG_ACTION123_tree=null; + CommonTree STRING_LITERAL124_tree=null; + CommonTree char_literal125_tree=null; + CommonTree char_literal126_tree=null; + CommonTree char_literal127_tree=null; + RewriteRuleTokenStream stream_ROOT=new RewriteRuleTokenStream(adaptor,"token ROOT"); + RewriteRuleTokenStream stream_ARG_ACTION=new RewriteRuleTokenStream(adaptor,"token ARG_ACTION"); + RewriteRuleTokenStream stream_TOKEN_REF=new RewriteRuleTokenStream(adaptor,"token TOKEN_REF"); + RewriteRuleTokenStream stream_92=new RewriteRuleTokenStream(adaptor,"token 92"); + RewriteRuleTokenStream stream_CHAR_LITERAL=new RewriteRuleTokenStream(adaptor,"token CHAR_LITERAL"); + RewriteRuleTokenStream stream_STRING_LITERAL=new RewriteRuleTokenStream(adaptor,"token STRING_LITERAL"); + RewriteRuleTokenStream stream_BANG=new RewriteRuleTokenStream(adaptor,"token BANG"); + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:313:5: ( ( CHAR_LITERAL -> CHAR_LITERAL | TOKEN_REF ( ARG_ACTION -> ^( TOKEN_REF ARG_ACTION ) | -> TOKEN_REF ) | STRING_LITERAL -> STRING_LITERAL | '.' -> '.' ) ( '^' -> ^( '^' $terminal) | '!' -> ^( '!' $terminal) )? ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:313:9: ( CHAR_LITERAL -> CHAR_LITERAL | TOKEN_REF ( ARG_ACTION -> ^( TOKEN_REF ARG_ACTION ) | -> TOKEN_REF ) | STRING_LITERAL -> STRING_LITERAL | '.' -> '.' ) ( '^' -> ^( '^' $terminal) | '!' -> ^( '!' $terminal) )? + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:313:9: ( CHAR_LITERAL -> CHAR_LITERAL | TOKEN_REF ( ARG_ACTION -> ^( TOKEN_REF ARG_ACTION ) | -> TOKEN_REF ) | STRING_LITERAL -> STRING_LITERAL | '.' -> '.' ) + int alt59=4; + switch ( input.LA(1) ) { + case CHAR_LITERAL: + { + alt59=1; + } + break; + case TOKEN_REF: + { + alt59=2; + } + break; + case STRING_LITERAL: + { + alt59=3; + } + break; + case 92: + { + alt59=4; + } + break; + default: + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 59, 0, input); + + throw nvae; + } + + switch (alt59) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:313:11: CHAR_LITERAL + { + CHAR_LITERAL121=(Token)match(input,CHAR_LITERAL,FOLLOW_CHAR_LITERAL_in_terminal2260); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_CHAR_LITERAL.add(CHAR_LITERAL121); + + + + // AST REWRITE + // elements: CHAR_LITERAL + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 313:27: -> CHAR_LITERAL + { + adaptor.addChild(root_0, stream_CHAR_LITERAL.nextNode()); + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:315:7: TOKEN_REF ( ARG_ACTION -> ^( TOKEN_REF ARG_ACTION ) | -> TOKEN_REF ) + { + TOKEN_REF122=(Token)match(input,TOKEN_REF,FOLLOW_TOKEN_REF_in_terminal2282); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_TOKEN_REF.add(TOKEN_REF122); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:316:4: ( ARG_ACTION -> ^( TOKEN_REF ARG_ACTION ) | -> TOKEN_REF ) + int alt58=2; + int LA58_0 = input.LA(1); + + if ( (LA58_0==ARG_ACTION) ) { + alt58=1; + } + else if ( (LA58_0==SEMPRED||(LA58_0>=TREE_BEGIN && LA58_0<=REWRITE)||(LA58_0>=TOKEN_REF && LA58_0<=ACTION)||LA58_0==RULE_REF||LA58_0==69||LA58_0==74||(LA58_0>=82 && LA58_0<=84)||(LA58_0>=89 && LA58_0<=92)) ) { + alt58=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 58, 0, input); + + throw nvae; + } + switch (alt58) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:316:6: ARG_ACTION + { + ARG_ACTION123=(Token)match(input,ARG_ACTION,FOLLOW_ARG_ACTION_in_terminal2289); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ARG_ACTION.add(ARG_ACTION123); + + + + // AST REWRITE + // elements: TOKEN_REF, ARG_ACTION + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 316:20: -> ^( TOKEN_REF ARG_ACTION ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:316:23: ^( TOKEN_REF ARG_ACTION ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_TOKEN_REF.nextNode(), root_1); + + adaptor.addChild(root_1, stream_ARG_ACTION.nextNode()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:317:12: + { + + // AST REWRITE + // elements: TOKEN_REF + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 317:12: -> TOKEN_REF + { + adaptor.addChild(root_0, stream_TOKEN_REF.nextNode()); + + } + + retval.tree = root_0;} + } + break; + + } + + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:319:7: STRING_LITERAL + { + STRING_LITERAL124=(Token)match(input,STRING_LITERAL,FOLLOW_STRING_LITERAL_in_terminal2328); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_STRING_LITERAL.add(STRING_LITERAL124); + + + + // AST REWRITE + // elements: STRING_LITERAL + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 319:25: -> STRING_LITERAL + { + adaptor.addChild(root_0, stream_STRING_LITERAL.nextNode()); + + } + + retval.tree = root_0;} + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:320:7: '.' + { + char_literal125=(Token)match(input,92,FOLLOW_92_in_terminal2343); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_92.add(char_literal125); + + + + // AST REWRITE + // elements: 92 + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 320:17: -> '.' + { + adaptor.addChild(root_0, stream_92.nextNode()); + + } + + retval.tree = root_0;} + } + break; + + } + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:322:3: ( '^' -> ^( '^' $terminal) | '!' -> ^( '!' $terminal) )? + int alt60=3; + int LA60_0 = input.LA(1); + + if ( (LA60_0==ROOT) ) { + alt60=1; + } + else if ( (LA60_0==BANG) ) { + alt60=2; + } + switch (alt60) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:322:5: '^' + { + char_literal126=(Token)match(input,ROOT,FOLLOW_ROOT_in_terminal2364); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ROOT.add(char_literal126); + + + + // AST REWRITE + // elements: terminal, ROOT + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 322:15: -> ^( '^' $terminal) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:322:18: ^( '^' $terminal) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_ROOT.nextNode(), root_1); + + adaptor.addChild(root_1, stream_retval.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:323:5: '!' + { + char_literal127=(Token)match(input,BANG,FOLLOW_BANG_in_terminal2385); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_BANG.add(char_literal127); + + + + // AST REWRITE + // elements: terminal, BANG + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 323:15: -> ^( '!' $terminal) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:323:18: ^( '!' $terminal) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_BANG.nextNode(), root_1); + + adaptor.addChild(root_1, stream_retval.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + + } + + + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "terminal" + + public static class notTerminal_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "notTerminal" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:327:1: notTerminal : ( CHAR_LITERAL | TOKEN_REF | STRING_LITERAL ); + public final ANTLRv3Parser.notTerminal_return notTerminal() throws RecognitionException { + ANTLRv3Parser.notTerminal_return retval = new ANTLRv3Parser.notTerminal_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token set128=null; + + CommonTree set128_tree=null; + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:328:2: ( CHAR_LITERAL | TOKEN_REF | STRING_LITERAL ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g: + { + root_0 = (CommonTree)adaptor.nil(); + + set128=(Token)input.LT(1); + if ( (input.LA(1)>=TOKEN_REF && input.LA(1)<=CHAR_LITERAL) ) { + input.consume(); + if ( state.backtracking==0 ) adaptor.addChild(root_0, (CommonTree)adaptor.create(set128)); + state.errorRecovery=false;state.failed=false; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + MismatchedSetException mse = new MismatchedSetException(null,input); + throw mse; + } + + + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "notTerminal" + + public static class ebnfSuffix_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "ebnfSuffix" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:333:1: ebnfSuffix : ( '?' -> OPTIONAL[op] | '*' -> CLOSURE[op] | '+' -> POSITIVE_CLOSURE[op] ); + public final ANTLRv3Parser.ebnfSuffix_return ebnfSuffix() throws RecognitionException { + ANTLRv3Parser.ebnfSuffix_return retval = new ANTLRv3Parser.ebnfSuffix_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token char_literal129=null; + Token char_literal130=null; + Token char_literal131=null; + + CommonTree char_literal129_tree=null; + CommonTree char_literal130_tree=null; + CommonTree char_literal131_tree=null; + RewriteRuleTokenStream stream_91=new RewriteRuleTokenStream(adaptor,"token 91"); + RewriteRuleTokenStream stream_74=new RewriteRuleTokenStream(adaptor,"token 74"); + RewriteRuleTokenStream stream_90=new RewriteRuleTokenStream(adaptor,"token 90"); + + + Token op = input.LT(1); + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:337:2: ( '?' -> OPTIONAL[op] | '*' -> CLOSURE[op] | '+' -> POSITIVE_CLOSURE[op] ) + int alt61=3; + switch ( input.LA(1) ) { + case 90: + { + alt61=1; + } + break; + case 74: + { + alt61=2; + } + break; + case 91: + { + alt61=3; + } + break; + default: + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 61, 0, input); + + throw nvae; + } + + switch (alt61) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:337:4: '?' + { + char_literal129=(Token)match(input,90,FOLLOW_90_in_ebnfSuffix2445); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_90.add(char_literal129); + + + + // AST REWRITE + // elements: + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 337:8: -> OPTIONAL[op] + { + adaptor.addChild(root_0, (CommonTree)adaptor.create(OPTIONAL, op)); + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:338:6: '*' + { + char_literal130=(Token)match(input,74,FOLLOW_74_in_ebnfSuffix2457); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_74.add(char_literal130); + + + + // AST REWRITE + // elements: + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 338:10: -> CLOSURE[op] + { + adaptor.addChild(root_0, (CommonTree)adaptor.create(CLOSURE, op)); + + } + + retval.tree = root_0;} + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:339:7: '+' + { + char_literal131=(Token)match(input,91,FOLLOW_91_in_ebnfSuffix2470); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_91.add(char_literal131); + + + + // AST REWRITE + // elements: + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 339:11: -> POSITIVE_CLOSURE[op] + { + adaptor.addChild(root_0, (CommonTree)adaptor.create(POSITIVE_CLOSURE, op)); + + } + + retval.tree = root_0;} + } + break; + + } + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "ebnfSuffix" + + public static class rewrite_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "rewrite" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:346:1: rewrite : ( (rew+= '->' preds+= SEMPRED predicated+= rewrite_alternative )* rew2= '->' last= rewrite_alternative -> ( ^( $rew $preds $predicated) )* ^( $rew2 $last) | ); + public final ANTLRv3Parser.rewrite_return rewrite() throws RecognitionException { + ANTLRv3Parser.rewrite_return retval = new ANTLRv3Parser.rewrite_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token rew2=null; + Token rew=null; + Token preds=null; + List list_rew=null; + List list_preds=null; + List list_predicated=null; + ANTLRv3Parser.rewrite_alternative_return last = null; + + RuleReturnScope predicated = null; + CommonTree rew2_tree=null; + CommonTree rew_tree=null; + CommonTree preds_tree=null; + RewriteRuleTokenStream stream_SEMPRED=new RewriteRuleTokenStream(adaptor,"token SEMPRED"); + RewriteRuleTokenStream stream_REWRITE=new RewriteRuleTokenStream(adaptor,"token REWRITE"); + RewriteRuleSubtreeStream stream_rewrite_alternative=new RewriteRuleSubtreeStream(adaptor,"rule rewrite_alternative"); + + Token firstToken = input.LT(1); + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:350:2: ( (rew+= '->' preds+= SEMPRED predicated+= rewrite_alternative )* rew2= '->' last= rewrite_alternative -> ( ^( $rew $preds $predicated) )* ^( $rew2 $last) | ) + int alt63=2; + int LA63_0 = input.LA(1); + + if ( (LA63_0==REWRITE) ) { + alt63=1; + } + else if ( (LA63_0==69||(LA63_0>=83 && LA63_0<=84)) ) { + alt63=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 63, 0, input); + + throw nvae; + } + switch (alt63) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:350:4: (rew+= '->' preds+= SEMPRED predicated+= rewrite_alternative )* rew2= '->' last= rewrite_alternative + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:350:4: (rew+= '->' preds+= SEMPRED predicated+= rewrite_alternative )* + loop62: + do { + int alt62=2; + int LA62_0 = input.LA(1); + + if ( (LA62_0==REWRITE) ) { + int LA62_1 = input.LA(2); + + if ( (LA62_1==SEMPRED) ) { + alt62=1; + } + + + } + + + switch (alt62) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:350:5: rew+= '->' preds+= SEMPRED predicated+= rewrite_alternative + { + rew=(Token)match(input,REWRITE,FOLLOW_REWRITE_in_rewrite2499); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_REWRITE.add(rew); + + if (list_rew==null) list_rew=new ArrayList(); + list_rew.add(rew); + + preds=(Token)match(input,SEMPRED,FOLLOW_SEMPRED_in_rewrite2503); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_SEMPRED.add(preds); + + if (list_preds==null) list_preds=new ArrayList(); + list_preds.add(preds); + + pushFollow(FOLLOW_rewrite_alternative_in_rewrite2507); + predicated=rewrite_alternative(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite_alternative.add(predicated.getTree()); + if (list_predicated==null) list_predicated=new ArrayList(); + list_predicated.add(predicated.getTree()); + + + } + break; + + default : + break loop62; + } + } while (true); + + rew2=(Token)match(input,REWRITE,FOLLOW_REWRITE_in_rewrite2515); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_REWRITE.add(rew2); + + pushFollow(FOLLOW_rewrite_alternative_in_rewrite2519); + last=rewrite_alternative(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite_alternative.add(last.getTree()); + + + // AST REWRITE + // elements: rew, predicated, last, rew2, preds + // token labels: rew2 + // rule labels: last, retval + // token list labels: rew, preds + // rule list labels: predicated + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleTokenStream stream_rew2=new RewriteRuleTokenStream(adaptor,"token rew2",rew2); + RewriteRuleTokenStream stream_rew=new RewriteRuleTokenStream(adaptor,"token rew", list_rew); + RewriteRuleTokenStream stream_preds=new RewriteRuleTokenStream(adaptor,"token preds", list_preds); + RewriteRuleSubtreeStream stream_last=new RewriteRuleSubtreeStream(adaptor,"token last",last!=null?last.tree:null); + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + RewriteRuleSubtreeStream stream_predicated=new RewriteRuleSubtreeStream(adaptor,"token predicated",list_predicated); + root_0 = (CommonTree)adaptor.nil(); + // 352:9: -> ( ^( $rew $preds $predicated) )* ^( $rew2 $last) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:352:12: ( ^( $rew $preds $predicated) )* + while ( stream_rew.hasNext()||stream_predicated.hasNext()||stream_preds.hasNext() ) { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:352:12: ^( $rew $preds $predicated) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_rew.nextNode(), root_1); + + adaptor.addChild(root_1, stream_preds.nextNode()); + adaptor.addChild(root_1, stream_predicated.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + stream_rew.reset(); + stream_predicated.reset(); + stream_preds.reset(); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:352:40: ^( $rew2 $last) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_rew2.nextNode(), root_1); + + adaptor.addChild(root_1, stream_last.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:354:2: + { + root_0 = (CommonTree)adaptor.nil(); + + } + break; + + } + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "rewrite" + + public static class rewrite_alternative_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "rewrite_alternative" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:356:1: rewrite_alternative options {backtrack=true; } : ( rewrite_template | rewrite_tree_alternative | -> ^( ALT[\"ALT\"] EPSILON[\"EPSILON\"] EOA[\"EOA\"] ) ); + public final ANTLRv3Parser.rewrite_alternative_return rewrite_alternative() throws RecognitionException { + ANTLRv3Parser.rewrite_alternative_return retval = new ANTLRv3Parser.rewrite_alternative_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + ANTLRv3Parser.rewrite_template_return rewrite_template132 = null; + + ANTLRv3Parser.rewrite_tree_alternative_return rewrite_tree_alternative133 = null; + + + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:358:2: ( rewrite_template | rewrite_tree_alternative | -> ^( ALT[\"ALT\"] EPSILON[\"EPSILON\"] EOA[\"EOA\"] ) ) + int alt64=3; + alt64 = dfa64.predict(input); + switch (alt64) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:358:4: rewrite_template + { + root_0 = (CommonTree)adaptor.nil(); + + pushFollow(FOLLOW_rewrite_template_in_rewrite_alternative2570); + rewrite_template132=rewrite_template(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) adaptor.addChild(root_0, rewrite_template132.getTree()); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:359:4: rewrite_tree_alternative + { + root_0 = (CommonTree)adaptor.nil(); + + pushFollow(FOLLOW_rewrite_tree_alternative_in_rewrite_alternative2575); + rewrite_tree_alternative133=rewrite_tree_alternative(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) adaptor.addChild(root_0, rewrite_tree_alternative133.getTree()); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:360:29: + { + + // AST REWRITE + // elements: + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 360:29: -> ^( ALT[\"ALT\"] EPSILON[\"EPSILON\"] EOA[\"EOA\"] ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:360:32: ^( ALT[\"ALT\"] EPSILON[\"EPSILON\"] EOA[\"EOA\"] ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(ALT, "ALT"), root_1); + + adaptor.addChild(root_1, (CommonTree)adaptor.create(EPSILON, "EPSILON")); + adaptor.addChild(root_1, (CommonTree)adaptor.create(EOA, "EOA")); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + + } + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "rewrite_alternative" + + public static class rewrite_tree_block_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "rewrite_tree_block" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:363:1: rewrite_tree_block : lp= '(' rewrite_tree_alternative ')' -> ^( BLOCK[$lp,\"BLOCK\"] rewrite_tree_alternative EOB[$lp,\"EOB\"] ) ; + public final ANTLRv3Parser.rewrite_tree_block_return rewrite_tree_block() throws RecognitionException { + ANTLRv3Parser.rewrite_tree_block_return retval = new ANTLRv3Parser.rewrite_tree_block_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token lp=null; + Token char_literal135=null; + ANTLRv3Parser.rewrite_tree_alternative_return rewrite_tree_alternative134 = null; + + + CommonTree lp_tree=null; + CommonTree char_literal135_tree=null; + RewriteRuleTokenStream stream_84=new RewriteRuleTokenStream(adaptor,"token 84"); + RewriteRuleTokenStream stream_82=new RewriteRuleTokenStream(adaptor,"token 82"); + RewriteRuleSubtreeStream stream_rewrite_tree_alternative=new RewriteRuleSubtreeStream(adaptor,"rule rewrite_tree_alternative"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:364:5: (lp= '(' rewrite_tree_alternative ')' -> ^( BLOCK[$lp,\"BLOCK\"] rewrite_tree_alternative EOB[$lp,\"EOB\"] ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:364:9: lp= '(' rewrite_tree_alternative ')' + { + lp=(Token)match(input,82,FOLLOW_82_in_rewrite_tree_block2617); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_82.add(lp); + + pushFollow(FOLLOW_rewrite_tree_alternative_in_rewrite_tree_block2619); + rewrite_tree_alternative134=rewrite_tree_alternative(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite_tree_alternative.add(rewrite_tree_alternative134.getTree()); + char_literal135=(Token)match(input,84,FOLLOW_84_in_rewrite_tree_block2621); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_84.add(char_literal135); + + + + // AST REWRITE + // elements: rewrite_tree_alternative + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 365:6: -> ^( BLOCK[$lp,\"BLOCK\"] rewrite_tree_alternative EOB[$lp,\"EOB\"] ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:365:9: ^( BLOCK[$lp,\"BLOCK\"] rewrite_tree_alternative EOB[$lp,\"EOB\"] ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(BLOCK, lp, "BLOCK"), root_1); + + adaptor.addChild(root_1, stream_rewrite_tree_alternative.nextTree()); + adaptor.addChild(root_1, (CommonTree)adaptor.create(EOB, lp, "EOB")); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "rewrite_tree_block" + + public static class rewrite_tree_alternative_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "rewrite_tree_alternative" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:368:1: rewrite_tree_alternative : ( rewrite_tree_element )+ -> ^( ALT[\"ALT\"] ( rewrite_tree_element )+ EOA[\"EOA\"] ) ; + public final ANTLRv3Parser.rewrite_tree_alternative_return rewrite_tree_alternative() throws RecognitionException { + ANTLRv3Parser.rewrite_tree_alternative_return retval = new ANTLRv3Parser.rewrite_tree_alternative_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + ANTLRv3Parser.rewrite_tree_element_return rewrite_tree_element136 = null; + + + RewriteRuleSubtreeStream stream_rewrite_tree_element=new RewriteRuleSubtreeStream(adaptor,"rule rewrite_tree_element"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:369:5: ( ( rewrite_tree_element )+ -> ^( ALT[\"ALT\"] ( rewrite_tree_element )+ EOA[\"EOA\"] ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:369:7: ( rewrite_tree_element )+ + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:369:7: ( rewrite_tree_element )+ + int cnt65=0; + loop65: + do { + int alt65=2; + int LA65_0 = input.LA(1); + + if ( (LA65_0==TREE_BEGIN||(LA65_0>=TOKEN_REF && LA65_0<=ACTION)||LA65_0==RULE_REF||LA65_0==82||LA65_0==93) ) { + alt65=1; + } + + + switch (alt65) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:369:7: rewrite_tree_element + { + pushFollow(FOLLOW_rewrite_tree_element_in_rewrite_tree_alternative2655); + rewrite_tree_element136=rewrite_tree_element(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite_tree_element.add(rewrite_tree_element136.getTree()); + + } + break; + + default : + if ( cnt65 >= 1 ) break loop65; + if (state.backtracking>0) {state.failed=true; return retval;} + EarlyExitException eee = + new EarlyExitException(65, input); + throw eee; + } + cnt65++; + } while (true); + + + + // AST REWRITE + // elements: rewrite_tree_element + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 369:29: -> ^( ALT[\"ALT\"] ( rewrite_tree_element )+ EOA[\"EOA\"] ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:369:32: ^( ALT[\"ALT\"] ( rewrite_tree_element )+ EOA[\"EOA\"] ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(ALT, "ALT"), root_1); + + if ( !(stream_rewrite_tree_element.hasNext()) ) { + throw new RewriteEarlyExitException(); + } + while ( stream_rewrite_tree_element.hasNext() ) { + adaptor.addChild(root_1, stream_rewrite_tree_element.nextTree()); + + } + stream_rewrite_tree_element.reset(); + adaptor.addChild(root_1, (CommonTree)adaptor.create(EOA, "EOA")); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "rewrite_tree_alternative" + + public static class rewrite_tree_element_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "rewrite_tree_element" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:372:1: rewrite_tree_element : ( rewrite_tree_atom | rewrite_tree_atom ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] rewrite_tree_atom EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | rewrite_tree ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] rewrite_tree EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> rewrite_tree ) | rewrite_tree_ebnf ); + public final ANTLRv3Parser.rewrite_tree_element_return rewrite_tree_element() throws RecognitionException { + ANTLRv3Parser.rewrite_tree_element_return retval = new ANTLRv3Parser.rewrite_tree_element_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + ANTLRv3Parser.rewrite_tree_atom_return rewrite_tree_atom137 = null; + + ANTLRv3Parser.rewrite_tree_atom_return rewrite_tree_atom138 = null; + + ANTLRv3Parser.ebnfSuffix_return ebnfSuffix139 = null; + + ANTLRv3Parser.rewrite_tree_return rewrite_tree140 = null; + + ANTLRv3Parser.ebnfSuffix_return ebnfSuffix141 = null; + + ANTLRv3Parser.rewrite_tree_ebnf_return rewrite_tree_ebnf142 = null; + + + RewriteRuleSubtreeStream stream_rewrite_tree_atom=new RewriteRuleSubtreeStream(adaptor,"rule rewrite_tree_atom"); + RewriteRuleSubtreeStream stream_rewrite_tree=new RewriteRuleSubtreeStream(adaptor,"rule rewrite_tree"); + RewriteRuleSubtreeStream stream_ebnfSuffix=new RewriteRuleSubtreeStream(adaptor,"rule ebnfSuffix"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:373:2: ( rewrite_tree_atom | rewrite_tree_atom ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] rewrite_tree_atom EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | rewrite_tree ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] rewrite_tree EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> rewrite_tree ) | rewrite_tree_ebnf ) + int alt67=4; + alt67 = dfa67.predict(input); + switch (alt67) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:373:4: rewrite_tree_atom + { + root_0 = (CommonTree)adaptor.nil(); + + pushFollow(FOLLOW_rewrite_tree_atom_in_rewrite_tree_element2683); + rewrite_tree_atom137=rewrite_tree_atom(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) adaptor.addChild(root_0, rewrite_tree_atom137.getTree()); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:374:4: rewrite_tree_atom ebnfSuffix + { + pushFollow(FOLLOW_rewrite_tree_atom_in_rewrite_tree_element2688); + rewrite_tree_atom138=rewrite_tree_atom(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite_tree_atom.add(rewrite_tree_atom138.getTree()); + pushFollow(FOLLOW_ebnfSuffix_in_rewrite_tree_element2690); + ebnfSuffix139=ebnfSuffix(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ebnfSuffix.add(ebnfSuffix139.getTree()); + + + // AST REWRITE + // elements: ebnfSuffix, rewrite_tree_atom + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 375:3: -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] rewrite_tree_atom EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:375:6: ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] rewrite_tree_atom EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_ebnfSuffix.nextNode(), root_1); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:375:20: ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] rewrite_tree_atom EOA[\"EOA\"] ) EOB[\"EOB\"] ) + { + CommonTree root_2 = (CommonTree)adaptor.nil(); + root_2 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(BLOCK, "BLOCK"), root_2); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:375:37: ^( ALT[\"ALT\"] rewrite_tree_atom EOA[\"EOA\"] ) + { + CommonTree root_3 = (CommonTree)adaptor.nil(); + root_3 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(ALT, "ALT"), root_3); + + adaptor.addChild(root_3, stream_rewrite_tree_atom.nextTree()); + adaptor.addChild(root_3, (CommonTree)adaptor.create(EOA, "EOA")); + + adaptor.addChild(root_2, root_3); + } + adaptor.addChild(root_2, (CommonTree)adaptor.create(EOB, "EOB")); + + adaptor.addChild(root_1, root_2); + } + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:376:6: rewrite_tree ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] rewrite_tree EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> rewrite_tree ) + { + pushFollow(FOLLOW_rewrite_tree_in_rewrite_tree_element2724); + rewrite_tree140=rewrite_tree(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite_tree.add(rewrite_tree140.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:377:3: ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] rewrite_tree EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> rewrite_tree ) + int alt66=2; + int LA66_0 = input.LA(1); + + if ( (LA66_0==74||(LA66_0>=90 && LA66_0<=91)) ) { + alt66=1; + } + else if ( (LA66_0==EOF||LA66_0==TREE_BEGIN||LA66_0==REWRITE||(LA66_0>=TOKEN_REF && LA66_0<=ACTION)||LA66_0==RULE_REF||LA66_0==69||(LA66_0>=82 && LA66_0<=84)||LA66_0==93) ) { + alt66=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 66, 0, input); + + throw nvae; + } + switch (alt66) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:377:5: ebnfSuffix + { + pushFollow(FOLLOW_ebnfSuffix_in_rewrite_tree_element2730); + ebnfSuffix141=ebnfSuffix(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ebnfSuffix.add(ebnfSuffix141.getTree()); + + + // AST REWRITE + // elements: rewrite_tree, ebnfSuffix + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 378:4: -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] rewrite_tree EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:378:7: ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] rewrite_tree EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_ebnfSuffix.nextNode(), root_1); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:378:20: ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] rewrite_tree EOA[\"EOA\"] ) EOB[\"EOB\"] ) + { + CommonTree root_2 = (CommonTree)adaptor.nil(); + root_2 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(BLOCK, "BLOCK"), root_2); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:378:37: ^( ALT[\"ALT\"] rewrite_tree EOA[\"EOA\"] ) + { + CommonTree root_3 = (CommonTree)adaptor.nil(); + root_3 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(ALT, "ALT"), root_3); + + adaptor.addChild(root_3, stream_rewrite_tree.nextTree()); + adaptor.addChild(root_3, (CommonTree)adaptor.create(EOA, "EOA")); + + adaptor.addChild(root_2, root_3); + } + adaptor.addChild(root_2, (CommonTree)adaptor.create(EOB, "EOB")); + + adaptor.addChild(root_1, root_2); + } + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:379:5: + { + + // AST REWRITE + // elements: rewrite_tree + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 379:5: -> rewrite_tree + { + adaptor.addChild(root_0, stream_rewrite_tree.nextTree()); + + } + + retval.tree = root_0;} + } + break; + + } + + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:381:6: rewrite_tree_ebnf + { + root_0 = (CommonTree)adaptor.nil(); + + pushFollow(FOLLOW_rewrite_tree_ebnf_in_rewrite_tree_element2776); + rewrite_tree_ebnf142=rewrite_tree_ebnf(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) adaptor.addChild(root_0, rewrite_tree_ebnf142.getTree()); + + } + break; + + } + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "rewrite_tree_element" + + public static class rewrite_tree_atom_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "rewrite_tree_atom" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:384:1: rewrite_tree_atom : ( CHAR_LITERAL | TOKEN_REF ( ARG_ACTION )? -> ^( TOKEN_REF ( ARG_ACTION )? ) | RULE_REF | STRING_LITERAL | d= '$' id -> LABEL[$d,$id.text] | ACTION ); + public final ANTLRv3Parser.rewrite_tree_atom_return rewrite_tree_atom() throws RecognitionException { + ANTLRv3Parser.rewrite_tree_atom_return retval = new ANTLRv3Parser.rewrite_tree_atom_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token d=null; + Token CHAR_LITERAL143=null; + Token TOKEN_REF144=null; + Token ARG_ACTION145=null; + Token RULE_REF146=null; + Token STRING_LITERAL147=null; + Token ACTION149=null; + ANTLRv3Parser.id_return id148 = null; + + + CommonTree d_tree=null; + CommonTree CHAR_LITERAL143_tree=null; + CommonTree TOKEN_REF144_tree=null; + CommonTree ARG_ACTION145_tree=null; + CommonTree RULE_REF146_tree=null; + CommonTree STRING_LITERAL147_tree=null; + CommonTree ACTION149_tree=null; + RewriteRuleTokenStream stream_ARG_ACTION=new RewriteRuleTokenStream(adaptor,"token ARG_ACTION"); + RewriteRuleTokenStream stream_TOKEN_REF=new RewriteRuleTokenStream(adaptor,"token TOKEN_REF"); + RewriteRuleTokenStream stream_93=new RewriteRuleTokenStream(adaptor,"token 93"); + RewriteRuleSubtreeStream stream_id=new RewriteRuleSubtreeStream(adaptor,"rule id"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:385:5: ( CHAR_LITERAL | TOKEN_REF ( ARG_ACTION )? -> ^( TOKEN_REF ( ARG_ACTION )? ) | RULE_REF | STRING_LITERAL | d= '$' id -> LABEL[$d,$id.text] | ACTION ) + int alt69=6; + switch ( input.LA(1) ) { + case CHAR_LITERAL: + { + alt69=1; + } + break; + case TOKEN_REF: + { + alt69=2; + } + break; + case RULE_REF: + { + alt69=3; + } + break; + case STRING_LITERAL: + { + alt69=4; + } + break; + case 93: + { + alt69=5; + } + break; + case ACTION: + { + alt69=6; + } + break; + default: + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 69, 0, input); + + throw nvae; + } + + switch (alt69) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:385:9: CHAR_LITERAL + { + root_0 = (CommonTree)adaptor.nil(); + + CHAR_LITERAL143=(Token)match(input,CHAR_LITERAL,FOLLOW_CHAR_LITERAL_in_rewrite_tree_atom2792); if (state.failed) return retval; + if ( state.backtracking==0 ) { + CHAR_LITERAL143_tree = (CommonTree)adaptor.create(CHAR_LITERAL143); + adaptor.addChild(root_0, CHAR_LITERAL143_tree); + } + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:386:6: TOKEN_REF ( ARG_ACTION )? + { + TOKEN_REF144=(Token)match(input,TOKEN_REF,FOLLOW_TOKEN_REF_in_rewrite_tree_atom2799); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_TOKEN_REF.add(TOKEN_REF144); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:386:16: ( ARG_ACTION )? + int alt68=2; + int LA68_0 = input.LA(1); + + if ( (LA68_0==ARG_ACTION) ) { + alt68=1; + } + switch (alt68) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:386:16: ARG_ACTION + { + ARG_ACTION145=(Token)match(input,ARG_ACTION,FOLLOW_ARG_ACTION_in_rewrite_tree_atom2801); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ARG_ACTION.add(ARG_ACTION145); + + + } + break; + + } + + + + // AST REWRITE + // elements: TOKEN_REF, ARG_ACTION + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 386:28: -> ^( TOKEN_REF ( ARG_ACTION )? ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:386:31: ^( TOKEN_REF ( ARG_ACTION )? ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_TOKEN_REF.nextNode(), root_1); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:386:43: ( ARG_ACTION )? + if ( stream_ARG_ACTION.hasNext() ) { + adaptor.addChild(root_1, stream_ARG_ACTION.nextNode()); + + } + stream_ARG_ACTION.reset(); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:387:9: RULE_REF + { + root_0 = (CommonTree)adaptor.nil(); + + RULE_REF146=(Token)match(input,RULE_REF,FOLLOW_RULE_REF_in_rewrite_tree_atom2822); if (state.failed) return retval; + if ( state.backtracking==0 ) { + RULE_REF146_tree = (CommonTree)adaptor.create(RULE_REF146); + adaptor.addChild(root_0, RULE_REF146_tree); + } + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:388:6: STRING_LITERAL + { + root_0 = (CommonTree)adaptor.nil(); + + STRING_LITERAL147=(Token)match(input,STRING_LITERAL,FOLLOW_STRING_LITERAL_in_rewrite_tree_atom2829); if (state.failed) return retval; + if ( state.backtracking==0 ) { + STRING_LITERAL147_tree = (CommonTree)adaptor.create(STRING_LITERAL147); + adaptor.addChild(root_0, STRING_LITERAL147_tree); + } + + } + break; + case 5 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:389:6: d= '$' id + { + d=(Token)match(input,93,FOLLOW_93_in_rewrite_tree_atom2838); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_93.add(d); + + pushFollow(FOLLOW_id_in_rewrite_tree_atom2840); + id148=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id148.getTree()); + + + // AST REWRITE + // elements: + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 389:15: -> LABEL[$d,$id.text] + { + adaptor.addChild(root_0, (CommonTree)adaptor.create(LABEL, d, (id148!=null?input.toString(id148.start,id148.stop):null))); + + } + + retval.tree = root_0;} + } + break; + case 6 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:390:4: ACTION + { + root_0 = (CommonTree)adaptor.nil(); + + ACTION149=(Token)match(input,ACTION,FOLLOW_ACTION_in_rewrite_tree_atom2851); if (state.failed) return retval; + if ( state.backtracking==0 ) { + ACTION149_tree = (CommonTree)adaptor.create(ACTION149); + adaptor.addChild(root_0, ACTION149_tree); + } + + } + break; + + } + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "rewrite_tree_atom" + + public static class rewrite_tree_ebnf_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "rewrite_tree_ebnf" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:393:1: rewrite_tree_ebnf : rewrite_tree_block ebnfSuffix -> ^( ebnfSuffix rewrite_tree_block ) ; + public final ANTLRv3Parser.rewrite_tree_ebnf_return rewrite_tree_ebnf() throws RecognitionException { + ANTLRv3Parser.rewrite_tree_ebnf_return retval = new ANTLRv3Parser.rewrite_tree_ebnf_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + ANTLRv3Parser.rewrite_tree_block_return rewrite_tree_block150 = null; + + ANTLRv3Parser.ebnfSuffix_return ebnfSuffix151 = null; + + + RewriteRuleSubtreeStream stream_rewrite_tree_block=new RewriteRuleSubtreeStream(adaptor,"rule rewrite_tree_block"); + RewriteRuleSubtreeStream stream_ebnfSuffix=new RewriteRuleSubtreeStream(adaptor,"rule ebnfSuffix"); + + Token firstToken = input.LT(1); + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:401:2: ( rewrite_tree_block ebnfSuffix -> ^( ebnfSuffix rewrite_tree_block ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:401:4: rewrite_tree_block ebnfSuffix + { + pushFollow(FOLLOW_rewrite_tree_block_in_rewrite_tree_ebnf2872); + rewrite_tree_block150=rewrite_tree_block(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite_tree_block.add(rewrite_tree_block150.getTree()); + pushFollow(FOLLOW_ebnfSuffix_in_rewrite_tree_ebnf2874); + ebnfSuffix151=ebnfSuffix(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ebnfSuffix.add(ebnfSuffix151.getTree()); + + + // AST REWRITE + // elements: rewrite_tree_block, ebnfSuffix + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 401:34: -> ^( ebnfSuffix rewrite_tree_block ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:401:37: ^( ebnfSuffix rewrite_tree_block ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot(stream_ebnfSuffix.nextNode(), root_1); + + adaptor.addChild(root_1, stream_rewrite_tree_block.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + if ( state.backtracking==0 ) { + + ((CommonTree)retval.tree).getToken().setLine(firstToken.getLine()); + ((CommonTree)retval.tree).getToken().setCharPositionInLine(firstToken.getCharPositionInLine()); + + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "rewrite_tree_ebnf" + + public static class rewrite_tree_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "rewrite_tree" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:404:1: rewrite_tree : '^(' rewrite_tree_atom ( rewrite_tree_element )* ')' -> ^( TREE_BEGIN rewrite_tree_atom ( rewrite_tree_element )* ) ; + public final ANTLRv3Parser.rewrite_tree_return rewrite_tree() throws RecognitionException { + ANTLRv3Parser.rewrite_tree_return retval = new ANTLRv3Parser.rewrite_tree_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token string_literal152=null; + Token char_literal155=null; + ANTLRv3Parser.rewrite_tree_atom_return rewrite_tree_atom153 = null; + + ANTLRv3Parser.rewrite_tree_element_return rewrite_tree_element154 = null; + + + CommonTree string_literal152_tree=null; + CommonTree char_literal155_tree=null; + RewriteRuleTokenStream stream_84=new RewriteRuleTokenStream(adaptor,"token 84"); + RewriteRuleTokenStream stream_TREE_BEGIN=new RewriteRuleTokenStream(adaptor,"token TREE_BEGIN"); + RewriteRuleSubtreeStream stream_rewrite_tree_element=new RewriteRuleSubtreeStream(adaptor,"rule rewrite_tree_element"); + RewriteRuleSubtreeStream stream_rewrite_tree_atom=new RewriteRuleSubtreeStream(adaptor,"rule rewrite_tree_atom"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:405:2: ( '^(' rewrite_tree_atom ( rewrite_tree_element )* ')' -> ^( TREE_BEGIN rewrite_tree_atom ( rewrite_tree_element )* ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:405:4: '^(' rewrite_tree_atom ( rewrite_tree_element )* ')' + { + string_literal152=(Token)match(input,TREE_BEGIN,FOLLOW_TREE_BEGIN_in_rewrite_tree2894); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_TREE_BEGIN.add(string_literal152); + + pushFollow(FOLLOW_rewrite_tree_atom_in_rewrite_tree2896); + rewrite_tree_atom153=rewrite_tree_atom(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite_tree_atom.add(rewrite_tree_atom153.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:405:27: ( rewrite_tree_element )* + loop70: + do { + int alt70=2; + int LA70_0 = input.LA(1); + + if ( (LA70_0==TREE_BEGIN||(LA70_0>=TOKEN_REF && LA70_0<=ACTION)||LA70_0==RULE_REF||LA70_0==82||LA70_0==93) ) { + alt70=1; + } + + + switch (alt70) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:405:27: rewrite_tree_element + { + pushFollow(FOLLOW_rewrite_tree_element_in_rewrite_tree2898); + rewrite_tree_element154=rewrite_tree_element(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite_tree_element.add(rewrite_tree_element154.getTree()); + + } + break; + + default : + break loop70; + } + } while (true); + + char_literal155=(Token)match(input,84,FOLLOW_84_in_rewrite_tree2901); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_84.add(char_literal155); + + + + // AST REWRITE + // elements: rewrite_tree_element, rewrite_tree_atom + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 406:3: -> ^( TREE_BEGIN rewrite_tree_atom ( rewrite_tree_element )* ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:406:6: ^( TREE_BEGIN rewrite_tree_atom ( rewrite_tree_element )* ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(TREE_BEGIN, "TREE_BEGIN"), root_1); + + adaptor.addChild(root_1, stream_rewrite_tree_atom.nextTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:406:37: ( rewrite_tree_element )* + while ( stream_rewrite_tree_element.hasNext() ) { + adaptor.addChild(root_1, stream_rewrite_tree_element.nextTree()); + + } + stream_rewrite_tree_element.reset(); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "rewrite_tree" + + public static class rewrite_template_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "rewrite_template" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:409:1: rewrite_template : ( id lp= '(' rewrite_template_args ')' (str= DOUBLE_QUOTE_STRING_LITERAL | str= DOUBLE_ANGLE_STRING_LITERAL ) -> ^( TEMPLATE[$lp,\"TEMPLATE\"] id rewrite_template_args $str) | rewrite_template_ref | rewrite_indirect_template_head | ACTION ); + public final ANTLRv3Parser.rewrite_template_return rewrite_template() throws RecognitionException { + ANTLRv3Parser.rewrite_template_return retval = new ANTLRv3Parser.rewrite_template_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token lp=null; + Token str=null; + Token char_literal158=null; + Token ACTION161=null; + ANTLRv3Parser.id_return id156 = null; + + ANTLRv3Parser.rewrite_template_args_return rewrite_template_args157 = null; + + ANTLRv3Parser.rewrite_template_ref_return rewrite_template_ref159 = null; + + ANTLRv3Parser.rewrite_indirect_template_head_return rewrite_indirect_template_head160 = null; + + + CommonTree lp_tree=null; + CommonTree str_tree=null; + CommonTree char_literal158_tree=null; + CommonTree ACTION161_tree=null; + RewriteRuleTokenStream stream_DOUBLE_ANGLE_STRING_LITERAL=new RewriteRuleTokenStream(adaptor,"token DOUBLE_ANGLE_STRING_LITERAL"); + RewriteRuleTokenStream stream_DOUBLE_QUOTE_STRING_LITERAL=new RewriteRuleTokenStream(adaptor,"token DOUBLE_QUOTE_STRING_LITERAL"); + RewriteRuleTokenStream stream_84=new RewriteRuleTokenStream(adaptor,"token 84"); + RewriteRuleTokenStream stream_82=new RewriteRuleTokenStream(adaptor,"token 82"); + RewriteRuleSubtreeStream stream_rewrite_template_args=new RewriteRuleSubtreeStream(adaptor,"rule rewrite_template_args"); + RewriteRuleSubtreeStream stream_id=new RewriteRuleSubtreeStream(adaptor,"rule id"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:421:2: ( id lp= '(' rewrite_template_args ')' (str= DOUBLE_QUOTE_STRING_LITERAL | str= DOUBLE_ANGLE_STRING_LITERAL ) -> ^( TEMPLATE[$lp,\"TEMPLATE\"] id rewrite_template_args $str) | rewrite_template_ref | rewrite_indirect_template_head | ACTION ) + int alt72=4; + alt72 = dfa72.predict(input); + switch (alt72) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:422:3: id lp= '(' rewrite_template_args ')' (str= DOUBLE_QUOTE_STRING_LITERAL | str= DOUBLE_ANGLE_STRING_LITERAL ) + { + pushFollow(FOLLOW_id_in_rewrite_template2933); + id156=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id156.getTree()); + lp=(Token)match(input,82,FOLLOW_82_in_rewrite_template2937); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_82.add(lp); + + pushFollow(FOLLOW_rewrite_template_args_in_rewrite_template2939); + rewrite_template_args157=rewrite_template_args(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite_template_args.add(rewrite_template_args157.getTree()); + char_literal158=(Token)match(input,84,FOLLOW_84_in_rewrite_template2941); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_84.add(char_literal158); + + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:423:3: (str= DOUBLE_QUOTE_STRING_LITERAL | str= DOUBLE_ANGLE_STRING_LITERAL ) + int alt71=2; + int LA71_0 = input.LA(1); + + if ( (LA71_0==DOUBLE_QUOTE_STRING_LITERAL) ) { + alt71=1; + } + else if ( (LA71_0==DOUBLE_ANGLE_STRING_LITERAL) ) { + alt71=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 71, 0, input); + + throw nvae; + } + switch (alt71) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:423:5: str= DOUBLE_QUOTE_STRING_LITERAL + { + str=(Token)match(input,DOUBLE_QUOTE_STRING_LITERAL,FOLLOW_DOUBLE_QUOTE_STRING_LITERAL_in_rewrite_template2949); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_DOUBLE_QUOTE_STRING_LITERAL.add(str); + + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:423:39: str= DOUBLE_ANGLE_STRING_LITERAL + { + str=(Token)match(input,DOUBLE_ANGLE_STRING_LITERAL,FOLLOW_DOUBLE_ANGLE_STRING_LITERAL_in_rewrite_template2955); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_DOUBLE_ANGLE_STRING_LITERAL.add(str); + + + } + break; + + } + + + + // AST REWRITE + // elements: id, rewrite_template_args, str + // token labels: str + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleTokenStream stream_str=new RewriteRuleTokenStream(adaptor,"token str",str); + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 424:3: -> ^( TEMPLATE[$lp,\"TEMPLATE\"] id rewrite_template_args $str) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:424:6: ^( TEMPLATE[$lp,\"TEMPLATE\"] id rewrite_template_args $str) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(TEMPLATE, lp, "TEMPLATE"), root_1); + + adaptor.addChild(root_1, stream_id.nextTree()); + adaptor.addChild(root_1, stream_rewrite_template_args.nextTree()); + adaptor.addChild(root_1, stream_str.nextNode()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:427:3: rewrite_template_ref + { + root_0 = (CommonTree)adaptor.nil(); + + pushFollow(FOLLOW_rewrite_template_ref_in_rewrite_template2982); + rewrite_template_ref159=rewrite_template_ref(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) adaptor.addChild(root_0, rewrite_template_ref159.getTree()); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:430:3: rewrite_indirect_template_head + { + root_0 = (CommonTree)adaptor.nil(); + + pushFollow(FOLLOW_rewrite_indirect_template_head_in_rewrite_template2991); + rewrite_indirect_template_head160=rewrite_indirect_template_head(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) adaptor.addChild(root_0, rewrite_indirect_template_head160.getTree()); + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:433:3: ACTION + { + root_0 = (CommonTree)adaptor.nil(); + + ACTION161=(Token)match(input,ACTION,FOLLOW_ACTION_in_rewrite_template3000); if (state.failed) return retval; + if ( state.backtracking==0 ) { + ACTION161_tree = (CommonTree)adaptor.create(ACTION161); + adaptor.addChild(root_0, ACTION161_tree); + } + + } + break; + + } + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "rewrite_template" + + public static class rewrite_template_ref_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "rewrite_template_ref" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:436:1: rewrite_template_ref : id lp= '(' rewrite_template_args ')' -> ^( TEMPLATE[$lp,\"TEMPLATE\"] id rewrite_template_args ) ; + public final ANTLRv3Parser.rewrite_template_ref_return rewrite_template_ref() throws RecognitionException { + ANTLRv3Parser.rewrite_template_ref_return retval = new ANTLRv3Parser.rewrite_template_ref_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token lp=null; + Token char_literal164=null; + ANTLRv3Parser.id_return id162 = null; + + ANTLRv3Parser.rewrite_template_args_return rewrite_template_args163 = null; + + + CommonTree lp_tree=null; + CommonTree char_literal164_tree=null; + RewriteRuleTokenStream stream_84=new RewriteRuleTokenStream(adaptor,"token 84"); + RewriteRuleTokenStream stream_82=new RewriteRuleTokenStream(adaptor,"token 82"); + RewriteRuleSubtreeStream stream_rewrite_template_args=new RewriteRuleSubtreeStream(adaptor,"rule rewrite_template_args"); + RewriteRuleSubtreeStream stream_id=new RewriteRuleSubtreeStream(adaptor,"rule id"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:438:2: ( id lp= '(' rewrite_template_args ')' -> ^( TEMPLATE[$lp,\"TEMPLATE\"] id rewrite_template_args ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:438:4: id lp= '(' rewrite_template_args ')' + { + pushFollow(FOLLOW_id_in_rewrite_template_ref3013); + id162=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id162.getTree()); + lp=(Token)match(input,82,FOLLOW_82_in_rewrite_template_ref3017); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_82.add(lp); + + pushFollow(FOLLOW_rewrite_template_args_in_rewrite_template_ref3019); + rewrite_template_args163=rewrite_template_args(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite_template_args.add(rewrite_template_args163.getTree()); + char_literal164=(Token)match(input,84,FOLLOW_84_in_rewrite_template_ref3021); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_84.add(char_literal164); + + + + // AST REWRITE + // elements: id, rewrite_template_args + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 439:3: -> ^( TEMPLATE[$lp,\"TEMPLATE\"] id rewrite_template_args ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:439:6: ^( TEMPLATE[$lp,\"TEMPLATE\"] id rewrite_template_args ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(TEMPLATE, lp, "TEMPLATE"), root_1); + + adaptor.addChild(root_1, stream_id.nextTree()); + adaptor.addChild(root_1, stream_rewrite_template_args.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "rewrite_template_ref" + + public static class rewrite_indirect_template_head_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "rewrite_indirect_template_head" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:442:1: rewrite_indirect_template_head : lp= '(' ACTION ')' '(' rewrite_template_args ')' -> ^( TEMPLATE[$lp,\"TEMPLATE\"] ACTION rewrite_template_args ) ; + public final ANTLRv3Parser.rewrite_indirect_template_head_return rewrite_indirect_template_head() throws RecognitionException { + ANTLRv3Parser.rewrite_indirect_template_head_return retval = new ANTLRv3Parser.rewrite_indirect_template_head_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token lp=null; + Token ACTION165=null; + Token char_literal166=null; + Token char_literal167=null; + Token char_literal169=null; + ANTLRv3Parser.rewrite_template_args_return rewrite_template_args168 = null; + + + CommonTree lp_tree=null; + CommonTree ACTION165_tree=null; + CommonTree char_literal166_tree=null; + CommonTree char_literal167_tree=null; + CommonTree char_literal169_tree=null; + RewriteRuleTokenStream stream_ACTION=new RewriteRuleTokenStream(adaptor,"token ACTION"); + RewriteRuleTokenStream stream_84=new RewriteRuleTokenStream(adaptor,"token 84"); + RewriteRuleTokenStream stream_82=new RewriteRuleTokenStream(adaptor,"token 82"); + RewriteRuleSubtreeStream stream_rewrite_template_args=new RewriteRuleSubtreeStream(adaptor,"rule rewrite_template_args"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:444:2: (lp= '(' ACTION ')' '(' rewrite_template_args ')' -> ^( TEMPLATE[$lp,\"TEMPLATE\"] ACTION rewrite_template_args ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:444:4: lp= '(' ACTION ')' '(' rewrite_template_args ')' + { + lp=(Token)match(input,82,FOLLOW_82_in_rewrite_indirect_template_head3049); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_82.add(lp); + + ACTION165=(Token)match(input,ACTION,FOLLOW_ACTION_in_rewrite_indirect_template_head3051); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ACTION.add(ACTION165); + + char_literal166=(Token)match(input,84,FOLLOW_84_in_rewrite_indirect_template_head3053); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_84.add(char_literal166); + + char_literal167=(Token)match(input,82,FOLLOW_82_in_rewrite_indirect_template_head3055); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_82.add(char_literal167); + + pushFollow(FOLLOW_rewrite_template_args_in_rewrite_indirect_template_head3057); + rewrite_template_args168=rewrite_template_args(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite_template_args.add(rewrite_template_args168.getTree()); + char_literal169=(Token)match(input,84,FOLLOW_84_in_rewrite_indirect_template_head3059); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_84.add(char_literal169); + + + + // AST REWRITE + // elements: ACTION, rewrite_template_args + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 445:3: -> ^( TEMPLATE[$lp,\"TEMPLATE\"] ACTION rewrite_template_args ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:445:6: ^( TEMPLATE[$lp,\"TEMPLATE\"] ACTION rewrite_template_args ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(TEMPLATE, lp, "TEMPLATE"), root_1); + + adaptor.addChild(root_1, stream_ACTION.nextNode()); + adaptor.addChild(root_1, stream_rewrite_template_args.nextTree()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "rewrite_indirect_template_head" + + public static class rewrite_template_args_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "rewrite_template_args" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:448:1: rewrite_template_args : ( rewrite_template_arg ( ',' rewrite_template_arg )* -> ^( ARGLIST ( rewrite_template_arg )+ ) | -> ARGLIST ); + public final ANTLRv3Parser.rewrite_template_args_return rewrite_template_args() throws RecognitionException { + ANTLRv3Parser.rewrite_template_args_return retval = new ANTLRv3Parser.rewrite_template_args_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token char_literal171=null; + ANTLRv3Parser.rewrite_template_arg_return rewrite_template_arg170 = null; + + ANTLRv3Parser.rewrite_template_arg_return rewrite_template_arg172 = null; + + + CommonTree char_literal171_tree=null; + RewriteRuleTokenStream stream_81=new RewriteRuleTokenStream(adaptor,"token 81"); + RewriteRuleSubtreeStream stream_rewrite_template_arg=new RewriteRuleSubtreeStream(adaptor,"rule rewrite_template_arg"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:449:2: ( rewrite_template_arg ( ',' rewrite_template_arg )* -> ^( ARGLIST ( rewrite_template_arg )+ ) | -> ARGLIST ) + int alt74=2; + int LA74_0 = input.LA(1); + + if ( (LA74_0==TOKEN_REF||LA74_0==RULE_REF) ) { + alt74=1; + } + else if ( (LA74_0==84) ) { + alt74=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 74, 0, input); + + throw nvae; + } + switch (alt74) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:449:4: rewrite_template_arg ( ',' rewrite_template_arg )* + { + pushFollow(FOLLOW_rewrite_template_arg_in_rewrite_template_args3083); + rewrite_template_arg170=rewrite_template_arg(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite_template_arg.add(rewrite_template_arg170.getTree()); + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:449:25: ( ',' rewrite_template_arg )* + loop73: + do { + int alt73=2; + int LA73_0 = input.LA(1); + + if ( (LA73_0==81) ) { + alt73=1; + } + + + switch (alt73) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:449:26: ',' rewrite_template_arg + { + char_literal171=(Token)match(input,81,FOLLOW_81_in_rewrite_template_args3086); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_81.add(char_literal171); + + pushFollow(FOLLOW_rewrite_template_arg_in_rewrite_template_args3088); + rewrite_template_arg172=rewrite_template_arg(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_rewrite_template_arg.add(rewrite_template_arg172.getTree()); + + } + break; + + default : + break loop73; + } + } while (true); + + + + // AST REWRITE + // elements: rewrite_template_arg + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 450:3: -> ^( ARGLIST ( rewrite_template_arg )+ ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:450:6: ^( ARGLIST ( rewrite_template_arg )+ ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(ARGLIST, "ARGLIST"), root_1); + + if ( !(stream_rewrite_template_arg.hasNext()) ) { + throw new RewriteEarlyExitException(); + } + while ( stream_rewrite_template_arg.hasNext() ) { + adaptor.addChild(root_1, stream_rewrite_template_arg.nextTree()); + + } + stream_rewrite_template_arg.reset(); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:451:4: + { + + // AST REWRITE + // elements: + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 451:4: -> ARGLIST + { + adaptor.addChild(root_0, (CommonTree)adaptor.create(ARGLIST, "ARGLIST")); + + } + + retval.tree = root_0;} + } + break; + + } + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "rewrite_template_args" + + public static class rewrite_template_arg_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "rewrite_template_arg" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:454:1: rewrite_template_arg : id '=' ACTION -> ^( ARG[$id.start] id ACTION ) ; + public final ANTLRv3Parser.rewrite_template_arg_return rewrite_template_arg() throws RecognitionException { + ANTLRv3Parser.rewrite_template_arg_return retval = new ANTLRv3Parser.rewrite_template_arg_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token char_literal174=null; + Token ACTION175=null; + ANTLRv3Parser.id_return id173 = null; + + + CommonTree char_literal174_tree=null; + CommonTree ACTION175_tree=null; + RewriteRuleTokenStream stream_71=new RewriteRuleTokenStream(adaptor,"token 71"); + RewriteRuleTokenStream stream_ACTION=new RewriteRuleTokenStream(adaptor,"token ACTION"); + RewriteRuleSubtreeStream stream_id=new RewriteRuleSubtreeStream(adaptor,"rule id"); + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:455:2: ( id '=' ACTION -> ^( ARG[$id.start] id ACTION ) ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:455:6: id '=' ACTION + { + pushFollow(FOLLOW_id_in_rewrite_template_arg3121); + id173=id(); + + state._fsp--; + if (state.failed) return retval; + if ( state.backtracking==0 ) stream_id.add(id173.getTree()); + char_literal174=(Token)match(input,71,FOLLOW_71_in_rewrite_template_arg3123); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_71.add(char_literal174); + + ACTION175=(Token)match(input,ACTION,FOLLOW_ACTION_in_rewrite_template_arg3125); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_ACTION.add(ACTION175); + + + + // AST REWRITE + // elements: ACTION, id + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 455:20: -> ^( ARG[$id.start] id ACTION ) + { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:455:23: ^( ARG[$id.start] id ACTION ) + { + CommonTree root_1 = (CommonTree)adaptor.nil(); + root_1 = (CommonTree)adaptor.becomeRoot((CommonTree)adaptor.create(ARG, (id173!=null?((Token)id173.start):null)), root_1); + + adaptor.addChild(root_1, stream_id.nextTree()); + adaptor.addChild(root_1, stream_ACTION.nextNode()); + + adaptor.addChild(root_0, root_1); + } + + } + + retval.tree = root_0;} + } + + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "rewrite_template_arg" + + public static class id_return extends ParserRuleReturnScope { + CommonTree tree; + public Object getTree() { return tree; } + }; + + // $ANTLR start "id" + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:458:1: id : ( TOKEN_REF -> ID[$TOKEN_REF] | RULE_REF -> ID[$RULE_REF] ); + public final ANTLRv3Parser.id_return id() throws RecognitionException { + ANTLRv3Parser.id_return retval = new ANTLRv3Parser.id_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + Token TOKEN_REF176=null; + Token RULE_REF177=null; + + CommonTree TOKEN_REF176_tree=null; + CommonTree RULE_REF177_tree=null; + RewriteRuleTokenStream stream_TOKEN_REF=new RewriteRuleTokenStream(adaptor,"token TOKEN_REF"); + RewriteRuleTokenStream stream_RULE_REF=new RewriteRuleTokenStream(adaptor,"token RULE_REF"); + + try { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:458:4: ( TOKEN_REF -> ID[$TOKEN_REF] | RULE_REF -> ID[$RULE_REF] ) + int alt75=2; + int LA75_0 = input.LA(1); + + if ( (LA75_0==TOKEN_REF) ) { + alt75=1; + } + else if ( (LA75_0==RULE_REF) ) { + alt75=2; + } + else { + if (state.backtracking>0) {state.failed=true; return retval;} + NoViableAltException nvae = + new NoViableAltException("", 75, 0, input); + + throw nvae; + } + switch (alt75) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:458:6: TOKEN_REF + { + TOKEN_REF176=(Token)match(input,TOKEN_REF,FOLLOW_TOKEN_REF_in_id3146); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_TOKEN_REF.add(TOKEN_REF176); + + + + // AST REWRITE + // elements: + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 458:16: -> ID[$TOKEN_REF] + { + adaptor.addChild(root_0, (CommonTree)adaptor.create(ID, TOKEN_REF176)); + + } + + retval.tree = root_0;} + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:459:4: RULE_REF + { + RULE_REF177=(Token)match(input,RULE_REF,FOLLOW_RULE_REF_in_id3156); if (state.failed) return retval; + if ( state.backtracking==0 ) stream_RULE_REF.add(RULE_REF177); + + + + // AST REWRITE + // elements: + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + if ( state.backtracking==0 ) { + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"token retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.nil(); + // 459:14: -> ID[$RULE_REF] + { + adaptor.addChild(root_0, (CommonTree)adaptor.create(ID, RULE_REF177)); + + } + + retval.tree = root_0;} + } + break; + + } + retval.stop = input.LT(-1); + + if ( state.backtracking==0 ) { + + retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0); + adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); + + } + finally { + } + return retval; + } + // $ANTLR end "id" + + // $ANTLR start synpred1_ANTLRv3 + public final void synpred1_ANTLRv3_fragment() throws RecognitionException { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:358:4: ( rewrite_template ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:358:4: rewrite_template + { + pushFollow(FOLLOW_rewrite_template_in_synpred1_ANTLRv32570); + rewrite_template(); + + state._fsp--; + if (state.failed) return ; + + } + } + // $ANTLR end synpred1_ANTLRv3 + + // $ANTLR start synpred2_ANTLRv3 + public final void synpred2_ANTLRv3_fragment() throws RecognitionException { + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:359:4: ( rewrite_tree_alternative ) + // /Users/scai/NetBeansProjects/gUnitBuilder/src/gunitbuilder/ANTLRv3.g:359:4: rewrite_tree_alternative + { + pushFollow(FOLLOW_rewrite_tree_alternative_in_synpred2_ANTLRv32575); + rewrite_tree_alternative(); + + state._fsp--; + if (state.failed) return ; + + } + } + // $ANTLR end synpred2_ANTLRv3 + + // Delegated rules + + public final boolean synpred2_ANTLRv3() { + state.backtracking++; + int start = input.mark(); + try { + synpred2_ANTLRv3_fragment(); // can never throw exception + } catch (RecognitionException re) { + System.err.println("impossible: "+re); + } + boolean success = !state.failed; + input.rewind(start); + state.backtracking--; + state.failed=false; + return success; + } + public final boolean synpred1_ANTLRv3() { + state.backtracking++; + int start = input.mark(); + try { + synpred1_ANTLRv3_fragment(); // can never throw exception + } catch (RecognitionException re) { + System.err.println("impossible: "+re); + } + boolean success = !state.failed; + input.rewind(start); + state.backtracking--; + state.failed=false; + return success; + } + + + protected DFA46 dfa46 = new DFA46(this); + protected DFA64 dfa64 = new DFA64(this); + protected DFA67 dfa67 = new DFA67(this); + protected DFA72 dfa72 = new DFA72(this); + static final String DFA46_eotS = + "\14\uffff"; + static final String DFA46_eofS = + "\14\uffff"; + static final String DFA46_minS = + "\3\40\5\uffff\2\52\2\uffff"; + static final String DFA46_maxS = + "\3\134\5\uffff\2\134\2\uffff"; + static final String DFA46_acceptS = + "\3\uffff\1\3\1\4\1\5\1\6\1\7\2\uffff\1\1\1\2"; + static final String DFA46_specialS = + "\14\uffff}>"; + static final String[] DFA46_transitionS = { + "\1\6\4\uffff\1\7\4\uffff\1\1\2\3\1\5\3\uffff\1\2\40\uffff\1"+ + "\4\6\uffff\1\3\2\uffff\1\3", + "\1\3\4\uffff\4\3\1\uffff\4\3\2\uffff\2\3\23\uffff\1\3\1\uffff"+ + "\1\10\2\uffff\1\3\7\uffff\3\3\2\uffff\1\11\1\uffff\4\3", + "\1\3\4\uffff\4\3\1\uffff\4\3\2\uffff\2\3\23\uffff\1\3\1\uffff"+ + "\1\10\2\uffff\1\3\7\uffff\3\3\2\uffff\1\11\1\uffff\4\3", + "", + "", + "", + "", + "", + "\3\12\4\uffff\1\12\40\uffff\1\13\6\uffff\1\12\2\uffff\1\12", + "\3\12\4\uffff\1\12\40\uffff\1\13\6\uffff\1\12\2\uffff\1\12", + "", + "" + }; + + static final short[] DFA46_eot = DFA.unpackEncodedString(DFA46_eotS); + static final short[] DFA46_eof = DFA.unpackEncodedString(DFA46_eofS); + static final char[] DFA46_min = DFA.unpackEncodedStringToUnsignedChars(DFA46_minS); + static final char[] DFA46_max = DFA.unpackEncodedStringToUnsignedChars(DFA46_maxS); + static final short[] DFA46_accept = DFA.unpackEncodedString(DFA46_acceptS); + static final short[] DFA46_special = DFA.unpackEncodedString(DFA46_specialS); + static final short[][] DFA46_transition; + + static { + int numStates = DFA46_transitionS.length; + DFA46_transition = new short[numStates][]; + for (int i=0; i ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id atom ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> ^( $labelOp id atom ) ) | id (labelOp= '=' | labelOp= '+=' ) block ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] ^( $labelOp id block ) EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> ^( $labelOp id block ) ) | atom ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] atom EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> atom ) | ebnf | ACTION | SEMPRED ( '=>' -> GATED_SEMPRED | -> SEMPRED ) | treeSpec ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] treeSpec EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> treeSpec ) );"; + } + } + static final String DFA64_eotS = + "\15\uffff"; + static final String DFA64_eofS = + "\15\uffff"; + static final String DFA64_minS = + "\4\45\1\0\2\uffff\2\45\1\uffff\2\45\1\112"; + static final String DFA64_maxS = + "\4\135\1\0\2\uffff\2\135\1\uffff\2\135\1\133"; + static final String DFA64_acceptS = + "\5\uffff\1\2\1\3\2\uffff\1\1\3\uffff"; + static final String DFA64_specialS = + "\4\uffff\1\0\10\uffff}>"; + static final String[] DFA64_transitionS = { + "\1\5\2\uffff\1\6\1\uffff\1\1\2\5\1\4\3\uffff\1\2\23\uffff\1"+ + "\6\14\uffff\1\3\2\6\10\uffff\1\5", + "\1\5\2\uffff\1\5\1\uffff\4\5\2\uffff\2\5\23\uffff\1\5\4\uffff"+ + "\1\5\7\uffff\1\7\2\5\5\uffff\2\5\1\uffff\1\5", + "\1\5\2\uffff\1\5\1\uffff\4\5\3\uffff\1\5\23\uffff\1\5\4\uffff"+ + "\1\5\7\uffff\1\7\2\5\5\uffff\2\5\1\uffff\1\5", + "\1\5\4\uffff\3\5\1\10\3\uffff\1\5\40\uffff\1\5\12\uffff\1\5", + "\1\uffff", + "", + "", + "\1\5\4\uffff\1\12\3\5\3\uffff\1\13\40\uffff\1\5\1\uffff\1\11"+ + "\10\uffff\1\5", + "\1\5\4\uffff\4\5\3\uffff\1\5\30\uffff\1\5\7\uffff\1\5\1\uffff"+ + "\1\14\5\uffff\2\5\1\uffff\1\5", + "", + "\1\5\4\uffff\4\5\2\uffff\2\5\25\uffff\1\11\2\uffff\1\5\7\uffff"+ + "\1\5\1\uffff\1\5\5\uffff\2\5\1\uffff\1\5", + "\1\5\4\uffff\4\5\3\uffff\1\5\25\uffff\1\11\2\uffff\1\5\7\uffff"+ + "\1\5\1\uffff\1\5\5\uffff\2\5\1\uffff\1\5", + "\1\5\7\uffff\1\11\7\uffff\2\5" + }; + + static final short[] DFA64_eot = DFA.unpackEncodedString(DFA64_eotS); + static final short[] DFA64_eof = DFA.unpackEncodedString(DFA64_eofS); + static final char[] DFA64_min = DFA.unpackEncodedStringToUnsignedChars(DFA64_minS); + static final char[] DFA64_max = DFA.unpackEncodedStringToUnsignedChars(DFA64_maxS); + static final short[] DFA64_accept = DFA.unpackEncodedString(DFA64_acceptS); + static final short[] DFA64_special = DFA.unpackEncodedString(DFA64_specialS); + static final short[][] DFA64_transition; + + static { + int numStates = DFA64_transitionS.length; + DFA64_transition = new short[numStates][]; + for (int i=0; i ^( ALT[\"ALT\"] EPSILON[\"EPSILON\"] EOA[\"EOA\"] ) );"; + } + public int specialStateTransition(int s, IntStream _input) throws NoViableAltException { + TokenStream input = (TokenStream)_input; + int _s = s; + switch ( s ) { + case 0 : + int LA64_4 = input.LA(1); + + + int index64_4 = input.index(); + input.rewind(); + s = -1; + if ( (synpred1_ANTLRv3()) ) {s = 9;} + + else if ( (synpred2_ANTLRv3()) ) {s = 5;} + + + input.seek(index64_4); + if ( s>=0 ) return s; + break; + } + if (state.backtracking>0) {state.failed=true; return -1;} + NoViableAltException nvae = + new NoViableAltException(getDescription(), 64, _s, input); + error(nvae); + throw nvae; + } + } + static final String DFA67_eotS = + "\16\uffff"; + static final String DFA67_eofS = + "\1\uffff\4\12\1\uffff\1\12\4\uffff\3\12"; + static final String DFA67_minS = + "\5\45\1\52\1\45\4\uffff\3\45"; + static final String DFA67_maxS = + "\5\135\1\61\1\135\4\uffff\3\135"; + static final String DFA67_acceptS = + "\7\uffff\1\3\1\4\1\2\1\1\3\uffff"; + static final String DFA67_specialS = + "\16\uffff}>"; + static final String[] DFA67_transitionS = { + "\1\7\4\uffff\1\2\1\4\1\1\1\6\3\uffff\1\3\40\uffff\1\10\12\uffff"+ + "\1\5", + "\1\12\2\uffff\1\12\1\uffff\4\12\3\uffff\1\12\23\uffff\1\12"+ + "\4\uffff\1\11\7\uffff\3\12\5\uffff\2\11\1\uffff\1\12", + "\1\12\2\uffff\1\12\1\uffff\4\12\2\uffff\1\13\1\12\23\uffff"+ + "\1\12\4\uffff\1\11\7\uffff\3\12\5\uffff\2\11\1\uffff\1\12", + "\1\12\2\uffff\1\12\1\uffff\4\12\3\uffff\1\12\23\uffff\1\12"+ + "\4\uffff\1\11\7\uffff\3\12\5\uffff\2\11\1\uffff\1\12", + "\1\12\2\uffff\1\12\1\uffff\4\12\3\uffff\1\12\23\uffff\1\12"+ + "\4\uffff\1\11\7\uffff\3\12\5\uffff\2\11\1\uffff\1\12", + "\1\14\6\uffff\1\15", + "\1\12\2\uffff\1\12\1\uffff\4\12\3\uffff\1\12\23\uffff\1\12"+ + "\4\uffff\1\11\7\uffff\3\12\5\uffff\2\11\1\uffff\1\12", + "", + "", + "", + "", + "\1\12\2\uffff\1\12\1\uffff\4\12\3\uffff\1\12\23\uffff\1\12"+ + "\4\uffff\1\11\7\uffff\3\12\5\uffff\2\11\1\uffff\1\12", + "\1\12\2\uffff\1\12\1\uffff\4\12\3\uffff\1\12\23\uffff\1\12"+ + "\4\uffff\1\11\7\uffff\3\12\5\uffff\2\11\1\uffff\1\12", + "\1\12\2\uffff\1\12\1\uffff\4\12\3\uffff\1\12\23\uffff\1\12"+ + "\4\uffff\1\11\7\uffff\3\12\5\uffff\2\11\1\uffff\1\12" + }; + + static final short[] DFA67_eot = DFA.unpackEncodedString(DFA67_eotS); + static final short[] DFA67_eof = DFA.unpackEncodedString(DFA67_eofS); + static final char[] DFA67_min = DFA.unpackEncodedStringToUnsignedChars(DFA67_minS); + static final char[] DFA67_max = DFA.unpackEncodedStringToUnsignedChars(DFA67_maxS); + static final short[] DFA67_accept = DFA.unpackEncodedString(DFA67_acceptS); + static final short[] DFA67_special = DFA.unpackEncodedString(DFA67_specialS); + static final short[][] DFA67_transition; + + static { + int numStates = DFA67_transitionS.length; + DFA67_transition = new short[numStates][]; + for (int i=0; i ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] rewrite_tree_atom EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | rewrite_tree ( ebnfSuffix -> ^( ebnfSuffix ^( BLOCK[\"BLOCK\"] ^( ALT[\"ALT\"] rewrite_tree EOA[\"EOA\"] ) EOB[\"EOB\"] ) ) | -> rewrite_tree ) | rewrite_tree_ebnf );"; + } + } + static final String DFA72_eotS = + "\22\uffff"; + static final String DFA72_eofS = + "\10\uffff\1\12\11\uffff"; + static final String DFA72_minS = + "\1\52\2\122\2\uffff\1\52\2\107\1\50\1\55\2\uffff\1\121\1\52\2\107"+ + "\1\55\1\121"; + static final String DFA72_maxS = + "\3\122\2\uffff\1\124\2\107\1\124\1\55\2\uffff\1\124\1\61\2\107\1"+ + "\55\1\124"; + static final String DFA72_acceptS = + "\3\uffff\1\3\1\4\5\uffff\1\2\1\1\6\uffff"; + static final String DFA72_specialS = + "\22\uffff}>"; + static final String[] DFA72_transitionS = { + "\1\1\2\uffff\1\4\3\uffff\1\2\40\uffff\1\3", + "\1\5", + "\1\5", + "", + "", + "\1\6\6\uffff\1\7\42\uffff\1\10", + "\1\11", + "\1\11", + "\1\12\11\uffff\2\13\21\uffff\1\12\15\uffff\2\12", + "\1\14", + "", + "", + "\1\15\2\uffff\1\10", + "\1\16\6\uffff\1\17", + "\1\20", + "\1\20", + "\1\21", + "\1\15\2\uffff\1\10" + }; + + static final short[] DFA72_eot = DFA.unpackEncodedString(DFA72_eotS); + static final short[] DFA72_eof = DFA.unpackEncodedString(DFA72_eofS); + static final char[] DFA72_min = DFA.unpackEncodedStringToUnsignedChars(DFA72_minS); + static final char[] DFA72_max = DFA.unpackEncodedStringToUnsignedChars(DFA72_maxS); + static final short[] DFA72_accept = DFA.unpackEncodedString(DFA72_acceptS); + static final short[] DFA72_special = DFA.unpackEncodedString(DFA72_specialS); + static final short[][] DFA72_transition; + + static { + int numStates = DFA72_transitionS.length; + DFA72_transition = new short[numStates][]; + for (int i=0; i ^( TEMPLATE[$lp,\"TEMPLATE\"] id rewrite_template_args $str) | rewrite_template_ref | rewrite_indirect_template_head | ACTION );"; + } + } + + + public static final BitSet FOLLOW_DOC_COMMENT_in_grammarDef347 = new BitSet(new long[]{0x0000000000000000L,0x000000000000001EL}); + public static final BitSet FOLLOW_65_in_grammarDef357 = new BitSet(new long[]{0x0000000000000000L,0x0000000000000010L}); + public static final BitSet FOLLOW_66_in_grammarDef375 = new BitSet(new long[]{0x0000000000000000L,0x0000000000000010L}); + public static final BitSet FOLLOW_67_in_grammarDef391 = new BitSet(new long[]{0x0000000000000000L,0x0000000000000010L}); + public static final BitSet FOLLOW_68_in_grammarDef432 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_id_in_grammarDef434 = new BitSet(new long[]{0x0000000000000000L,0x0000000000000020L}); + public static final BitSet FOLLOW_69_in_grammarDef436 = new BitSet(new long[]{0x0002461080000010L,0x0000000000003900L}); + public static final BitSet FOLLOW_optionsSpec_in_grammarDef438 = new BitSet(new long[]{0x0002461080000010L,0x0000000000003900L}); + public static final BitSet FOLLOW_tokensSpec_in_grammarDef441 = new BitSet(new long[]{0x0002461080000010L,0x0000000000003900L}); + public static final BitSet FOLLOW_attrScope_in_grammarDef444 = new BitSet(new long[]{0x0002461080000010L,0x0000000000003900L}); + public static final BitSet FOLLOW_action_in_grammarDef447 = new BitSet(new long[]{0x0002461080000010L,0x0000000000003900L}); + public static final BitSet FOLLOW_rule_in_grammarDef455 = new BitSet(new long[]{0x0002461080000010L,0x0000000000003900L}); + public static final BitSet FOLLOW_EOF_in_grammarDef463 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_TOKENS_in_tokensSpec524 = new BitSet(new long[]{0x0000040000000000L}); + public static final BitSet FOLLOW_tokenSpec_in_tokensSpec526 = new BitSet(new long[]{0x0000040000000000L,0x0000000000000040L}); + public static final BitSet FOLLOW_70_in_tokensSpec529 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_TOKEN_REF_in_tokenSpec549 = new BitSet(new long[]{0x0000000000000000L,0x00000000000000A0L}); + public static final BitSet FOLLOW_71_in_tokenSpec555 = new BitSet(new long[]{0x0000180000000000L}); + public static final BitSet FOLLOW_STRING_LITERAL_in_tokenSpec560 = new BitSet(new long[]{0x0000000000000000L,0x0000000000000020L}); + public static final BitSet FOLLOW_CHAR_LITERAL_in_tokenSpec564 = new BitSet(new long[]{0x0000000000000000L,0x0000000000000020L}); + public static final BitSet FOLLOW_69_in_tokenSpec603 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_SCOPE_in_attrScope614 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_id_in_attrScope616 = new BitSet(new long[]{0x0000200000000000L}); + public static final BitSet FOLLOW_ACTION_in_attrScope618 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_72_in_action641 = new BitSet(new long[]{0x0002040000000000L,0x0000000000000006L}); + public static final BitSet FOLLOW_actionScopeName_in_action644 = new BitSet(new long[]{0x0000000000000000L,0x0000000000000200L}); + public static final BitSet FOLLOW_73_in_action646 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_id_in_action650 = new BitSet(new long[]{0x0000200000000000L}); + public static final BitSet FOLLOW_ACTION_in_action652 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_id_in_actionScopeName678 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_65_in_actionScopeName685 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_66_in_actionScopeName702 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_OPTIONS_in_optionsSpec718 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_option_in_optionsSpec721 = new BitSet(new long[]{0x0000000000000000L,0x0000000000000020L}); + public static final BitSet FOLLOW_69_in_optionsSpec723 = new BitSet(new long[]{0x0002040000000000L,0x0000000000000040L}); + public static final BitSet FOLLOW_70_in_optionsSpec727 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_id_in_option752 = new BitSet(new long[]{0x0000000000000000L,0x0000000000000080L}); + public static final BitSet FOLLOW_71_in_option754 = new BitSet(new long[]{0x00029C0000000000L,0x0000000000000400L}); + public static final BitSet FOLLOW_optionValue_in_option756 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_id_in_optionValue785 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_STRING_LITERAL_in_optionValue795 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_CHAR_LITERAL_in_optionValue805 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_INT_in_optionValue815 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_74_in_optionValue825 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_DOC_COMMENT_in_rule854 = new BitSet(new long[]{0x0002041000000000L,0x0000000000003800L}); + public static final BitSet FOLLOW_75_in_rule864 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_76_in_rule866 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_77_in_rule868 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_FRAGMENT_in_rule870 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_id_in_rule878 = new BitSet(new long[]{0x0001408080000000L,0x000000000001C100L}); + public static final BitSet FOLLOW_BANG_in_rule884 = new BitSet(new long[]{0x0001400080000000L,0x000000000001C100L}); + public static final BitSet FOLLOW_ARG_ACTION_in_rule893 = new BitSet(new long[]{0x0000400080000000L,0x000000000001C100L}); + public static final BitSet FOLLOW_78_in_rule902 = new BitSet(new long[]{0x0001000000000000L}); + public static final BitSet FOLLOW_ARG_ACTION_in_rule906 = new BitSet(new long[]{0x0000400080000000L,0x0000000000018100L}); + public static final BitSet FOLLOW_throwsSpec_in_rule914 = new BitSet(new long[]{0x0000400080000000L,0x0000000000008100L}); + public static final BitSet FOLLOW_optionsSpec_in_rule917 = new BitSet(new long[]{0x0000000080000000L,0x0000000000008100L}); + public static final BitSet FOLLOW_ruleScopeSpec_in_rule920 = new BitSet(new long[]{0x0000000000000000L,0x0000000000008100L}); + public static final BitSet FOLLOW_ruleAction_in_rule923 = new BitSet(new long[]{0x0000000000000000L,0x0000000000008100L}); + public static final BitSet FOLLOW_79_in_rule928 = new BitSet(new long[]{0x00023D2100000000L,0x00000000120C0000L}); + public static final BitSet FOLLOW_altList_in_rule930 = new BitSet(new long[]{0x0000000000000000L,0x0000000000000020L}); + public static final BitSet FOLLOW_69_in_rule932 = new BitSet(new long[]{0x0000000000000002L,0x0000000000600000L}); + public static final BitSet FOLLOW_exceptionGroup_in_rule936 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_72_in_ruleAction1038 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_id_in_ruleAction1040 = new BitSet(new long[]{0x0000200000000000L}); + public static final BitSet FOLLOW_ACTION_in_ruleAction1042 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_80_in_throwsSpec1063 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_id_in_throwsSpec1065 = new BitSet(new long[]{0x0000000000000002L,0x0000000000020000L}); + public static final BitSet FOLLOW_81_in_throwsSpec1069 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_id_in_throwsSpec1071 = new BitSet(new long[]{0x0000000000000002L,0x0000000000020000L}); + public static final BitSet FOLLOW_SCOPE_in_ruleScopeSpec1094 = new BitSet(new long[]{0x0000200000000000L}); + public static final BitSet FOLLOW_ACTION_in_ruleScopeSpec1096 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_SCOPE_in_ruleScopeSpec1109 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_id_in_ruleScopeSpec1111 = new BitSet(new long[]{0x0000000000000000L,0x0000000000020020L}); + public static final BitSet FOLLOW_81_in_ruleScopeSpec1114 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_id_in_ruleScopeSpec1116 = new BitSet(new long[]{0x0000000000000000L,0x0000000000020020L}); + public static final BitSet FOLLOW_69_in_ruleScopeSpec1120 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_SCOPE_in_ruleScopeSpec1134 = new BitSet(new long[]{0x0000200000000000L}); + public static final BitSet FOLLOW_ACTION_in_ruleScopeSpec1136 = new BitSet(new long[]{0x0000000080000000L}); + public static final BitSet FOLLOW_SCOPE_in_ruleScopeSpec1140 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_id_in_ruleScopeSpec1142 = new BitSet(new long[]{0x0000000000000000L,0x0000000000020020L}); + public static final BitSet FOLLOW_81_in_ruleScopeSpec1145 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_id_in_ruleScopeSpec1147 = new BitSet(new long[]{0x0000000000000000L,0x0000000000020020L}); + public static final BitSet FOLLOW_69_in_ruleScopeSpec1151 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_82_in_block1183 = new BitSet(new long[]{0x00027D2100000000L,0x00000000121C8000L}); + public static final BitSet FOLLOW_optionsSpec_in_block1192 = new BitSet(new long[]{0x0000000000000000L,0x0000000000008000L}); + public static final BitSet FOLLOW_79_in_block1196 = new BitSet(new long[]{0x00023D2100000000L,0x00000000121C0000L}); + public static final BitSet FOLLOW_alternative_in_block1205 = new BitSet(new long[]{0x0000010000000000L,0x0000000000180000L}); + public static final BitSet FOLLOW_rewrite_in_block1207 = new BitSet(new long[]{0x0000000000000000L,0x0000000000180000L}); + public static final BitSet FOLLOW_83_in_block1211 = new BitSet(new long[]{0x00023D2100000000L,0x00000000121C0000L}); + public static final BitSet FOLLOW_alternative_in_block1215 = new BitSet(new long[]{0x0000010000000000L,0x0000000000180000L}); + public static final BitSet FOLLOW_rewrite_in_block1217 = new BitSet(new long[]{0x0000000000000000L,0x0000000000180000L}); + public static final BitSet FOLLOW_84_in_block1232 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_alternative_in_altList1289 = new BitSet(new long[]{0x0000010000000000L,0x0000000000080000L}); + public static final BitSet FOLLOW_rewrite_in_altList1291 = new BitSet(new long[]{0x0000000000000002L,0x0000000000080000L}); + public static final BitSet FOLLOW_83_in_altList1295 = new BitSet(new long[]{0x00023D2100000000L,0x00000000120C0000L}); + public static final BitSet FOLLOW_alternative_in_altList1299 = new BitSet(new long[]{0x0000010000000000L,0x0000000000080000L}); + public static final BitSet FOLLOW_rewrite_in_altList1301 = new BitSet(new long[]{0x0000000000000002L,0x0000000000080000L}); + public static final BitSet FOLLOW_element_in_alternative1349 = new BitSet(new long[]{0x00023C2100000002L,0x0000000012040000L}); + public static final BitSet FOLLOW_exceptionHandler_in_exceptionGroup1400 = new BitSet(new long[]{0x0000000000000002L,0x0000000000600000L}); + public static final BitSet FOLLOW_finallyClause_in_exceptionGroup1407 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_finallyClause_in_exceptionGroup1415 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_85_in_exceptionHandler1435 = new BitSet(new long[]{0x0001000000000000L}); + public static final BitSet FOLLOW_ARG_ACTION_in_exceptionHandler1437 = new BitSet(new long[]{0x0000200000000000L}); + public static final BitSet FOLLOW_ACTION_in_exceptionHandler1439 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_86_in_finallyClause1469 = new BitSet(new long[]{0x0000200000000000L}); + public static final BitSet FOLLOW_ACTION_in_finallyClause1471 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_elementNoOptionSpec_in_element1493 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_id_in_elementNoOptionSpec1504 = new BitSet(new long[]{0x0000000000000000L,0x0000000000800080L}); + public static final BitSet FOLLOW_71_in_elementNoOptionSpec1509 = new BitSet(new long[]{0x00021C0000000000L,0x0000000012000000L}); + public static final BitSet FOLLOW_87_in_elementNoOptionSpec1513 = new BitSet(new long[]{0x00021C0000000000L,0x0000000012000000L}); + public static final BitSet FOLLOW_atom_in_elementNoOptionSpec1516 = new BitSet(new long[]{0x0000000000000002L,0x000000000C000400L}); + public static final BitSet FOLLOW_ebnfSuffix_in_elementNoOptionSpec1522 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_id_in_elementNoOptionSpec1581 = new BitSet(new long[]{0x0000000000000000L,0x0000000000800080L}); + public static final BitSet FOLLOW_71_in_elementNoOptionSpec1586 = new BitSet(new long[]{0x0000000000000000L,0x0000000000040000L}); + public static final BitSet FOLLOW_87_in_elementNoOptionSpec1590 = new BitSet(new long[]{0x0000000000000000L,0x0000000000040000L}); + public static final BitSet FOLLOW_block_in_elementNoOptionSpec1593 = new BitSet(new long[]{0x0000000000000002L,0x000000000C000400L}); + public static final BitSet FOLLOW_ebnfSuffix_in_elementNoOptionSpec1599 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_atom_in_elementNoOptionSpec1658 = new BitSet(new long[]{0x0000000000000002L,0x000000000C000400L}); + public static final BitSet FOLLOW_ebnfSuffix_in_elementNoOptionSpec1664 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_ebnf_in_elementNoOptionSpec1710 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_ACTION_in_elementNoOptionSpec1717 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_SEMPRED_in_elementNoOptionSpec1724 = new BitSet(new long[]{0x0000000000000002L,0x0000000001000000L}); + public static final BitSet FOLLOW_88_in_elementNoOptionSpec1728 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_treeSpec_in_elementNoOptionSpec1747 = new BitSet(new long[]{0x0000000000000002L,0x000000000C000400L}); + public static final BitSet FOLLOW_ebnfSuffix_in_elementNoOptionSpec1753 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_range_in_atom1805 = new BitSet(new long[]{0x000000C000000002L}); + public static final BitSet FOLLOW_ROOT_in_atom1812 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_BANG_in_atom1816 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_terminal_in_atom1844 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_notSet_in_atom1852 = new BitSet(new long[]{0x000000C000000002L}); + public static final BitSet FOLLOW_ROOT_in_atom1859 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_BANG_in_atom1863 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_RULE_REF_in_atom1891 = new BitSet(new long[]{0x000100C000000002L}); + public static final BitSet FOLLOW_ARG_ACTION_in_atom1897 = new BitSet(new long[]{0x000000C000000002L}); + public static final BitSet FOLLOW_ROOT_in_atom1907 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_BANG_in_atom1911 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_89_in_notSet1994 = new BitSet(new long[]{0x00001C0000000000L,0x0000000000040000L}); + public static final BitSet FOLLOW_notTerminal_in_notSet2000 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_block_in_notSet2014 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_TREE_BEGIN_in_treeSpec2038 = new BitSet(new long[]{0x00023C2100000000L,0x0000000012040000L}); + public static final BitSet FOLLOW_element_in_treeSpec2040 = new BitSet(new long[]{0x00023C2100000000L,0x0000000012040000L}); + public static final BitSet FOLLOW_element_in_treeSpec2044 = new BitSet(new long[]{0x00023C2100000000L,0x0000000012140000L}); + public static final BitSet FOLLOW_84_in_treeSpec2049 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_block_in_ebnf2081 = new BitSet(new long[]{0x0000000000000002L,0x000000000D000400L}); + public static final BitSet FOLLOW_90_in_ebnf2089 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_74_in_ebnf2106 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_91_in_ebnf2123 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_88_in_ebnf2140 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_CHAR_LITERAL_in_range2223 = new BitSet(new long[]{0x0000000000002000L}); + public static final BitSet FOLLOW_RANGE_in_range2225 = new BitSet(new long[]{0x0000100000000000L}); + public static final BitSet FOLLOW_CHAR_LITERAL_in_range2229 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_CHAR_LITERAL_in_terminal2260 = new BitSet(new long[]{0x000000C000000002L}); + public static final BitSet FOLLOW_TOKEN_REF_in_terminal2282 = new BitSet(new long[]{0x000100C000000002L}); + public static final BitSet FOLLOW_ARG_ACTION_in_terminal2289 = new BitSet(new long[]{0x000000C000000002L}); + public static final BitSet FOLLOW_STRING_LITERAL_in_terminal2328 = new BitSet(new long[]{0x000000C000000002L}); + public static final BitSet FOLLOW_92_in_terminal2343 = new BitSet(new long[]{0x000000C000000002L}); + public static final BitSet FOLLOW_ROOT_in_terminal2364 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_BANG_in_terminal2385 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_set_in_notTerminal0 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_90_in_ebnfSuffix2445 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_74_in_ebnfSuffix2457 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_91_in_ebnfSuffix2470 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_REWRITE_in_rewrite2499 = new BitSet(new long[]{0x0000000100000000L}); + public static final BitSet FOLLOW_SEMPRED_in_rewrite2503 = new BitSet(new long[]{0x00023D2000000000L,0x0000000020040000L}); + public static final BitSet FOLLOW_rewrite_alternative_in_rewrite2507 = new BitSet(new long[]{0x0000010000000000L}); + public static final BitSet FOLLOW_REWRITE_in_rewrite2515 = new BitSet(new long[]{0x00023C2000000000L,0x0000000020040000L}); + public static final BitSet FOLLOW_rewrite_alternative_in_rewrite2519 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_rewrite_template_in_rewrite_alternative2570 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_rewrite_tree_alternative_in_rewrite_alternative2575 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_82_in_rewrite_tree_block2617 = new BitSet(new long[]{0x00023C2000000000L,0x0000000020040000L}); + public static final BitSet FOLLOW_rewrite_tree_alternative_in_rewrite_tree_block2619 = new BitSet(new long[]{0x0000000000000000L,0x0000000000100000L}); + public static final BitSet FOLLOW_84_in_rewrite_tree_block2621 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_rewrite_tree_element_in_rewrite_tree_alternative2655 = new BitSet(new long[]{0x00023C2000000002L,0x0000000020040000L}); + public static final BitSet FOLLOW_rewrite_tree_atom_in_rewrite_tree_element2683 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_rewrite_tree_atom_in_rewrite_tree_element2688 = new BitSet(new long[]{0x0000000000000000L,0x000000000C000400L}); + public static final BitSet FOLLOW_ebnfSuffix_in_rewrite_tree_element2690 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_rewrite_tree_in_rewrite_tree_element2724 = new BitSet(new long[]{0x0000000000000002L,0x000000000C000400L}); + public static final BitSet FOLLOW_ebnfSuffix_in_rewrite_tree_element2730 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_rewrite_tree_ebnf_in_rewrite_tree_element2776 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_CHAR_LITERAL_in_rewrite_tree_atom2792 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_TOKEN_REF_in_rewrite_tree_atom2799 = new BitSet(new long[]{0x0001000000000002L}); + public static final BitSet FOLLOW_ARG_ACTION_in_rewrite_tree_atom2801 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_RULE_REF_in_rewrite_tree_atom2822 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_STRING_LITERAL_in_rewrite_tree_atom2829 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_93_in_rewrite_tree_atom2838 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_id_in_rewrite_tree_atom2840 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_ACTION_in_rewrite_tree_atom2851 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_rewrite_tree_block_in_rewrite_tree_ebnf2872 = new BitSet(new long[]{0x0000000000000000L,0x000000000C000400L}); + public static final BitSet FOLLOW_ebnfSuffix_in_rewrite_tree_ebnf2874 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_TREE_BEGIN_in_rewrite_tree2894 = new BitSet(new long[]{0x00023C0000000000L,0x0000000020000000L}); + public static final BitSet FOLLOW_rewrite_tree_atom_in_rewrite_tree2896 = new BitSet(new long[]{0x00023C2000000000L,0x0000000020140000L}); + public static final BitSet FOLLOW_rewrite_tree_element_in_rewrite_tree2898 = new BitSet(new long[]{0x00023C2000000000L,0x0000000020140000L}); + public static final BitSet FOLLOW_84_in_rewrite_tree2901 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_id_in_rewrite_template2933 = new BitSet(new long[]{0x0000000000000000L,0x0000000000040000L}); + public static final BitSet FOLLOW_82_in_rewrite_template2937 = new BitSet(new long[]{0x0002040000000000L,0x0000000000100000L}); + public static final BitSet FOLLOW_rewrite_template_args_in_rewrite_template2939 = new BitSet(new long[]{0x0000000000000000L,0x0000000000100000L}); + public static final BitSet FOLLOW_84_in_rewrite_template2941 = new BitSet(new long[]{0x000C000000000000L}); + public static final BitSet FOLLOW_DOUBLE_QUOTE_STRING_LITERAL_in_rewrite_template2949 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_DOUBLE_ANGLE_STRING_LITERAL_in_rewrite_template2955 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_rewrite_template_ref_in_rewrite_template2982 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_rewrite_indirect_template_head_in_rewrite_template2991 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_ACTION_in_rewrite_template3000 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_id_in_rewrite_template_ref3013 = new BitSet(new long[]{0x0000000000000000L,0x0000000000040000L}); + public static final BitSet FOLLOW_82_in_rewrite_template_ref3017 = new BitSet(new long[]{0x0002040000000000L,0x0000000000100000L}); + public static final BitSet FOLLOW_rewrite_template_args_in_rewrite_template_ref3019 = new BitSet(new long[]{0x0000000000000000L,0x0000000000100000L}); + public static final BitSet FOLLOW_84_in_rewrite_template_ref3021 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_82_in_rewrite_indirect_template_head3049 = new BitSet(new long[]{0x0000200000000000L}); + public static final BitSet FOLLOW_ACTION_in_rewrite_indirect_template_head3051 = new BitSet(new long[]{0x0000000000000000L,0x0000000000100000L}); + public static final BitSet FOLLOW_84_in_rewrite_indirect_template_head3053 = new BitSet(new long[]{0x0000000000000000L,0x0000000000040000L}); + public static final BitSet FOLLOW_82_in_rewrite_indirect_template_head3055 = new BitSet(new long[]{0x0002040000000000L,0x0000000000100000L}); + public static final BitSet FOLLOW_rewrite_template_args_in_rewrite_indirect_template_head3057 = new BitSet(new long[]{0x0000000000000000L,0x0000000000100000L}); + public static final BitSet FOLLOW_84_in_rewrite_indirect_template_head3059 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_rewrite_template_arg_in_rewrite_template_args3083 = new BitSet(new long[]{0x0000000000000002L,0x0000000000020000L}); + public static final BitSet FOLLOW_81_in_rewrite_template_args3086 = new BitSet(new long[]{0x0002040000000000L}); + public static final BitSet FOLLOW_rewrite_template_arg_in_rewrite_template_args3088 = new BitSet(new long[]{0x0000000000000002L,0x0000000000020000L}); + public static final BitSet FOLLOW_id_in_rewrite_template_arg3121 = new BitSet(new long[]{0x0000000000000000L,0x0000000000000080L}); + public static final BitSet FOLLOW_71_in_rewrite_template_arg3123 = new BitSet(new long[]{0x0000200000000000L}); + public static final BitSet FOLLOW_ACTION_in_rewrite_template_arg3125 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_TOKEN_REF_in_id3146 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_RULE_REF_in_id3156 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_rewrite_template_in_synpred1_ANTLRv32570 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_rewrite_tree_alternative_in_synpred2_ANTLRv32575 = new BitSet(new long[]{0x0000000000000002L}); + +} \ No newline at end of file diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/StGUnit.g b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/StGUnit.g new file mode 100644 index 0000000..44458bb --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/StGUnit.g @@ -0,0 +1,212 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Leon Jen-Yuan Su +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. +*/ + +grammar StGUnit; + +options {language=Java;} + +tokens { + OK = 'OK'; + FAIL = 'FAIL'; + DOC_COMMENT; +} + +@header { +package org.antlr.gunit.swingui.parsers; +import org.antlr.gunit.swingui.model.*; +} + +@lexer::header {package org.antlr.gunit.swingui;} + +@members { +public Translator.TestSuiteAdapter adapter ;; +} + +gUnitDef + : 'gunit' name=id {adapter.setGrammarName($name.text);} + ('walks' id)? ';' + header? suite* + ; + +header + : '@header' ACTION + ; + +suite + : ( parserRule=RULE_REF ('walks' RULE_REF)? + {adapter.startRule($parserRule.text);} + | lexerRule=TOKEN_REF + {adapter.startRule($lexerRule.text);} + ) + ':' + test+ + {adapter.endRule();} + ; + +test + : input expect + {adapter.addTestCase($input.in, $expect.out);} + ; + +expect returns [ITestCaseOutput out] + : OK {$out = adapter.createBoolOutput(true);} + | FAIL {$out = adapter.createBoolOutput(false);} + | 'returns' RETVAL {$out = adapter.createReturnOutput($RETVAL.text);} + | '->' output {$out = adapter.createStdOutput($output.text);} + | '->' AST {$out = adapter.createAstOutput($AST.text);} + ; + +input returns [ITestCaseInput in] + : STRING {$in = adapter.createStringInput($STRING.text);} + | ML_STRING {$in = adapter.createMultiInput($ML_STRING.text);} + | fileInput {$in = adapter.createFileInput($fileInput.path);} + ; + +output + : STRING + | ML_STRING + | ACTION + ; + +fileInput returns [String path] + : id {$path = $id.text;} (EXT {$path += $EXT.text;})? + ; + +id : TOKEN_REF + | RULE_REF + ; + +// L E X I C A L R U L E S + +SL_COMMENT + : '//' ~('\r'|'\n')* '\r'? '\n' {$channel=HIDDEN;} + ; + +ML_COMMENT + : '/*' {$channel=HIDDEN;} .* '*/' + ; + +STRING + : '"' ( ESC | ~('\\'|'"') )* '"' + ; + +ML_STRING + : '<<' .* '>>' + ; + +TOKEN_REF + : 'A'..'Z' ('a'..'z'|'A'..'Z'|'_'|'0'..'9')* + ; + +RULE_REF + : 'a'..'z' ('a'..'z'|'A'..'Z'|'_'|'0'..'9')* + ; + +EXT : '.'('a'..'z'|'A'..'Z'|'0'..'9')+; + +RETVAL : NESTED_RETVAL + ; + +fragment +NESTED_RETVAL : + '[' + ( options {greedy=false;} + : NESTED_RETVAL + | . + )* + ']' + ; + +AST : NESTED_AST (' '? NESTED_AST)*; + +fragment +NESTED_AST : + '(' + ( options {greedy=false;} + : NESTED_AST + | . + )* + ')' + ; + +ACTION + : NESTED_ACTION + ; + +fragment +NESTED_ACTION : + '{' + ( options {greedy=false; k=3;} + : NESTED_ACTION + | STRING_LITERAL + | CHAR_LITERAL + | . + )* + '}' + ; + +fragment +CHAR_LITERAL + : '\'' ( ESC | ~('\''|'\\') ) '\'' + ; + +fragment +STRING_LITERAL + : '"' ( ESC | ~('\\'|'"') )* '"' + ; + +fragment +ESC : '\\' + ( 'n' + | 'r' + | 't' + | 'b' + | 'f' + | '"' + | '\'' + | '\\' + | '>' + | 'u' XDIGIT XDIGIT XDIGIT XDIGIT + | . // unknown, leave as it is + ) + ; + +fragment +XDIGIT : + '0' .. '9' + | 'a' .. 'f' + | 'A' .. 'F' + ; + +WS : ( ' ' + | '\t' + | '\r'? '\n' + )+ + {$channel=HIDDEN;} + ; diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/StGUnit.tokens b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/StGUnit.tokens new file mode 100644 index 0000000..38297d0 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/StGUnit.tokens @@ -0,0 +1,37 @@ +RETVAL=10 +NESTED_ACTION=20 +AST=11 +T__28=28 +EXT=14 +FAIL=5 +WS=24 +STRING=12 +OK=4 +ACTION=7 +TOKEN_REF=9 +ESC=17 +XDIGIT=23 +RULE_REF=8 +T__29=29 +NESTED_AST=19 +T__30=30 +CHAR_LITERAL=22 +T__31=31 +STRING_LITERAL=21 +T__27=27 +T__26=26 +ML_STRING=13 +T__25=25 +ML_COMMENT=16 +SL_COMMENT=15 +DOC_COMMENT=6 +NESTED_RETVAL=18 +'->'=31 +'gunit'=25 +'OK'=4 +'returns'=30 +'FAIL'=5 +'walks'=26 +':'=29 +'@header'=28 +';'=27 diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/StGUnitLexer.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/StGUnitLexer.java new file mode 100644 index 0000000..9a331b6 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/parsers/StGUnitLexer.java @@ -0,0 +1,1984 @@ +// $ANTLR 3.1.1 /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g 2009-02-15 15:43:43 +package org.antlr.gunit.swingui.parsers; + +import org.antlr.runtime.*; +import java.util.Stack; +import java.util.List; +import java.util.ArrayList; + +public class StGUnitLexer extends Lexer { + public static final int RETVAL=10; + public static final int AST=11; + public static final int NESTED_ACTION=20; + public static final int T__28=28; + public static final int EXT=14; + public static final int FAIL=5; + public static final int WS=24; + public static final int STRING=12; + public static final int OK=4; + public static final int ACTION=7; + public static final int TOKEN_REF=9; + public static final int ESC=17; + public static final int XDIGIT=23; + public static final int RULE_REF=8; + public static final int T__29=29; + public static final int NESTED_AST=19; + public static final int T__30=30; + public static final int CHAR_LITERAL=22; + public static final int T__31=31; + public static final int EOF=-1; + public static final int T__27=27; + public static final int STRING_LITERAL=21; + public static final int T__26=26; + public static final int T__25=25; + public static final int ML_STRING=13; + public static final int ML_COMMENT=16; + public static final int SL_COMMENT=15; + public static final int DOC_COMMENT=6; + public static final int NESTED_RETVAL=18; + + // delegates + // delegators + + public StGUnitLexer() {;} + public StGUnitLexer(CharStream input) { + this(input, new RecognizerSharedState()); + } + public StGUnitLexer(CharStream input, RecognizerSharedState state) { + super(input,state); + + } + public String getGrammarFileName() { return "/Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g"; } + + // $ANTLR start "OK" + public final void mOK() throws RecognitionException { + try { + int _type = OK; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:9:4: ( 'OK' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:9:6: 'OK' + { + match("OK"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "OK" + + // $ANTLR start "FAIL" + public final void mFAIL() throws RecognitionException { + try { + int _type = FAIL; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:10:6: ( 'FAIL' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:10:8: 'FAIL' + { + match("FAIL"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "FAIL" + + // $ANTLR start "T__25" + public final void mT__25() throws RecognitionException { + try { + int _type = T__25; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:11:7: ( 'gunit' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:11:9: 'gunit' + { + match("gunit"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__25" + + // $ANTLR start "T__26" + public final void mT__26() throws RecognitionException { + try { + int _type = T__26; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:12:7: ( 'walks' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:12:9: 'walks' + { + match("walks"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__26" + + // $ANTLR start "T__27" + public final void mT__27() throws RecognitionException { + try { + int _type = T__27; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:13:7: ( ';' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:13:9: ';' + { + match(';'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__27" + + // $ANTLR start "T__28" + public final void mT__28() throws RecognitionException { + try { + int _type = T__28; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:14:7: ( '@header' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:14:9: '@header' + { + match("@header"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__28" + + // $ANTLR start "T__29" + public final void mT__29() throws RecognitionException { + try { + int _type = T__29; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:15:7: ( ':' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:15:9: ':' + { + match(':'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__29" + + // $ANTLR start "T__30" + public final void mT__30() throws RecognitionException { + try { + int _type = T__30; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:16:7: ( 'returns' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:16:9: 'returns' + { + match("returns"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__30" + + // $ANTLR start "T__31" + public final void mT__31() throws RecognitionException { + try { + int _type = T__31; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:17:7: ( '->' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:17:9: '->' + { + match("->"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "T__31" + + // $ANTLR start "SL_COMMENT" + public final void mSL_COMMENT() throws RecognitionException { + try { + int _type = SL_COMMENT; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:108:3: ( '//' (~ ( '\\r' | '\\n' ) )* ( '\\r' )? '\\n' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:108:5: '//' (~ ( '\\r' | '\\n' ) )* ( '\\r' )? '\\n' + { + match("//"); + + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:108:10: (~ ( '\\r' | '\\n' ) )* + loop1: + do { + int alt1=2; + int LA1_0 = input.LA(1); + + if ( ((LA1_0>='\u0000' && LA1_0<='\t')||(LA1_0>='\u000B' && LA1_0<='\f')||(LA1_0>='\u000E' && LA1_0<='\uFFFF')) ) { + alt1=1; + } + + + switch (alt1) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:108:10: ~ ( '\\r' | '\\n' ) + { + if ( (input.LA(1)>='\u0000' && input.LA(1)<='\t')||(input.LA(1)>='\u000B' && input.LA(1)<='\f')||(input.LA(1)>='\u000E' && input.LA(1)<='\uFFFF') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + break; + + default : + break loop1; + } + } while (true); + + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:108:24: ( '\\r' )? + int alt2=2; + int LA2_0 = input.LA(1); + + if ( (LA2_0=='\r') ) { + alt2=1; + } + switch (alt2) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:108:24: '\\r' + { + match('\r'); + + } + break; + + } + + match('\n'); + _channel=HIDDEN; + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "SL_COMMENT" + + // $ANTLR start "ML_COMMENT" + public final void mML_COMMENT() throws RecognitionException { + try { + int _type = ML_COMMENT; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:112:2: ( '/*' ( . )* '*/' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:112:4: '/*' ( . )* '*/' + { + match("/*"); + + _channel=HIDDEN; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:112:28: ( . )* + loop3: + do { + int alt3=2; + int LA3_0 = input.LA(1); + + if ( (LA3_0=='*') ) { + int LA3_1 = input.LA(2); + + if ( (LA3_1=='/') ) { + alt3=2; + } + else if ( ((LA3_1>='\u0000' && LA3_1<='.')||(LA3_1>='0' && LA3_1<='\uFFFF')) ) { + alt3=1; + } + + + } + else if ( ((LA3_0>='\u0000' && LA3_0<=')')||(LA3_0>='+' && LA3_0<='\uFFFF')) ) { + alt3=1; + } + + + switch (alt3) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:112:28: . + { + matchAny(); + + } + break; + + default : + break loop3; + } + } while (true); + + match("*/"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "ML_COMMENT" + + // $ANTLR start "STRING" + public final void mSTRING() throws RecognitionException { + try { + int _type = STRING; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:116:2: ( '\"' ( ESC | ~ ( '\\\\' | '\"' ) )* '\"' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:116:4: '\"' ( ESC | ~ ( '\\\\' | '\"' ) )* '\"' + { + match('\"'); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:116:8: ( ESC | ~ ( '\\\\' | '\"' ) )* + loop4: + do { + int alt4=3; + int LA4_0 = input.LA(1); + + if ( (LA4_0=='\\') ) { + alt4=1; + } + else if ( ((LA4_0>='\u0000' && LA4_0<='!')||(LA4_0>='#' && LA4_0<='[')||(LA4_0>=']' && LA4_0<='\uFFFF')) ) { + alt4=2; + } + + + switch (alt4) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:116:10: ESC + { + mESC(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:116:16: ~ ( '\\\\' | '\"' ) + { + if ( (input.LA(1)>='\u0000' && input.LA(1)<='!')||(input.LA(1)>='#' && input.LA(1)<='[')||(input.LA(1)>=']' && input.LA(1)<='\uFFFF') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + break; + + default : + break loop4; + } + } while (true); + + match('\"'); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "STRING" + + // $ANTLR start "ML_STRING" + public final void mML_STRING() throws RecognitionException { + try { + int _type = ML_STRING; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:120:2: ( '<<' ( . )* '>>' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:120:4: '<<' ( . )* '>>' + { + match("<<"); + + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:120:9: ( . )* + loop5: + do { + int alt5=2; + int LA5_0 = input.LA(1); + + if ( (LA5_0=='>') ) { + int LA5_1 = input.LA(2); + + if ( (LA5_1=='>') ) { + alt5=2; + } + else if ( ((LA5_1>='\u0000' && LA5_1<='=')||(LA5_1>='?' && LA5_1<='\uFFFF')) ) { + alt5=1; + } + + + } + else if ( ((LA5_0>='\u0000' && LA5_0<='=')||(LA5_0>='?' && LA5_0<='\uFFFF')) ) { + alt5=1; + } + + + switch (alt5) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:120:9: . + { + matchAny(); + + } + break; + + default : + break loop5; + } + } while (true); + + match(">>"); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "ML_STRING" + + // $ANTLR start "TOKEN_REF" + public final void mTOKEN_REF() throws RecognitionException { + try { + int _type = TOKEN_REF; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:124:2: ( 'A' .. 'Z' ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )* ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:124:4: 'A' .. 'Z' ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )* + { + matchRange('A','Z'); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:124:13: ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )* + loop6: + do { + int alt6=2; + int LA6_0 = input.LA(1); + + if ( ((LA6_0>='0' && LA6_0<='9')||(LA6_0>='A' && LA6_0<='Z')||LA6_0=='_'||(LA6_0>='a' && LA6_0<='z')) ) { + alt6=1; + } + + + switch (alt6) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g: + { + if ( (input.LA(1)>='0' && input.LA(1)<='9')||(input.LA(1)>='A' && input.LA(1)<='Z')||input.LA(1)=='_'||(input.LA(1)>='a' && input.LA(1)<='z') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + break; + + default : + break loop6; + } + } while (true); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "TOKEN_REF" + + // $ANTLR start "RULE_REF" + public final void mRULE_REF() throws RecognitionException { + try { + int _type = RULE_REF; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:128:2: ( 'a' .. 'z' ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )* ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:128:4: 'a' .. 'z' ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )* + { + matchRange('a','z'); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:128:13: ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )* + loop7: + do { + int alt7=2; + int LA7_0 = input.LA(1); + + if ( ((LA7_0>='0' && LA7_0<='9')||(LA7_0>='A' && LA7_0<='Z')||LA7_0=='_'||(LA7_0>='a' && LA7_0<='z')) ) { + alt7=1; + } + + + switch (alt7) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g: + { + if ( (input.LA(1)>='0' && input.LA(1)<='9')||(input.LA(1)>='A' && input.LA(1)<='Z')||input.LA(1)=='_'||(input.LA(1)>='a' && input.LA(1)<='z') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + break; + + default : + break loop7; + } + } while (true); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "RULE_REF" + + // $ANTLR start "EXT" + public final void mEXT() throws RecognitionException { + try { + int _type = EXT; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:131:5: ( '.' ( 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' )+ ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:131:7: '.' ( 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' )+ + { + match('.'); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:131:10: ( 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' )+ + int cnt8=0; + loop8: + do { + int alt8=2; + int LA8_0 = input.LA(1); + + if ( ((LA8_0>='0' && LA8_0<='9')||(LA8_0>='A' && LA8_0<='Z')||(LA8_0>='a' && LA8_0<='z')) ) { + alt8=1; + } + + + switch (alt8) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g: + { + if ( (input.LA(1)>='0' && input.LA(1)<='9')||(input.LA(1)>='A' && input.LA(1)<='Z')||(input.LA(1)>='a' && input.LA(1)<='z') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + break; + + default : + if ( cnt8 >= 1 ) break loop8; + EarlyExitException eee = + new EarlyExitException(8, input); + throw eee; + } + cnt8++; + } while (true); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "EXT" + + // $ANTLR start "RETVAL" + public final void mRETVAL() throws RecognitionException { + try { + int _type = RETVAL; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:133:8: ( NESTED_RETVAL ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:133:10: NESTED_RETVAL + { + mNESTED_RETVAL(); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "RETVAL" + + // $ANTLR start "NESTED_RETVAL" + public final void mNESTED_RETVAL() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:137:15: ( '[' ( options {greedy=false; } : NESTED_RETVAL | . )* ']' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:138:2: '[' ( options {greedy=false; } : NESTED_RETVAL | . )* ']' + { + match('['); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:139:2: ( options {greedy=false; } : NESTED_RETVAL | . )* + loop9: + do { + int alt9=3; + int LA9_0 = input.LA(1); + + if ( (LA9_0==']') ) { + alt9=3; + } + else if ( (LA9_0=='[') ) { + alt9=1; + } + else if ( ((LA9_0>='\u0000' && LA9_0<='Z')||LA9_0=='\\'||(LA9_0>='^' && LA9_0<='\uFFFF')) ) { + alt9=2; + } + + + switch (alt9) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:140:4: NESTED_RETVAL + { + mNESTED_RETVAL(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:141:4: . + { + matchAny(); + + } + break; + + default : + break loop9; + } + } while (true); + + match(']'); + + } + + } + finally { + } + } + // $ANTLR end "NESTED_RETVAL" + + // $ANTLR start "AST" + public final void mAST() throws RecognitionException { + try { + int _type = AST; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:146:5: ( NESTED_AST ( ( ' ' )? NESTED_AST )* ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:146:7: NESTED_AST ( ( ' ' )? NESTED_AST )* + { + mNESTED_AST(); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:146:18: ( ( ' ' )? NESTED_AST )* + loop11: + do { + int alt11=2; + int LA11_0 = input.LA(1); + + if ( (LA11_0==' '||LA11_0=='(') ) { + alt11=1; + } + + + switch (alt11) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:146:19: ( ' ' )? NESTED_AST + { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:146:19: ( ' ' )? + int alt10=2; + int LA10_0 = input.LA(1); + + if ( (LA10_0==' ') ) { + alt10=1; + } + switch (alt10) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:146:19: ' ' + { + match(' '); + + } + break; + + } + + mNESTED_AST(); + + } + break; + + default : + break loop11; + } + } while (true); + + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "AST" + + // $ANTLR start "NESTED_AST" + public final void mNESTED_AST() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:149:12: ( '(' ( options {greedy=false; } : NESTED_AST | . )* ')' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:150:2: '(' ( options {greedy=false; } : NESTED_AST | . )* ')' + { + match('('); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:151:2: ( options {greedy=false; } : NESTED_AST | . )* + loop12: + do { + int alt12=3; + int LA12_0 = input.LA(1); + + if ( (LA12_0==')') ) { + alt12=3; + } + else if ( (LA12_0=='(') ) { + alt12=1; + } + else if ( ((LA12_0>='\u0000' && LA12_0<='\'')||(LA12_0>='*' && LA12_0<='\uFFFF')) ) { + alt12=2; + } + + + switch (alt12) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:152:4: NESTED_AST + { + mNESTED_AST(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:153:4: . + { + matchAny(); + + } + break; + + default : + break loop12; + } + } while (true); + + match(')'); + + } + + } + finally { + } + } + // $ANTLR end "NESTED_AST" + + // $ANTLR start "ACTION" + public final void mACTION() throws RecognitionException { + try { + int _type = ACTION; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:159:2: ( NESTED_ACTION ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:159:4: NESTED_ACTION + { + mNESTED_ACTION(); + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "ACTION" + + // $ANTLR start "NESTED_ACTION" + public final void mNESTED_ACTION() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:163:15: ( '{' ( options {greedy=false; k=3; } : NESTED_ACTION | STRING_LITERAL | CHAR_LITERAL | . )* '}' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:164:2: '{' ( options {greedy=false; k=3; } : NESTED_ACTION | STRING_LITERAL | CHAR_LITERAL | . )* '}' + { + match('{'); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:165:2: ( options {greedy=false; k=3; } : NESTED_ACTION | STRING_LITERAL | CHAR_LITERAL | . )* + loop13: + do { + int alt13=5; + alt13 = dfa13.predict(input); + switch (alt13) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:166:4: NESTED_ACTION + { + mNESTED_ACTION(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:167:4: STRING_LITERAL + { + mSTRING_LITERAL(); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:168:4: CHAR_LITERAL + { + mCHAR_LITERAL(); + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:169:4: . + { + matchAny(); + + } + break; + + default : + break loop13; + } + } while (true); + + match('}'); + + } + + } + finally { + } + } + // $ANTLR end "NESTED_ACTION" + + // $ANTLR start "CHAR_LITERAL" + public final void mCHAR_LITERAL() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:176:2: ( '\\'' ( ESC | ~ ( '\\'' | '\\\\' ) ) '\\'' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:176:4: '\\'' ( ESC | ~ ( '\\'' | '\\\\' ) ) '\\'' + { + match('\''); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:176:9: ( ESC | ~ ( '\\'' | '\\\\' ) ) + int alt14=2; + int LA14_0 = input.LA(1); + + if ( (LA14_0=='\\') ) { + alt14=1; + } + else if ( ((LA14_0>='\u0000' && LA14_0<='&')||(LA14_0>='(' && LA14_0<='[')||(LA14_0>=']' && LA14_0<='\uFFFF')) ) { + alt14=2; + } + else { + NoViableAltException nvae = + new NoViableAltException("", 14, 0, input); + + throw nvae; + } + switch (alt14) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:176:11: ESC + { + mESC(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:176:17: ~ ( '\\'' | '\\\\' ) + { + if ( (input.LA(1)>='\u0000' && input.LA(1)<='&')||(input.LA(1)>='(' && input.LA(1)<='[')||(input.LA(1)>=']' && input.LA(1)<='\uFFFF') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + break; + + } + + match('\''); + + } + + } + finally { + } + } + // $ANTLR end "CHAR_LITERAL" + + // $ANTLR start "STRING_LITERAL" + public final void mSTRING_LITERAL() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:181:2: ( '\"' ( ESC | ~ ( '\\\\' | '\"' ) )* '\"' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:181:4: '\"' ( ESC | ~ ( '\\\\' | '\"' ) )* '\"' + { + match('\"'); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:181:8: ( ESC | ~ ( '\\\\' | '\"' ) )* + loop15: + do { + int alt15=3; + int LA15_0 = input.LA(1); + + if ( (LA15_0=='\\') ) { + alt15=1; + } + else if ( ((LA15_0>='\u0000' && LA15_0<='!')||(LA15_0>='#' && LA15_0<='[')||(LA15_0>=']' && LA15_0<='\uFFFF')) ) { + alt15=2; + } + + + switch (alt15) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:181:10: ESC + { + mESC(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:181:16: ~ ( '\\\\' | '\"' ) + { + if ( (input.LA(1)>='\u0000' && input.LA(1)<='!')||(input.LA(1)>='#' && input.LA(1)<='[')||(input.LA(1)>=']' && input.LA(1)<='\uFFFF') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + break; + + default : + break loop15; + } + } while (true); + + match('\"'); + + } + + } + finally { + } + } + // $ANTLR end "STRING_LITERAL" + + // $ANTLR start "ESC" + public final void mESC() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:185:5: ( '\\\\' ( 'n' | 'r' | 't' | 'b' | 'f' | '\"' | '\\'' | '\\\\' | '>' | 'u' XDIGIT XDIGIT XDIGIT XDIGIT | . ) ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:185:7: '\\\\' ( 'n' | 'r' | 't' | 'b' | 'f' | '\"' | '\\'' | '\\\\' | '>' | 'u' XDIGIT XDIGIT XDIGIT XDIGIT | . ) + { + match('\\'); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:186:3: ( 'n' | 'r' | 't' | 'b' | 'f' | '\"' | '\\'' | '\\\\' | '>' | 'u' XDIGIT XDIGIT XDIGIT XDIGIT | . ) + int alt16=11; + alt16 = dfa16.predict(input); + switch (alt16) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:186:5: 'n' + { + match('n'); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:187:5: 'r' + { + match('r'); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:188:5: 't' + { + match('t'); + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:189:5: 'b' + { + match('b'); + + } + break; + case 5 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:190:5: 'f' + { + match('f'); + + } + break; + case 6 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:191:5: '\"' + { + match('\"'); + + } + break; + case 7 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:192:5: '\\'' + { + match('\''); + + } + break; + case 8 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:193:5: '\\\\' + { + match('\\'); + + } + break; + case 9 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:194:5: '>' + { + match('>'); + + } + break; + case 10 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:195:5: 'u' XDIGIT XDIGIT XDIGIT XDIGIT + { + match('u'); + mXDIGIT(); + mXDIGIT(); + mXDIGIT(); + mXDIGIT(); + + } + break; + case 11 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:196:5: . + { + matchAny(); + + } + break; + + } + + + } + + } + finally { + } + } + // $ANTLR end "ESC" + + // $ANTLR start "XDIGIT" + public final void mXDIGIT() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:201:8: ( '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g: + { + if ( (input.LA(1)>='0' && input.LA(1)<='9')||(input.LA(1)>='A' && input.LA(1)<='F')||(input.LA(1)>='a' && input.LA(1)<='f') ) { + input.consume(); + + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + recover(mse); + throw mse;} + + + } + + } + finally { + } + } + // $ANTLR end "XDIGIT" + + // $ANTLR start "WS" + public final void mWS() throws RecognitionException { + try { + int _type = WS; + int _channel = DEFAULT_TOKEN_CHANNEL; + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:207:4: ( ( ' ' | '\\t' | ( '\\r' )? '\\n' )+ ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:207:6: ( ' ' | '\\t' | ( '\\r' )? '\\n' )+ + { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:207:6: ( ' ' | '\\t' | ( '\\r' )? '\\n' )+ + int cnt18=0; + loop18: + do { + int alt18=4; + switch ( input.LA(1) ) { + case ' ': + { + alt18=1; + } + break; + case '\t': + { + alt18=2; + } + break; + case '\n': + case '\r': + { + alt18=3; + } + break; + + } + + switch (alt18) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:207:8: ' ' + { + match(' '); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:208:5: '\\t' + { + match('\t'); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:209:5: ( '\\r' )? '\\n' + { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:209:5: ( '\\r' )? + int alt17=2; + int LA17_0 = input.LA(1); + + if ( (LA17_0=='\r') ) { + alt17=1; + } + switch (alt17) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:209:5: '\\r' + { + match('\r'); + + } + break; + + } + + match('\n'); + + } + break; + + default : + if ( cnt18 >= 1 ) break loop18; + EarlyExitException eee = + new EarlyExitException(18, input); + throw eee; + } + cnt18++; + } while (true); + + _channel=HIDDEN; + + } + + state.type = _type; + state.channel = _channel; + } + finally { + } + } + // $ANTLR end "WS" + + public void mTokens() throws RecognitionException { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:8: ( OK | FAIL | T__25 | T__26 | T__27 | T__28 | T__29 | T__30 | T__31 | SL_COMMENT | ML_COMMENT | STRING | ML_STRING | TOKEN_REF | RULE_REF | EXT | RETVAL | AST | ACTION | WS ) + int alt19=20; + alt19 = dfa19.predict(input); + switch (alt19) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:10: OK + { + mOK(); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:13: FAIL + { + mFAIL(); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:18: T__25 + { + mT__25(); + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:24: T__26 + { + mT__26(); + + } + break; + case 5 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:30: T__27 + { + mT__27(); + + } + break; + case 6 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:36: T__28 + { + mT__28(); + + } + break; + case 7 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:42: T__29 + { + mT__29(); + + } + break; + case 8 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:48: T__30 + { + mT__30(); + + } + break; + case 9 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:54: T__31 + { + mT__31(); + + } + break; + case 10 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:60: SL_COMMENT + { + mSL_COMMENT(); + + } + break; + case 11 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:71: ML_COMMENT + { + mML_COMMENT(); + + } + break; + case 12 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:82: STRING + { + mSTRING(); + + } + break; + case 13 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:89: ML_STRING + { + mML_STRING(); + + } + break; + case 14 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:99: TOKEN_REF + { + mTOKEN_REF(); + + } + break; + case 15 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:109: RULE_REF + { + mRULE_REF(); + + } + break; + case 16 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:118: EXT + { + mEXT(); + + } + break; + case 17 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:122: RETVAL + { + mRETVAL(); + + } + break; + case 18 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:129: AST + { + mAST(); + + } + break; + case 19 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:133: ACTION + { + mACTION(); + + } + break; + case 20 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:1:140: WS + { + mWS(); + + } + break; + + } + + } + + + protected DFA13 dfa13 = new DFA13(this); + protected DFA16 dfa16 = new DFA16(this); + protected DFA19 dfa19 = new DFA19(this); + static final String DFA13_eotS = + "\116\uffff"; + static final String DFA13_eofS = + "\116\uffff"; + static final String DFA13_minS = + "\1\0\2\uffff\2\0\2\uffff\1\0\1\uffff\3\0\1\uffff\2\0\1\uffff\2\0"+ + "\74\uffff"; + static final String DFA13_maxS = + "\1\uffff\2\uffff\2\uffff\2\uffff\1\uffff\1\uffff\3\uffff\1\uffff"+ + "\2\uffff\1\uffff\2\uffff\74\uffff"; + static final String DFA13_acceptS = + "\1\uffff\1\5\1\1\2\uffff\2\4\1\uffff\1\2\3\uffff\1\4\5\uffff\37"+ + "\2\1\3\4\uffff\1\3\5\uffff\16\3\4\uffff"; + static final String DFA13_specialS = + "\1\0\2\uffff\1\1\1\2\2\uffff\1\3\1\uffff\1\4\1\5\1\6\1\uffff\1\7"+ + "\1\10\1\uffff\1\11\1\12\74\uffff}>"; + static final String[] DFA13_transitionS = { + "\42\5\1\3\4\5\1\4\123\5\1\2\1\5\1\1\uff82\5", + "", + "", + "\42\13\1\10\4\13\1\11\64\13\1\12\36\13\1\7\1\13\1\6\uff82\13", + "\42\21\1\16\4\21\1\5\64\21\1\20\36\21\1\15\1\21\1\14\uff82"+ + "\21", + "", + "", + "\42\27\1\23\4\27\1\24\64\27\1\26\36\27\1\22\1\27\1\25\uff82"+ + "\27", + "", + "\42\34\1\33\4\34\1\35\64\34\1\30\36\34\1\32\1\34\1\31\uff82"+ + "\34", + "\42\52\1\40\4\52\1\41\26\52\1\50\35\52\1\47\5\52\1\45\3\52"+ + "\1\46\7\52\1\42\3\52\1\43\1\52\1\44\1\51\5\52\1\37\1\52\1\36"+ + "\uff82\52", + "\42\60\1\55\4\60\1\56\64\60\1\57\36\60\1\54\1\60\1\53\uff82"+ + "\60", + "", + "\47\5\1\61\uffd8\5", + "\47\5\1\66\uffd8\5", + "", + "\42\110\1\101\4\110\1\102\26\110\1\104\35\110\1\103\5\110\1"+ + "\77\3\110\1\100\7\110\1\74\3\110\1\75\1\110\1\76\1\105\5\110"+ + "\1\107\1\110\1\106\uff82\110", + "\47\5\1\111\uffd8\5", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "" + }; + + static final short[] DFA13_eot = DFA.unpackEncodedString(DFA13_eotS); + static final short[] DFA13_eof = DFA.unpackEncodedString(DFA13_eofS); + static final char[] DFA13_min = DFA.unpackEncodedStringToUnsignedChars(DFA13_minS); + static final char[] DFA13_max = DFA.unpackEncodedStringToUnsignedChars(DFA13_maxS); + static final short[] DFA13_accept = DFA.unpackEncodedString(DFA13_acceptS); + static final short[] DFA13_special = DFA.unpackEncodedString(DFA13_specialS); + static final short[][] DFA13_transition; + + static { + int numStates = DFA13_transitionS.length; + DFA13_transition = new short[numStates][]; + for (int i=0; i='\u0000' && LA13_0<='!')||(LA13_0>='#' && LA13_0<='&')||(LA13_0>='(' && LA13_0<='z')||LA13_0=='|'||(LA13_0>='~' && LA13_0<='\uFFFF')) ) {s = 5;} + + if ( s>=0 ) return s; + break; + case 1 : + int LA13_3 = input.LA(1); + + s = -1; + if ( (LA13_3=='}') ) {s = 6;} + + else if ( (LA13_3=='{') ) {s = 7;} + + else if ( (LA13_3=='\"') ) {s = 8;} + + else if ( (LA13_3=='\'') ) {s = 9;} + + else if ( (LA13_3=='\\') ) {s = 10;} + + else if ( ((LA13_3>='\u0000' && LA13_3<='!')||(LA13_3>='#' && LA13_3<='&')||(LA13_3>='(' && LA13_3<='[')||(LA13_3>=']' && LA13_3<='z')||LA13_3=='|'||(LA13_3>='~' && LA13_3<='\uFFFF')) ) {s = 11;} + + if ( s>=0 ) return s; + break; + case 2 : + int LA13_4 = input.LA(1); + + s = -1; + if ( (LA13_4=='}') ) {s = 12;} + + else if ( (LA13_4=='{') ) {s = 13;} + + else if ( (LA13_4=='\"') ) {s = 14;} + + else if ( (LA13_4=='\'') ) {s = 5;} + + else if ( (LA13_4=='\\') ) {s = 16;} + + else if ( ((LA13_4>='\u0000' && LA13_4<='!')||(LA13_4>='#' && LA13_4<='&')||(LA13_4>='(' && LA13_4<='[')||(LA13_4>=']' && LA13_4<='z')||LA13_4=='|'||(LA13_4>='~' && LA13_4<='\uFFFF')) ) {s = 17;} + + if ( s>=0 ) return s; + break; + case 3 : + int LA13_7 = input.LA(1); + + s = -1; + if ( (LA13_7=='{') ) {s = 18;} + + else if ( (LA13_7=='\"') ) {s = 19;} + + else if ( (LA13_7=='\'') ) {s = 20;} + + else if ( (LA13_7=='}') ) {s = 21;} + + else if ( (LA13_7=='\\') ) {s = 22;} + + else if ( ((LA13_7>='\u0000' && LA13_7<='!')||(LA13_7>='#' && LA13_7<='&')||(LA13_7>='(' && LA13_7<='[')||(LA13_7>=']' && LA13_7<='z')||LA13_7=='|'||(LA13_7>='~' && LA13_7<='\uFFFF')) ) {s = 23;} + + if ( s>=0 ) return s; + break; + case 4 : + int LA13_9 = input.LA(1); + + s = -1; + if ( (LA13_9=='\\') ) {s = 24;} + + else if ( (LA13_9=='}') ) {s = 25;} + + else if ( (LA13_9=='{') ) {s = 26;} + + else if ( (LA13_9=='\"') ) {s = 27;} + + else if ( ((LA13_9>='\u0000' && LA13_9<='!')||(LA13_9>='#' && LA13_9<='&')||(LA13_9>='(' && LA13_9<='[')||(LA13_9>=']' && LA13_9<='z')||LA13_9=='|'||(LA13_9>='~' && LA13_9<='\uFFFF')) ) {s = 28;} + + else if ( (LA13_9=='\'') ) {s = 29;} + + if ( s>=0 ) return s; + break; + case 5 : + int LA13_10 = input.LA(1); + + s = -1; + if ( (LA13_10=='}') ) {s = 30;} + + else if ( (LA13_10=='{') ) {s = 31;} + + else if ( (LA13_10=='\"') ) {s = 32;} + + else if ( (LA13_10=='\'') ) {s = 33;} + + else if ( (LA13_10=='n') ) {s = 34;} + + else if ( (LA13_10=='r') ) {s = 35;} + + else if ( (LA13_10=='t') ) {s = 36;} + + else if ( (LA13_10=='b') ) {s = 37;} + + else if ( (LA13_10=='f') ) {s = 38;} + + else if ( (LA13_10=='\\') ) {s = 39;} + + else if ( (LA13_10=='>') ) {s = 40;} + + else if ( (LA13_10=='u') ) {s = 41;} + + else if ( ((LA13_10>='\u0000' && LA13_10<='!')||(LA13_10>='#' && LA13_10<='&')||(LA13_10>='(' && LA13_10<='=')||(LA13_10>='?' && LA13_10<='[')||(LA13_10>=']' && LA13_10<='a')||(LA13_10>='c' && LA13_10<='e')||(LA13_10>='g' && LA13_10<='m')||(LA13_10>='o' && LA13_10<='q')||LA13_10=='s'||(LA13_10>='v' && LA13_10<='z')||LA13_10=='|'||(LA13_10>='~' && LA13_10<='\uFFFF')) ) {s = 42;} + + if ( s>=0 ) return s; + break; + case 6 : + int LA13_11 = input.LA(1); + + s = -1; + if ( (LA13_11=='}') ) {s = 43;} + + else if ( (LA13_11=='{') ) {s = 44;} + + else if ( (LA13_11=='\"') ) {s = 45;} + + else if ( (LA13_11=='\'') ) {s = 46;} + + else if ( (LA13_11=='\\') ) {s = 47;} + + else if ( ((LA13_11>='\u0000' && LA13_11<='!')||(LA13_11>='#' && LA13_11<='&')||(LA13_11>='(' && LA13_11<='[')||(LA13_11>=']' && LA13_11<='z')||LA13_11=='|'||(LA13_11>='~' && LA13_11<='\uFFFF')) ) {s = 48;} + + if ( s>=0 ) return s; + break; + case 7 : + int LA13_13 = input.LA(1); + + s = -1; + if ( (LA13_13=='\'') ) {s = 49;} + + else if ( ((LA13_13>='\u0000' && LA13_13<='&')||(LA13_13>='(' && LA13_13<='\uFFFF')) ) {s = 5;} + + if ( s>=0 ) return s; + break; + case 8 : + int LA13_14 = input.LA(1); + + s = -1; + if ( (LA13_14=='\'') ) {s = 54;} + + else if ( ((LA13_14>='\u0000' && LA13_14<='&')||(LA13_14>='(' && LA13_14<='\uFFFF')) ) {s = 5;} + + if ( s>=0 ) return s; + break; + case 9 : + int LA13_16 = input.LA(1); + + s = -1; + if ( (LA13_16=='n') ) {s = 60;} + + else if ( (LA13_16=='r') ) {s = 61;} + + else if ( (LA13_16=='t') ) {s = 62;} + + else if ( (LA13_16=='b') ) {s = 63;} + + else if ( (LA13_16=='f') ) {s = 64;} + + else if ( (LA13_16=='\"') ) {s = 65;} + + else if ( (LA13_16=='\'') ) {s = 66;} + + else if ( (LA13_16=='\\') ) {s = 67;} + + else if ( (LA13_16=='>') ) {s = 68;} + + else if ( (LA13_16=='u') ) {s = 69;} + + else if ( (LA13_16=='}') ) {s = 70;} + + else if ( (LA13_16=='{') ) {s = 71;} + + else if ( ((LA13_16>='\u0000' && LA13_16<='!')||(LA13_16>='#' && LA13_16<='&')||(LA13_16>='(' && LA13_16<='=')||(LA13_16>='?' && LA13_16<='[')||(LA13_16>=']' && LA13_16<='a')||(LA13_16>='c' && LA13_16<='e')||(LA13_16>='g' && LA13_16<='m')||(LA13_16>='o' && LA13_16<='q')||LA13_16=='s'||(LA13_16>='v' && LA13_16<='z')||LA13_16=='|'||(LA13_16>='~' && LA13_16<='\uFFFF')) ) {s = 72;} + + if ( s>=0 ) return s; + break; + case 10 : + int LA13_17 = input.LA(1); + + s = -1; + if ( (LA13_17=='\'') ) {s = 73;} + + else if ( ((LA13_17>='\u0000' && LA13_17<='&')||(LA13_17>='(' && LA13_17<='\uFFFF')) ) {s = 5;} + + if ( s>=0 ) return s; + break; + } + NoViableAltException nvae = + new NoViableAltException(getDescription(), 13, _s, input); + error(nvae); + throw nvae; + } + } + static final String DFA16_eotS = + "\12\uffff\1\13\2\uffff"; + static final String DFA16_eofS = + "\15\uffff"; + static final String DFA16_minS = + "\1\0\11\uffff\1\60\2\uffff"; + static final String DFA16_maxS = + "\1\uffff\11\uffff\1\146\2\uffff"; + static final String DFA16_acceptS = + "\1\uffff\1\1\1\2\1\3\1\4\1\5\1\6\1\7\1\10\1\11\1\uffff\1\13\1\12"; + static final String DFA16_specialS = + "\1\0\14\uffff}>"; + static final String[] DFA16_transitionS = { + "\42\13\1\6\4\13\1\7\26\13\1\11\35\13\1\10\5\13\1\4\3\13\1\5"+ + "\7\13\1\1\3\13\1\2\1\13\1\3\1\12\uff8a\13", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "\12\14\7\uffff\6\14\32\uffff\6\14", + "", + "" + }; + + static final short[] DFA16_eot = DFA.unpackEncodedString(DFA16_eotS); + static final short[] DFA16_eof = DFA.unpackEncodedString(DFA16_eofS); + static final char[] DFA16_min = DFA.unpackEncodedStringToUnsignedChars(DFA16_minS); + static final char[] DFA16_max = DFA.unpackEncodedStringToUnsignedChars(DFA16_maxS); + static final short[] DFA16_accept = DFA.unpackEncodedString(DFA16_acceptS); + static final short[] DFA16_special = DFA.unpackEncodedString(DFA16_specialS); + static final short[][] DFA16_transition; + + static { + int numStates = DFA16_transitionS.length; + DFA16_transition = new short[numStates][]; + for (int i=0; i' | 'u' XDIGIT XDIGIT XDIGIT XDIGIT | . )"; + } + public int specialStateTransition(int s, IntStream _input) throws NoViableAltException { + IntStream input = _input; + int _s = s; + switch ( s ) { + case 0 : + int LA16_0 = input.LA(1); + + s = -1; + if ( (LA16_0=='n') ) {s = 1;} + + else if ( (LA16_0=='r') ) {s = 2;} + + else if ( (LA16_0=='t') ) {s = 3;} + + else if ( (LA16_0=='b') ) {s = 4;} + + else if ( (LA16_0=='f') ) {s = 5;} + + else if ( (LA16_0=='\"') ) {s = 6;} + + else if ( (LA16_0=='\'') ) {s = 7;} + + else if ( (LA16_0=='\\') ) {s = 8;} + + else if ( (LA16_0=='>') ) {s = 9;} + + else if ( (LA16_0=='u') ) {s = 10;} + + else if ( ((LA16_0>='\u0000' && LA16_0<='!')||(LA16_0>='#' && LA16_0<='&')||(LA16_0>='(' && LA16_0<='=')||(LA16_0>='?' && LA16_0<='[')||(LA16_0>=']' && LA16_0<='a')||(LA16_0>='c' && LA16_0<='e')||(LA16_0>='g' && LA16_0<='m')||(LA16_0>='o' && LA16_0<='q')||LA16_0=='s'||(LA16_0>='v' && LA16_0<='\uFFFF')) ) {s = 11;} + + if ( s>=0 ) return s; + break; + } + NoViableAltException nvae = + new NoViableAltException(getDescription(), 16, _s, input); + error(nvae); + throw nvae; + } + } + static final String DFA19_eotS = + "\1\uffff\2\15\2\16\3\uffff\1\16\13\uffff\1\33\1\15\3\16\3\uffff"+ + "\1\15\3\16\1\44\3\16\1\uffff\1\50\1\51\1\16\2\uffff\1\16\1\54\1"+ + "\uffff"; + static final String DFA19_eofS = + "\55\uffff"; + static final String DFA19_minS = + "\1\11\1\113\1\101\1\165\1\141\3\uffff\1\145\1\uffff\1\52\11\uffff"+ + "\1\60\1\111\1\156\1\154\1\164\3\uffff\1\114\1\151\1\153\1\165\1"+ + "\60\1\164\1\163\1\162\1\uffff\2\60\1\156\2\uffff\1\163\1\60\1\uffff"; + static final String DFA19_maxS = + "\1\173\1\113\1\101\1\165\1\141\3\uffff\1\145\1\uffff\1\57\11\uffff"+ + "\1\172\1\111\1\156\1\154\1\164\3\uffff\1\114\1\151\1\153\1\165\1"+ + "\172\1\164\1\163\1\162\1\uffff\2\172\1\156\2\uffff\1\163\1\172\1"+ + "\uffff"; + static final String DFA19_acceptS = + "\5\uffff\1\5\1\6\1\7\1\uffff\1\11\1\uffff\1\14\1\15\1\16\1\17\1"+ + "\20\1\21\1\22\1\23\1\24\5\uffff\1\12\1\13\1\1\10\uffff\1\2\3\uffff"+ + "\1\3\1\4\2\uffff\1\10"; + static final String DFA19_specialS = + "\55\uffff}>"; + static final String[] DFA19_transitionS = { + "\2\23\2\uffff\1\23\22\uffff\1\23\1\uffff\1\13\5\uffff\1\21\4"+ + "\uffff\1\11\1\17\1\12\12\uffff\1\7\1\5\1\14\3\uffff\1\6\5\15"+ + "\1\2\10\15\1\1\13\15\1\20\5\uffff\6\16\1\3\12\16\1\10\4\16\1"+ + "\4\3\16\1\22", + "\1\24", + "\1\25", + "\1\26", + "\1\27", + "", + "", + "", + "\1\30", + "", + "\1\32\4\uffff\1\31", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "\12\15\7\uffff\32\15\4\uffff\1\15\1\uffff\32\15", + "\1\34", + "\1\35", + "\1\36", + "\1\37", + "", + "", + "", + "\1\40", + "\1\41", + "\1\42", + "\1\43", + "\12\15\7\uffff\32\15\4\uffff\1\15\1\uffff\32\15", + "\1\45", + "\1\46", + "\1\47", + "", + "\12\16\7\uffff\32\16\4\uffff\1\16\1\uffff\32\16", + "\12\16\7\uffff\32\16\4\uffff\1\16\1\uffff\32\16", + "\1\52", + "", + "", + "\1\53", + "\12\16\7\uffff\32\16\4\uffff\1\16\1\uffff\32\16", + "" + }; + + static final short[] DFA19_eot = DFA.unpackEncodedString(DFA19_eotS); + static final short[] DFA19_eof = DFA.unpackEncodedString(DFA19_eofS); + static final char[] DFA19_min = DFA.unpackEncodedStringToUnsignedChars(DFA19_minS); + static final char[] DFA19_max = DFA.unpackEncodedStringToUnsignedChars(DFA19_maxS); + static final short[] DFA19_accept = DFA.unpackEncodedString(DFA19_acceptS); + static final short[] DFA19_special = DFA.unpackEncodedString(DFA19_specialS); + static final short[][] DFA19_transition; + + static { + int numStates = DFA19_transitionS.length; + DFA19_transition = new short[numStates][]; + for (int i=0; i", "", "", "", "OK", "FAIL", "DOC_COMMENT", "ACTION", "RULE_REF", "TOKEN_REF", "RETVAL", "AST", "STRING", "ML_STRING", "EXT", "SL_COMMENT", "ML_COMMENT", "ESC", "NESTED_RETVAL", "NESTED_AST", "NESTED_ACTION", "STRING_LITERAL", "CHAR_LITERAL", "XDIGIT", "WS", "'gunit'", "'walks'", "';'", "'@header'", "':'", "'returns'", "'->'" + }; + public static final int RETVAL=10; + public static final int NESTED_ACTION=20; + public static final int AST=11; + public static final int T__28=28; + public static final int EXT=14; + public static final int FAIL=5; + public static final int WS=24; + public static final int STRING=12; + public static final int OK=4; + public static final int ACTION=7; + public static final int TOKEN_REF=9; + public static final int ESC=17; + public static final int XDIGIT=23; + public static final int RULE_REF=8; + public static final int T__29=29; + public static final int NESTED_AST=19; + public static final int T__30=30; + public static final int CHAR_LITERAL=22; + public static final int T__31=31; + public static final int EOF=-1; + public static final int STRING_LITERAL=21; + public static final int T__27=27; + public static final int T__26=26; + public static final int ML_STRING=13; + public static final int T__25=25; + public static final int ML_COMMENT=16; + public static final int SL_COMMENT=15; + public static final int DOC_COMMENT=6; + public static final int NESTED_RETVAL=18; + + // delegates + // delegators + + + public StGUnitParser(TokenStream input) { + this(input, new RecognizerSharedState()); + } + public StGUnitParser(TokenStream input, RecognizerSharedState state) { + super(input, state); + + } + + + public String[] getTokenNames() { return StGUnitParser.tokenNames; } + public String getGrammarFileName() { return "/Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g"; } + + + public Translator.TestSuiteAdapter adapter ;; + + + + // $ANTLR start "gUnitDef" + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:51:1: gUnitDef : 'gunit' name= id ( 'walks' id )? ';' ( header )? ( suite )* ; + public final void gUnitDef() throws RecognitionException { + StGUnitParser.id_return name = null; + + + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:52:2: ( 'gunit' name= id ( 'walks' id )? ';' ( header )? ( suite )* ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:52:4: 'gunit' name= id ( 'walks' id )? ';' ( header )? ( suite )* + { + match(input,25,FOLLOW_25_in_gUnitDef68); + pushFollow(FOLLOW_id_in_gUnitDef72); + name=id(); + + state._fsp--; + + adapter.setGrammarName((name!=null?input.toString(name.start,name.stop):null)); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:53:6: ( 'walks' id )? + int alt1=2; + int LA1_0 = input.LA(1); + + if ( (LA1_0==26) ) { + alt1=1; + } + switch (alt1) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:53:7: 'walks' id + { + match(input,26,FOLLOW_26_in_gUnitDef82); + pushFollow(FOLLOW_id_in_gUnitDef84); + id(); + + state._fsp--; + + + } + break; + + } + + match(input,27,FOLLOW_27_in_gUnitDef88); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:54:3: ( header )? + int alt2=2; + int LA2_0 = input.LA(1); + + if ( (LA2_0==28) ) { + alt2=1; + } + switch (alt2) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:54:3: header + { + pushFollow(FOLLOW_header_in_gUnitDef93); + header(); + + state._fsp--; + + + } + break; + + } + + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:54:11: ( suite )* + loop3: + do { + int alt3=2; + int LA3_0 = input.LA(1); + + if ( ((LA3_0>=RULE_REF && LA3_0<=TOKEN_REF)) ) { + alt3=1; + } + + + switch (alt3) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:54:11: suite + { + pushFollow(FOLLOW_suite_in_gUnitDef96); + suite(); + + state._fsp--; + + + } + break; + + default : + break loop3; + } + } while (true); + + + } + + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + } + finally { + } + return ; + } + // $ANTLR end "gUnitDef" + + + // $ANTLR start "header" + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:57:1: header : '@header' ACTION ; + public final void header() throws RecognitionException { + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:58:2: ( '@header' ACTION ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:58:4: '@header' ACTION + { + match(input,28,FOLLOW_28_in_header108); + match(input,ACTION,FOLLOW_ACTION_in_header110); + + } + + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + } + finally { + } + return ; + } + // $ANTLR end "header" + + + // $ANTLR start "suite" + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:61:1: suite : (parserRule= RULE_REF ( 'walks' RULE_REF )? | lexerRule= TOKEN_REF ) ':' ( test )+ ; + public final void suite() throws RecognitionException { + Token parserRule=null; + Token lexerRule=null; + + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:62:2: ( (parserRule= RULE_REF ( 'walks' RULE_REF )? | lexerRule= TOKEN_REF ) ':' ( test )+ ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:62:4: (parserRule= RULE_REF ( 'walks' RULE_REF )? | lexerRule= TOKEN_REF ) ':' ( test )+ + { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:62:4: (parserRule= RULE_REF ( 'walks' RULE_REF )? | lexerRule= TOKEN_REF ) + int alt5=2; + int LA5_0 = input.LA(1); + + if ( (LA5_0==RULE_REF) ) { + alt5=1; + } + else if ( (LA5_0==TOKEN_REF) ) { + alt5=2; + } + else { + NoViableAltException nvae = + new NoViableAltException("", 5, 0, input); + + throw nvae; + } + switch (alt5) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:62:6: parserRule= RULE_REF ( 'walks' RULE_REF )? + { + parserRule=(Token)match(input,RULE_REF,FOLLOW_RULE_REF_in_suite127); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:62:26: ( 'walks' RULE_REF )? + int alt4=2; + int LA4_0 = input.LA(1); + + if ( (LA4_0==26) ) { + alt4=1; + } + switch (alt4) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:62:27: 'walks' RULE_REF + { + match(input,26,FOLLOW_26_in_suite130); + match(input,RULE_REF,FOLLOW_RULE_REF_in_suite132); + + } + break; + + } + + adapter.startRule((parserRule!=null?parserRule.getText():null)); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:64:5: lexerRule= TOKEN_REF + { + lexerRule=(Token)match(input,TOKEN_REF,FOLLOW_TOKEN_REF_in_suite154); + adapter.startRule((lexerRule!=null?lexerRule.getText():null)); + + } + break; + + } + + match(input,29,FOLLOW_29_in_suite168); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:68:3: ( test )+ + int cnt6=0; + loop6: + do { + int alt6=2; + switch ( input.LA(1) ) { + case RULE_REF: + { + int LA6_2 = input.LA(2); + + if ( ((LA6_2>=OK && LA6_2<=FAIL)||LA6_2==EXT||(LA6_2>=30 && LA6_2<=31)) ) { + alt6=1; + } + + + } + break; + case TOKEN_REF: + { + int LA6_3 = input.LA(2); + + if ( ((LA6_3>=OK && LA6_3<=FAIL)||LA6_3==EXT||(LA6_3>=30 && LA6_3<=31)) ) { + alt6=1; + } + + + } + break; + case STRING: + case ML_STRING: + { + alt6=1; + } + break; + + } + + switch (alt6) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:68:3: test + { + pushFollow(FOLLOW_test_in_suite172); + test(); + + state._fsp--; + + + } + break; + + default : + if ( cnt6 >= 1 ) break loop6; + EarlyExitException eee = + new EarlyExitException(6, input); + throw eee; + } + cnt6++; + } while (true); + + adapter.endRule(); + + } + + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + } + finally { + } + return ; + } + // $ANTLR end "suite" + + + // $ANTLR start "test" + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:72:1: test : input expect ; + public final void test() throws RecognitionException { + ITestCaseInput input1 = null; + + ITestCaseOutput expect2 = null; + + + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:73:2: ( input expect ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:73:4: input expect + { + pushFollow(FOLLOW_input_in_test188); + input1=input(); + + state._fsp--; + + pushFollow(FOLLOW_expect_in_test190); + expect2=expect(); + + state._fsp--; + + adapter.addTestCase(input1, expect2); + + } + + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + } + finally { + } + return ; + } + // $ANTLR end "test" + + + // $ANTLR start "expect" + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:77:1: expect returns [ITestCaseOutput out] : ( OK | FAIL | 'returns' RETVAL | '->' output | '->' AST ); + public final ITestCaseOutput expect() throws RecognitionException { + ITestCaseOutput out = null; + + Token RETVAL3=null; + Token AST5=null; + StGUnitParser.output_return output4 = null; + + + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:78:2: ( OK | FAIL | 'returns' RETVAL | '->' output | '->' AST ) + int alt7=5; + switch ( input.LA(1) ) { + case OK: + { + alt7=1; + } + break; + case FAIL: + { + alt7=2; + } + break; + case 30: + { + alt7=3; + } + break; + case 31: + { + int LA7_4 = input.LA(2); + + if ( (LA7_4==AST) ) { + alt7=5; + } + else if ( (LA7_4==ACTION||(LA7_4>=STRING && LA7_4<=ML_STRING)) ) { + alt7=4; + } + else { + NoViableAltException nvae = + new NoViableAltException("", 7, 4, input); + + throw nvae; + } + } + break; + default: + NoViableAltException nvae = + new NoViableAltException("", 7, 0, input); + + throw nvae; + } + + switch (alt7) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:78:4: OK + { + match(input,OK,FOLLOW_OK_in_expect210); + out = adapter.createBoolOutput(true); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:79:4: FAIL + { + match(input,FAIL,FOLLOW_FAIL_in_expect219); + out = adapter.createBoolOutput(false); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:80:4: 'returns' RETVAL + { + match(input,30,FOLLOW_30_in_expect227); + RETVAL3=(Token)match(input,RETVAL,FOLLOW_RETVAL_in_expect229); + out = adapter.createReturnOutput((RETVAL3!=null?RETVAL3.getText():null)); + + } + break; + case 4 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:81:4: '->' output + { + match(input,31,FOLLOW_31_in_expect236); + pushFollow(FOLLOW_output_in_expect238); + output4=output(); + + state._fsp--; + + out = adapter.createStdOutput((output4!=null?input.toString(output4.start,output4.stop):null)); + + } + break; + case 5 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:82:4: '->' AST + { + match(input,31,FOLLOW_31_in_expect245); + AST5=(Token)match(input,AST,FOLLOW_AST_in_expect247); + out = adapter.createAstOutput((AST5!=null?AST5.getText():null)); + + } + break; + + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + } + finally { + } + return out; + } + // $ANTLR end "expect" + + + // $ANTLR start "input" + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:85:1: input returns [ITestCaseInput in] : ( STRING | ML_STRING | fileInput ); + public final ITestCaseInput input() throws RecognitionException { + ITestCaseInput in = null; + + Token STRING6=null; + Token ML_STRING7=null; + String fileInput8 = null; + + + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:86:2: ( STRING | ML_STRING | fileInput ) + int alt8=3; + switch ( input.LA(1) ) { + case STRING: + { + alt8=1; + } + break; + case ML_STRING: + { + alt8=2; + } + break; + case RULE_REF: + case TOKEN_REF: + { + alt8=3; + } + break; + default: + NoViableAltException nvae = + new NoViableAltException("", 8, 0, input); + + throw nvae; + } + + switch (alt8) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:86:4: STRING + { + STRING6=(Token)match(input,STRING,FOLLOW_STRING_in_input264); + in = adapter.createStringInput((STRING6!=null?STRING6.getText():null)); + + } + break; + case 2 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:87:4: ML_STRING + { + ML_STRING7=(Token)match(input,ML_STRING,FOLLOW_ML_STRING_in_input273); + in = adapter.createMultiInput((ML_STRING7!=null?ML_STRING7.getText():null)); + + } + break; + case 3 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:88:4: fileInput + { + pushFollow(FOLLOW_fileInput_in_input280); + fileInput8=fileInput(); + + state._fsp--; + + in = adapter.createFileInput(fileInput8); + + } + break; + + } + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + } + finally { + } + return in; + } + // $ANTLR end "input" + + public static class output_return extends ParserRuleReturnScope { + }; + + // $ANTLR start "output" + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:91:1: output : ( STRING | ML_STRING | ACTION ); + public final StGUnitParser.output_return output() throws RecognitionException { + StGUnitParser.output_return retval = new StGUnitParser.output_return(); + retval.start = input.LT(1); + + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:92:2: ( STRING | ML_STRING | ACTION ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g: + { + if ( input.LA(1)==ACTION||(input.LA(1)>=STRING && input.LA(1)<=ML_STRING) ) { + input.consume(); + state.errorRecovery=false; + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + throw mse; + } + + + } + + retval.stop = input.LT(-1); + + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + } + finally { + } + return retval; + } + // $ANTLR end "output" + + + // $ANTLR start "fileInput" + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:97:1: fileInput returns [String path] : id ( EXT )? ; + public final String fileInput() throws RecognitionException { + String path = null; + + Token EXT10=null; + StGUnitParser.id_return id9 = null; + + + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:98:2: ( id ( EXT )? ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:98:4: id ( EXT )? + { + pushFollow(FOLLOW_id_in_fileInput319); + id9=id(); + + state._fsp--; + + path = (id9!=null?input.toString(id9.start,id9.stop):null); + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:98:27: ( EXT )? + int alt9=2; + int LA9_0 = input.LA(1); + + if ( (LA9_0==EXT) ) { + alt9=1; + } + switch (alt9) { + case 1 : + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:98:28: EXT + { + EXT10=(Token)match(input,EXT,FOLLOW_EXT_in_fileInput324); + path += (EXT10!=null?EXT10.getText():null); + + } + break; + + } + + + } + + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + } + finally { + } + return path; + } + // $ANTLR end "fileInput" + + public static class id_return extends ParserRuleReturnScope { + }; + + // $ANTLR start "id" + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:101:1: id : ( TOKEN_REF | RULE_REF ); + public final StGUnitParser.id_return id() throws RecognitionException { + StGUnitParser.id_return retval = new StGUnitParser.id_return(); + retval.start = input.LT(1); + + try { + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g:101:5: ( TOKEN_REF | RULE_REF ) + // /Users/scai/NetBeansProjects/gUnitEditor/src/org/antlr/gunit/swingui/StGUnit.g: + { + if ( (input.LA(1)>=RULE_REF && input.LA(1)<=TOKEN_REF) ) { + input.consume(); + state.errorRecovery=false; + } + else { + MismatchedSetException mse = new MismatchedSetException(null,input); + throw mse; + } + + + } + + retval.stop = input.LT(-1); + + } + catch (RecognitionException re) { + reportError(re); + recover(input,re); + } + finally { + } + return retval; + } + // $ANTLR end "id" + + // Delegated rules + + + + + public static final BitSet FOLLOW_25_in_gUnitDef68 = new BitSet(new long[]{0x0000000000000300L}); + public static final BitSet FOLLOW_id_in_gUnitDef72 = new BitSet(new long[]{0x000000000C000000L}); + public static final BitSet FOLLOW_26_in_gUnitDef82 = new BitSet(new long[]{0x0000000000000300L}); + public static final BitSet FOLLOW_id_in_gUnitDef84 = new BitSet(new long[]{0x0000000008000000L}); + public static final BitSet FOLLOW_27_in_gUnitDef88 = new BitSet(new long[]{0x0000000010000302L}); + public static final BitSet FOLLOW_header_in_gUnitDef93 = new BitSet(new long[]{0x0000000000000302L}); + public static final BitSet FOLLOW_suite_in_gUnitDef96 = new BitSet(new long[]{0x0000000000000302L}); + public static final BitSet FOLLOW_28_in_header108 = new BitSet(new long[]{0x0000000000000080L}); + public static final BitSet FOLLOW_ACTION_in_header110 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_RULE_REF_in_suite127 = new BitSet(new long[]{0x0000000024000000L}); + public static final BitSet FOLLOW_26_in_suite130 = new BitSet(new long[]{0x0000000000000100L}); + public static final BitSet FOLLOW_RULE_REF_in_suite132 = new BitSet(new long[]{0x0000000020000000L}); + public static final BitSet FOLLOW_TOKEN_REF_in_suite154 = new BitSet(new long[]{0x0000000020000000L}); + public static final BitSet FOLLOW_29_in_suite168 = new BitSet(new long[]{0x0000000000003300L}); + public static final BitSet FOLLOW_test_in_suite172 = new BitSet(new long[]{0x0000000000003302L}); + public static final BitSet FOLLOW_input_in_test188 = new BitSet(new long[]{0x00000000C0000030L}); + public static final BitSet FOLLOW_expect_in_test190 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_OK_in_expect210 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_FAIL_in_expect219 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_30_in_expect227 = new BitSet(new long[]{0x0000000000000400L}); + public static final BitSet FOLLOW_RETVAL_in_expect229 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_31_in_expect236 = new BitSet(new long[]{0x0000000000003080L}); + public static final BitSet FOLLOW_output_in_expect238 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_31_in_expect245 = new BitSet(new long[]{0x0000000000000800L}); + public static final BitSet FOLLOW_AST_in_expect247 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_STRING_in_input264 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_ML_STRING_in_input273 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_fileInput_in_input280 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_set_in_output0 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_id_in_fileInput319 = new BitSet(new long[]{0x0000000000004002L}); + public static final BitSet FOLLOW_EXT_in_fileInput324 = new BitSet(new long[]{0x0000000000000002L}); + public static final BitSet FOLLOW_set_in_id0 = new BitSet(new long[]{0x0000000000000002L}); + +} \ No newline at end of file diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/runner/NotifiedTestExecuter.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/runner/NotifiedTestExecuter.java new file mode 100644 index 0000000..8cba661 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/runner/NotifiedTestExecuter.java @@ -0,0 +1,44 @@ +package org.antlr.gunit.swingui.runner; + +import org.antlr.gunit.*; +import org.antlr.gunit.swingui.model.*; + +/** + * + * @author scai + */ +public class NotifiedTestExecuter extends gUnitExecutor { + + private TestSuite testSuite ; + + public NotifiedTestExecuter(GrammarInfo grammarInfo, ClassLoader loader, String testsuiteDir, TestSuite suite) { + super(grammarInfo, loader, testsuiteDir); + testSuite = suite; + } + + @Override + public void onFail(ITestCase failTest) { + if(failTest == null) throw new IllegalArgumentException("Null fail test"); + + final String ruleName = failTest.getTestedRuleName(); + if(ruleName == null) throw new NullPointerException("Null rule name"); + + final Rule rule = testSuite.getRule(ruleName); + final TestCase failCase = (TestCase) rule.getElementAt(failTest.getTestCaseIndex()); + failCase.setPass(false); + //System.out.println(String.format("[FAIL] %s (%d) ", failTest.getTestedRuleName(), failTest.getTestCaseIndex())); + } + + @Override + public void onPass(ITestCase passTest) { + if(passTest == null) throw new IllegalArgumentException("Null pass test"); + + final String ruleName = passTest.getTestedRuleName(); + if(ruleName == null) throw new NullPointerException("Null rule name"); + + final Rule rule = testSuite.getRule(ruleName); + final TestCase passCase = (TestCase) rule.getElementAt(passTest.getTestCaseIndex()); + passCase.setPass(true); + //System.out.println(String.format("[PASS] %s (%d) ", passTest.getTestedRuleName(), passTest.getTestCaseIndex())); + } +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/runner/ParserLoader.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/runner/ParserLoader.java new file mode 100644 index 0000000..8c55566 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/runner/ParserLoader.java @@ -0,0 +1,79 @@ +package org.antlr.gunit.swingui.runner; + +import java.io.*; + +public class ParserLoader extends ClassLoader { + + private String ParserClassFile; + private String LexerClassFile; + private String ParserClassName; + private String LexerClassName; + + private byte[] parserBytes = null; + private byte[] lexerBytes = null; + + private Class ParserClass = null; + private Class LexerClass = null; + + public ParserLoader(String grammarName, String classDir) { + ParserClassName = grammarName + "Parser"; + LexerClassName = grammarName + "Lexer"; + ParserClassFile = classDir + File.separator + ParserClassName + ".class"; + LexerClassFile = classDir + File.separator + LexerClassName + ".class"; + + prepareClasses(); + } + + private void prepareClasses() { + try { + final InputStream inLexer = new FileInputStream(LexerClassFile); + lexerBytes = new byte[inLexer.available()]; + inLexer.read(lexerBytes); + inLexer.close(); + + final InputStream inParser = new FileInputStream(ParserClassFile); + parserBytes = new byte[inParser.available()]; + inParser.read(parserBytes); + inParser.close(); + } catch (Exception e) { + e.printStackTrace(); + throw new Error(e); + } + } + + @Override + public synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException { + + if(name.endsWith(LexerClassName)) { + + // load lexer class + + if(LexerClass != null) return LexerClass; + + LexerClass = defineClass(null, lexerBytes, 0, lexerBytes.length); + resolveClass(LexerClass); + return LexerClass; + + } else if(name.endsWith(ParserClassName)) { + + // load parser class + + if(ParserClass != null) return ParserClass; + + ParserClass = defineClass(null, parserBytes, 0, parserBytes.length); + resolveClass(ParserClass); + return ParserClass; + + } else { + + // load system class + + return findSystemClass(name); + + } + } + + private String stripPackage(String className) { + return className.substring(className.lastIndexOf("/")); + } +} diff --git a/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/runner/gUnitAdapter.java b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/runner/gUnitAdapter.java new file mode 100644 index 0000000..e9b283f --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/java/org/antlr/gunit/swingui/runner/gUnitAdapter.java @@ -0,0 +1,53 @@ +/** + * + */ +package org.antlr.gunit.swingui.runner; + +import java.io.File; +import org.antlr.runtime.*; +import org.antlr.runtime.CharStream; +import org.antlr.gunit.*; +import org.antlr.gunit.swingui.model.TestSuite; + +/** + * Adapter between gUnitEditor Swing GUI and gUnit command-line tool. + * @author scai + */ +public class gUnitAdapter { + + private ParserLoader loader = new ParserLoader( + "ini", "/Users/scai/Desktop/gUnitEditor/ini") ; + + + public void run(String testSuiteFileName, TestSuite testSuite) { + if (testSuiteFileName == null || testSuiteFileName.equals("")) + throw new IllegalArgumentException("Null test suite file name."); + + try { + + // Parse gUnit test suite file + final CharStream input = new ANTLRFileStream(testSuiteFileName); + final gUnitLexer lexer = new gUnitLexer(input); + final CommonTokenStream tokens = new CommonTokenStream(lexer); + final GrammarInfo grammarInfo = new GrammarInfo(); + final gUnitParser parser = new gUnitParser(tokens, grammarInfo); + parser.gUnitDef(); // parse gunit script and save elements to grammarInfo + + // Get test suite dir + final File f = new File(testSuiteFileName); + final String fullPath = f.getCanonicalPath(); + final String filename = f.getName(); + final String testsuiteDir = + fullPath.substring(0, fullPath.length()-filename.length()); + + // Execute test suite + final gUnitExecutor executer = new NotifiedTestExecuter( + grammarInfo, loader, testsuiteDir, testSuite); + executer.execTest(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/antlr-3.1.3/gunit/src/main/resources/org/antlr/gunit/gUnitTestResult.stg b/antlr-3.1.3/gunit/src/main/resources/org/antlr/gunit/gUnitTestResult.stg new file mode 100644 index 0000000..40aec57 --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/resources/org/antlr/gunit/gUnitTestResult.stg @@ -0,0 +1,49 @@ +/* + [The "BSD licence"] + Copyright (c) 2007-2008 Leon, Jen-Yuan Su + 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. +*/ +group gUnitTestResult; + +testResult(title, num_of_test, num_of_failure, failure, has_invalid, num_of_invalid, invalid) ::= << +----------------------------------------------------------------------- + with <num_of_test> tests +----------------------------------------------------------------------- +<num_of_failure> failures found: +<failure:{<it.header> +expected: <it.expectedResult> +actual: <it.actualResult> + +}> +<if(has_invalid)> +<num_of_invalid> invalid inputs found: +<invalid:{<it.header> +invalid input: <it.actual> +}> +<endif> + +Tests run: <num_of_test>, Failures: <num_of_failure> + +>> diff --git a/antlr-3.1.3/gunit/src/main/resources/org/antlr/gunit/junit.stg b/antlr-3.1.3/gunit/src/main/resources/org/antlr/gunit/junit.stg new file mode 100644 index 0000000..d2c162a --- /dev/null +++ b/antlr-3.1.3/gunit/src/main/resources/org/antlr/gunit/junit.stg @@ -0,0 +1,83 @@ +/* + [The "BSD licence"] + Copyright (c) 2007-2008 Leon, Jen-Yuan Su + 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. +*/ +group junit; + +classHeader(header,junitFileName,hasPackage,packagePath,lexerPath,parserPath,treeParserPath,isTreeGrammar) ::= << +<header> + +import org.antlr.gunit.gUnitBaseTest; + +public class <junitFileName> extends gUnitBaseTest { + + public void setUp() { + <if(hasPackage)><\t><\t>this.packagePath = "<packagePath>";<endif> + this.lexerPath = "<lexerPath>"; + this.parserPath = "<parserPath>"; + <if(isTreeGrammar)><\t><\t>this.treeParserPath = "<treeParserPath>";<endif> + }<\n><\n> +>> + +testTreeRuleMethod(methodName,testTreeRuleName,testRuleName,testInput,isFile,tokenType,expecting) ::= << + public void <methodName>() throws Exception { + // test input: <testInput> + Object retval = execTreeParser(<testTreeRuleName>, <testRuleName>, <testInput>, <isFile>); + Object actual = examineExecResult(<tokenType>, retval); + Object expecting = <expecting>; + + assertEquals("testing rule "+<testTreeRuleName>, expecting, actual); + }<\n><\n> +>> + +testTreeRuleMethod2(methodName,testTreeRuleName,testRuleName,testInput,isFile,returnType,expecting) ::= << + public void <methodName>() throws Exception { + // test input: <testInput> + <returnType> retval = (<returnType>)execTreeParser(<testTreeRuleName>, <testRuleName>, <testInput>, <isFile>); + + assertTrue("testing rule "+<testTreeRuleName>, <expecting>); + }<\n><\n> +>> + +testRuleMethod(isLexicalRule,methodName,testRuleName,testInput,isFile,tokenType,expecting) ::= << + public void <methodName>() throws Exception { + // test input: <testInput> + Object retval = <if(isLexicalRule)>execLexer<else>execParser<endif>(<testRuleName>, <testInput>, <isFile>); + Object actual = examineExecResult(<tokenType>, retval); + Object expecting = <expecting>; + + assertEquals("testing rule "+<testRuleName>, expecting, actual); + }<\n><\n> +>> + +testRuleMethod2(methodName,testRuleName,testInput,isFile,returnType,expecting) ::= << + public void <methodName>() throws Exception { + // test input: <testInput> + <returnType> retval = (<returnType>)execParser(<testRuleName>, <testInput>, <isFile>); + + assertTrue("testing rule "+<testRuleName>, <expecting>); + }<\n><\n> +>> diff --git a/antlr-3.1.3/gunit/src/test/java/org/antlr/gunit/GunitTest.java b/antlr-3.1.3/gunit/src/test/java/org/antlr/gunit/GunitTest.java new file mode 100644 index 0000000..3df86d0 --- /dev/null +++ b/antlr-3.1.3/gunit/src/test/java/org/antlr/gunit/GunitTest.java @@ -0,0 +1,38 @@ +package org.antlr.gunit; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for gUnit itself.... + */ +public class GunitTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public GunitTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( GunitTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testGunitTest() + { + assertTrue( true ); + } +} diff --git a/antlr-3.1.3/lib/._stringtemplate-3.2.jar b/antlr-3.1.3/lib/._stringtemplate-3.2.jar new file mode 100644 index 0000000..d7755e3 Binary files /dev/null and b/antlr-3.1.3/lib/._stringtemplate-3.2.jar differ diff --git a/antlr-3.1.3/lib/antlr-2.7.7.jar b/antlr-3.1.3/lib/antlr-2.7.7.jar new file mode 100644 index 0000000..5e5f14b Binary files /dev/null and b/antlr-3.1.3/lib/antlr-2.7.7.jar differ diff --git a/antlr-3.1.3/lib/antlr-3.1.3.jar b/antlr-3.1.3/lib/antlr-3.1.3.jar new file mode 100644 index 0000000..0ec52f8 Binary files /dev/null and b/antlr-3.1.3/lib/antlr-3.1.3.jar differ diff --git a/antlr-3.1.3/lib/antlr-runtime-3.1.3.jar b/antlr-3.1.3/lib/antlr-runtime-3.1.3.jar new file mode 100644 index 0000000..b0a9ea6 Binary files /dev/null and b/antlr-3.1.3/lib/antlr-runtime-3.1.3.jar differ diff --git a/antlr-3.1.3/lib/gunit.jar b/antlr-3.1.3/lib/gunit.jar new file mode 100644 index 0000000..751ce34 Binary files /dev/null and b/antlr-3.1.3/lib/gunit.jar differ diff --git a/antlr-3.1.3/lib/stringtemplate-3.2.jar b/antlr-3.1.3/lib/stringtemplate-3.2.jar new file mode 100644 index 0000000..8e41531 Binary files /dev/null and b/antlr-3.1.3/lib/stringtemplate-3.2.jar differ diff --git a/antlr-3.1.3/runtime/ActionScript/AUTHORS b/antlr-3.1.3/runtime/ActionScript/AUTHORS new file mode 100644 index 0000000..aaa0faa --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/AUTHORS @@ -0,0 +1 @@ +George Scott <george dot scott dash antlr at gmail dot com>: Main developer of ActionScript target. diff --git a/antlr-3.1.3/runtime/ActionScript/LICENSE b/antlr-3.1.3/runtime/ActionScript/LICENSE new file mode 100644 index 0000000..1d1d5d6 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/LICENSE @@ -0,0 +1,26 @@ +[The "BSD licence"] +Copyright (c) 2003-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. diff --git a/antlr-3.1.3/runtime/ActionScript/README b/antlr-3.1.3/runtime/ActionScript/README new file mode 100644 index 0000000..d16d6f3 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/README @@ -0,0 +1,66 @@ +1) ABOUT +======== + +This is the ActionScript package 'antlr3', which is required to use parsers created +by the ANTLR3 tool. See <http://www.antlr.org/> for more information about +ANTLR3. + + +2) STATUS +========= + +The ActionScript target for ANTLR3 is considered a solid beta at this point. Most things +should work, but a comprehensive suite of tests has not been built yet, so some areas may +be untested. + +3) DOWNLOAD +=========== + +This runtime is part of the ANTLR distribution. The latest version can be found +at <http://www.antlr.org/download.html>. + +If you are interested in the latest, most bleeding edge version, have a look at +the perforce depot at <http://fisheye2.cenqua.com/browse/antlr>. There are +tarballs ready to download, so you don't have to install the perforce client. + + +4) USAGE +=============== + +Include the antlr3.swc from the lib directory into your flex project. + +5) DOCUMENTATION +================ + +API Documentation is available in the file "antlr3-asdoc.zip" User documentation +(as far as it exists) can be found in the wiki +<http://www.antlr.org/wiki/display/ANTLR3/Antlr3ActionScriptTarget> + + +6) REPORTING BUGS +================= + +Please send bug reports to the ANTLR mailing list +<http://www.antlr.org:8080/mailman/listinfo/antlr-interest> or +<george.scott-antrl@gmail.com>. Direct e-mail is preferable as the antlr-interest +list is not read on a daily basis by the maintainers. + +Existing bugs may appear someday in the bugtracker: +<http://www.antlr.org:8888/browse/ANTLR> + + +7) HACKING +========== + +Only the runtime package can be found here. There are also some StringTemplate +files in 'src/org/antlr/codegen/templates/ActionScript/' and some Java code in +'src/org/antlr/codegen/ActionScriptTarget.java' (of the main ANTLR3 source +distribution). + +The 'project' directory contains everything you need to rebuild the library from source. +It also includes all unit tests. + +Please send patches to <george.scott-antlr@gmail.com>. For larger code contributions you'll +have to sign the "Developer's Certificate of Origin", which can be found on +<http://www.antlr.org/license.html> or use the feedback form at +<http://www.antlr.org/misc/feedback>. diff --git a/antlr-3.1.3/runtime/ActionScript/lib/antlr3.swc b/antlr-3.1.3/runtime/ActionScript/lib/antlr3.swc new file mode 100644 index 0000000..69cd780 Binary files /dev/null and b/antlr-3.1.3/runtime/ActionScript/lib/antlr3.swc differ diff --git a/antlr-3.1.3/runtime/ActionScript/project/.actionScriptProperties b/antlr-3.1.3/runtime/ActionScript/project/.actionScriptProperties new file mode 100644 index 0000000..547b8d7 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/.actionScriptProperties @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="UTF-8"?> +<actionScriptProperties mainApplicationPath="Antlr3Runtime.as" version="3"> +<compiler additionalCompilerArguments="" copyDependentFiles="false" enableModuleDebug="true" generateAccessible="false" htmlExpressInstall="true" htmlGenerate="false" htmlHistoryManagement="false" htmlPlayerVersion="9.0.28" htmlPlayerVersionCheck="true" outputFolderPath="build" sourceFolderPath="src" strict="true" useApolloConfig="true" verifyDigests="true" warn="true"> +<compilerSourcePath/> +<libraryPath defaultLinkType="1"> +<libraryPathEntry kind="4" path=""> +<modifiedEntries> +<libraryPathEntry kind="3" linkType="4" path="${PROJECT_FRAMEWORKS}/libs/framework.swc" useDefaultLinkType="true"> +<crossDomainRsls> +<crossDomainRslEntry autoExtract="true" policyFileUrl="" rslUrl="framework_3.0.0.477.swz"/> +<crossDomainRslEntry autoExtract="true" policyFileUrl="" rslUrl="framework_3.0.0.477.swf"/> +</crossDomainRsls> +</libraryPathEntry> +</modifiedEntries> +</libraryPathEntry> +<libraryPathEntry kind="1" linkType="1" path="lib"/> +</libraryPath> +<sourceAttachmentPath/> +</compiler> +<applications> +<application path="Antlr3Runtime.as"/> +<application path="Antlr3Test.mxml"/> +</applications> +<modules/> +<buildCSSFiles/> +</actionScriptProperties> diff --git a/antlr-3.1.3/runtime/ActionScript/project/.flexLibProperties b/antlr-3.1.3/runtime/ActionScript/project/.flexLibProperties new file mode 100644 index 0000000..1090456 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/.flexLibProperties @@ -0,0 +1,58 @@ +<?xml version="1.0" encoding="UTF-8"?> +<flexLibProperties version="1"> +<includeClasses> +<classEntry path="org.antlr.runtime.BitSet"/> +<classEntry path="org.antlr.runtime.tree.CommonErrorNode"/> +<classEntry path="org.antlr.runtime.tree.CommonTreeAdaptor"/> +<classEntry path="org.antlr.runtime.tree.BaseTree"/> +<classEntry path="org.antlr.runtime.MismatchedSetException"/> +<classEntry path="org.antlr.runtime.RecognitionException"/> +<classEntry path="org.antlr.runtime.NoViableAltException"/> +<classEntry path="org.antlr.runtime.Parser"/> +<classEntry path="org.antlr.runtime.tree.RewriteRuleNodeStream"/> +<classEntry path="org.antlr.runtime.TokenRewriteStream"/> +<classEntry path="org.antlr.runtime.Lexer"/> +<classEntry path="org.antlr.runtime.FailedPredicateException"/> +<classEntry path="org.antlr.runtime.CharStream"/> +<classEntry path="org.antlr.runtime.TokenConstants"/> +<classEntry path="org.antlr.runtime.MismatchedTokenException"/> +<classEntry path="org.antlr.runtime.tree.RewriteEarlyExitException"/> +<classEntry path="org.antlr.runtime.MismatchedNotSetException"/> +<classEntry path="org.antlr.runtime.tree.TreeAdaptor"/> +<classEntry path="org.antlr.runtime.ParserRuleReturnScope"/> +<classEntry path="org.antlr.runtime.RuleReturnScope"/> +<classEntry path="org.antlr.runtime.tree.RewriteCardinalityException"/> +<classEntry path="org.antlr.runtime.BaseRecognizer"/> +<classEntry path="org.antlr.runtime.RecognizerSharedState"/> +<classEntry path="org.antlr.runtime.tree.TreeConstants"/> +<classEntry path="org.antlr.runtime.CharStreamConstants"/> +<classEntry path="org.antlr.runtime.TokenSource"/> +<classEntry path="org.antlr.runtime.tree.CommonTree"/> +<classEntry path="org.antlr.runtime.MismatchedTreeNodeException"/> +<classEntry path="org.antlr.runtime.tree.RewriteRuleSubtreeStream"/> +<classEntry path="org.antlr.runtime.tree.TreeRuleReturnScope"/> +<classEntry path="org.antlr.runtime.CommonToken"/> +<classEntry path="org.antlr.runtime.CommonTokenStream"/> +<classEntry path="org.antlr.runtime.tree.RewriteRuleTokenStream"/> +<classEntry path="org.antlr.runtime.EarlyExitException"/> +<classEntry path="org.antlr.runtime.CharStreamState"/> +<classEntry path="org.antlr.runtime.tree.CommonTreeNodeStream"/> +<classEntry path="org.antlr.runtime.tree.RewriteRuleElementStream"/> +<classEntry path="org.antlr.runtime.ANTLRFileStream"/> +<classEntry path="org.antlr.runtime.tree.TreeNodeStream"/> +<classEntry path="org.antlr.runtime.tree.TreeParser"/> +<classEntry path="org.antlr.runtime.tree.BaseTreeAdaptor"/> +<classEntry path="org.antlr.runtime.IntStream"/> +<classEntry path="org.antlr.runtime.tree.Tree"/> +<classEntry path="org.antlr.runtime.MissingTokenException"/> +<classEntry path="org.antlr.runtime.UnwantedTokenException"/> +<classEntry path="org.antlr.runtime.ANTLRStringStream"/> +<classEntry path="org.antlr.runtime.tree.RewriteEmptyStreamException"/> +<classEntry path="org.antlr.runtime.DFA"/> +<classEntry path="org.antlr.runtime.Token"/> +<classEntry path="org.antlr.runtime.MismatchedRangeException"/> +<classEntry path="org.antlr.runtime.TokenStream"/> +</includeClasses> +<includeResources/> +<namespaceManifests/> +</flexLibProperties> diff --git a/antlr-3.1.3/runtime/ActionScript/project/.project b/antlr-3.1.3/runtime/ActionScript/project/.project new file mode 100644 index 0000000..59c9f74 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/.project @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>Antlr3Runtime</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>com.adobe.flexbuilder.project.flexbuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>com.adobe.flexbuilder.project.flexlibnature</nature> + <nature>com.adobe.flexbuilder.project.actionscriptnature</nature> + </natures> +</projectDescription> diff --git a/antlr-3.1.3/runtime/ActionScript/project/.settings/org.eclipse.core.resources.prefs b/antlr-3.1.3/runtime/ActionScript/project/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..836da99 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,3 @@ +#Tue Dec 11 16:01:13 PST 2007 +eclipse.preferences.version=1 +encoding/<project>=utf-8 diff --git a/antlr-3.1.3/runtime/ActionScript/project/README.txt b/antlr-3.1.3/runtime/ActionScript/project/README.txt new file mode 100644 index 0000000..84d1eb8 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/README.txt @@ -0,0 +1,14 @@ +This directory contains the source files required to build the runtime and the associated tests. + +Running Unit Tests +================== +The unit tests can be run using the ant "test" target. The tests use flexUnit and the flexUnit ant integration provided by Peter Martin. +FlexUnit is available at: + +http://code.google.com/p/as3flexunitlib/ + +The latest ant integration from Peter Martin can be found on his blog: + +http://weblogs.macromedia.com/pmartin/archives/2007/09/flexunit_for_an_2.cfm + + diff --git a/antlr-3.1.3/runtime/ActionScript/project/build.xml b/antlr-3.1.3/runtime/ActionScript/project/build.xml new file mode 100644 index 0000000..0f3a209 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/build.xml @@ -0,0 +1,84 @@ +<project name="Antlr3ActionScriptRuntime" basedir="." default="all"> + + <property environment="env"/> + <property name="build.dir" value="${basedir}/build"/> + <property name="build.lib.dir" value="${build.dir}/lib"/> + <property name="build.doc.dir" value="${build.dir}/doc"/> + <property name="build.test.dir" value="${build.dir}/test"/> + <property name="build.test.output.dir" value="${build.test.dir}/output"/> + <property name="src.dir" value="${basedir}/src"/> + + <property name="FLEX_HOME" value="${env.FLEX_HOME}"/> + + <!-- Assume SDK 3.0 or greater has the ant tasks --> + <taskdef resource="flexTasks.tasks" classpath="${FLEX_HOME}/ant/lib/flexTasks.jar"/> + <taskdef resource="com/adobe/ac/ant/tasks/tasks.properties" classpath="${basedir}/lib/FlexAntTasks.jar"/> + + <fileset id="sources" dir="${src.dir}"> + <include name="**/*.as"/> + </fileset> + + <pathconvert property="sourceClasses" refid="sources" pathsep=" "> + <filtermapper> + <replacestring from="${src.dir}/" to=""/> <!-- Strip off directory --> + <replacestring from=".as" to=""/> <!-- Strip off extension --> + <replacestring from="/" to="."/> <!-- Convert path to package --> + </filtermapper> + </pathconvert> + + <target name="all" depends="build"/> + + <target name="check-env"> + <fail> + <condition> + <not> + <isset property="FLEX_HOME"/> + </not> + </condition> + </fail> + </target> + + <target name="build" depends="check-env"> + <echo>File are ${sourceClasses}</echo> + <compc output="${build.lib.dir}/antlr3.swc" include-classes="${sourceClasses}"> + <load-config filename="${FLEX_HOME}/frameworks/air-config.xml"/> + <source-path path-element="${basedir}/src"/> + </compc> + </target> + + <target name="compile-tests"> + <mxmlc debug="true" file="${basedir}/test/Antlr3Test.mxml" output="${build.test.dir}/testAntlr3.swf"> + <source-path path-element="${basedir}/test"/> + <!-- List of SWC files or directories that contain SWC files. --> + <compiler.library-path dir="${basedir}" append="true"> + <include name="lib/*.swc" /> + <include name="build/lib/*.swc" /> + </compiler.library-path> + </mxmlc> + </target> + + <target name="test" depends="compile-tests"> + <flexunit timeout="0" swf="${build.test.dir}/testAntlr3.swf" toDir="${build.test.output.dir}" haltonfailure="false"/> + <junitreport toDir="${build.test.output.dir}"> + <fileset dir="${build.test.output.dir}"> + <include name="TEST-*.xml"/> + </fileset> + <report format="frames" todir="${build.test.output.dir}/html"/> + </junitreport> + </target> + + <target name="clean"> + <delete dir="${build.dir}"/> + </target> + + <target name="docs"> + <exec executable="${FLEX_HOME}/bin/aasdoc" failonerror="true"> + <arg line="-doc-sources ${src.dir}"/> + <arg line="-window-title 'ANTLR 3 Runtime'"/> + <arg line="-output ${build.doc.dir}"/> + </exec> + <zip destfile="${build.dir}/antlr3-asdoc.zip"> + <zipfileset dir="${build.doc.dir}" prefix="asdoc"/> + </zip> + </target> +</project> diff --git a/antlr-3.1.3/runtime/ActionScript/project/lib/FlexAntTasks.jar b/antlr-3.1.3/runtime/ActionScript/project/lib/FlexAntTasks.jar new file mode 100644 index 0000000..67f2789 Binary files /dev/null and b/antlr-3.1.3/runtime/ActionScript/project/lib/FlexAntTasks.jar differ diff --git a/antlr-3.1.3/runtime/ActionScript/project/lib/FlexUnitOptional.swc b/antlr-3.1.3/runtime/ActionScript/project/lib/FlexUnitOptional.swc new file mode 100644 index 0000000..1365507 Binary files /dev/null and b/antlr-3.1.3/runtime/ActionScript/project/lib/FlexUnitOptional.swc differ diff --git a/antlr-3.1.3/runtime/ActionScript/project/lib/flexunit.swc b/antlr-3.1.3/runtime/ActionScript/project/lib/flexunit.swc new file mode 100755 index 0000000..70bd0cd Binary files /dev/null and b/antlr-3.1.3/runtime/ActionScript/project/lib/flexunit.swc differ diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/ANTLRFileStream.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/ANTLRFileStream.as new file mode 100644 index 0000000..202e004 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/ANTLRFileStream.as @@ -0,0 +1,36 @@ +package org.antlr.runtime { + import flash.filesystem.File; + import flash.filesystem.FileMode; + import flash.filesystem.FileStream; + import flash.system.System; + + public class ANTLRFileStream extends ANTLRStringStream { + protected var _file:File; + + public function ANTLRFileStream(file:File, encoding:String = null) { + load(file, encoding); + } + + public function load(file:File, encoding:String = null):void { + _file = file; + if (encoding == null) { + encoding = File.systemCharset; + } + + var stream:FileStream = new FileStream(); + + try { + stream.open(file, FileMode.READ); + data = stream.readMultiByte(file.size, encoding); + n = data.length; + } + finally { + stream.close(); + } + } + + public override function get sourceName():String { + return _file.name; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/ANTLRStringStream.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/ANTLRStringStream.as new file mode 100644 index 0000000..8b375a0 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/ANTLRStringStream.as @@ -0,0 +1,212 @@ +/* + [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. +*/ +package org.antlr.runtime { + + public class ANTLRStringStream implements CharStream { + /** The data being scanned */ + protected var data:String; + + /** How many characters are actually in the buffer */ + protected var n:int; + + /** 0..n-1 index into string of next char */ + protected var p:int = 0; + + /** line number 1..n within the input */ + protected var _line:int = 1; + + /** The index of the character relative to the beginning of the line 0..n-1 */ + protected var _charPositionInLine:int = 0; + + /** tracks how deep mark() calls are nested */ + protected var markDepth:int = 0; + + /** A list of CharStreamState objects that tracks the stream state + * values line, charPositionInLine, and p that can change as you + * move through the input stream. Indexed from 1..markDepth. + * A null is kept @ index 0. Create upon first call to mark(). + */ + protected var markers:Array; + + /** Track the last mark() call result value for use in rewind(). */ + protected var lastMarker:int; + + protected var _sourceName:String; + + protected var _lineDelimiter:String; + + /** Copy data in string to a local char array */ + public function ANTLRStringStream(input:String = null, lineDelimiter:String = "\n") { + this._lineDelimiter = lineDelimiter; + if (input != null) { + this.data = input; + this.n = input.length; + } + } + + /** Reset the stream so that it's in the same state it was + * when the object was created *except* the data array is not + * touched. + */ + public function reset():void { + p = 0; + _line = 1; + _charPositionInLine = 0; + markDepth = 0; + } + + public function consume():void { + if ( p < n ) { + _charPositionInLine++; + if ( data.charAt(p)==_lineDelimiter ) { + _line++; + _charPositionInLine=0; + } + p++; + } + } + + public function LA(i:int):int { + if ( i==0 ) { + return 0; // undefined + } + if ( i<0 ) { + i++; // e.g., translate LA(-1) to use offset i=0; then data[p+0-1] + if ( (p+i-1) < 0 ) { + return CharStreamConstants.EOF; // invalid; no char before first char + } + } + + if ( (p+i-1) >= n ) { + return CharStreamConstants.EOF; + } + return data.charCodeAt(p+i-1); + } + + public function LT(i:int):int { + return LA(i); + } + + /** Return the current input symbol index 0..n where n indicates the + * last symbol has been read. The index is the index of char to + * be returned from LA(1). + */ + public function get index():int { + return p; + } + + public function get size():int { + return n; + } + + public function mark():int { + if ( markers==null ) { + markers = new Array(); + markers.push(null); // depth 0 means no backtracking, leave blank + } + markDepth++; + var state:CharStreamState = null; + if ( markDepth>=markers.length ) { + state = new CharStreamState(); + markers.push(state); + } + else { + state = CharStreamState(markers[markDepth]); + } + state.p = p; + state.line = _line; + state.charPositionInLine = _charPositionInLine; + lastMarker = markDepth; + return markDepth; + } + + public function rewindTo(m:int):void { + var state:CharStreamState = CharStreamState(markers[m]); + // restore stream state + seek(state.p); + _line = state.line; + _charPositionInLine = state.charPositionInLine; + release(m); + } + + public function rewind():void { + rewindTo(lastMarker); + } + + public function release(marker:int):void { + // unwind any other markers made after m and release m + markDepth = marker; + // release this marker + markDepth--; + } + + /** consume() ahead until p==index; can't just set p=index as we must + * update line and charPositionInLine. + */ + public function seek(index:int):void { + if ( index<=p ) { + p = index; // just jump; don't update stream state (line, ...) + return; + } + // seek forward, consume until p hits index + while ( p<index ) { + consume(); + } + } + + public function substring(start:int, stop:int):String { + return data.substring(start, stop + 1); + } + + public function get line():int { + return _line; + } + + public function get charPositionInLine():int { + return _charPositionInLine; + } + + public function set line(line:int):void { + this._line = line; + } + + public function set charPositionInLine(pos:int):void { + this._charPositionInLine = pos; + } + + public function get sourceName():String { + return _sourceName; + } + + public function set sourceName(sourceName:String):void { + _sourceName = sourceName; + } + + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/BaseRecognizer.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/BaseRecognizer.as new file mode 100644 index 0000000..90c3feb --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/BaseRecognizer.as @@ -0,0 +1,813 @@ +package org.antlr.runtime { + + /** A generic recognizer that can handle recognizers generated from + * lexer, parser, and tree grammars. This is all the parsing + * support code essentially; most of it is error recovery stuff and + * backtracking. + */ + public class BaseRecognizer { + public static const MEMO_RULE_FAILED:int = -2; + public static const MEMO_RULE_UNKNOWN:int = -1; + public static const INITIAL_FOLLOW_STACK_SIZE:int = 100; + + // copies from Token object for convenience in actions + public static const DEFAULT_TOKEN_CHANNEL:int = TokenConstants.DEFAULT_CHANNEL; + public static const HIDDEN:int = TokenConstants.HIDDEN_CHANNEL; + + public static const NEXT_TOKEN_RULE_NAME:String = "nextToken"; + + /** State of a lexer, parser, or tree parser are collected into a state + * object so the state can be shared. This sharing is needed to + * have one grammar import others and share same error variables + * and other state variables. It's a kind of explicit multiple + * inheritance via delegation of methods and shared state. + * + */ + public var state:RecognizerSharedState; // TODO - Place in private Namespace - cannot be private + + public function BaseRecognizer(state:RecognizerSharedState = null) { + if ( state == null ) { // don't ever let us have a null state + state = new RecognizerSharedState(); + } + this.state = state; + } + + /** reset the parser's state; subclasses must rewinds the input stream */ + public function reset():void { + // wack everything related to error recovery + if (state == null) { + return; + } + state._fsp = -1; + state.errorRecovery = false; + state.lastErrorIndex = -1; + state.failed = false; + state.syntaxErrors = 0; + // wack everything related to backtracking and memoization + state.backtracking = 0; + for (var i:int = 0; state.ruleMemo!=null && i < state.ruleMemo.length; i++) { // wipe cache + state.ruleMemo[i] = null; + } + } + + /** Match current input symbol against ttype. Attempt + * single token insertion or deletion error recovery. If + * that fails, throw MismatchedTokenException. + * + * To turn off single token insertion or deletion error + * recovery, override mismatchRecover() and have it call + * plain mismatch(), which does not recover. Then any error + * in a rule will cause an exception and immediate exit from + * rule. Rule would recover by resynchronizing to the set of + * symbols that can follow rule ref. + */ + public function matchStream(input:IntStream, ttype:int, follow:BitSet):Object { + //System.out.println("match "+((TokenStream)input).LT(1)); + var matchedSymbol:Object = getCurrentInputSymbol(input); + if ( input.LA(1)==ttype ) { + input.consume(); + state.errorRecovery = false; + state.failed = false; + return matchedSymbol; + } + if ( state.backtracking>0 ) { + state.failed = true; + return matchedSymbol; + } + matchedSymbol = recoverFromMismatchedToken(input, ttype, follow); + return matchedSymbol; + } + + /** Match the wildcard: in a symbol */ + public function matchAnyStream(input:IntStream):void { + state.errorRecovery = false; + state.failed = false; + input.consume(); + } + + public function mismatchIsUnwantedToken(input:IntStream, ttype:int):Boolean { + return input.LA(2)==ttype; + } + + public function mismatchIsMissingToken(input:IntStream, follow:BitSet):Boolean { + if ( follow==null ) { + // we have no information about the follow; we can only consume + // a single token and hope for the best + return false; + } + // compute what can follow this grammar element reference + if ( follow.member(TokenConstants.EOR_TOKEN_TYPE) ) { + var viableTokensFollowingThisRule:BitSet = computeContextSensitiveRuleFOLLOW(); + follow = follow.or(viableTokensFollowingThisRule); + if ( state._fsp>=0 ) { // remove EOR if we're not the start symbol + follow.remove(TokenConstants.EOR_TOKEN_TYPE); + } + } + // if current token is consistent with what could come after set + // then we know we're missing a token; error recovery is free to + // "insert" the missing token + + //System.out.println("LT(1)="+((TokenStream)input).LT(1)); + + // BitSet cannot handle negative numbers like -1 (EOF) so I leave EOR + // in follow set to indicate that the fall of the start symbol is + // in the set (EOF can follow). + if ( follow.member(input.LA(1)) || follow.member(TokenConstants.EOR_TOKEN_TYPE) ) { + //System.out.println("LT(1)=="+((TokenStream)input).LT(1)+" is consistent with what follows; inserting..."); + return true; + } + return false; + } + + /** Factor out what to do upon token mismatch so tree parsers can behave + * differently. Override and call mismatchRecover(input, ttype, follow) + * to get single token insertion and deletion. Use this to turn of + * single token insertion and deletion. Override mismatchRecover + * to call this instead. + */ + protected function mismatch(input:IntStream, ttype:int, follow:BitSet):void + { + if ( mismatchIsUnwantedToken(input, ttype) ) { + throw new UnwantedTokenException(ttype, input); + } + else if ( mismatchIsMissingToken(input, follow) ) { + throw new MissingTokenException(ttype, input, null); + } + throw new MismatchedTokenException(ttype, input); + } + + /** Report a recognition problem. + * + * This method sets errorRecovery to indicate the parser is recovering + * not parsing. Once in recovery mode, no errors are generated. + * To get out of recovery mode, the parser must successfully match + * a token (after a resync). So it will go: + * + * 1. error occurs + * 2. enter recovery mode, report error + * 3. consume until token found in resynch set + * 4. try to resume parsing + * 5. next match() will reset errorRecovery mode + * + * If you override, make sure to update syntaxErrors if you care about that. + */ + public function reportError(e:RecognitionException):void { + // if we've already reported an error and have not matched a token + // yet successfully, don't report any errors. + if ( state.errorRecovery ) { + //System.err.print("[SPURIOUS] "); + return; + } + state.syntaxErrors++; // don't count spurious + state.errorRecovery = true; + + displayRecognitionError(this.tokenNames, e); + } + + public function displayRecognitionError(tokenNames:Array, + e:RecognitionException):void + { + var hdr:String = getErrorHeader(e); + var msg:String = getErrorMessage(e, tokenNames); + emitErrorMessage(hdr+" "+msg); + } + + /** What error message should be generated for the various + * exception types? + * + * Not very object-oriented code, but I like having all error message + * generation within one method rather than spread among all of the + * exception classes. This also makes it much easier for the exception + * handling because the exception classes do not have to have pointers back + * to this object to access utility routines and so on. Also, changing + * the message for an exception type would be difficult because you + * would have to subclassing exception, but then somehow get ANTLR + * to make those kinds of exception objects instead of the default. + * This looks weird, but trust me--it makes the most sense in terms + * of flexibility. + * + * For grammar debugging, you will want to override this to add + * more information such as the stack frame with + * getRuleInvocationStack(e, this.getClass().getName()) and, + * for no viable alts, the decision description and state etc... + * + * Override this to change the message generated for one or more + * exception types. + */ + public function getErrorMessage(e:RecognitionException, tokenNames:Array):String { + var msg:String = e.message; + var tokenName:String = null; + if ( e is UnwantedTokenException ) { + var ute:UnwantedTokenException = UnwantedTokenException(e); + tokenName="<unknown>"; + if ( ute.expecting== TokenConstants.EOF ) { + tokenName = "EOF"; + } + else { + tokenName = tokenNames[ute.expecting]; + } + msg = "extraneous input "+getTokenErrorDisplay(ute.unexpectedToken)+ + " expecting "+tokenName; + } + else if ( e is MissingTokenException ) { + var mite:MissingTokenException = MissingTokenException(e); + tokenName="<unknown>"; + if ( mite.expecting == TokenConstants.EOF ) { + tokenName = "EOF"; + } + else { + tokenName = tokenNames[mite.expecting]; + } + msg = "missing "+tokenName+" at "+getTokenErrorDisplay(e.token); + } + else if ( e is MismatchedTokenException ) { + var mte:MismatchedTokenException = MismatchedTokenException(e); + tokenName="<unknown>"; + if ( mte.expecting== TokenConstants.EOF ) { + tokenName = "EOF"; + } + else { + tokenName = tokenNames[mte.expecting]; + } + msg = "mismatched input "+getTokenErrorDisplay(e.token)+ + " expecting "+tokenName; + } + else if ( e is MismatchedTreeNodeException ) { + var mtne:MismatchedTreeNodeException = MismatchedTreeNodeException(e); + tokenName="<unknown>"; + if ( mtne.expecting==TokenConstants.EOF ) { + tokenName = "EOF"; + } + else { + tokenName = tokenNames[mtne.expecting]; + } + msg = "mismatched tree node: "+mtne.node+ + " expecting "+tokenName; + } + else if ( e is NoViableAltException ) { + var nvae:NoViableAltException = NoViableAltException(e); + // for development, can add "decision=<<"+nvae.grammarDecisionDescription+">>" + // and "(decision="+nvae.decisionNumber+") and + // "state "+nvae.stateNumber + msg = "no viable alternative at input "+getTokenErrorDisplay(e.token); + } + else if ( e is EarlyExitException ) { + var eee:EarlyExitException = EarlyExitException(e); + // for development, can add "(decision="+eee.decisionNumber+")" + msg = "required (...)+ loop did not match anything at input "+ + getTokenErrorDisplay(e.token); + } + else if ( e is MismatchedSetException ) { + var mse:MismatchedSetException = MismatchedSetException(e); + msg = "mismatched input "+getTokenErrorDisplay(e.token)+ + " expecting set "+mse.expecting; + } + else if ( e is MismatchedNotSetException ) { + var mnse:MismatchedNotSetException = MismatchedNotSetException(e); + msg = "mismatched input "+getTokenErrorDisplay(e.token)+ + " expecting set "+mnse.expecting; + } + else if ( e is FailedPredicateException ) { + var fpe:FailedPredicateException = FailedPredicateException(e); + msg = "rule "+fpe.ruleName+" failed predicate: {"+ + fpe.predicateText+"}?"; + } + return msg; + } + + /** Get number of recognition errors (lexer, parser, tree parser). Each + * recognizer tracks its own number. So parser and lexer each have + * separate count. Does not count the spurious errors found between + * an error and next valid token match + * + * See also reportError() + */ + public function get numberOfSyntaxErrors():int { + return state.syntaxErrors; + } + + /** What is the error header, normally line/character position information? */ + public function getErrorHeader(e:RecognitionException):String { + return "line "+e.line+":"+e.charPositionInLine; + } + + /** How should a token be displayed in an error message? The default + * is to display just the text, but during development you might + * want to have a lot of information spit out. Override in that case + * to use t.toString() (which, for CommonToken, dumps everything about + * the token). This is better than forcing you to override a method in + * your token objects because you don't have to go modify your lexer + * so that it creates a new Java type. + */ + public function getTokenErrorDisplay(t:Token):String { + var s:String = t.text; + if ( s==null ) { + if ( t.type==TokenConstants.EOF ) { + s = "<EOF>"; + } + else { + s = "<"+t.type+">"; + } + } + s = s.replace("\n","\\\\n"); + s = s.replace("\r","\\\\r"); + s = s.replace("\t","\\\\t"); + return "'"+s+"'"; + } + + /** Override this method to change where error messages go */ + public function emitErrorMessage(msg:String):void { + trace(msg); + } + + /** Recover from an error found on the input stream. This is + * for NoViableAlt and mismatched symbol exceptions. If you enable + * single token insertion and deletion, this will usually not + * handle mismatched symbol exceptions but there could be a mismatched + * token that the match() routine could not recover from. + */ + public function recoverStream(input:IntStream, re:RecognitionException):void { + if ( state.lastErrorIndex==input.index) { + // uh oh, another error at same token index; must be a case + // where LT(1) is in the recovery token set so nothing is + // consumed; consume a single token so at least to prevent + // an infinite loop; this is a failsafe. + input.consume(); + } + state.lastErrorIndex = input.index; + var followSet:BitSet = computeErrorRecoverySet(); + beginResync(); + consumeUntil(input, followSet); + endResync(); + } + + /** A hook to listen in on the token consumption during error recovery. + * The DebugParser subclasses this to fire events to the listenter. + */ + public function beginResync():void { + } + + public function endResync():void { + } + + /* Compute the error recovery set for the current rule. During + * rule invocation, the parser pushes the set of tokens that can + * follow that rule reference on the stack; this amounts to + * computing FIRST of what follows the rule reference in the + * enclosing rule. This local follow set only includes tokens + * from within the rule; i.e., the FIRST computation done by + * ANTLR stops at the end of a rule. + * + * EXAMPLE + * + * When you find a "no viable alt exception", the input is not + * consistent with any of the alternatives for rule r. The best + * thing to do is to consume tokens until you see something that + * can legally follow a call to r *or* any rule that called r. + * You don't want the exact set of viable next tokens because the + * input might just be missing a token--you might consume the + * rest of the input looking for one of the missing tokens. + * + * Consider grammar: + * + * a : '[' b ']' + * | '(' b ')' + * ; + * b : c '^' INT ; + * c : ID + * | INT + * ; + * + * At each rule invocation, the set of tokens that could follow + * that rule is pushed on a stack. Here are the various "local" + * follow sets: + * + * FOLLOW(b1_in_a) = FIRST(']') = ']' + * FOLLOW(b2_in_a) = FIRST(')') = ')' + * FOLLOW(c_in_b) = FIRST('^') = '^' + * + * Upon erroneous input "[]", the call chain is + * + * a -> b -> c + * + * and, hence, the follow context stack is: + * + * depth local follow set after call to rule + * 0 <EOF> a (from main()) + * 1 ']' b + * 3 '^' c + * + * Notice that ')' is not included, because b would have to have + * been called from a different context in rule a for ')' to be + * included. + * + * For error recovery, we cannot consider FOLLOW(c) + * (context-sensitive or otherwise). We need the combined set of + * all context-sensitive FOLLOW sets--the set of all tokens that + * could follow any reference in the call chain. We need to + * resync to one of those tokens. Note that FOLLOW(c)='^' and if + * we resync'd to that token, we'd consume until EOF. We need to + * sync to context-sensitive FOLLOWs for a, b, and c: {']','^'}. + * In this case, for input "[]", LA(1) is in this set so we would + * not consume anything and after printing an error rule c would + * return normally. It would not find the required '^' though. + * At this point, it gets a mismatched token error and throws an + * exception (since LA(1) is not in the viable following token + * set). The rule exception handler tries to recover, but finds + * the same recovery set and doesn't consume anything. Rule b + * exits normally returning to rule a. Now it finds the ']' (and + * with the successful match exits errorRecovery mode). + * + * So, you cna see that the parser walks up call chain looking + * for the token that was a member of the recovery set. + * + * Errors are not generated in errorRecovery mode. + * + * ANTLR's error recovery mechanism is based upon original ideas: + * + * "Algorithms + Data Structures = Programs" by Niklaus Wirth + * + * and + * + * "A note on error recovery in recursive descent parsers": + * http://portal.acm.org/citation.cfm?id=947902.947905 + * + * Later, Josef Grosch had some good ideas: + * + * "Efficient and Comfortable Error Recovery in Recursive Descent + * Parsers": + * ftp://www.cocolab.com/products/cocktail/doca4.ps/ell.ps.zip + * + * Like Grosch I implemented local FOLLOW sets that are combined + * at run-time upon error to avoid overhead during parsing. + */ + protected function computeErrorRecoverySet():BitSet { + return combineFollows(false); + } + + /** Compute the context-sensitive FOLLOW set for current rule. + * This is set of token types that can follow a specific rule + * reference given a specific call chain. You get the set of + * viable tokens that can possibly come next (lookahead depth 1) + * given the current call chain. Contrast this with the + * definition of plain FOLLOW for rule r: + * + * FOLLOW(r)={x | S=>*alpha r beta in G and x in FIRST(beta)} + * + * where x in T* and alpha, beta in V*; T is set of terminals and + * V is the set of terminals and nonterminals. In other words, + * FOLLOW(r) is the set of all tokens that can possibly follow + * references to r in *any* sentential form (context). At + * runtime, however, we know precisely which context applies as + * we have the call chain. We may compute the exact (rather + * than covering superset) set of following tokens. + * + * For example, consider grammar: + * + * stat : ID '=' expr ';' // FOLLOW(stat)=={EOF} + * | "return" expr '.' + * ; + * expr : atom ('+' atom)* ; // FOLLOW(expr)=={';','.',')'} + * atom : INT // FOLLOW(atom)=={'+',')',';','.'} + * | '(' expr ')' + * ; + * + * The FOLLOW sets are all inclusive whereas context-sensitive + * FOLLOW sets are precisely what could follow a rule reference. + * For input input "i=(3);", here is the derivation: + * + * stat => ID '=' expr ';' + * => ID '=' atom ('+' atom)* ';' + * => ID '=' '(' expr ')' ('+' atom)* ';' + * => ID '=' '(' atom ')' ('+' atom)* ';' + * => ID '=' '(' INT ')' ('+' atom)* ';' + * => ID '=' '(' INT ')' ';' + * + * At the "3" token, you'd have a call chain of + * + * stat -> expr -> atom -> expr -> atom + * + * What can follow that specific nested ref to atom? Exactly ')' + * as you can see by looking at the derivation of this specific + * input. Contrast this with the FOLLOW(atom)={'+',')',';','.'}. + * + * You want the exact viable token set when recovering from a + * token mismatch. Upon token mismatch, if LA(1) is member of + * the viable next token set, then you know there is most likely + * a missing token in the input stream. "Insert" one by just not + * throwing an exception. + */ + protected function computeContextSensitiveRuleFOLLOW():BitSet { + return combineFollows(true); + } + + protected function combineFollows(exact:Boolean):BitSet { + var top:int = state._fsp; + var followSet:BitSet = new BitSet(); + for (var i:int=top; i>=0; i--) { + var localFollowSet:BitSet = state.following[i]; + followSet.orInPlace(localFollowSet); + if ( exact ) { + // can we see end of rule? + if ( localFollowSet.member(TokenConstants.EOR_TOKEN_TYPE) ) { + // Only leave EOR in set if at top (start rule); this lets + // us know if have to include follow(start rule); i.e., EOF + if ( i>0 ) { + followSet.remove(TokenConstants.EOR_TOKEN_TYPE); + } + } + else { // can't see end of rule, quit + break; + } + } + } + return followSet; + } + + /** Attempt to recover from a single missing or extra token. + * + * EXTRA TOKEN + * + * LA(1) is not what we are looking for. If LA(2) has the right token, + * however, then assume LA(1) is some extra spurious token. Delete it + * and LA(2) as if we were doing a normal match(), which advances the + * input. + * + * MISSING TOKEN + * + * If current token is consistent with what could come after + * ttype then it is ok to "insert" the missing token, else throw + * exception For example, Input "i=(3;" is clearly missing the + * ')'. When the parser returns from the nested call to expr, it + * will have call chain: + * + * stat -> expr -> atom + * + * and it will be trying to match the ')' at this point in the + * derivation: + * + * => ID '=' '(' INT ')' ('+' atom)* ';' + * ^ + * match() will see that ';' doesn't match ')' and report a + * mismatched token error. To recover, it sees that LA(1)==';' + * is in the set of tokens that can follow the ')' token + * reference in rule atom. It can assume that you forgot the ')'. + */ + public function recoverFromMismatchedToken(input:IntStream, + ttype:int, + follow:BitSet):Object { + var e:RecognitionException = null; + // if next token is what we are looking for then "delete" this token + if ( mismatchIsUnwantedToken(input, ttype) ) { + e = new UnwantedTokenException(ttype, input); + /* + System.err.println("recoverFromMismatchedToken deleting "+ + ((TokenStream)input).LT(1)+ + " since "+((TokenStream)input).LT(2)+" is what we want"); + */ + beginResync(); + input.consume(); // simply delete extra token + endResync(); + reportError(e); // report after consuming so AW sees the token in the exception + // we want to return the token we're actually matching + var matchedSymbol:Object = getCurrentInputSymbol(input); + input.consume(); // move past ttype token as if all were ok + return matchedSymbol; + } + // can't recover with single token deletion, try insertion + if ( mismatchIsMissingToken(input, follow) ) { + var inserted:Object = getMissingSymbol(input, e, ttype, follow); + e = new MissingTokenException(ttype, input, inserted); + reportError(e); // report after inserting so AW sees the token in the exception + return inserted; + } + // even that didn't work; must throw the exception + e = new MismatchedTokenException(ttype, input); + throw e; + } + + /** Not currently used */ + public function recoverFromMismatchedSet(input:IntStream, + e:RecognitionException, + follow:BitSet):Object + { + if ( mismatchIsMissingToken(input, follow) ) { + // System.out.println("missing token"); + reportError(e); + // we don't know how to conjure up a token for sets yet + return getMissingSymbol(input, e, TokenConstants.INVALID_TOKEN_TYPE, follow); + } + // TODO do single token deletion like above for Token mismatch + throw e; + } + + /** Match needs to return the current input symbol, which gets put + * into the label for the associated token ref; e.g., x=ID. Token + * and tree parsers need to return different objects. Rather than test + * for input stream type or change the IntStream interface, I use + * a simple method to ask the recognizer to tell me what the current + * input symbol is. + * + * This is ignored for lexers. + */ + protected function getCurrentInputSymbol(input:IntStream):Object { return null; } + + /** Conjure up a missing token during error recovery. + * + * The recognizer attempts to recover from single missing + * symbols. But, actions might refer to that missing symbol. + * For example, x=ID {f($x);}. The action clearly assumes + * that there has been an identifier matched previously and that + * $x points at that token. If that token is missing, but + * the next token in the stream is what we want we assume that + * this token is missing and we keep going. Because we + * have to return some token to replace the missing token, + * we have to conjure one up. This method gives the user control + * over the tokens returned for missing tokens. Mostly, + * you will want to create something special for identifier + * tokens. For literals such as '{' and ',', the default + * action in the parser or tree parser works. It simply creates + * a CommonToken of the appropriate type. The text will be the token. + * If you change what tokens must be created by the lexer, + * override this method to create the appropriate tokens. + */ + protected function getMissingSymbol(input:IntStream, + e:RecognitionException, + expectedTokenType:int, + follow:BitSet):Object + { + return null; + } + + public function consumeUntilToken(input:IntStream, tokenType:int):void { + //System.out.println("consumeUntil "+tokenType); + var ttype:int = input.LA(1); + while (ttype != TokenConstants.EOF && ttype != tokenType) { + input.consume(); + ttype = input.LA(1); + } + } + + /** Consume tokens until one matches the given token set */ + public function consumeUntil(input:IntStream, bitSet:BitSet):void { + //trace("consumeUntil("+bitSet.toStringFromTokens(tokenNames)+")"); + var ttype:int = input.LA(1); + while (ttype != TokenConstants.EOF && !bitSet.member(ttype) ) { + //trace("consume during recover LA(1)="+tokenNames[input.LA(1)]); + input.consume(); + ttype = input.LA(1); + } + } + + /** Push a rule's follow set using our own hardcoded stack */ + protected function pushFollow(fset:BitSet):void { + state.following[++state._fsp] = fset; + } + + public function get backtrackingLevel():int { + return state.backtracking; + } + + public function set backtrakingLevel(n:int):void { + state.backtracking = n; + } + + /** Return whether or not a backtracking attempt failed. */ + public function get failed():Boolean { + return state.failed; + } + + /** Used to print out token names like ID during debugging and + * error reporting. The generated parsers implement a method + * that overrides this to point to their String[] tokenNames. + */ + public function get tokenNames():Array { + return null; + } + + /** For debugging and other purposes, might want the grammar name. + * Have ANTLR generate an implementation for this method. + */ + public function get grammarFileName():String { + return null; + } + + public function get sourceName():String { + return null; + } + + /** A convenience method for use most often with template rewrites. + * Convert a List<Token> to List<String> + */ + public function toStrings(tokens:Array):Array { + if ( tokens==null ) return null; + var strings:Array = new Array(tokens.length); + for (var i:int = 0; i<tokens.length; i++) { + strings.push(tokens[i].text); + } + return strings; + } + + /** Given a rule number and a start token index number, return + * MEMO_RULE_UNKNOWN if the rule has not parsed input starting from + * start index. If this rule has parsed input starting from the + * start index before, then return where the rule stopped parsing. + * It returns the index of the last token matched by the rule. + * + * For now we use a hashtable and just the slow Object-based one. + * Later, we can make a special one for ints and also one that + * tosses out data after we commit past input position i. + */ + public function getRuleMemoization(ruleIndex:int, ruleStartIndex:int):int { + if ( state.ruleMemo[ruleIndex]==undefined ) { + state.ruleMemo[ruleIndex] = new Array(); + } + var stopIndex:* = state.ruleMemo[ruleIndex][ruleStartIndex]; + if ( stopIndex == undefined ) { + return MEMO_RULE_UNKNOWN; + } + return stopIndex as int; + } + + /** Has this rule already parsed input at the current index in the + * input stream? Return the stop token index or MEMO_RULE_UNKNOWN. + * If we attempted but failed to parse properly before, return + * MEMO_RULE_FAILED. + * + * This method has a side-effect: if we have seen this input for + * this rule and successfully parsed before, then seek ahead to + * 1 past the stop token matched for this rule last time. + */ + public function alreadyParsedRule(input:IntStream, ruleIndex:int):Boolean { + var stopIndex:int = getRuleMemoization(ruleIndex, input.index); + if ( stopIndex==MEMO_RULE_UNKNOWN ) { + return false; + } + if ( stopIndex==MEMO_RULE_FAILED ) { + //System.out.println("rule "+ruleIndex+" will never succeed"); + state.failed=true; + } + else { + //System.out.println("seen rule "+ruleIndex+" before; skipping ahead to @"+(stopIndex+1)+" failed="+failed); + input.seek(stopIndex+1); // jump to one past stop token + } + return true; + } + + /** Record whether or not this rule parsed the input at this position + * successfully. Use a standard java hashtable for now. + */ + public function memoize(input:IntStream, + ruleIndex:int, + ruleStartIndex:int):void + { + var stopTokenIndex:int = state.failed ? MEMO_RULE_FAILED : input.index - 1; + if ( state.ruleMemo==null ) { + trace("!!!!!!!!! memo array is null for "+ grammarFileName); + } + if ( ruleIndex >= state.ruleMemo.length ) { + trace("!!!!!!!!! memo size is "+state.ruleMemo.length+", but rule index is "+ruleIndex); + } + + if ( state.ruleMemo[ruleIndex]!=null ) { + state.ruleMemo[ruleIndex][ruleStartIndex] = stopTokenIndex; + } + } + + /** return how many rule/input-index pairs there are in total. + * TODO: this includes synpreds. :( + */ + public function getRuleMemoizationCacheSize():int { + var n:int = 0; + for (var i:int = 0; state.ruleMemo!=null && i < state.ruleMemo.length; i++) { + var ruleMap:Object = state.ruleMemo[i]; + if ( ruleMap!=null ) { + n += ruleMap.length; // how many input indexes are recorded? + } + } + return n; + } + + public function traceInSymbol(ruleName:String, ruleIndex:int, inputSymbol:Object):void { + trace("enter "+ruleName+" "+inputSymbol); + if ( state.backtracking>0 ) { + trace(" backtracking="+state.backtracking); + } + trace(); + } + + public function traceOutSymbol(ruleName:String, + ruleIndex:int, + inputSymbol:Object):void + { + trace("exit "+ruleName+" "+inputSymbol); + if ( state.backtracking>0 ) { + trace(" backtracking="+state.backtracking); + if ( state.failed ) trace(" failed"); + else trace(" succeeded"); + } + trace(); + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/BitSet.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/BitSet.as new file mode 100644 index 0000000..8630b8e --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/BitSet.as @@ -0,0 +1,267 @@ +/* + [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. +*/ +package org.antlr.runtime { + + /**A stripped-down version of org.antlr.misc.BitSet that is just + * good enough to handle runtime requirements such as FOLLOW sets + * for automatic error recovery. + */ + public class BitSet { + protected static const BITS:uint = 32; // number of bits / int + protected static const LOG_BITS:uint = 5; // 2^5 == 32 + + /* We will often need to do a mod operator (i mod nbits). Its + * turns out that, for powers of two, this mod operation is + * same as (i & (nbits-1)). Since mod is slow, we use a + * precomputed mod mask to do the mod instead. + */ + protected static const MOD_MASK:uint = BITS - 1; + + /** The actual data bits */ + protected var bits:Array; + + /** Construction from a static array of longs */ + public function BitSet(bits:Array = null) { + if (bits == null) { + this.bits = new Array(); + } + else { + this.bits = new Array(bits.length); + for (var i:int = 0; i < bits.length; i++) { + this.bits[i] = bits[i]; + } + } + } + + public static function of(... args):BitSet { + var s:BitSet = new BitSet(); + for (var i:int = 0; i < args.length; i++) { + s.add(args[i]); + } + return s; + } + + /** return this | a in a new set */ + public function or(a:BitSet):BitSet { + if ( a==null ) { + return this; + } + var s:BitSet = this.clone(); + s.orInPlace(a); + return s; + } + + /** or this element into this set (grow as necessary to accommodate) */ + public function add(el:int):void { + var n:int = wordNumber(el); + if (n >= bits.length) { + growToInclude(el); + } + bits[n] |= bitMask(el); + } + + /** + * Grows the set to a larger number of bits. + * @param bit element that must fit in set + */ + public function growToInclude(bit:int):void { + var newSize:int = Math.max(bits.length << 1, numWordsToHold(bit)); + bits.length = newSize; + } + + public function orInPlace(a:BitSet):void { + if ( a==null ) { + return; + } + // If this is smaller than a, grow this first + if (a.bits.length > bits.length) { + this.bits.length = a.bits.length; + } + var min:int = Math.min(bits.length, a.bits.length); + for (var i:int = min - 1; i >= 0; i--) { + bits[i] |= a.bits[i]; + } + } + + /** + * Sets the size of a set. + * @param nwords how many words the new set should be + */ + private function set size(nwords:int):void { + bits.length = nwords; + } + + private static function bitMask(bitNumber:int):int { + var bitPosition:int = bitNumber & MOD_MASK; // bitNumber mod BITS + return 1 << bitPosition; + } + + public function clone():BitSet { + var s:BitSet = new BitSet(bits); + return s; + } + + public function get size():int { + var deg:uint = 0; + for (var i:int = bits.length - 1; i >= 0; i--) { + var word:uint = bits[i]; + if (word != 0) { + for (var bit:int = BITS - 1; bit >= 0; bit--) { + if ((word & (1 << bit)) != 0) { + deg++; + } + } + } + } + return deg; + } + + public function equals(other:Object):Boolean { + if ( other == null || !(other is BitSet) ) { + return false; + } + + var otherSet:BitSet = BitSet(other); + + var n:int = Math.min(this.bits.length, otherSet.bits.length); + + // for any bits in common, compare + for (var i:int=0; i<n; i++) { + if (this.bits[i] != otherSet.bits[i]) { + return false; + } + } + + // make sure any extra bits are off + + if (this.bits.length > n) { + for (i = n+1; i<this.bits.length; i++) { + if (this.bits[i] != 0) { + return false; + } + } + } + else if (otherSet.bits.length > n) { + for (i = n+1; i<otherSet.bits.length; i++) { + if (otherSet.bits[i] != 0) { + return false; + } + } + } + + return true; + } + + public function member(el:int):Boolean { + if ( el<0 ) { + return false; + } + var n:int = wordNumber(el); + if (n >= bits.length) return false; + return (bits[n] & bitMask(el)) != 0; + } + + // remove this element from this set + public function remove(el:int):void { + var n:int = wordNumber(el); + if (n < bits.length) { + bits[n] &= ~bitMask(el); + } + } + + public function get isNil():Boolean { + for (var i:int = bits.length - 1; i >= 0; i--) { + if (bits[i] != 0) return false; + } + return true; + } + + private final function numWordsToHold(el:int):int { + return (el >> LOG_BITS) + 1; + } + + public function get numBits():int { + return bits.length << LOG_BITS; // num words * bits per word + } + + /** return how much space is being used by the bits array not + * how many actually have member bits on. + */ + public function get lengthInLongWords():int { + return bits.length; + } + + public function toArray():Array { + var elems:Array = new Array[this.bits.length]; + var en:int = 0; + for (var i:int = 0; i < (bits.length << LOG_BITS); i++) { + if (member(i)) { + elems[en++] = i; + } + } + return elems; + } + + public function toPackedArray():Array { + return bits; + } + + private static function wordNumber(bit:uint):uint { + return bit >> LOG_BITS; // bit / BITS + } + + public function toString():String { + return toStringFromTokens(null); + } + + public function toStringFromTokens(tokenNames:Array):String { + var buf:String = ""; + const separator:String = ","; + var havePrintedAnElement:Boolean = false; + buf = buf + '{'; + + for (var i:int = 0; i < (bits.length << LOG_BITS); i++) { + if (member(i)) { + if (i > 0 && havePrintedAnElement ) { + buf += separator; + } + if ( tokenNames!=null ) { + buf += tokenNames[i]; + } + else { + buf += i; + } + havePrintedAnElement = true; + } + } + buf += '}'; + return buf; + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/CharStream.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/CharStream.as new file mode 100644 index 0000000..ceca043 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/CharStream.as @@ -0,0 +1,57 @@ +/* + [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. +*/ +package org.antlr.runtime { + /** A source of characters for an ANTLR lexer */ + public interface CharStream extends IntStream { + + /** For infinite streams, you don't need this; primarily I'm providing + * a useful interface for action code. Just make sure actions don't + * use this on streams that don't support it. + */ + function substring(start:int, stop:int):String; + + /** Get the ith character of lookahead. This is the same usually as + * LA(i). This will be used for labels in the generated + * lexer code. I'd prefer to return a char here type-wise, but it's + * probably better to be 32-bit clean and be consistent with LA. + */ + function LT(i:int):int; + + /** ANTLR tracks the line information automatically */ + function get line():int; + + /** Because this stream can rewind, we need to be able to reset the line */ + function set line(line:int):void; + + function set charPositionInLine(pos:int):void; + + /** The index of the character relative to the beginning of the line 0..n-1 */ + function get charPositionInLine():int; + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/CharStreamConstants.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/CharStreamConstants.as new file mode 100644 index 0000000..b13246a --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/CharStreamConstants.as @@ -0,0 +1,7 @@ +package org.antlr.runtime +{ + public class CharStreamConstants + { + public static const EOF:int = -1; + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/CharStreamState.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/CharStreamState.as new file mode 100644 index 0000000..21e053b --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/CharStreamState.as @@ -0,0 +1,47 @@ +/* + [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. +*/ +package org.antlr.runtime { + + /** When walking ahead with cyclic DFA or for syntactic predicates, + * we need to record the state of the input stream (char index, + * line, etc...) so that we can rewind the state after scanning ahead. + * + * This is the complete state of a stream. + */ + public class CharStreamState { + /** Index into the char stream of next lookahead char */ + public var p:int; + + /** What line number is the scanner at before processing buffer[p]? */ + public var line:int; + + /** What char position 0..n-1 in line is scanner before processing buffer[p]? */ + public var charPositionInLine:int; + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/CommonToken.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/CommonToken.as new file mode 100644 index 0000000..72845c2 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/CommonToken.as @@ -0,0 +1,183 @@ +/* + [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. +*/ +package org.antlr.runtime { + + + +public class CommonToken implements Token { + protected var _type:int; + protected var _line:int; + protected var _charPositionInLine:int = -1; // set to invalid position + protected var _channel:int = TokenConstants.DEFAULT_CHANNEL; + protected var _input:CharStream; + + /** We need to be able to change the text once in a while. If + * this is non-null, then getText should return this. Note that + * start/stop are not affected by changing this. + */ + protected var _text:String; + + /** What token number is this from 0..n-1 tokens; < 0 implies invalid index */ + protected var _index:int = -1; + + /** The char position into the input buffer where this token starts */ + protected var _start:int; + + /** The char position into the input buffer where this token stops */ + protected var _stop:int; + + public function CommonToken(type:int, text:String = null) { + this._type = type; + this._text = text; + } + + public static function createFromStream(input:CharStream, type:int, channel:int, start:int, stop:int):CommonToken { + var token:CommonToken = new CommonToken(type); + token._input = input; + token._channel = channel; + token._start = start; + token._stop = stop; + return token; + } + + public static function cloneToken(oldToken:Token):CommonToken { + var token:CommonToken = new CommonToken(oldToken.type, oldToken.text); + token._line = oldToken.line; + token._index = oldToken.tokenIndex; + token._charPositionInLine = oldToken.charPositionInLine; + token._channel = oldToken.channel; + if ( oldToken is CommonToken ) { + token._start = CommonToken(oldToken).startIndex; + token._stop = CommonToken(oldToken).stopIndex; + } + return token; + } + + public function get type():int { + return _type; + } + + public function set line(line:int):void { + _line = line; + } + + public function get text():String { + if ( _text!=null ) { + return _text; + } + if ( _input==null ) { + return null; + } + _text = _input.substring(_start, _stop); + return _text; + } + + /** Override the text for this token. getText() will return this text + * rather than pulling from the buffer. Note that this does not mean + * that start/stop indexes are not valid. It means that that input + * was converted to a new string in the token object. + */ + public function set text(text:String):void { + _text = text; + } + + public function get line():int { + return _line; + } + + public function get charPositionInLine():int { + return _charPositionInLine; + } + + public function set charPositionInLine(charPositionInLine:int):void { + _charPositionInLine = charPositionInLine; + } + + public function get channel():int { + return _channel; + } + + public function set channel(channel:int):void { + _channel = channel; + } + + public function set type(type:int):void { + _type = type; + } + + public function get startIndex():int { + return _start; + } + + public function set startIndex(start:int):void { + _start = start; + } + + public function get stopIndex():int { + return _stop; + } + + public function set stopIndex(stop:int):void { + _stop = stop; + } + + public function get tokenIndex():int { + return _index; + } + + public function set tokenIndex(index:int):void { + _index = index; + } + + public function get inputStream():CharStream { + return _input; + } + + public function set inputStream(input:CharStream):void { + _input = input; + } + + public function toString():String { + var channelStr:String = ""; + if ( channel>0 ) { + channelStr=",channel="+channel; + } + var txt:String = text; + if ( txt!=null ) { + txt = txt.replace("\n", "\\\\n"); + txt = txt.replace("\r", "\\\\r"); + txt = txt.replace("\t", "\\\\t"); + } + else { + txt = "<no text>"; + } + return "[@"+tokenIndex+","+startIndex+":"+stopIndex+"='"+txt+"',<"+type+">"+channelStr+","+line+":"+charPositionInLine+"]"; + } +} + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/CommonTokenStream.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/CommonTokenStream.as new file mode 100644 index 0000000..afebc09 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/CommonTokenStream.as @@ -0,0 +1,371 @@ +/* + [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. +*/ +package org.antlr.runtime { + + /** The most common stream of tokens is one where every token is buffered up + * and tokens are prefiltered for a certain channel (the parser will only + * see these tokens and cannot change the filter channel number during the + * parse). + * + * TODO: how to access the full token stream? How to track all tokens matched per rule? + */ + public class CommonTokenStream implements TokenStream { + protected var _tokenSource:TokenSource; + + /** Record every single token pulled from the source so we can reproduce + * chunks of it later. + */ + protected var tokens:Array = new Array(); + + /** Map<tokentype, channel> to override some Tokens' channel numbers */ + protected var channelOverrideMap:Array; + + /** Set<tokentype>; discard any tokens with this type */ + protected var discardSet:Array; + + /** Skip tokens on any channel but this one; this is how we skip whitespace... */ + protected var channel:int = TokenConstants.DEFAULT_CHANNEL; + + /** By default, track all incoming tokens */ + protected var _discardOffChannelTokens:Boolean = false; + + /** Track the last mark() call result value for use in rewind(). */ + protected var lastMarker:int; + + /** The index into the tokens list of the current token (next token + * to consume). p==-1 indicates that the tokens list is empty + */ + protected var p:int = -1; + + public function CommonTokenStream(tokenSource:TokenSource = null, channel:int = TokenConstants.DEFAULT_CHANNEL) { + _tokenSource = tokenSource; + this.channel = channel; + } + + /** Reset this token stream by setting its token source. */ + public function set tokenSource(tokenSource:TokenSource):void { + _tokenSource = tokenSource; + tokens.clear(); + p = -1; + channel = TokenConstants.DEFAULT_CHANNEL; + } + + /** Load all tokens from the token source and put in tokens. + * This is done upon first LT request because you might want to + * set some token type / channel overrides before filling buffer. + */ + protected function fillBuffer():void { + var index:int = 0; + var t:Token = tokenSource.nextToken(); + while ( t!=null && t.type != CharStreamConstants.EOF ) { + var discard:Boolean = false; + // is there a channel override for token type? + if ( channelOverrideMap != null ) { + if (channelOverrideMap[t.type] != undefined) { + t.channel = channelOverrideMap[t.type]; + } + } + if ( discardSet !=null && + discardSet[t.type] == true ) + { + discard = true; + } + else if ( _discardOffChannelTokens && t.channel != this.channel ) { + discard = true; + } + if ( !discard ) { + t.tokenIndex = index; + tokens.push(t); + index++; + } + t = tokenSource.nextToken(); + } + // leave p pointing at first token on channel + p = 0; + p = skipOffTokenChannels(p); + } + + /** Move the input pointer to the next incoming token. The stream + * must become active with LT(1) available. consume() simply + * moves the input pointer so that LT(1) points at the next + * input symbol. Consume at least one token. + * + * Walk past any token not on the channel the parser is listening to. + */ + public function consume():void { + if ( p<tokens.length ) { + p++; + p = skipOffTokenChannels(p); // leave p on valid token + } + } + + /** Given a starting index, return the index of the first on-channel + * token. + */ + protected function skipOffTokenChannels(i:int):int { + var n:int = tokens.length; + while ( i<n && (Token(tokens[i])).channel != channel) { + i++; + } + return i; + } + + protected function skipOffTokenChannelsReverse(i:int):int { + while ( i>= 0 && (Token(tokens[i])).channel != channel) { + i--; + } + return i; + } + + /** A simple filter mechanism whereby you can tell this token stream + * to force all tokens of type ttype to be on channel. For example, + * when interpreting, we cannot exec actions so we need to tell + * the stream to force all WS and NEWLINE to be a different, ignored + * channel. + */ + public function setTokenTypeChannel(ttype:int, channel:int):void { + if ( channelOverrideMap==null ) { + channelOverrideMap = new Array(); + } + channelOverrideMap[ttype] = channel; + } + + public function discardTokenType(ttype:int):void { + if ( discardSet==null ) { + discardSet = new Array(); + } + discardSet[ttype] = true; + } + + public function discardOffChannelTokens(discardOffChannelTokens:Boolean):void { + _discardOffChannelTokens = discardOffChannelTokens; + } + + public function getTokens():Array { + if ( p == -1 ) { + fillBuffer(); + } + return tokens; + } + + public function getTokensRange(start:int, stop:int):Array { + return getTokensBitSet(start, stop, null); + } + + /** Given a start and stop index, return a List of all tokens in + * the token type BitSet. Return null if no tokens were found. This + * method looks at both on and off channel tokens. + * + * Renamed from getTokens + */ + public function getTokensBitSet(start:int, stop:int, types:BitSet):Array { + if ( p == -1 ) { + fillBuffer(); + } + if ( stop>=tokens.length ) { + stop=tokens.length-1; + } + if ( start<0 ) { + start=0; + } + if ( start>stop ) { + return null; + } + + // list = tokens[start:stop]:{Token t, t.getType() in types} + var filteredTokens:Array = new Array(); + for (var i:int=start; i<=stop; i++) { + var t:Token = Token(tokens[i]); + if ( types==null || types.member(t.type) ) { + filteredTokens.push(t); + } + } + if ( filteredTokens.length==0 ) { + filteredTokens = null; + } + return filteredTokens; + } + + public function getTokensArray(start:int, stop:int, types:Array):Array { + return getTokensBitSet(start,stop,new BitSet(types)); + } + + public function getTokensInt(start:int, stop:int, ttype:int):Array { + return getTokensBitSet(start,stop,BitSet.of(ttype)); + } + + /** Get the ith token from the current position 1..n where k=1 is the + * first symbol of lookahead. + */ + public function LT(k:int):Token { + if ( p == -1 ) { + fillBuffer(); + } + if ( k==0 ) { + return null; + } + if ( k<0 ) { + return LB(-k); + } + //System.out.print("LT(p="+p+","+k+")="); + if ( (p+k-1) >= tokens.length ) { + return TokenConstants.EOF_TOKEN; + } + //System.out.println(tokens.get(p+k-1)); + var i:int = p; + var n:int = 1; + // find k good tokens + while ( n<k ) { + // skip off-channel tokens + i = skipOffTokenChannels(i+1); // leave p on valid token + n++; + } + if ( i>=tokens.length ) { + return TokenConstants.EOF_TOKEN; + } + return Token(tokens[i]); + } + + /** Look backwards k tokens on-channel tokens */ + protected function LB(k:int):Token { + //System.out.print("LB(p="+p+","+k+") "); + if ( p == -1 ) { + fillBuffer(); + } + if ( k==0 ) { + return null; + } + if ( (p-k)<0 ) { + return null; + } + + var i:int = p; + var n:int = 1; + // find k good tokens looking backwards + while ( n<=k ) { + // skip off-channel tokens + i = skipOffTokenChannelsReverse(i-1); // leave p on valid token + n++; + } + if ( i<0 ) { + return null; + } + return Token(tokens[i]); + } + + /** Return absolute token i; ignore which channel the tokens are on; + * that is, count all tokens not just on-channel tokens. + */ + public function getToken(i:int):Token { + return Token(tokens[i]); + } + + public function LA(i:int):int { + return LT(i).type; + } + + public function mark():int { + if ( p == -1 ) { + fillBuffer(); + } + lastMarker = index; + return lastMarker; + } + + public function release(marker:int):void { + // no resources to release + } + + public function get size():int { + return tokens.length; + } + + public function get index():int { + return p; + } + + public function reset():void { + p = 0; + lastMarker = 0; + } + + public function rewindTo(marker:int):void { + seek(marker); + } + + public function rewind():void { + seek(lastMarker); + } + + public function seek(index:int):void { + p = index; + } + + public function get tokenSource():TokenSource { + return _tokenSource; + } + + public function get sourceName():String { + return tokenSource.sourceName; + } + + public function toString():String { + if ( p == -1 ) { + fillBuffer(); + } + return toStringWithRange(0, tokens.length-1); + } + + public function toStringWithRange(start:int, stop:int):String { + if ( start<0 || stop<0 ) { + return null; + } + if ( p == -1 ) { + fillBuffer(); + } + if ( stop>=tokens.length ) { + stop = tokens.length-1; + } + var buf:String = ""; + for (var i:int = start; i <= stop; i++) { + var t:Token = Token(tokens[i]); + buf += t.text; + } + return buf.toString(); + } + + public function toStringWithTokenRange(start:Token, stop:Token):String { + if ( start!=null && stop!=null ) { + return toStringWithRange(start.tokenIndex, stop.tokenIndex); + } + return null; + } + } + + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/DFA.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/DFA.as new file mode 100644 index 0000000..fc471a7 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/DFA.as @@ -0,0 +1,225 @@ +package org.antlr.runtime { + + /** A DFA implemented as a set of transition tables. + * + * Any state that has a semantic predicate edge is special; those states + * are generated with if-then-else structures in a specialStateTransition() + * which is generated by cyclicDFA template. + * + * There are at most 32767 states (16-bit signed short). + * Could get away with byte sometimes but would have to generate different + * types and the simulation code too. For a point of reference, the Java + * lexer's Tokens rule DFA has 326 states roughly. + */ + public class DFA { + protected var eot:Array; // short[] + protected var eof:Array; // short[] + protected var min:Array; // char[] + protected var max:Array; // char[] + protected var accept:Array; //short[] + protected var special:Array; // short[] + protected var transition:Array; // short[][] + + protected var decisionNumber:int; + + /** Which recognizer encloses this DFA? Needed to check backtracking */ + protected var recognizer:BaseRecognizer; + + private var _description:String; + + public static const debug:Boolean = false; + + public function DFA(recognizer:BaseRecognizer, decisionNumber:int, description:String, + eot:Array, eof:Array, min:Array, max:Array, accept:Array, special:Array, transition:Array, + specialStateTransitionFunction:Function = null, errorFunction:Function = null) { + this.recognizer = recognizer; + this.decisionNumber = decisionNumber; + this._description = description; + + this.eot = eot; + this.eof = eof; + this.min = min; + this.max = max; + this.accept = accept; + this.special = special; + this.transition = transition; + + if (specialStateTransitionFunction != null) { + specialStateTransition = specialStateTransitionFunction; + } + + if (errorFunction != null) { + error = errorFunction; + } + + } + /** From the input stream, predict what alternative will succeed + * using this DFA (representing the covering regular approximation + * to the underlying CFL). Return an alternative number 1..n. Throw + * an exception upon error. + */ + public function predict(input:IntStream):int { + if ( debug ) { + trace("Enter DFA.predict for decision "+decisionNumber); + } + + var mark:int = input.mark(); // remember where decision started in input + var s:int = 0; // we always start at s0 + try { + while ( true ) { + if ( debug ) trace("DFA "+decisionNumber+" state "+s+" LA(1)="+String.fromCharCode(input.LA(1))+"("+input.LA(1)+ + "), index="+input.index); + var specialState:int = special[s]; + if ( specialState>=0 ) { + if ( debug ) { + trace("DFA "+decisionNumber+ + " state "+s+" is special state "+specialState); + } + s = specialStateTransition(this, specialState,input); + if ( debug ) { + trace("DFA "+decisionNumber+ + " returns from special state "+specialState+" to "+s); + } + if ( s==-1 ) { + noViableAlt(s,input); + return 0; + } + input.consume(); + continue; + } + if ( accept[s] >= 1 ) { + if ( debug ) trace("accept; predict "+accept[s]+" from state "+s); + return accept[s]; + } + // look for a normal char transition + var c:int = input.LA(1); // -1 == \uFFFF, all tokens fit in 65000 space + if (c>=min[s] && c<=max[s]) { + var snext:int = transition[s][c-min[s]]; // move to next state + if ( snext < 0 ) { + // was in range but not a normal transition + // must check EOT, which is like the else clause. + // eot[s]>=0 indicates that an EOT edge goes to another + // state. + if ( eot[s]>=0 ) { // EOT Transition to accept state? + if ( debug ) trace("EOT transition"); + s = eot[s]; + input.consume(); + // TODO: I had this as return accept[eot[s]] + // which assumed here that the EOT edge always + // went to an accept...faster to do this, but + // what about predicated edges coming from EOT + // target? + continue; + } + noViableAlt(s,input); + return 0; + } + s = snext; + input.consume(); + continue; + } + if ( eot[s]>=0 ) { // EOT Transition? + if ( debug ) trace("EOT transition"); + s = eot[s]; + input.consume(); + continue; + } + if ( c==TokenConstants.EOF && eof[s]>=0 ) { // EOF Transition to accept state? + if ( debug ) trace("accept via EOF; predict "+accept[eof[s]]+" from "+eof[s]); + return accept[eof[s]]; + } + // not in range and not EOF/EOT, must be invalid symbol + if ( debug ) { + trace("min["+s+"]="+min[s]); + trace("max["+s+"]="+max[s]); + trace("eot["+s+"]="+eot[s]); + trace("eof["+s+"]="+eof[s]); + for (var p:int=0; p<transition[s].length; p++) { + trace(transition[s][p]+" "); + } + trace(); + } + noViableAlt(s,input); + return 0; + } + } + finally { + input.rewindTo(mark); + } + // not reached -- added due to bug in Flex compiler reachability analysis of while loop with no breaks + return -1; + } + + protected function noViableAlt(s:int, input:IntStream):void { + if (recognizer.state.backtracking>0) { + recognizer.state.failed=true; + return; + } + var nvae:NoViableAltException = + new NoViableAltException(description, + decisionNumber, + s, + input); + error(nvae); + throw nvae; + } + + /** A hook for debugging interface */ + public var error:Function = function(nvae:NoViableAltException):NoViableAltException { return nvae; } + + public var specialStateTransition:Function = function(dfa:DFA, s:int, input:IntStream):int { + return -1; + } + + public function get description():String { + return _description; + } + + /** Given a String that has a run-length-encoding of some unsigned shorts + * like "\1\2\3\9", convert to short[] {2,9,9,9}. We do this to avoid + * static short[] which generates so much init code that the class won't + * compile. :( + */ + public static function unpackEncodedString(encodedString:String, unsigned:Boolean = false):Array { + // walk first to find how big it is. + /* Don't pre-allocate + var size:int = 0; + for (var i:int=0; i<encodedString.length; i+=2) { + size += encodedString.charCodeAt(i); + } + */ + var data:Array = new Array(); + var di:int = 0; + for (var i:int=0; i<encodedString.length; i+=2) { + var n:int = encodedString.charCodeAt(i); + if (n > 0x8000) { + // need to read another byte + i++; + var lowBits:int = encodedString.charCodeAt(i); + n &= 0xff; + n <<= 8; + n |= lowBits; + } + var v:int = encodedString.charCodeAt(i+1); + if (v > 0x8000) { + // need to read another byte + i++; + lowBits = encodedString.charCodeAt(i); + v &= 0xff; + v <<= 8; + v |= lowBits; + } + if (!unsigned && v > 0x7fff) { + v = -(0xffff - v + 1); + } + // add v n times to data + for (var j:int=1; j<=n; j++) { + data[di++] = v; + } + } + return data; + } + + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/EarlyExitException.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/EarlyExitException.as new file mode 100644 index 0000000..0604efc --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/EarlyExitException.as @@ -0,0 +1,39 @@ +/* + [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. +*/ +package org.antlr.runtime +{ + /** The recognizer did not match anything for a (..)+ loop. */ + public class EarlyExitException extends RecognitionException { + public var decisionNumber:int; + + public function EarlyExitException(decisionNumber:int, input:IntStream) { + super(input); + this.decisionNumber = decisionNumber; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/FailedPredicateException.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/FailedPredicateException.as new file mode 100644 index 0000000..88713ff --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/FailedPredicateException.as @@ -0,0 +1,49 @@ +/* + [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. +*/ +package org.antlr.runtime { + + /** A semantic predicate failed during validation. Validation of predicates + * occurs when normally parsing the alternative just like matching a token. + * Disambiguating predicate evaluation occurs when we hoist a predicate into + * a prediction decision. + */ + public class FailedPredicateException extends RecognitionException { + public var ruleName:String; + public var predicateText:String; + + public function FailedPredicateException(input:IntStream, + ruleName:String, + predicateText:String) + { + super(input); + this.ruleName = ruleName; + this.predicateText = predicateText; + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/IntStream.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/IntStream.as new file mode 100644 index 0000000..71e193b --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/IntStream.as @@ -0,0 +1,122 @@ +/* + [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. +*/ +package org.antlr.runtime { + /** A simple stream of integers used when all I care about is the char + * or token type sequence (such as interpretation). + */ + public interface IntStream { + function consume():void; + + /** Get int at current input pointer + i ahead where i=1 is next int. + * Negative indexes are allowed. LA(-1) is previous token (token + * just matched). LA(-i) where i is before first token should + * yield -1, invalid char / EOF. + */ + function LA(i:int):int; + + /** Tell the stream to start buffering if it hasn't already. Return + * current input position, index(), or some other marker so that + * when passed to rewind() you get back to the same spot. + * rewind(mark()) should not affect the input cursor. The Lexer + * track line/col info as well as input index so its markers are + * not pure input indexes. Same for tree node streams. + */ + function mark():int; + + /** Return the current input symbol index 0..n where n indicates the + * last symbol has been read. The index is the symbol about to be + * read not the most recently read symbol. + */ + function get index():int; + + /** Reset the stream so that next call to index would return marker. + * The marker will usually be index() but it doesn't have to be. It's + * just a marker to indicate what state the stream was in. This is + * essentially calling release() and seek(). If there are markers + * created after this marker argument, this routine must unroll them + * like a stack. Assume the state the stream was in when this marker + * was created. + */ + function rewindTo(marker:int):void; + + /** Rewind to the input position of the last marker. + * Used currently only after a cyclic DFA and just + * before starting a sem/syn predicate to get the + * input position back to the start of the decision. + * Do not "pop" the marker off the state. mark(i) + * and rewind(i) should balance still. It is + * like invoking rewind(last marker) but it should not "pop" + * the marker off. It's like seek(last marker's input position). + */ + function rewind():void; + + /** You may want to commit to a backtrack but don't want to force the + * stream to keep bookkeeping objects around for a marker that is + * no longer necessary. This will have the same behavior as + * rewind() except it releases resources without the backward seek. + * This must throw away resources for all markers back to the marker + * argument. So if you're nested 5 levels of mark(), and then release(2) + * you have to release resources for depths 2..5. + */ + function release(marker:int):void; + + /** Set the input cursor to the position indicated by index. This is + * normally used to seek ahead in the input stream. No buffering is + * required to do this unless you know your stream will use seek to + * move backwards such as when backtracking. + * + * This is different from rewind in its multi-directional + * requirement and in that its argument is strictly an input cursor (index). + * + * For char streams, seeking forward must update the stream state such + * as line number. For seeking backwards, you will be presumably + * backtracking using the mark/rewind mechanism that restores state and + * so this method does not need to update state when seeking backwards. + * + * Currently, this method is only used for efficient backtracking using + * memoization, but in the future it may be used for incremental parsing. + * + * The index is 0..n-1. A seek to position i means that LA(1) will + * return the ith symbol. So, seeking to 0 means LA(1) will return the + * first element in the stream. + */ + function seek(index:int):void; + + /** Only makes sense for streams that buffer everything up probably, but + * might be useful to display the entire stream or for testing. This + * value includes a single EOF. + */ + function get size():int; + + /** Where are you getting symbols from? Normally, implementations will + * pass the buck all the way to the lexer who can ask its input stream + * for the file name or whatever. + */ + function get sourceName():String; + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/Lexer.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/Lexer.as new file mode 100644 index 0000000..a1c4937 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/Lexer.as @@ -0,0 +1,323 @@ +/* + [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. +*/ +package org.antlr.runtime { + + /** A lexer is recognizer that draws input symbols from a character stream. + * lexer grammars result in a subclass of this object. A Lexer object + * uses simplified match() and error recovery mechanisms in the interest + * of speed. + */ + public class Lexer extends BaseRecognizer implements TokenSource { + /** Where is the lexer drawing characters from? */ + protected var input:CharStream; + + public function Lexer(input:CharStream = null, state:RecognizerSharedState = null) { + super(state); + this.input = input; + } + + public override function reset():void { + super.reset(); // reset all recognizer state variables + // wack Lexer state variables + if ( input!=null ) { + input.seek(0); // rewind the input + } + if ( state==null ) { + return; // no shared state work to do + } + state.token = null; + state.type = TokenConstants.INVALID_TOKEN_TYPE; + state.channel = TokenConstants.DEFAULT_CHANNEL; + state.tokenStartCharIndex = -1; + state.tokenStartCharPositionInLine = -1; + state.tokenStartLine = -1; + state.text = null; + } + + /** Return a token from this source; i.e., match a token on the char + * stream. + */ + public function nextToken():Token { + while (true) { + state.token = null; + state.channel = TokenConstants.DEFAULT_CHANNEL; + state.tokenStartCharIndex = input.index; + state.tokenStartCharPositionInLine = input.charPositionInLine; + state.tokenStartLine = input.line; + state.text = null; + if ( input.LA(1)==CharStreamConstants.EOF ) { + return TokenConstants.EOF_TOKEN; + } + try { + mTokens(); + if ( state.token==null ) { + emit(); + } + else if ( state.token==TokenConstants.SKIP_TOKEN ) { + continue; + } + return state.token; + } + catch (nva:NoViableAltException) { + reportError(nva); + recover(nva); // throw out current char and try again + } + catch (re:RecognitionException) { + reportError(re); + // match() routine has already called recover() + } + } + // Can't happen, but will quiet complier error + return null; + } + + /** Instruct the lexer to skip creating a token for current lexer rule + * and look for another token. nextToken() knows to keep looking when + * a lexer rule finishes with token set to SKIP_TOKEN. Recall that + * if token==null at end of any token rule, it creates one for you + * and emits it. + */ + public function skip():void { + state.token = TokenConstants.SKIP_TOKEN; + } + + /** This is the lexer entry point that sets instance var 'token' */ + public function mTokens():void { + // abstract function + throw new Error("Not implemented"); + } + + /** Set the char stream and reset the lexer */ + public function set charStream(input:CharStream):void { + this.input = null; + reset(); + this.input = input; + } + + public function get charStream():CharStream { + return input; + } + + public override function get sourceName():String { + return input.sourceName; + } + + /** Currently does not support multiple emits per nextToken invocation + * for efficiency reasons. Subclass and override this method and + * nextToken (to push tokens into a list and pull from that list rather + * than a single variable as this implementation does). + */ + public function emitToken(token:Token):void { + state.token = token; + } + + /** The standard method called to automatically emit a token at the + * outermost lexical rule. The token object should point into the + * char buffer start..stop. If there is a text override in 'text', + * use that to set the token's text. Override this method to emit + * custom Token objects. + */ + public function emit():Token { + var t:Token = CommonToken.createFromStream(input, state.type, state.channel, state.tokenStartCharIndex, charIndex - 1); + t.line = state.tokenStartLine; + t.text = state.text; + t.charPositionInLine = state.tokenStartCharPositionInLine; + emitToken(t); + return t; + } + + public function matchString(s:String):void { + var i:int = 0; + while ( i<s.length ) { + if ( input.LA(1) != s.charCodeAt(i) ) { + if ( state.backtracking>0 ) { + state.failed = true; + return; + } + var mte:MismatchedTokenException = + new MismatchedTokenException(s.charCodeAt(i), input); + recover(mte); + throw mte; + } + i++; + input.consume(); + state.failed = false; + } + } + + public function matchAny():void { + input.consume(); + } + + public function match(c:int):void { + if ( input.LA(1)!=c ) { + if ( state.backtracking>0 ) { + state.failed = true; + return; + } + var mte:MismatchedTokenException = + new MismatchedTokenException(c, input); + recover(mte); // don't really recover; just consume in lexer + throw mte; + } + input.consume(); + state.failed = false; + } + + public function matchRange(a:int, b:int):void + { + if ( input.LA(1)<a || input.LA(1)>b ) { + if ( state.backtracking>0 ) { + state.failed = true; + return; + } + var mre:MismatchedRangeException = + new MismatchedRangeException(a,b,input); + recover(mre); + throw mre; + } + input.consume(); + state.failed = false; + } + + public function get line():int { + return input.line; + } + + public function get charPositionInLine():int { + return input.charPositionInLine; + } + + /** What is the index of the current character of lookahead? */ + public function get charIndex():int { + return input.index; + } + + /** Return the text matched so far for the current token or any + * text override. + */ + public function get text():String { + if ( state.text!=null ) { + return state.text; + } + return input.substring(state.tokenStartCharIndex, charIndex-1); + } + + /** Set the complete text of this token; it wipes any previous + * changes to the text. + */ + public function set text(text:String):void { + state.text = text; + } + + public override function reportError(e:RecognitionException):void { + displayRecognitionError(this.tokenNames, e); + } + + public override function getErrorMessage(e:RecognitionException, tokenNames:Array):String { + var msg:String = null; + if ( e is MismatchedTokenException ) { + var mte:MismatchedTokenException = MismatchedTokenException(e); + msg = "mismatched character "+getCharErrorDisplay(e.c)+" expecting "+getCharErrorDisplay(mte.expecting); + } + else if ( e is NoViableAltException ) { + var nvae:NoViableAltException = NoViableAltException(e); + // for development, can add "decision=<<"+nvae.grammarDecisionDescription+">>" + // and "(decision="+nvae.decisionNumber+") and + // "state "+nvae.stateNumber + msg = "no viable alternative at character "+getCharErrorDisplay(e.c); + } + else if ( e is EarlyExitException ) { + var eee:EarlyExitException = EarlyExitException(e); + // for development, can add "(decision="+eee.decisionNumber+")" + msg = "required (...)+ loop did not match anything at character "+getCharErrorDisplay(e.c); + } + else if ( e is MismatchedNotSetException ) { + var mnse:MismatchedNotSetException = MismatchedNotSetException(e); + msg = "mismatched character "+getCharErrorDisplay(e.c)+" expecting set "+mnse.expecting; + } + else if ( e is MismatchedSetException ) { + var mse:MismatchedSetException = MismatchedSetException(e); + msg = "mismatched character "+getCharErrorDisplay(e.c)+" expecting set "+mse.expecting; + } + else if ( e is MismatchedRangeException ) { + var mre:MismatchedRangeException = MismatchedRangeException(e); + msg = "mismatched character "+getCharErrorDisplay(e.c)+" expecting set "+ + getCharErrorDisplay(mre.a)+".."+getCharErrorDisplay(mre.b); + } + else { + msg = super.getErrorMessage(e, tokenNames); + } + return msg; + } + + public function getCharErrorDisplay(c:int):String { + var s:String = String.fromCharCode(c); + switch ( c ) { + case TokenConstants.EOF : + s = "<EOF>"; + break; + case '\n' : + s = "\\n"; + break; + case '\t' : + s = "\\t"; + break; + case '\r' : + s = "\\r"; + break; + } + return "'"+s+"'"; + } + + /** Lexers can normally match any char in it's vocabulary after matching + * a token, so do the easy thing and just kill a character and hope + * it all works out. You can instead use the rule invocation stack + * to do sophisticated error recovery if you are in a fragment rule. + * + * @return This method should return the exception it was provided as an + * argument. This differs from the Java runtime so that an exception variable + * does not need to be declared in the generated code, thus reducing a large + * number of compiler warnings in generated code. + */ + public function recover(re:RecognitionException):RecognitionException { + input.consume(); + return re; + } + + public function traceIn(ruleName:String, ruleIndex:int):void { + var inputSymbol:String = String.fromCharCode(input.LT(1))+" line="+ line +":"+ charPositionInLine; + super.traceInSymbol(ruleName, ruleIndex, inputSymbol); + } + + public function traceOut(ruleName:String, ruleIndex:int):void { + var inputSymbol:String = String.fromCharCode(input.LT(1))+" line="+ line +":"+ charPositionInLine; + super.traceOutSymbol(ruleName, ruleIndex, inputSymbol); + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MismatchedNotSetException.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MismatchedNotSetException.as new file mode 100644 index 0000000..0e4bb9c --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MismatchedNotSetException.as @@ -0,0 +1,38 @@ +/* + [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. +*/ +package org.antlr.runtime { + + public class MismatchedNotSetException extends MismatchedSetException { + + public function MismatchedNotSetException(expecting:BitSet, input:IntStream) { + super(expecting, input); + } + + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MismatchedRangeException.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MismatchedRangeException.as new file mode 100644 index 0000000..98864bd --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MismatchedRangeException.as @@ -0,0 +1,44 @@ +/* + [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. +*/ +package org.antlr.runtime +{ + public class MismatchedRangeException extends RecognitionException { + public var a:int, b:int; + + public function MismatchedRangeException(a:int, b:int, input:IntStream) { + super(input); + this.a = a; + this.b = b; + } + + public function toString():String { + return "MismatchedNotSetException("+unexpectedType+" not in ["+a+","+b+"])"; + } + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MismatchedSetException.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MismatchedSetException.as new file mode 100644 index 0000000..bc0d20f --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MismatchedSetException.as @@ -0,0 +1,42 @@ +/* + [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. +*/ +package org.antlr.runtime { + + public class MismatchedSetException extends RecognitionException { + public var expecting:BitSet; + + public function MismatchedSetException(expecting:BitSet, input:IntStream) { + super(input); + this.expecting = expecting; + } + + public function toString():String { + return "MismatchedSetException("+unexpectedType+"!="+expecting+")"; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MismatchedTokenException.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MismatchedTokenException.as new file mode 100644 index 0000000..b7fb324 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MismatchedTokenException.as @@ -0,0 +1,45 @@ +/* + [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. +*/ +package org.antlr.runtime { + + /** A mismatched char or Token or tree node */ + public class MismatchedTokenException extends RecognitionException { + public var expecting:int = TokenConstants.INVALID_TOKEN_TYPE; + + public function MismatchedTokenException(expecting:int, input:IntStream) { + super(input); + this.expecting = expecting; + } + + public function toString():String { + return "MismatchedTokenException("+unexpectedType+"!="+expecting+")"; + } + } + + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MismatchedTreeNodeException.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MismatchedTreeNodeException.as new file mode 100644 index 0000000..c008531 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MismatchedTreeNodeException.as @@ -0,0 +1,16 @@ +package org.antlr.runtime { + import org.antlr.runtime.tree.TreeNodeStream; + + public class MismatchedTreeNodeException extends RecognitionException { + public var expecting:int; + + public function MismatchedTreeNodeException(expecting:int, input:TreeNodeStream) { + super(input); + this.expecting = expecting; + } + + public function toString():String { + return "MismatchedTreeNodeException("+unexpectedType+"!="+expecting+")"; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MissingTokenException.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MissingTokenException.as new file mode 100644 index 0000000..fe1abf0 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/MissingTokenException.as @@ -0,0 +1,28 @@ +package org.antlr.runtime { + + public class MissingTokenException extends MismatchedTokenException { + + public var inserted:Object; + + public function MissingTokenException(expecting:int, input:IntStream, inserted:Object) { + super(expecting, input); + this.inserted = inserted; + } + + public function get missingType():int { + return expecting; + } + + public override function toString():String { + if ( inserted!=null && token!=null ) { + return "MissingTokenException(inserted "+inserted+" at "+token.text+")"; + } + if ( token!=null ) { + return "MissingTokenException(at "+token.text+")"; + } + return "MissingTokenException"; + } + + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/NoViableAltException.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/NoViableAltException.as new file mode 100644 index 0000000..80e7a67 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/NoViableAltException.as @@ -0,0 +1,56 @@ +/* + [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. +*/ +package org.antlr.runtime { + + public class NoViableAltException extends RecognitionException { + public var grammarDecisionDescription:String; + public var decisionNumber:int; + public var stateNumber:int; + + public function NoViableAltException(grammarDecisionDescription:String, + decisionNumber:int, + stateNumber:int, + input:IntStream) + { + super(input); + this.grammarDecisionDescription = grammarDecisionDescription; + this.decisionNumber = decisionNumber; + this.stateNumber = stateNumber; + } + + public function toString():String { + if ( input is CharStream ) { + return "NoViableAltException('"+String.fromCharCode(unexpectedType)+"'@["+grammarDecisionDescription+"])"; + } + else { + return "NoViableAltException("+unexpectedType+"@["+grammarDecisionDescription+"])"; + } + } + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/Parser.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/Parser.as new file mode 100644 index 0000000..c158337 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/Parser.as @@ -0,0 +1,104 @@ +/* + [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. +*/ +package org.antlr.runtime { + import org.antlr.runtime.tree.TreeAdaptor; + + /** A parser for TokenStreams. "parser grammars" result in a subclass + * of this. + */ + public class Parser extends BaseRecognizer { + protected var input:TokenStream; + + public function Parser(input:TokenStream, state:RecognizerSharedState = null) { + super(state); + tokenStream = input; + } + + public override function reset():void { + super.reset(); // reset all recognizer state variables + if ( input!=null ) { + input.seek(0); // rewind the input + } + } + + protected override function getCurrentInputSymbol(input:IntStream):Object { + return TokenStream(input).LT(1); + } + + protected override function getMissingSymbol(input:IntStream, + e:RecognitionException, + expectedTokenType:int, + follow:BitSet):Object { + var tokenText:String = null; + if ( expectedTokenType==TokenConstants.EOF ) tokenText = "<missing EOF>"; + else tokenText = "<missing "+tokenNames[expectedTokenType]+">"; + var t:CommonToken = new CommonToken(expectedTokenType, tokenText); + var current:Token = TokenStream(input).LT(1); + if ( current.type == TokenConstants.EOF ) { + current = TokenStream(input).LT(-1); + } + t.line = current.line; + t.charPositionInLine = current.charPositionInLine; + t.channel = DEFAULT_TOKEN_CHANNEL; + return t; + } + + /** Set the token stream and reset the parser */ + public function set tokenStream(input:TokenStream):void { + this.input = null; + reset(); + this.input = input; + } + + public function get tokenStream():TokenStream { + return input; + } + + public override function get sourceName():String { + return input.sourceName; + } + + public function set treeAdaptor(adaptor:TreeAdaptor):void { + // do nothing, implemented in generated code + } + + public function get treeAdaptor():TreeAdaptor { + // implementation provided in generated code + return null; + } + + public function traceIn(ruleName:String, ruleIndex:int):void { + super.traceInSymbol(ruleName, ruleIndex, input.LT(1)); + } + + public function traceOut(ruleName:String, ruleIndex:int):void { + super.traceOutSymbol(ruleName, ruleIndex, input.LT(1)); + } + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/ParserRuleReturnScope.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/ParserRuleReturnScope.as new file mode 100644 index 0000000..8131543 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/ParserRuleReturnScope.as @@ -0,0 +1,55 @@ +package org.antlr.runtime +{ + /** Rules that return more than a single value must return an object + * containing all the values. Besides the properties defined in + * RuleLabelScope.predefinedRulePropertiesScope there may be user-defined + * return values. This class simply defines the minimum properties that + * are always defined and methods to access the others that might be + * available depending on output option such as template and tree. + * + * Note text is not an actual property of the return value, it is computed + * from start and stop using the input stream's toString() method. I + * could add a ctor to this so that we can pass in and store the input + * stream, but I'm not sure we want to do that. It would seem to be undefined + * to get the .text property anyway if the rule matches tokens from multiple + * input streams. + * + * I do not use getters for fields of objects that are used simply to + * group values such as this aggregate. + */ + public class ParserRuleReturnScope extends RuleReturnScope { + private var _startToken:Token; + private var _stopToken:Token; + private var _tree:Object; // if output=AST this contains the tree + private var _values:Object = new Object(); // contains the return values + + public override function get start():Object { + return _startToken; + } + + public function set start(token:Object):void { + _startToken = Token(token); + } + + public override function get stop():Object { + return _stopToken; + } + + public function set stop(token:Object):void { + _stopToken = Token(token); + } + + /** Has a value potentially if output=AST; */ + public override function get tree():Object { + return _tree; + } + + public function set tree(tree:Object):void { + _tree = tree; + } + + public function get values():Object { + return _values; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/RecognitionException.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/RecognitionException.as new file mode 100644 index 0000000..7710b96 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/RecognitionException.as @@ -0,0 +1,183 @@ +/* + [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. +*/ +package org.antlr.runtime { + import org.antlr.runtime.tree.Tree; + import org.antlr.runtime.tree.TreeNodeStream; + import org.antlr.runtime.tree.TreeAdaptor; + import org.antlr.runtime.tree.CommonTree; + /** The root of the ANTLR exception hierarchy. + * + * To avoid English-only error messages and to generally make things + * as flexible as possible, these exceptions are not created with strings, + * but rather the information necessary to generate an error. Then + * the various reporting methods in Parser and Lexer can be overridden + * to generate a localized error message. For example, MismatchedToken + * exceptions are built with the expected token type. + * So, don't expect getMessage() to return anything. + * + * Note that as of Java 1.4, you can access the stack trace, which means + * that you can compute the complete trace of rules from the start symbol. + * This gives you considerable context information with which to generate + * useful error messages. + * + * ANTLR generates code that throws exceptions upon recognition error and + * also generates code to catch these exceptions in each rule. If you + * want to quit upon first error, you can turn off the automatic error + * handling mechanism using rulecatch action, but you still need to + * override methods mismatch and recoverFromMismatchSet. + * + * In general, the recognition exceptions can track where in a grammar a + * problem occurred and/or what was the expected input. While the parser + * knows its state (such as current input symbol and line info) that + * state can change before the exception is reported so current token index + * is computed and stored at exception time. From this info, you can + * perhaps print an entire line of input not just a single token, for example. + * Better to just say the recognizer had a problem and then let the parser + * figure out a fancy report. + */ + public class RecognitionException extends Error { + /** What input stream did the error occur in? */ + public var input:IntStream; + + /** What is index of token/char were we looking at when the error occurred? */ + public var index:int; + + /** The current Token when an error occurred. Since not all streams + * can retrieve the ith Token, we have to track the Token object. + * For parsers. Even when it's a tree parser, token might be set. + */ + public var token:Token; + + /** If this is a tree parser exception, node is set to the node with + * the problem. + */ + public var node:Object; + + /** The current char when an error occurred. For lexers. */ + public var c:int; + + /** Track the line at which the error occurred in case this is + * generated from a lexer. We need to track this since the + * unexpected char doesn't carry the line info. + */ + public var line:int; + + public var charPositionInLine:int; + + /** If you are parsing a tree node stream, you will encounter som + * imaginary nodes w/o line/col info. We now search backwards looking + * for most recent token with line/col info, but notify getErrorHeader() + * that info is approximate. + */ + public var approximateLineInfo:Boolean; + + public function RecognitionException(input:IntStream = null) { + if (input == null) { + return; + } + this.input = input; + this.index = input.index; + if ( input is TokenStream ) { + this.token = TokenStream(input).LT(1); + this.line = token.line; + this.charPositionInLine = token.charPositionInLine; + } + if ( input is TreeNodeStream ) { + extractInformationFromTreeNodeStream(input); + } + else if ( input is CharStream ) { + this.c = input.LA(1); + this.line = CharStream(input).line; + this.charPositionInLine = CharStream(input).charPositionInLine; + } + else { + this.c = input.LA(1); + } + } + + protected function extractInformationFromTreeNodeStream(input:IntStream):void { + var nodes:TreeNodeStream = TreeNodeStream(input); + this.node = nodes.LT(1); + var adaptor:TreeAdaptor = nodes.treeAdaptor; + var payload:Token = adaptor.getToken(node); + if ( payload!=null ) { + this.token = payload; + if ( payload.line<= 0 ) { + // imaginary node; no line/pos info; scan backwards + var i:int = -1; + var priorNode:Object = nodes.LT(i); + while ( priorNode!=null ) { + var priorPayload:Token = adaptor.getToken(priorNode); + if ( priorPayload!=null && priorPayload.line > 0 ) { + // we found the most recent real line / pos info + this.line = priorPayload.line; + this.charPositionInLine = priorPayload.charPositionInLine; + this.approximateLineInfo = true; + break; + } + --i; + priorNode = nodes.LT(i); + } + } + else { // node created from real token + this.line = payload.line; + this.charPositionInLine = payload.charPositionInLine; + } + } + else if ( this.node is Tree) { + this.line = this.node.line; + this.charPositionInLine = this.node.charPositionInLine; + if ( this.node is CommonTree) { + this.token = this.node.token; + } + } + else { + var type:int = adaptor.getType(this.node); + var text:String = adaptor.getText(this.node); + this.token = new CommonToken(type, text); + } + } + + /** Return the token type or char of the unexpected input element */ + public function get unexpectedType():int { + if ( input is TokenStream ) { + return token.type; + } + else if ( input is TreeNodeStream ) { + var nodes:TreeNodeStream = TreeNodeStream(input); + var adaptor:TreeAdaptor = nodes.treeAdaptor; + return adaptor.getType(node); + } + else { + return c; + } + + } + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/RecognizerSharedState.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/RecognizerSharedState.as new file mode 100644 index 0000000..fb505fa --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/RecognizerSharedState.as @@ -0,0 +1,93 @@ +package org.antlr.runtime { + + /** The set of fields needed by an abstract recognizer to recognize input + * and recover from errors etc... As a separate state object, it can be + * shared among multiple grammars; e.g., when one grammar imports another. + * + * These fields are publically visible but the actual state pointer per + * parser is protected. + */ + public class RecognizerSharedState { + /** Track the set of token types that can follow any rule invocation. + * Stack grows upwards. When it hits the max, it grows 2x in size + * and keeps going. + */ + public var following:Array = new Array(BaseRecognizer.INITIAL_FOLLOW_STACK_SIZE); + public var _fsp:int = -1; + + /** This is true when we see an error and before having successfully + * matched a token. Prevents generation of more than one error message + * per error. + */ + public var errorRecovery:Boolean = false; + + /** The index into the input stream where the last error occurred. + * This is used to prevent infinite loops where an error is found + * but no token is consumed during recovery...another error is found, + * ad naseum. This is a failsafe mechanism to guarantee that at least + * one token/tree node is consumed for two errors. + */ + public var lastErrorIndex:int = -1; + + /** In lieu of a return value, this indicates that a rule or token + * has failed to match. Reset to false upon valid token match. + */ + public var failed:Boolean = false; + + /** Did the recognizer encounter a syntax error? Track how many. */ + public var syntaxErrors:int = 0; + + /** If 0, no backtracking is going on. Safe to exec actions etc... + * If >0 then it's the level of backtracking. + */ + public var backtracking:int = 0; + + /** An Array[size num rules] of Arrays that tracks + * the stop token index for each rule. ruleMemo[ruleIndex] is + * the memoization table for ruleIndex. For key ruleStartIndex, you + * get back the stop token for associated rule or MEMO_RULE_FAILED. + * + * This is only used if rule memoization is on (which it is by default). + */ + public var ruleMemo:Array; + + + // LEXER FIELDS (must be in same state object to avoid casting + // constantly in generated code and Lexer object) :( + + + /** The goal of all lexer rules/methods is to create a token object. + * This is an instance variable as multiple rules may collaborate to + * create a single token. nextToken will return this object after + * matching lexer rule(s). If you subclass to allow multiple token + * emissions, then set this to the last token to be matched or + * something nonnull so that the auto token emit mechanism will not + * emit another token. + */ + public var token:Token; + + /** What character index in the stream did the current token start at? + * Needed, for example, to get the text for current token. Set at + * the start of nextToken. + */ + public var tokenStartCharIndex:int = -1; + + /** The line on which the first character of the token resides */ + public var tokenStartLine:int; + + /** The character position of first character within the line */ + public var tokenStartCharPositionInLine:int; + + /** The channel number for the current token */ + public var channel:int; + + /** The token type for the current token */ + public var type:int; + + /** You can set the text for the current token to override what is in + * the input char buffer. Use setText() or can set this instance var. + */ + public var text:String; + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/RuleReturnScope.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/RuleReturnScope.as new file mode 100644 index 0000000..bb1fc48 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/RuleReturnScope.as @@ -0,0 +1,13 @@ +package org.antlr.runtime +{ + /** Rules can return start/stop info as well as possible trees and templates */ + public class RuleReturnScope { + /** Return the start token or tree */ + public function get start():Object { return null; } + /** Return the stop token or tree */ + public function get stop():Object { return null; } + /** Has a value potentially if output=AST; */ + public function get tree():Object { return null; } + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/Token.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/Token.as new file mode 100644 index 0000000..d3cc9b2 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/Token.as @@ -0,0 +1,64 @@ +/* + [The "BSD licence"] + Copyright (c) 2005-2007 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. +*/ +package org.antlr.runtime { + public interface Token { + + /** Get the text of the token */ + function get text():String; + function set text(text:String):void; + + function get type():int; + function set type(ttype:int):void; + + /** The line number on which this token was matched; line=1..n */ + function get line():int; + function set line(line:int):void; + + /** The index of the first character relative to the beginning of the line 0..n-1 */ + function get charPositionInLine():int; + function set charPositionInLine(pos:int):void; + + function get channel():int; + function set channel(channel:int):void; + + /** An index from 0..n-1 of the token object in the input stream. + * This must be valid in order to use the ANTLRWorks debugger. + */ + function get tokenIndex():int; + function set tokenIndex(index:int):void; + + /** From what character stream was this token created? You don't have to + * implement but it's nice to know where a Token comes from if you have + * include files etc... on the input. + */ + function get inputStream():CharStream; + function set inputStream(input:CharStream):void; + + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/TokenConstants.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/TokenConstants.as new file mode 100644 index 0000000..e1288a2 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/TokenConstants.as @@ -0,0 +1,35 @@ +package org.antlr.runtime { + public class TokenConstants { + public static const EOR_TOKEN_TYPE:int = 1; + + /** imaginary tree navigation type; traverse "get child" link */ + public static const DOWN:int = 2; + /** imaginary tree navigation type; finish with a child list */ + public static const UP:int = 3; + + public static const MIN_TOKEN_TYPE:int = UP+1; + + public static const EOF:int = CharStreamConstants.EOF; + public static const EOF_TOKEN:Token = new CommonToken(EOF); + + public static const INVALID_TOKEN_TYPE:int = 0; + public static const INVALID_TOKEN:Token = new CommonToken(INVALID_TOKEN_TYPE); + + /** In an action, a lexer rule can set token to this SKIP_TOKEN and ANTLR + * will avoid creating a token for this symbol and try to fetch another. + */ + public static const SKIP_TOKEN:Token = new CommonToken(INVALID_TOKEN_TYPE); + + /** All tokens go to the parser (unless skip() is called in that rule) + * on a particular "channel". The parser tunes to a particular channel + * so that whitespace etc... can go to the parser on a "hidden" channel. + */ + public static const DEFAULT_CHANNEL:int = 0; + + /** Anything on different channel than DEFAULT_CHANNEL is not parsed + * by parser. + */ + public static const HIDDEN_CHANNEL:int = 99; + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/TokenRewriteStream.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/TokenRewriteStream.as new file mode 100644 index 0000000..a15eea6 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/TokenRewriteStream.as @@ -0,0 +1,509 @@ +/* + [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. +*/ +package org.antlr.runtime { + import flash.utils.getQualifiedClassName; + + + /** Useful for dumping out the input stream after doing some + * augmentation or other manipulations. + * + * You can insert stuff, replace, and delete chunks. Note that the + * operations are done lazily--only if you convert the buffer to a + * String. This is very efficient because you are not moving data around + * all the time. As the buffer of tokens is converted to strings, the + * toString() method(s) check to see if there is an operation at the + * current index. If so, the operation is done and then normal String + * rendering continues on the buffer. This is like having multiple Turing + * machine instruction streams (programs) operating on a single input tape. :) + * + * Since the operations are done lazily at toString-time, operations do not + * screw up the token index values. That is, an insert operation at token + * index i does not change the index values for tokens i+1..n-1. + * + * Because operations never actually alter the buffer, you may always get + * the original token stream back without undoing anything. Since + * the instructions are queued up, you can easily simulate transactions and + * roll back any changes if there is an error just by removing instructions. + * For example, + * + * var input:CharStream = new ANTLRFileStream("input"); + * var lex:TLexer = new TLexer(input); + * var tokens:TokenRewriteStream = new TokenRewriteStream(lex); + * var parser:T = new T(tokens); + * parser.startRule(); + * + * Then in the rules, you can execute + * var t:Token t, u:Token; + * ... + * input.insertAfter(t, "text to put after t");} + * input.insertAfter(u, "text after u");} + * trace(tokens.toString()); + * + * Actually, you have to cast the 'input' to a TokenRewriteStream. :( + * + * You can also have multiple "instruction streams" and get multiple + * rewrites from a single pass over the input. Just name the instruction + * streams and use that name again when printing the buffer. This could be + * useful for generating a C file and also its header file--all from the + * same buffer: + * + * tokens.insertAfter("pass1", t, "text to put after t");} + * tokens.insertAfter("pass2", u, "text after u");} + * trace(tokens.toString("pass1")); + * trace(tokens.toString("pass2")); + * + * If you don't use named rewrite streams, a "default" stream is used as + * the first example shows. + */ + public class TokenRewriteStream extends CommonTokenStream { + public static const DEFAULT_PROGRAM_NAME:String = "default"; + public static const MIN_TOKEN_INDEX:int = 0; + + /** You may have multiple, named streams of rewrite operations. + * I'm calling these things "programs." + * Maps String (name) -> rewrite (List) + */ + protected var programs:Object = new Object(); + + /** Map String (program name) -> Integer index */ + protected var lastRewriteTokenIndexes:Object = new Object(); + + public function TokenRewriteStream(tokenSource:TokenSource = null, channel:int = TokenConstants.DEFAULT_CHANNEL) { + super(tokenSource, channel); + programs[DEFAULT_PROGRAM_NAME] = new Array(); + } + + /** Rollback the instruction stream for a program so that + * the indicated instruction (via instructionIndex) is no + * longer in the stream. UNTESTED! + */ + public function rollback(instructionIndex:int, programName:String = DEFAULT_PROGRAM_NAME):void { + var isn:Array = programs[programName] as Array; + if ( isn != null ) { + programs[programName] = isn.slice(MIN_TOKEN_INDEX,instructionIndex); + } + } + + /** Reset the program so that no instructions exist */ + public function deleteProgram(programName:String = DEFAULT_PROGRAM_NAME):void { + rollback(MIN_TOKEN_INDEX, programName); + } + + public function insertAfterToken(t:Token, text:Object, programName:String = DEFAULT_PROGRAM_NAME):void { + insertAfter(t.tokenIndex, text, programName); + } + + public function insertAfter(index:int, text:Object, programName:String = DEFAULT_PROGRAM_NAME):void { + // to insert after, just insert before next index (even if past end) + insertBefore(index+1, text, programName); + } + + public function insertBeforeToken(t:Token, text:Object, programName:String = DEFAULT_PROGRAM_NAME):void { + insertBefore(t.tokenIndex, text, programName); + } + + public function insertBefore(index:int, text:Object, programName:String = DEFAULT_PROGRAM_NAME):void { + var op:RewriteOperation = new InsertBeforeOp(index,text); + var rewrites:Array = getProgram(programName); + op.instructionIndex = rewrites.length; + rewrites.push(op); + } + + public function replace(index:int, text:Object, programName:String = DEFAULT_PROGRAM_NAME):void { + replaceRange(index, index, text, programName); + } + + public function replaceRange(fromIndex:int, toIndex:int, text:Object, programName:String = DEFAULT_PROGRAM_NAME):void { + if ( fromIndex > toIndex || fromIndex<0 || toIndex<0 || toIndex >= tokens.length ) { + throw new Error("replace: range invalid: "+fromIndex+".."+toIndex+"(size="+tokens.length+")"); + } + var op:RewriteOperation = new ReplaceOp(fromIndex, toIndex, text); + var rewrites:Array = getProgram(programName); + op.instructionIndex = rewrites.length; + rewrites.push(op); + } + + public function replaceToken(indexT:Token, text:Object, programName:String = DEFAULT_PROGRAM_NAME):void { + replaceTokenRange(indexT, indexT, text, programName); + } + + public function replaceTokenRange(fromToken:Token, toToken:Token, text:Object, programName:String = DEFAULT_PROGRAM_NAME):void { + replaceRange(fromToken.tokenIndex, toToken.tokenIndex, text, programName); + } + + public function remove(index:int, programName:String = DEFAULT_PROGRAM_NAME):void { + removeRange(index, index, programName); + } + + public function removeRange(fromIndex:int, toIndex:int, programName:String = DEFAULT_PROGRAM_NAME):void { + replaceRange(fromIndex, toIndex, null, programName); + } + + public function removeToken(token:Token, programName:String = DEFAULT_PROGRAM_NAME):void { + removeTokenRange(token, token, programName); + } + + public function removeTokenRange(fromToken:Token, toToken:Token, programName:String = DEFAULT_PROGRAM_NAME):void { + replaceTokenRange(fromToken, toToken, null, programName); + } + + public function getLastRewriteTokenIndex(programName:String = DEFAULT_PROGRAM_NAME):int { + var i:* = lastRewriteTokenIndexes[programName]; + if ( i == undefined ) { + return -1; + } + return i as int; + } + + protected function setLastRewriteTokenIndex(programName:String, i:int):void { + lastRewriteTokenIndexes[programName] = i; + } + + protected function getProgram(name:String):Array { + var isn:Array = programs[name] as Array; + if ( isn==null ) { + isn = initializeProgram(name); + } + return isn; + } + + private function initializeProgram(name:String):Array { + var isn:Array = new Array(); + programs[name] = isn; + return isn; + } + + public function toOriginalString():String { + return toOriginalStringWithRange(MIN_TOKEN_INDEX, size-1); + } + + public function toOriginalStringWithRange(start:int, end:int):String { + var buf:String = new String(); + for (var i:int=start; i>=MIN_TOKEN_INDEX && i<=end && i<tokens.length; i++) { + buf += getToken(i).text; + } + return buf.toString(); + } + + public override function toString():String { + return toStringWithRange(MIN_TOKEN_INDEX, size-1); + } + + public override function toStringWithRange(start:int, end:int):String { + return toStringWithRangeAndProgram(start, end, DEFAULT_PROGRAM_NAME); + } + + public function toStringWithRangeAndProgram(start:int, end:int, programName:String):String { + var rewrites:Array = programs[programName] as Array; + + // ensure start/end are in range + if ( end > tokens.length-1 ) end = tokens.length-1; + if ( start < 0 ) start = 0; + + if ( rewrites==null || rewrites.length==0 ) { + return toOriginalStringWithRange(start,end); // no instructions to execute + } + var state:RewriteState = new RewriteState(); + state.tokens = tokens; + + // First, optimize instruction stream + var indexToOp:Array = reduceToSingleOperationPerIndex(rewrites); + + // Walk buffer, executing instructions and emitting tokens + var i:int = start; + while ( i <= end && i < tokens.length ) { + var op:RewriteOperation = RewriteOperation(indexToOp[i]); + indexToOp[i] = undefined; // remove so any left have index size-1 + var t:Token = Token(tokens[i]); + if ( op==null ) { + // no operation at that index, just dump token + state.buf += t.text; + i++; // move to next token + } + else { + i = op.execute(state); // execute operation and skip + } + } + + // include stuff after end if it's last index in buffer + // So, if they did an insertAfter(lastValidIndex, "foo"), include + // foo if end==lastValidIndex. + if ( end==tokens.length-1 ) { + // Scan any remaining operations after last token + // should be included (they will be inserts). + for each (op in indexToOp) { + if (op == null) continue; + if ( op.index >= tokens.length-1 ) state.buf += op.text; + } + } + + return state.buf; + } + + /** We need to combine operations and report invalid operations (like + * overlapping replaces that are not completed nested). Inserts to + * same index need to be combined etc... Here are the cases: + * + * I.i.u I.j.v leave alone, nonoverlapping + * I.i.u I.i.v combine: Iivu + * + * R.i-j.u R.x-y.v | i-j in x-y delete first R + * R.i-j.u R.i-j.v delete first R + * R.i-j.u R.x-y.v | x-y in i-j ERROR + * R.i-j.u R.x-y.v | boundaries overlap ERROR + * + * I.i.u R.x-y.v | i in x-y delete I + * I.i.u R.x-y.v | i not in x-y leave alone, nonoverlapping + * R.x-y.v I.i.u | i in x-y ERROR + * R.x-y.v I.x.u R.x-y.uv (combine, delete I) + * R.x-y.v I.i.u | i not in x-y leave alone, nonoverlapping + * + * I.i.u = insert u before op @ index i + * R.x-y.u = replace x-y indexed tokens with u + * + * First we need to examine replaces. For any replace op: + * + * 1. wipe out any insertions before op within that range. + * 2. Drop any replace op before that is contained completely within + * that range. + * 3. Throw exception upon boundary overlap with any previous replace. + * + * Then we can deal with inserts: + * + * 1. for any inserts to same index, combine even if not adjacent. + * 2. for any prior replace with same left boundary, combine this + * insert with replace and delete this replace. + * 3. throw exception if index in same range as previous replace + * + * Don't actually delete; make op null in list. Easier to walk list. + * Later we can throw as we add to index -> op map. + * + * Note that I.2 R.2-2 will wipe out I.2 even though, technically, the + * inserted stuff would be before the replace range. But, if you + * add tokens in front of a method body '{' and then delete the method + * body, I think the stuff before the '{' you added should disappear too. + * + * Return a map from token index to operation. + */ + protected function reduceToSingleOperationPerIndex(rewrites:Array):Array { + //System.out.println("rewrites="+rewrites); + + // WALK REPLACES + for (var i:int = 0; i < rewrites.length; i++) { + var op:RewriteOperation = RewriteOperation(rewrites[i]); + if ( op==null ) continue; + if ( !(op is ReplaceOp) ) continue; + var rop:ReplaceOp = ReplaceOp(rewrites[i]); + // Wipe prior inserts within range + var inserts:Array = getKindOfOps(rewrites, InsertBeforeOp, i); + for (var j:int = 0; j < inserts.length; j++) { + var iop:InsertBeforeOp = InsertBeforeOp(inserts[j]); + if ( iop.index >= rop.index && iop.index <= rop.lastIndex ) { + rewrites[iop.instructionIndex] = null; // delete insert as it's a no-op. + } + } + // Drop any prior replaces contained within + var prevReplaces:Array = getKindOfOps(rewrites, ReplaceOp, i); + for (j = 0; j < prevReplaces.length; j++) { + var prevRop:ReplaceOp = ReplaceOp(prevReplaces[j]); + if ( prevRop.index>=rop.index && prevRop.lastIndex <= rop.lastIndex ) { + rewrites[prevRop.instructionIndex] = null; // delete replace as it's a no-op. + continue; + } + // throw exception unless disjoint or identical + var disjoint:Boolean = + prevRop.lastIndex<rop.index || prevRop.index > rop.lastIndex; + var same:Boolean = + prevRop.index==rop.index && prevRop.lastIndex==rop.lastIndex; + if ( !disjoint && !same ) { + throw new Error("replace op boundaries of "+rop+ + " overlap with previous "+prevRop); + } + } + } + + // WALK INSERTS + for (i = 0; i < rewrites.length; i++) { + op = RewriteOperation(rewrites[i]); + if ( op==null ) continue; + if ( !(op is InsertBeforeOp) ) continue; + iop = InsertBeforeOp(rewrites[i]); + // combine current insert with prior if any at same index + var prevInserts:Array = getKindOfOps(rewrites, InsertBeforeOp, i); + for (j = 0; j < prevInserts.length; j++) { + var prevIop:InsertBeforeOp = InsertBeforeOp(prevInserts[j]); + if ( prevIop.index == iop.index ) { // combine objects + // convert to strings...we're in process of toString'ing + // whole token buffer so no lazy eval issue with any templates + iop.text = catOpText(iop.text,prevIop.text); + rewrites[prevIop.instructionIndex] = null; // delete redundant prior insert + } + } + // look for replaces where iop.index is in range; error + prevReplaces = getKindOfOps(rewrites, ReplaceOp, i); + for (j = 0; j < prevReplaces.length; j++) { + rop = ReplaceOp(prevReplaces[j]); + if ( iop.index == rop.index ) { + rop.text = catOpText(iop.text,rop.text); + rewrites[i] = null; // delete current insert + continue; + } + if ( iop.index >= rop.index && iop.index <= rop.lastIndex ) { + throw new Error("insert op "+iop+ + " within boundaries of previous "+rop); + } + } + } + // System.out.println("rewrites after="+rewrites); + var m:Array = new Array(); + for (i = 0; i < rewrites.length; i++) { + op = RewriteOperation(rewrites[i]); + if ( op==null ) continue; // ignore deleted ops + if ( m[op.index] != undefined ) { + throw new Error("should only be one op per index"); + } + m[op.index] = op; + } + //System.out.println("index to op: "+m); + return m; + } + + protected function catOpText(a:Object, b:Object):String { + var x:String = ""; + var y:String = ""; + if ( a!=null ) x = a.toString(); + if ( b!=null ) y = b.toString(); + return x+y; + } + + /** Get all operations before an index of a particular kind */ + protected function getKindOfOps(rewrites:Array, kind:Class, before:int = -1):Array { + if (before == -1) { + before = rewrites.length; + } + var ops:Array = new Array(); + for (var i:int=0; i<before && i<rewrites.length; i++) { + var op:RewriteOperation = RewriteOperation(rewrites[i]); + if ( op==null ) continue; // ignore deleted + if ( getQualifiedClassName(op) == getQualifiedClassName(kind) ) ops.push(op); + } + return ops; + } + + + public function toDebugString():String { + return toDebugStringWithRange(MIN_TOKEN_INDEX, size-1); + } + + public function toDebugStringWithRange(start:int, end:int):String { + var buf:String = new String(); + for (var i:int=start; i>=MIN_TOKEN_INDEX && i<=end && i<tokens.length; i++) { + buf += getToken(i); + } + return buf; + } + + + } +} + import org.antlr.runtime.Token; + + +// Define the rewrite operation hierarchy + +class RewriteState { + public var buf:String = new String(); + public var tokens:Array; +} + +class RewriteOperation { + /** What index into rewrites List are we? */ + internal var instructionIndex:int; + /** Token buffer index. */ + public var index:int; + internal var text:Object; + public function RewriteOperation(index:int, text:Object) { + this.index = index; + this.text = text; + } + /** Execute the rewrite operation by possibly adding to the buffer. + * Return the index of the next token to operate on. + */ + public function execute(state:RewriteState):int { + return index; + } +} + +class InsertBeforeOp extends RewriteOperation { + public function InsertBeforeOp(index:int, text:Object) { + super(index,text); + } + + public override function execute(state:RewriteState):int { + state.buf += text; + state.buf += Token(state.tokens[index]).text; + return index + 1; + } + + public function toString():String { + return "<InsertBeforeOp@" + index + ":\"" + text + "\">"; + } +} + +/** I'm going to try replacing range from x..y with (y-x)+1 ReplaceOp + * instructions. + */ +class ReplaceOp extends RewriteOperation { + public var lastIndex:int; + + public function ReplaceOp(fromIndex:int, toIndex:int, text:Object) { + super(fromIndex, text); + lastIndex = toIndex; + } + + public override function execute(state:RewriteState):int { + if ( text!=null ) { + state.buf += text; + } + return lastIndex+1; + } + + public function toString():String { + return "<ReplaceOp@" + index + ".." + lastIndex + ":\"" + text + "\">"; + } +} + +class DeleteOp extends ReplaceOp { + public function DeleteOp(fromIndex:int, toIndex:int) { + super(fromIndex, toIndex, null); + } + + public override function toString():String { + return "<DeleteOp@" + index + ".." + lastIndex + ">"; + } +} diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/TokenSource.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/TokenSource.as new file mode 100644 index 0000000..2c1b90f --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/TokenSource.as @@ -0,0 +1,55 @@ +/* + [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. +*/ + +package org.antlr.runtime { + /** A source of tokens must provide a sequence of tokens via nextToken() + * and also must reveal it's source of characters; CommonToken's text is + * computed from a CharStream; it only store indices into the char stream. + * + * Errors from the lexer are never passed to the parser. Either you want + * to keep going or you do not upon token recognition error. If you do not + * want to continue lexing then you do not want to continue parsing. Just + * throw an exception not under RecognitionException and Java will naturally + * toss you all the way out of the recognizers. If you want to continue + * lexing then you should not throw an exception to the parser--it has already + * requested a token. Keep lexing until you get a valid one. Just report + * errors and keep going, looking for a valid token. + */ + public interface TokenSource { + /** Return a Token object from your input stream (usually a CharStream). + * Do not fail/return upon lexing error; keep chewing on the characters + * until you get a good one; errors are not passed through to the parser. + */ + function nextToken():Token; + + /** Where are you getting tokens from? normally the implication will simply + * ask lexers input stream. + */ + function get sourceName():String; + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/TokenStream.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/TokenStream.as new file mode 100644 index 0000000..3bb374b --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/TokenStream.as @@ -0,0 +1,70 @@ +/* + [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. +*/ +package org.antlr.runtime { + + /** A stream of tokens accessing tokens from a TokenSource */ + public interface TokenStream extends IntStream { + /** Get Token at current input pointer + i ahead where i=1 is next Token. + * i<0 indicates tokens in the past. So -1 is previous token and -2 is + * two tokens ago. LT(0) is undefined. For i>=n, return Token.EOFToken. + * Return null for LT(0) and any index that results in an absolute address + * that is negative. + */ + function LT(k:int):Token; + + /** Get a token at an absolute index i; 0..n-1. This is really only + * needed for profiling and debugging and token stream rewriting. + * If you don't want to buffer up tokens, then this method makes no + * sense for you. Naturally you can't use the rewrite stream feature. + * I believe DebugTokenStream can easily be altered to not use + * this method, removing the dependency. + */ + function getToken(i:int):Token; + + /** Where is this stream pulling tokens from? This is not the name, but + * the object that provides Token objects. + */ + function get tokenSource():TokenSource; + + /** Return the text of all tokens from start to stop, inclusive. + * If the stream does not buffer all the tokens then it can just + * return "" or null; Users should not access $ruleLabel.text in + * an action of course in that case. + */ + function toStringWithRange(start:int, stop:int):String; + + /** Because the user is not required to use a token with an index stored + * in it, we must provide a means for two token objects themselves to + * indicate the start/end location. Most often this will just delegate + * to the other toString(int,int). This is also parallel with + * the TreeNodeStream.toString(Object,Object). + */ + function toStringWithTokenRange(start:Token, stop:Token):String; + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/UnwantedTokenException.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/UnwantedTokenException.as new file mode 100644 index 0000000..c641bc8 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/UnwantedTokenException.as @@ -0,0 +1,25 @@ +package org.antlr.runtime +{ + public class UnwantedTokenException extends MismatchedTokenException + { + public function UnwantedTokenException(expecting:int, input:IntStream) + { + super(expecting, input); + } + + public function get unexpectedToken():Token { + return token; + } + + public override function toString():String { + var exp:String = ", expected "+expecting; + if ( expecting==TokenConstants.INVALID_TOKEN_TYPE ) { + exp = ""; + } + if ( token==null ) { + return "UnwantedTokenException(found="+null+exp+")"; + } + return "UnwantedTokenException(found="+token.text+exp+")"; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/BaseTree.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/BaseTree.as new file mode 100644 index 0000000..8a8880f --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/BaseTree.as @@ -0,0 +1,357 @@ +/* + [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. +*/ +package org.antlr.runtime.tree { + /** A generic tree implementation with no payload. You must subclass to + * actually have any user data. ANTLR v3 uses a list of children approach + * instead of the child-sibling approach in v2. A flat tree (a list) is + * an empty node whose children represent the list. An empty, but + * non-null node is called "nil". + */ + public class BaseTree implements Tree { + protected var _children:Array; + + /** Create a new node from an existing node does nothing for BaseTree + * as there are no fields other than the children list, which cannot + * be copied as the children are not considered part of this node. + */ + public function BaseTree(node:Tree = null) { + } + + public function getChild(i:int):Tree { + if ( _children==null || i>=_children.length ) { + return null; + } + return BaseTree(_children[i]); + } + + /** Get the children internal List; note that if you directly mess with + * the list, do so at your own risk. + */ + public function get children():Array { + return _children; + } + + public function getFirstChildWithType(type:int):Tree { + for (var i:int = 0; _children!=null && i < _children.length; i++) { + var t:Tree = Tree(_children[i]); + if ( t.type==type ) { + return t; + } + } + return null; + } + + public function get childCount():int { + if ( _children==null ) { + return 0; + } + return _children.length; + } + + /** Add t as child of this node. + * + * Warning: if t has no children, but child does + * and child isNil then this routine moves children to t via + * t.children = child.children; i.e., without copying the array. + */ + public function addChild(t:Tree):void { + if ( t==null ) { + return; // do nothing upon addChild(null) + } + var childTree:BaseTree = BaseTree(t); + if ( childTree.isNil ) { // t is an empty node possibly with children + if ( this._children!=null && this._children == childTree._children ) { + throw new Error("attempt to add child list to itself"); + } + // just add all of childTree's children to this + if ( childTree._children!=null ) { + if ( this._children!=null ) { // must copy, this has children already + var n:int = childTree._children.length; + for (var i:int = 0; i < n; i++) { + var c:Tree = Tree(childTree._children[i]); + this.children.push(c); + // handle double-link stuff for each child of nil root + c.parent = this; + c.childIndex = children.length-1; + } + } + else { + // no children for this but t has children; just set pointer + // call general freshener routine + this._children = childTree.children; + this.freshenParentAndChildIndexes(); + } + } + } + else { // child is not nil (don't care about children) + if ( _children==null ) { + _children = new Array(); // create children list on demand + } + _children.push(t); + childTree.parent = this; + childTree.childIndex = children.length-1; + } + } + + /** Add all elements of kids list as children of this node */ + public function addChildren(kids:Array):void { + for (var i:int = 0; i < kids.length; i++) { + var t:Tree = Tree(kids[i]); + addChild(t); + } + } + + public function setChild(i:int, t:Tree):void { + if ( t==null ) { + return; + } + if ( t.isNil ) { + throw new Error("Can't set single child to a list"); + } + if ( _children==null ) { + _children = new Array(); + } + _children[i] = t; + t.parent = this; + t.childIndex = i; + } + + public function deleteChild(i:int):Object { + if ( _children==null ) { + return null; + } + var killed:BaseTree = BaseTree(children.remove(i)); + // walk rest and decrement their child indexes + this.freshenParentAndChildIndexesFrom(i); + return killed; + } + + /** Delete children from start to stop and replace with t even if t is + * a list (nil-root tree). num of children can increase or decrease. + * For huge child lists, inserting children can force walking rest of + * children to set their childindex; could be slow. + */ + public function replaceChildren(startChildIndex:int, stopChildIndex:int, t:Object):void { + if ( children==null ) { + throw new Error("indexes invalid; no children in list"); + } + var replacingHowMany:int = stopChildIndex - startChildIndex + 1; + var replacingWithHowMany:int; + var newTree:BaseTree = BaseTree(t); + var newChildren:Array = null; + // normalize to a list of children to add: newChildren + if ( newTree.isNil ) { + newChildren = newTree.children; + } + else { + newChildren = new Array(1); + newChildren.add(newTree); + } + replacingWithHowMany = newChildren.length; + var numNewChildren:int = newChildren.length; + var delta:int = replacingHowMany - replacingWithHowMany; + // if same number of nodes, do direct replace + if ( delta == 0 ) { + var j:int = 0; // index into new children + for (var i:int=startChildIndex; i<=stopChildIndex; i++) { + var child:BaseTree = BaseTree(newChildren[j]); + children[i] = child; + child.parent = this; + child.childIndex= i; + j++; + } + } + else if ( delta > 0 ) { // fewer new nodes than there were + // set children and then delete extra + for (j=0; j<numNewChildren; j++) { + children[startChildIndex+j] = newChildren[j]; + } + var indexToDelete:int = startChildIndex+numNewChildren; + for (var c:int=indexToDelete; c<=stopChildIndex; c++) { + // delete same index, shifting everybody down each time + var killed:BaseTree = BaseTree(children.remove(indexToDelete)); + } + freshenParentAndChildIndexesFrom(startChildIndex); + } + else { // more new nodes than were there before + // fill in as many children as we can (replacingHowMany) w/o moving data + for (j=0; j<replacingHowMany; j++) { + children[startChildIndex+j] = newChildren[j]; + } + var numToInsert:int = replacingWithHowMany-replacingHowMany; + for (j=replacingHowMany; j<replacingWithHowMany; j++) { + children.splice(startChildIndex+j, 0, newChildren[j]); + } + freshenParentAndChildIndexesFrom(startChildIndex); + } + } + + public function get isNil():Boolean { + return false; + } + + /** Set the parent and child index values for all child of t */ + public function freshenParentAndChildIndexes():void { + freshenParentAndChildIndexesFrom(0); + } + + public function freshenParentAndChildIndexesFrom(offset:int):void { + var n:int = childCount; + for (var c:int = offset; c < n; c++) { + var child:Tree = Tree(getChild(c)); + child.childIndex = c; + child.parent = this; + } + } + + public function sanityCheckParentAndChildIndexes():void { + sanityCheckParentAndChildIndexesFrom(null, -1); + } + + public function sanityCheckParentAndChildIndexesFrom(parent:Tree, i:int):void { + if ( parent!=this.parent ) { + throw new Error("parents don't match; expected "+parent+" found "+this.parent); + } + if ( i!=this.childIndex ) { + throw new Error("child indexes don't match; expected "+i+" found "+this.childIndex); + } + var n:int = this.childCount; + for (var c:int = 0; c < n; c++) { + var child:CommonTree = CommonTree(this.getChild(c)); + child.sanityCheckParentAndChildIndexesFrom(this, c); + } + } + + /** BaseTree doesn't track child indexes. */ + public function get childIndex():int { + return 0; + } + + public function set childIndex(index:int):void { + } + + /** BaseTree doesn't track parent pointers. */ + public function get parent():Tree { + return null; + } + public function set parent(t:Tree):void { + } + + /** Walk upwards looking for ancestor with this token type. */ + public function hasAncestor(ttype:int):Boolean { return getAncestor(ttype)!=null; } + + /** Walk upwards and get first ancestor with this token type. */ + public function getAncestor(ttype:int):Tree { + var t:Tree = this; + t = t.parent; + while ( t!=null ) { + if ( t.type==ttype ) return t; + t = t.parent; + } + return null; + } + + /** Return a list of all ancestors of this node. The first node of + * list is the root and the last is the parent of this node. + */ + public function get ancestors():Array { + if ( parent==null ) return null; + var ancestors:Array = new Array(); + var t:Tree = this; + t = t.parent; + while ( t!=null ) { + ancestors.unshift(t); // insert at start + t = t.parent; + } + return ancestors; + } + + /** Print out a whole tree not just a node */ + public function toStringTree():String { + if ( _children==null || _children.length==0 ) { + return String(this); + } + var buf:String = ""; + if ( !isNil ) { + buf += "("; + buf += String(this); + buf += ' '; + } + for (var i:int = 0; _children!=null && i < _children.length; i++) { + var t:BaseTree = BaseTree(_children[i]); + if ( i>0 ) { + buf += ' '; + } + buf += t.toStringTree(); + } + if ( !isNil ) { + buf += ")"; + } + return buf; + } + + public function get line():int { + return 0; + } + + public function get charPositionInLine():int { + return 0; + } + + // "Abstract" functions since there are no abstract classes in actionscript + + public function dupNode():Tree { + throw new Error("Not implemented"); + } + + public function get type():int { + throw new Error("Not implemented"); + } + + public function get text():String { + throw new Error("Not implemented"); + } + + public function get tokenStartIndex():int { + throw new Error("Not implemented"); + } + + public function set tokenStartIndex(index:int):void { + throw new Error("Not implemented"); + } + + public function get tokenStopIndex():int { + throw new Error("Not implemented"); + } + + public function set tokenStopIndex(index:int):void { + throw new Error("Not implemented"); + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/BaseTreeAdaptor.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/BaseTreeAdaptor.as new file mode 100644 index 0000000..c0b5c10 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/BaseTreeAdaptor.as @@ -0,0 +1,313 @@ +package org.antlr.runtime.tree { + + import flash.utils.Dictionary; + + import mx.utils.ObjectUtil; + + import org.antlr.runtime.*; + + /** A TreeAdaptor that works with any Tree implementation. */ + public class BaseTreeAdaptor implements TreeAdaptor { + /** System.identityHashCode() is not always unique; we have to + * track ourselves. That's ok, it's only for debugging, though it's + * expensive: we have to create a hashtable with all tree nodes in it. + */ + protected var treeToUniqueIDMap:Dictionary; + protected var uniqueNodeID:int = 1; + + public function nil():Object { + return createWithPayload(null); + } + + /** create tree node that holds the start and stop tokens associated + * with an error. + * + * If you specify your own kind of tree nodes, you will likely have to + * override this method. CommonTree returns Token.INVALID_TOKEN_TYPE + * if no token payload but you might have to set token type for diff + * node type. + * + * You don't have to subclass CommonErrorNode; you will likely need to + * subclass your own tree node class to avoid class cast exception. + */ + public function errorNode(input:TokenStream, start:Token, stop:Token, + e:RecognitionException):Object { + var t:CommonErrorNode = new CommonErrorNode(input, start, stop, e); + //System.out.println("returning error node '"+t+"' @index="+input.index()); + return t; + } + + public function isNil(tree:Object):Boolean { + return Tree(tree).isNil; + } + + public function dupTree(tree:Object):Object { + return dupTreeWithParent(tree, null); + } + + /** This is generic in the sense that it will work with any kind of + * tree (not just Tree interface). It invokes the adaptor routines + * not the tree node routines to do the construction. + */ + public function dupTreeWithParent(t:Object, parent:Object):Object { + if ( t==null ) { + return null; + } + var newTree:Object = dupNode(t); + // ensure new subtree root has parent/child index set + setChildIndex(newTree, getChildIndex(t)); // same index in new tree + setParent(newTree, parent); + var n:int = getChildCount(t); + for (var i:int = 0; i < n; i++) { + var child:Object = getChild(t, i); + var newSubTree:Object = dupTreeWithParent(child, t); + addChild(newTree, newSubTree); + } + return newTree; + } + + /** Add a child to the tree t. If child is a flat tree (a list), make all + * in list children of t. Warning: if t has no children, but child does + * and child isNil then you can decide it is ok to move children to t via + * t.children = child.children; i.e., without copying the array. Just + * make sure that this is consistent with have the user will build + * ASTs. + */ + public function addChild(t:Object, child:Object):void { + if ( t!=null && child!=null ) { + Tree(t).addChild(Tree(child)); + } + } + + /** If oldRoot is a nil root, just copy or move the children to newRoot. + * If not a nil root, make oldRoot a child of newRoot. + * + * old=^(nil a b c), new=r yields ^(r a b c) + * old=^(a b c), new=r yields ^(r ^(a b c)) + * + * If newRoot is a nil-rooted single child tree, use the single + * child as the new root node. + * + * old=^(nil a b c), new=^(nil r) yields ^(r a b c) + * old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) + * + * If oldRoot was null, it's ok, just return newRoot (even if isNil). + * + * old=null, new=r yields r + * old=null, new=^(nil r) yields ^(nil r) + * + * Return newRoot. Throw an exception if newRoot is not a + * simple node or nil root with a single child node--it must be a root + * node. If newRoot is ^(nil x) return x as newRoot. + * + * Be advised that it's ok for newRoot to point at oldRoot's + * children; i.e., you don't have to copy the list. We are + * constructing these nodes so we should have this control for + * efficiency. + */ + public function becomeRoot(newRoot:Object, oldRoot:Object):Object { + // If new Root is token, then create a Tree. + if (newRoot is Token) { + newRoot = createWithPayload(Token(newRoot)); + } + + var newRootTree:Tree = Tree(newRoot); + var oldRootTree:Tree = Tree(oldRoot); + if ( oldRoot==null ) { + return newRoot; + } + // handle ^(nil real-node) + if ( newRootTree.isNil ) { + var nc:int = newRootTree.childCount; + if ( nc==1 ) newRootTree = Tree(newRootTree.getChild(0)); + else if ( nc >1 ) { + // TODO: make tree run time exceptions hierarchy + throw new Error("more than one node as root (TODO: make exception hierarchy)"); + } + } + // add oldRoot to newRoot; addChild takes care of case where oldRoot + // is a flat list (i.e., nil-rooted tree). All children of oldRoot + // are added to newRoot. + newRootTree.addChild(oldRootTree); + return newRootTree; + } + + /** Transform ^(nil x) to x and nil to null */ + public function rulePostProcessing(root:Object):Object { + var r:Tree = Tree(root); + if ( r!=null && r.isNil ) { + if ( r.childCount==0 ) { + r = null; + } + else if ( r.childCount==1 ) { + r = Tree(r.getChild(0)); + // whoever invokes rule will set parent and child index + r.parent = null; + r.childIndex = -1; + } + } + return r; + } + + public function createFromToken(tokenType:int, fromToken:Token, text:String = null):Object { + fromToken = createToken(fromToken); + fromToken.type = tokenType; + if (text != null) { + fromToken.text = text; + } + return createWithPayload(fromToken); + } + + public function createFromType(tokenType:int, text:String):Object { + var fromToken:Token = createTokenFromType(tokenType, text); + return createWithPayload(fromToken); + } + + public function getType(t:Object):int { + return Tree(t).type; + } + + public function setType(t:Object, type:int):void { + throw new Error("don't know enough about Tree node"); + } + + public function getText(t:Object):String { + return Tree(t).text; + } + + public function setText(t:Object, text:String):void { + throw new Error("don't know enough about Tree node"); + } + + public function getChild(t:Object, i:int):Object { + return Tree(t).getChild(i); + } + + public function setChild(t:Object, i:int, child:Object):void { + Tree(t).setChild(i, Tree(child)); + } + + public function deleteChild(t:Object, i:int):Object { + return Tree(t).deleteChild(i); + } + + public function getChildCount(t:Object):int { + return Tree(t).childCount; + } + + public function getUniqueID(node:Object):int { + if ( treeToUniqueIDMap==null ) { + treeToUniqueIDMap = new Dictionary(); + } + if (treeToUniqueIDMap.hasOwnProperty(node)) { + return treeToUniqueIDMap[node]; + } + + var ID:int = uniqueNodeID; + treeToUniqueIDMap[node] = ID; + uniqueNodeID++; + return ID; + + } + + /** Tell me how to create a token for use with imaginary token nodes. + * For example, there is probably no input symbol associated with imaginary + * token DECL, but you need to create it as a payload or whatever for + * the DECL node as in ^(DECL type ID). + * + * If you care what the token payload objects' type is, you should + * override this method and any other createToken variant. + */ + public function createTokenFromType(tokenType:int, text:String):Token { + throw new Error("Not implemented - abstract function"); + } + + /** Tell me how to create a token for use with imaginary token nodes. + * For example, there is probably no input symbol associated with imaginary + * token DECL, but you need to create it as a payload or whatever for + * the DECL node as in ^(DECL type ID). + * + * This is a variant of createToken where the new token is derived from + * an actual real input token. Typically this is for converting '{' + * tokens to BLOCK etc... You'll see + * + * r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ; + * + * If you care what the token payload objects' type is, you should + * override this method and any other createToken variant. + * + */ + public function createToken(fromToken:Token):Token { + throw new Error("Not implemented - abstract function"); + } + + public function createWithPayload(payload:Token):Object { + throw new Error("Not implemented - abstract function"); + } + + public function dupNode(t:Object):Object { + throw new Error("Not implemented - abstract function"); + } + + public function getToken(t:Object):Token { + throw new Error("Not implemented - abstract function"); + } + + public function setTokenBoundaries(t:Object, startToken:Token, stopToken:Token):void { + throw new Error("Not implemented - abstract function"); + } + + public function getTokenStartIndex(t:Object):int { + throw new Error("Not implemented - abstract function"); + } + + public function getTokenStopIndex(t:Object):int { + throw new Error("Not implemented - abstract function"); + } + + public function getParent(t:Object):Object { + throw new Error("Not implemented - abstract function"); + } + + public function setParent(t:Object, parent:Object):void { + throw new Error("Not implemented - abstract function"); + } + + public function getChildIndex(t:Object):int { + throw new Error("Not implemented - abstract function"); + } + + public function setChildIndex(t:Object, index:int):void { + throw new Error("Not implemented - abstract function"); + } + + public function replaceChildren(parent:Object, startChildIndex:int, stopChildIndex:int, t:Object):void { + throw new Error("Not implemented - abstract function"); + } + + public function create(... args):Object { + if (args.length == 1 && args[0] is Token) { + return createWithPayload(args[0]); + } + else if (args.length == 2 && + args[0] is int && + args[1] is Token) { + return createFromToken(args[0], args[1]); + } + else if (args.length == 3 && + args[0] is int && + args[1] is Token && + args[2] is String) { + return createFromToken(args[0], args[1], args[2]); + } + else if (args.length == 2 && + args[0] is int && + args[1] is String) { + return createFromType(args[0], args[1]); + } + throw new Error("No methods signature for arguments : " + ObjectUtil.toString(args)); + } + } + + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/CommonErrorNode.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/CommonErrorNode.as new file mode 100644 index 0000000..d6eb8a7 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/CommonErrorNode.as @@ -0,0 +1,84 @@ +package org.antlr.runtime.tree +{ + + import org.antlr.runtime.*; + + public class CommonErrorNode extends CommonTree { + + public var input:IntStream; + public var start:Token; + public var stop:Token; + public var trappedException:RecognitionException; + + public function CommonErrorNode(input:TokenStream, start:Token, stop:Token, + e:RecognitionException) + { + //System.out.println("start: "+start+", stop: "+stop); + if ( stop==null || + (stop.tokenIndex < start.tokenIndex && + stop.type!=TokenConstants.EOF) ) + { + // sometimes resync does not consume a token (when LT(1) is + // in follow set. So, stop will be 1 to left to start. adjust. + // Also handle case where start is the first token and no token + // is consumed during recovery; LT(-1) will return null. + stop = start; + } + this.input = input; + this.start = start; + this.stop = stop; + this.trappedException = e; + } + + public override function get isNil():Boolean { + return false; + } + + public function getType():int { + return TokenConstants.INVALID_TOKEN_TYPE; + } + + public function getText():String { + var badText:String = null; + if ( start is Token ) { + var i:int = Token(start).tokenIndex; + var j:int = Token(stop).tokenIndex; + if ( Token(stop).type == TokenConstants.EOF ) { + j = TokenStream(input).size; + } + badText = TokenStream(input).toStringWithRange(i, j); + } + else if ( start is Tree ) { + badText = TreeNodeStream(input).toStringWithRange(start, stop); + } + else { + // people should subclass if they alter the tree type so this + // next one is for sure correct. + badText = "<unknown>"; + } + return badText; + } + + public override function toString():String { + if ( trappedException is MissingTokenException ) { + return "<missing type: "+ + MissingTokenException(trappedException).missingType+ + ">"; + } + else if ( trappedException is UnwantedTokenException ) { + return "<extraneous: "+ + UnwantedTokenException(trappedException).unexpectedToken+ + ", resync="+getText()+">"; + } + else if ( trappedException is MismatchedTokenException ) { + return "<mismatched token: "+trappedException.token+", resync="+getText()+">"; + } + else if ( trappedException is NoViableAltException ) { + return "<unexpected: "+trappedException.token+ + ", resync="+getText()+">"; + } + return "<error: "+getText()+">"; + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/CommonTree.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/CommonTree.as new file mode 100644 index 0000000..8714268 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/CommonTree.as @@ -0,0 +1,166 @@ +/* + [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. +*/ +package org.antlr.runtime.tree { + import org.antlr.runtime.Token; + import org.antlr.runtime.TokenConstants; + + /** A tree node that is wrapper for a Token object. After 3.0 release + * while building tree rewrite stuff, it became clear that computing + * parent and child index is very difficult and cumbersome. Better to + * spend the space in every tree node. If you don't want these extra + * fields, it's easy to cut them out in your own BaseTree subclass. + */ + public class CommonTree extends BaseTree { + /** A single token is the payload */ + protected var _token:Token; + + /** What token indexes bracket all tokens associated with this node + * and below? + */ + public var startIndex:int=-1, stopIndex:int=-1; + + /** Who is the parent node of this node; if null, implies node is root */ + protected var _parent:CommonTree; + + /** What index is this node in the child list? Range: 0..n-1 */ + protected var _childIndex:int = -1; + + public function CommonTree(node:CommonTree = null) { + if (node != null) { + super(node); + this._token = node._token; + this.startIndex = node.startIndex; + this.stopIndex = node.stopIndex; + } + } + + public static function createFromToken(t:Token):CommonTree { + var ct:CommonTree = new CommonTree(); + ct._token = t; + return ct; + } + + public function get token():Token { + return _token; + } + + public override function dupNode():Tree { + return new CommonTree(this); + } + + public override function get isNil():Boolean { + return _token==null; + } + + public override function get type():int { + if ( _token==null ) { + return TokenConstants.INVALID_TOKEN_TYPE; + } + return _token.type; + } + + public override function get text():String { + if ( _token==null ) { + return null; + } + return _token.text; + } + + public override function get line():int { + if ( _token==null || _token.line==0 ) { + if ( childCount >0 ) { + return getChild(0).line; + } + return 0; + } + return _token.line; + } + + public override function get charPositionInLine():int { + if ( _token==null || _token.charPositionInLine==-1 ) { + if ( childCount>0 ) { + return getChild(0).charPositionInLine; + } + return 0; + } + return _token.charPositionInLine; + } + + public override function get tokenStartIndex():int { + if ( startIndex==-1 && _token!=null ) { + return _token.tokenIndex; + } + return startIndex; + } + + public override function set tokenStartIndex(index:int):void { + startIndex = index; + } + + public override function get tokenStopIndex():int { + if ( stopIndex==-1 && _token!=null ) { + return _token.tokenIndex; + } + return stopIndex; + } + + public override function set tokenStopIndex(index:int):void { + stopIndex = index; + } + + public override function get childIndex():int { + return _childIndex; + } + + public override function get parent():Tree { + return _parent; + } + + public override function set parent(t:Tree):void { + this._parent = CommonTree(t); + } + + public override function set childIndex(index:int):void { + this._childIndex = index; + } + + public function toString():String { + if ( isNil ) { + return "nil"; + } + if ( type==TokenConstants.INVALID_TOKEN_TYPE ) { + return "<errornode>"; + } + if ( token==null ) { + return null; + } + return _token.text; + } + } + +} diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/CommonTreeAdaptor.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/CommonTreeAdaptor.as new file mode 100644 index 0000000..b89fc3b --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/CommonTreeAdaptor.as @@ -0,0 +1,172 @@ +package org.antlr.runtime.tree { + import org.antlr.runtime.CommonToken; + import org.antlr.runtime.Token; + import org.antlr.runtime.TokenConstants; + + /** A TreeAdaptor that works with any Tree implementation. It provides + * really just factory methods; all the work is done by BaseTreeAdaptor. + * If you would like to have different tokens created than ClassicToken + * objects, you need to override this and then set the parser tree adaptor to + * use your subclass. + * + * To get your parser to build nodes of a different type, override + * create(Token), errorNode(), and to be safe, YourTreeClass.dupNode(). + * dupNode is called to duplicate nodes during rewrite operations. + */ + public class CommonTreeAdaptor extends BaseTreeAdaptor { + /** Duplicate a node. This is part of the factory; + * override if you want another kind of node to be built. + * + * I could use reflection to prevent having to override this + * but reflection is slow. + */ + public override function dupNode(t:Object):Object { + if ( t==null ) { + return null; + } + return (Tree(t)).dupNode(); + } + + public override function createWithPayload(payload:Token):Object { + return CommonTree.createFromToken(payload); + } + + /** Tell me how to create a token for use with imaginary token nodes. + * For example, there is probably no input symbol associated with imaginary + * token DECL, but you need to create it as a payload or whatever for + * the DECL node as in ^(DECL type ID). + * + * If you care what the token payload objects' type is, you should + * override this method and any other createToken variant. + */ + public override function createTokenFromType(tokenType:int, text:String):Token { + return new CommonToken(tokenType, text); + } + + /** Tell me how to create a token for use with imaginary token nodes. + * For example, there is probably no input symbol associated with imaginary + * token DECL, but you need to create it as a payload or whatever for + * the DECL node as in ^(DECL type ID). + * + * This is a variant of createToken where the new token is derived from + * an actual real input token. Typically this is for converting '{' + * tokens to BLOCK etc... You'll see + * + * r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ; + * + * If you care what the token payload objects' type is, you should + * override this method and any other createToken variant. + */ + public override function createToken(fromToken:Token):Token { + return CommonToken.cloneToken(fromToken); + } + + /** Track start/stop token for subtree root created for a rule. + * Only works with Tree nodes. For rules that match nothing, + * seems like this will yield start=i and stop=i-1 in a nil node. + * Might be useful info so I'll not force to be i..i. + */ + public override function setTokenBoundaries(t:Object, startToken:Token, stopToken:Token):void { + if ( t==null ) { + return; + } + var start:int = 0; + var stop:int = 0; + if ( startToken!=null ) { + start = startToken.tokenIndex; + } + if ( stopToken!=null ) { + stop = stopToken.tokenIndex; + } + Tree(t).tokenStartIndex = start; + Tree(t).tokenStopIndex = stop; + } + + public override function getTokenStartIndex(t:Object):int { + if ( t==null ) { + return -1; + } + return Tree(t).tokenStartIndex; + } + + public override function getTokenStopIndex(t:Object):int { + if ( t==null ) { + return -1; + } + return Tree(t).tokenStopIndex; + } + + public override function getText(t:Object):String { + if ( t==null ) { + return null; + } + return Tree(t).text; + } + + public override function getType(t:Object):int { + if ( t==null ) { + return TokenConstants.INVALID_TOKEN_TYPE; + } + return Tree(t).type;; + } + + /** What is the Token associated with this node? If + * you are not using CommonTree, then you must + * override this in your own adaptor. + */ + public override function getToken(t:Object):Token { + if ( t is CommonTree ) { + return CommonTree(t).token; + } + return null; // no idea what to do + } + + public override function getChild(t:Object, i:int):Object { + if ( t==null ) { + return null; + } + return Tree(t).getChild(i); + } + + public override function getChildCount(t:Object):int { + if ( t==null ) { + return 0; + } + return Tree(t).childCount; + } + + public override function getParent(t:Object):Object { + if (t == null) { + return null; + } + return Tree(t).parent; + } + + public override function setParent(t:Object, parent:Object):void { + if (t != null) { + Tree(t).parent = Tree(parent); + } + } + + public override function getChildIndex(t:Object):int { + if (t == null) { + return 0; + } + return Tree(t).childIndex; + } + + public override function setChildIndex(t:Object, index:int):void { + if (t != null) { + Tree(t).childIndex = index; + } + } + + public override function replaceChildren(parent:Object, startChildIndex:int, stopChildIndex:int, t:Object):void { + if ( parent!=null ) { + Tree(parent).replaceChildren(startChildIndex, stopChildIndex, t); + } + } + + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/CommonTreeNodeStream.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/CommonTreeNodeStream.as new file mode 100644 index 0000000..c79aa08 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/CommonTreeNodeStream.as @@ -0,0 +1,438 @@ +/* +[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. +*/ +package org.antlr.runtime.tree { + + import org.antlr.runtime.TokenConstants; + import org.antlr.runtime.TokenStream; + + + /** A buffered stream of tree nodes. Nodes can be from a tree of ANY kind. + * + * This node stream sucks all nodes out of the tree specified in + * the constructor during construction and makes pointers into + * the tree using an array of Object pointers. The stream necessarily + * includes pointers to DOWN and UP and EOF nodes. + * + * This stream knows how to mark/release for backtracking. + * + * This stream is most suitable for tree interpreters that need to + * jump around a lot or for tree parsers requiring speed (at cost of memory). + * There is some duplicated functionality here with UnBufferedTreeNodeStream + * but just in bookkeeping, not tree walking etc... + * + * @see UnBufferedTreeNodeStream + */ + public class CommonTreeNodeStream implements TreeNodeStream { + public static const DEFAULT_INITIAL_BUFFER_SIZE:int = 100; + public static const INITIAL_CALL_STACK_SIZE:int = 10; + + // all these navigation nodes are shared and hence they + // cannot contain any line/column info + + protected var down:Object; + protected var up:Object; + protected var eof:Object; + + /** The complete mapping from stream index to tree node. + * This buffer includes pointers to DOWN, UP, and EOF nodes. + * It is built upon ctor invocation. The elements are type + * Object as we don't what the trees look like. + * + * Load upon first need of the buffer so we can set token types + * of interest for reverseIndexing. Slows us down a wee bit to + * do all of the if p==-1 testing everywhere though. + */ + protected var nodes:Array; + + /** Pull nodes from which tree? */ + protected var root:Object; + + /** IF this tree (root) was created from a token stream, track it. */ + protected var tokens:TokenStream; + + /** What tree adaptor was used to build these trees */ + internal var adaptor:TreeAdaptor; + + /** Reuse same DOWN, UP navigation nodes unless this is true */ + protected var uniqueNavigationNodes:Boolean = false; + + /** The index into the nodes list of the current node (next node + * to consume). If -1, nodes array not filled yet. + */ + protected var p:int = -1; + + /** Track the last mark() call result value for use in rewind(). */ + protected var lastMarker:int; + + /** Stack of indexes used for push/pop calls */ + protected var calls:Array; + + public function CommonTreeNodeStream(tree:Object, adaptor:TreeAdaptor = null, initialBufferSize:int = DEFAULT_INITIAL_BUFFER_SIZE) { + if (tree == null) { + // return uninitalized for static resuse function + return; + } + this.root = tree; + this.adaptor = adaptor == null ? new CommonTreeAdaptor() : adaptor; + + nodes = new Array(); + down = this.adaptor.createFromType(TokenConstants.DOWN, "DOWN"); + up = this.adaptor.createFromType(TokenConstants.UP, "UP"); + eof = this.adaptor.createFromType(TokenConstants.EOF, "EOF"); + } + + /** Reuse an existing node stream's buffer of nodes. Do not point at a + * node stream that can change. Must have static node list. start/stop + * are indexes into the parent.nodes stream. We avoid making a new + * list of nodes like this. + */ + public static function reuse(parent:CommonTreeNodeStream, start:int, stop:int):CommonTreeNodeStream { + var stream:CommonTreeNodeStream = new CommonTreeNodeStream(null); + stream.root = parent.root; + stream.adaptor = parent.adaptor; + stream.nodes = parent.nodes.slice(start, stop); + stream.down = parent.down; + stream.up = parent.up; + stream.eof = parent.eof; + return stream; + } + + /** Walk tree with depth-first-search and fill nodes buffer. + * Don't do DOWN, UP nodes if its a list (t is isNil). + */ + protected function fillBuffer():void { + fillBufferTo(root); + //System.out.println("revIndex="+tokenTypeToStreamIndexesMap); + p = 0; // buffer of nodes intialized now + } + + public function fillBufferTo(t:Object):void { + var nil:Boolean = adaptor.isNil(t); + if ( !nil ) { + nodes.push(t); // add this node + } + // add DOWN node if t has children + var n:int = adaptor.getChildCount(t); + if ( !nil && n>0 ) { + addNavigationNode(TokenConstants.DOWN); + } + // and now add all its children + for (var c:int=0; c<n; c++) { + var child:Object = adaptor.getChild(t,c); + fillBufferTo(child); + } + // add UP node if t has children + if ( !nil && n>0 ) { + addNavigationNode(TokenConstants.UP); + } + } + + /** What is the stream index for node? 0..n-1 + * Return -1 if node not found. + */ + protected function getNodeIndex(node:Object):int { + if ( p==-1 ) { + fillBuffer(); + } + for (var i:int = 0; i < nodes.length; i++) { + var t:Object = nodes[i]; + if ( t===node ) { + return i; + } + } + return -1; + } + + /** As we flatten the tree, we use UP, DOWN nodes to represent + * the tree structure. When debugging we need unique nodes + * so instantiate new ones when uniqueNavigationNodes is true. + */ + protected function addNavigationNode(ttype:int):void { + var navNode:Object = null; + if ( ttype==TokenConstants.DOWN ) { + if ( hasUniqueNavigationNodes) { + navNode = adaptor.createFromType(TokenConstants.DOWN, "DOWN"); + } + else { + navNode = down; + } + } + else { + if ( hasUniqueNavigationNodes ) { + navNode = adaptor.createFromType(TokenConstants.UP, "UP"); + } + else { + navNode = up; + } + } + nodes.push(navNode); + } + + public function getNode(i:int):Object { + if ( p==-1 ) { + fillBuffer(); + } + return nodes[i]; + } + + public function LT(k:int):Object { + if ( p==-1 ) { + fillBuffer(); + } + if ( k==0 ) { + return null; + } + if ( k<0 ) { + return LB(-k); + } + //System.out.print("LT(p="+p+","+k+")="); + if ( (p+k-1) >= nodes.length ) { + return eof; + } + return nodes[p+k-1]; + } + + public function get currentSymbol():Object { return LT(1); } + + /** Look backwards k nodes */ + protected function LB(k:int):Object { + if ( k==0 ) { + return null; + } + if ( (p-k)<0 ) { + return null; + } + return nodes[p-k]; + } + + public function get treeSource():Object { + return root; + } + + public function get sourceName():String { + return tokenStream.sourceName; + } + + public function get tokenStream():TokenStream { + return tokens; + } + + public function set tokenStream(tokens:TokenStream):void { + this.tokens = tokens; + } + + public function get treeAdaptor():TreeAdaptor { + return adaptor; + } + + public function set treeAdaptor(adaptor:TreeAdaptor):void { + this.adaptor = adaptor; + } + + public function get hasUniqueNavigationNodes():Boolean { + return uniqueNavigationNodes; + } + + public function set hasUniqueNavigationNodes(uniqueNavigationNodes:Boolean):void { + this.uniqueNavigationNodes = uniqueNavigationNodes; + } + + public function consume():void { + if ( p==-1 ) { + fillBuffer(); + } + p++; + } + + public function LA(i:int):int { + return adaptor.getType(LT(i)); + } + + public function mark():int { + if ( p==-1 ) { + fillBuffer(); + } + lastMarker = index; + return lastMarker; + } + + public function release(marker:int):void { + // no resources to release + } + + public function get index():int { + return p; + } + + public function rewindTo(marker:int):void { + seek(marker); + } + + public function rewind():void { + seek(lastMarker); + } + + public function seek(index:int):void { + if ( p==-1 ) { + fillBuffer(); + } + p = index; + } + + /** Make stream jump to a new location, saving old location. + * Switch back with pop(). + */ + public function push(index:int):void { + if ( calls==null ) { + calls = new Array(); + } + calls.push(p); // save current index + seek(index); + } + + /** Seek back to previous index saved during last push() call. + * Return top of stack (return index). + */ + public function pop():int { + var ret:int = calls.pop(); + seek(ret); + return ret; + } + + public function reset():void { + p = 0; + lastMarker = 0; + if (calls != null) { + calls = new Array(); + } + } + + public function get size():int { + if ( p==-1 ) { + fillBuffer(); + } + return nodes.length; + } + + // TREE REWRITE INTERFACE + public function replaceChildren(parent:Object, startChildIndex:int, stopChildIndex:int, t:Object):void { + if ( parent!=null ) { + adaptor.replaceChildren(parent, startChildIndex, stopChildIndex, t); + } + } + + /** Used for testing, just return the token type stream */ + public function toString():String { + if ( p==-1 ) { + fillBuffer(); + } + var buf:String = ""; + for (var i:int = 0; i < nodes.length; i++) { + var t:Object = nodes[i]; + buf += " "; + buf += (adaptor.getType(t)); + } + return buf.toString(); + } + + /** Debugging */ + public function toTokenString(start:int, stop:int):String { + if ( p==-1 ) { + fillBuffer(); + } + var buf:String = ""; + for (var i:int = start; i < nodes.size() && i <= stop; i++) { + var t:Object = nodes[i]; + buf += " "; + buf += adaptor.getToken(t); + } + return buf; + } + + public function toStringWithRange(start:Object, stop:Object):String { + if ( start==null || stop==null ) { + return null; + } + if ( p==-1 ) { + fillBuffer(); + } + trace("stop: "+stop); + if ( start is CommonTree ) + trace("toString: "+CommonTree(start).token+", "); + else + trace(start); + if ( stop is CommonTree ) + trace(CommonTree(stop).token); + else + trace(stop); + // if we have the token stream, use that to dump text in order + if ( tokens!=null ) { + var beginTokenIndex:int = adaptor.getTokenStartIndex(start); + var endTokenIndex:int = adaptor.getTokenStopIndex(stop); + // if it's a tree, use start/stop index from start node + // else use token range from start/stop nodes + if ( adaptor.getType(stop)==TokenConstants.UP ) { + endTokenIndex = adaptor.getTokenStopIndex(start); + } + else if ( adaptor.getType(stop)==TokenConstants.EOF ) { + endTokenIndex = size-2; // don't use EOF + } + return tokens.toStringWithRange(beginTokenIndex, endTokenIndex); + } + // walk nodes looking for start + var t:Object = null; + var i:int = 0; + for (; i < nodes.length; i++) { + t = nodes[i]; + if ( t==start ) { + break; + } + } + // now walk until we see stop, filling string buffer with text + var buf:String = ""; + t = nodes[i]; + while ( t!=stop ) { + var text:String = adaptor.getText(t); + if ( text==null ) { + text = " "+ adaptor.getType(t); + } + buf += text; + i++; + t = nodes[i]; + } + // include stop node too + text = adaptor.getText(stop); + if ( text==null ) { + text = " " + adaptor.getType(stop); + } + buf += text; + return buf.toString(); + } + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteCardinalityException.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteCardinalityException.as new file mode 100644 index 0000000..ff92744 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteCardinalityException.as @@ -0,0 +1,49 @@ +/* + [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. +*/ +package org.antlr.runtime.tree { + /** Base class for all exceptions thrown during AST rewrite construction. + * This signifies a case where the cardinality of two or more elements + * in a subrule are different: (ID INT)+ where |ID|!=|INT| + */ + public class RewriteCardinalityException extends Error { + + public var elementDescription:String; + + public function RewriteCardinalityException(elementDescription:String) { + super(elementDescription); + this.elementDescription = elementDescription; + } + + public function getMessage():String { + if ( elementDescription!=null ) { + return elementDescription; + } + return null; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteEarlyExitException.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteEarlyExitException.as new file mode 100644 index 0000000..a8a9359 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteEarlyExitException.as @@ -0,0 +1,38 @@ +/* + [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. +*/ +package org.antlr.runtime.tree { + + /** No elements within a (...)+ in a rewrite rule */ + public class RewriteEarlyExitException extends RewriteCardinalityException { + + public function RewriteEarlyExitException(elementDescription:String = null) { + super(elementDescription); + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteEmptyStreamException.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteEmptyStreamException.as new file mode 100644 index 0000000..5fe9901 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteEmptyStreamException.as @@ -0,0 +1,37 @@ +/* + [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. +*/ +package org.antlr.runtime.tree +{ + /** Ref to ID or expr but no tokens in ID stream or subtrees in expr stream */ + public class RewriteEmptyStreamException extends RewriteCardinalityException { + public function RewriteEmptyStreamException(elementDescription:String) { + super(elementDescription); + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteRuleElementStream.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteRuleElementStream.as new file mode 100644 index 0000000..b667683 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteRuleElementStream.as @@ -0,0 +1,200 @@ +/* + [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. +*/ +package org.antlr.runtime.tree { + + /** A generic list of elements tracked in an alternative to be used in + * a -> rewrite rule. We need to subclass to fill in the next() method, + * which returns either an AST node wrapped around a token payload or + * an existing subtree. + * + * Once you start next()ing, do not try to add more elements. It will + * break the cursor tracking I believe. + * + * @see org.antlr.runtime.tree.RewriteRuleSubtreeStream + * @see org.antlr.runtime.tree.RewriteRuleTokenStream + * + * TODO: add mechanism to detect/puke on modification after reading from stream + */ + public class RewriteRuleElementStream { + /** Cursor 0..n-1. If singleElement!=null, cursor is 0 until you next(), + * which bumps it to 1 meaning no more elements. + */ + protected var cursor:int = 0; + + /** Track single elements w/o creating a list. Upon 2nd add, alloc list */ + protected var singleElement:Object; + + /** The list of tokens or subtrees we are tracking */ + protected var elements:Array; + + /** Once a node / subtree has been used in a stream, it must be dup'd + * from then on. Streams are reset after subrules so that the streams + * can be reused in future subrules. So, reset must set a dirty bit. + * If dirty, then next() always returns a dup. + * + * I wanted to use "naughty bit" here, but couldn't think of a way + * to use "naughty". + */ + protected var dirty:Boolean = false; + + /** The element or stream description; usually has name of the token or + * rule reference that this list tracks. Can include rulename too, but + * the exception would track that info. + */ + protected var elementDescription:String; + protected var adaptor:TreeAdaptor; + + public function RewriteRuleElementStream(adaptor:TreeAdaptor, elementDescription:String, element:Object = null) { + this.elementDescription = elementDescription; + this.adaptor = adaptor; + if (element != null) { + if (element is Array) { + /** Create a stream, but feed off an existing list */ + this.elements = element as Array; + } + else { + /** Create a stream with one element */ + add(element); + } + } + } + + /** Reset the condition of this stream so that it appears we have + * not consumed any of its elements. Elements themselves are untouched. + * Once we reset the stream, any future use will need duplicates. Set + * the dirty bit. + */ + public function reset():void { + cursor = 0; + dirty = true; + } + + public function add(el:Object):void { + //System.out.println("add '"+elementDescription+"' is "+el); + if ( el==null ) { + return; + } + if ( elements!=null ) { // if in list, just add + elements.push(el); + return; + } + if ( singleElement == null ) { // no elements yet, track w/o list + singleElement = el; + return; + } + // adding 2nd element, move to list + elements = new Array(5); + elements.push(singleElement); + singleElement = null; + elements.push(el); + } + + /** Return the next element in the stream. If out of elements, throw + * an exception unless size()==1. If size is 1, then return elements[0]. + * Return a duplicate node/subtree if stream is out of elements and + * size==1. If we've already used the element, dup (dirty bit set). + */ + public function nextTree():Object { + var n:int = size; + var el:Object; + if ( dirty || (cursor>=n && n==1) ) { + // if out of elements and size is 1, dup + el = _next(); + return dup(el); + } + // test size above then fetch + el = _next(); + return el; + } + + /** do the work of getting the next element, making sure that it's + * a tree node or subtree. Deal with the optimization of single- + * element list versus list of size > 1. Throw an exception + * if the stream is empty or we're out of elements and size>1. + * protected so you can override in a subclass if necessary. + */ + protected function _next():Object { + var n:int = size; + if ( n ==0 ) { + throw new RewriteEmptyStreamException(elementDescription); + } + if ( cursor>= n) { // out of elements? + if ( n ==1 ) { // if size is 1, it's ok; return and we'll dup + return toTree(singleElement); + } + // out of elements and size was not 1, so we can't dup + throw new RewriteCardinalityException(elementDescription); + } + // we have elements + if ( singleElement!=null ) { + cursor++; // move cursor even for single element list + return toTree(singleElement); + } + // must have more than one in list, pull from elements + var o:Object = toTree(elements[cursor]); + cursor++; + return o; + } + + /** When constructing trees, sometimes we need to dup a token or AST + * subtree. Dup'ing a token means just creating another AST node + * around it. For trees, you must call the adaptor.dupTree() unless + * the element is for a tree root; then it must be a node dup. + */ + protected function dup(el:Object):Object { + throw new Error("Not implemented"); // should be abstract + } + + /** Ensure stream emits trees; tokens must be converted to AST nodes. + * AST nodes can be passed through unmolested. + */ + protected function toTree(el:Object):Object { + return el; + } + + public function get hasNext():Boolean { + return (singleElement != null && cursor < 1) || + (elements!=null && cursor < elements.length); + } + + public function get size():int { + var n:int = 0; + if ( singleElement != null ) { + n = 1; + } + if ( elements!=null ) { + return elements.length; + } + return n; + } + + public function get description():String { + return elementDescription; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteRuleNodeStream.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteRuleNodeStream.as new file mode 100644 index 0000000..a7ab2e8 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteRuleNodeStream.as @@ -0,0 +1,27 @@ + +package org.antlr.runtime.tree { + + /** Queues up nodes matched on left side of -> in a tree parser. This is + * the analog of RewriteRuleTokenStream for normal parsers. + */ + public class RewriteRuleNodeStream extends RewriteRuleElementStream { + + public function RewriteRuleNodeStream(adaptor:TreeAdaptor, elementDescription:String, element:Object = null) { + super(adaptor, elementDescription, element); + } + + public function nextNode():Object { + return _next(); + } + + protected override function toTree(el:Object):Object { + return adaptor.dupNode(el); + } + + protected override function dup(el:Object):Object { + // we dup every node, so don't have to worry about calling dup; short- + // circuited next() so it doesn't call. + throw new Error("dup can't be called for a node stream."); + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteRuleSubtreeStream.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteRuleSubtreeStream.as new file mode 100644 index 0000000..d499fef --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteRuleSubtreeStream.as @@ -0,0 +1,67 @@ +/* + [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. +*/ +package org.antlr.runtime.tree { + + public class RewriteRuleSubtreeStream extends RewriteRuleElementStream { + + public function RewriteRuleSubtreeStream(adaptor:TreeAdaptor, elementDescription:String, element:Object=null) { + super(adaptor, elementDescription, element); + } + + /** Treat next element as a single node even if it's a subtree. + * This is used instead of next() when the result has to be a + * tree root node. Also prevents us from duplicating recently-added + * children; e.g., ^(type ID)+ adds ID to type and then 2nd iteration + * must dup the type node, but ID has been added. + * + * Referencing a rule result twice is ok; dup entire tree as + * we can't be adding trees as root; e.g., expr expr. + * + * Hideous code duplication here with super.next(). Can't think of + * a proper way to refactor. This needs to always call dup node + * and super.next() doesn't know which to call: dup node or dup tree. + */ + public function nextNode():Object { + var n:int = size; + var el:Object; + if ( dirty || (cursor>=n && n==1) ) { + // if out of elements and size is 1, dup (at most a single node + // since this is for making root nodes). + el = _next(); + return adaptor.dupNode(el); + } + // test size above then fetch + el = _next(); + return el; + } + + protected override function dup(el:Object):Object { + return adaptor.dupTree(el); + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteRuleTokenStream.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteRuleTokenStream.as new file mode 100644 index 0000000..beac244 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/RewriteRuleTokenStream.as @@ -0,0 +1,60 @@ +/* + [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. +*/ +package org.antlr.runtime.tree { + import org.antlr.runtime.Token; + + + public class RewriteRuleTokenStream extends RewriteRuleElementStream { + + public function RewriteRuleTokenStream(adaptor:TreeAdaptor, elementDescription:String, element:Object=null) { + super(adaptor, elementDescription, element); + } + + /** Get next token from stream and make a node for it */ + public function nextNode():Object { + var t:Token = Token(_next()); + return adaptor.createWithPayload(t); + } + + public function nextToken():Token { + return Token(_next()); + } + + /** Don't convert to a tree unless they explicitly call nextTree. + * This way we can do hetero tree nodes in rewrite. + */ + protected override function toTree(el:Object):Object { + return el; + } + + protected override function dup(el:Object):Object { + throw new Error("dup can't be called for a token stream."); + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/Tree.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/Tree.as new file mode 100644 index 0000000..7a402d5 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/Tree.as @@ -0,0 +1,96 @@ +package org.antlr.runtime.tree { + /** What does a tree look like? ANTLR has a number of support classes + * such as CommonTreeNodeStream that work on these kinds of trees. You + * don't have to make your trees implement this interface, but if you do, + * you'll be able to use more support code. + * + * NOTE: When constructing trees, ANTLR can build any kind of tree; it can + * even use Token objects as trees if you add a child list to your tokens. + * + * This is a tree node without any payload; just navigation and factory stuff. + */ + public interface Tree { + + function getChild(i:int):Tree; + + function get childCount():int; + + // Tree tracks parent and child index now > 3.0 + + function get parent():Tree; + + function set parent(t:Tree):void; + + /** Is there is a node above with token type ttype? */ + function hasAncestor(ttype:int):Boolean; + + /** Walk upwards and get first ancestor with this token type. */ + function getAncestor(ttype:int):Tree; + + /** Return a list of all ancestors of this node. The first node of + * list is the root and the last is the parent of this node. + */ + function get ancestors():Array; + + + /** This node is what child index? 0..n-1 */ + function get childIndex():int; + + function set childIndex(index:int):void; + + /** Set the parent and child index values for all children */ + function freshenParentAndChildIndexes():void; + + /** Add t as a child to this node. If t is null, do nothing. If t + * is nil, add all children of t to this' children. + */ + function addChild(t:Tree):void; + + /** Set ith child (0..n-1) to t; t must be non-null and non-nil node */ + function setChild(i:int, t:Tree):void; + + function deleteChild(i:int):Object; + + /** Delete children from start to stop and replace with t even if t is + * a list (nil-root tree). num of children can increase or decrease. + * For huge child lists, inserting children can force walking rest of + * children to set their childindex; could be slow. + */ + function replaceChildren(startChildIndex:int, stopChildIndex:int, t:Object):void; + + /** Indicates the node is a nil node but may still have children, meaning + * the tree is a flat list. + */ + function get isNil():Boolean; + + /** What is the smallest token index (indexing from 0) for this node + * and its children? + */ + function get tokenStartIndex():int; + + function set tokenStartIndex(index:int):void; + + /** What is the largest token index (indexing from 0) for this node + * and its children? + */ + function get tokenStopIndex():int; + + function set tokenStopIndex(index:int):void; + + function dupNode():Tree; + + /** Return a token type; needed for tree parsing */ + function get type():int; + + function get text():String; + + /** In case we don't have a token payload, what is the line for errors? */ + function get line():int; + + function get charPositionInLine():int; + + function toStringTree():String; + + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/TreeAdaptor.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/TreeAdaptor.as new file mode 100644 index 0000000..a8d5d61 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/TreeAdaptor.as @@ -0,0 +1,253 @@ +/* + [The "BSD licence"] + Copyright (c) 2005-2007 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. +*/ +package org.antlr.runtime.tree { + + import org.antlr.runtime.*; + + /** How to create and navigate trees. Rather than have a separate factory + * and adaptor, I've merged them. Makes sense to encapsulate. + * + * This takes the place of the tree construction code generated in the + * generated code in 2.x and the ASTFactory. + * + * I do not need to know the type of a tree at all so they are all + * generic Objects. This may increase the amount of typecasting needed. :( + */ + public interface TreeAdaptor { + // C o n s t r u c t i o n + + /** Create a tree node from Token object; for CommonTree type trees, + * then the token just becomes the payload. This is the most + * common create call. + * + * Override if you want another kind of node to be built. + */ + function createWithPayload(payload:Token):Object; + + /** Duplicate a single tree node. + * Override if you want another kind of node to be built. + */ + function dupNode(treeNode:Object):Object; + + /** Duplicate tree recursively, using dupNode() for each node */ + function dupTree(tree:Object):Object; + + /** Return a nil node (an empty but non-null node) that can hold + * a list of element as the children. If you want a flat tree (a list) + * use "t=adaptor.nil(); t.addChild(x); t.addChild(y);" + */ + function nil():Object; + + /** Return a tree node representing an error. This node records the + * tokens consumed during error recovery. The start token indicates the + * input symbol at which the error was detected. The stop token indicates + * the last symbol consumed during recovery. + * + * You must specify the input stream so that the erroneous text can + * be packaged up in the error node. The exception could be useful + * to some applications; default implementation stores ptr to it in + * the CommonErrorNode. + * + * This only makes sense during token parsing, not tree parsing. + * Tree parsing should happen only when parsing and tree construction + * succeed. + */ + function errorNode(input:TokenStream, start:Token, stop:Token, e:RecognitionException):Object; + + /** Is tree considered a nil node used to make lists of child nodes? */ + function isNil(tree:Object):Boolean; + + /** Add a child to the tree t. If child is a flat tree (a list), make all + * in list children of t. Warning: if t has no children, but child does + * and child isNil then you can decide it is ok to move children to t via + * t.children = child.children; i.e., without copying the array. Just + * make sure that this is consistent with have the user will build + * ASTs. Do nothing if t or child is null. + */ + function addChild(t:Object, child:Object):void; + + /** If oldRoot is a nil root, just copy or move the children to newRoot. + * If not a nil root, make oldRoot a child of newRoot. + * + * old=^(nil a b c), new=r yields ^(r a b c) + * old=^(a b c), new=r yields ^(r ^(a b c)) + * + * If newRoot is a nil-rooted single child tree, use the single + * child as the new root node. + * + * old=^(nil a b c), new=^(nil r) yields ^(r a b c) + * old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) + * + * If oldRoot was null, it's ok, just return newRoot (even if isNil). + * + * old=null, new=r yields r + * old=null, new=^(nil r) yields ^(nil r) + * + * Return newRoot. Throw an exception if newRoot is not a + * simple node or nil root with a single child node--it must be a root + * node. If newRoot is ^(nil x) return x as newRoot. + * + * Be advised that it's ok for newRoot to point at oldRoot's + * children; i.e., you don't have to copy the list. We are + * constructing these nodes so we should have this control for + * efficiency. + */ + function becomeRoot(newRoot:Object, oldRoot:Object):Object; + + /** Given the root of the subtree created for this rule, post process + * it to do any simplifications or whatever you want. A required + * behavior is to convert ^(nil singleSubtree) to singleSubtree + * as the setting of start/stop indexes relies on a single non-nil root + * for non-flat trees. + * + * Flat trees such as for lists like "idlist : ID+ ;" are left alone + * unless there is only one ID. For a list, the start/stop indexes + * are set in the nil node. + * + * This method is executed after all rule tree construction and right + * before setTokenBoundaries(). + */ + function rulePostProcessing(root:Object):Object; + + /** For identifying trees. + * + * How to identify nodes so we can say "add node to a prior node"? + * Even becomeRoot is an issue. Use System.identityHashCode(node) + * usually. + */ + function getUniqueID(node:Object):int; + + + // R e w r i t e R u l e s + + /** Create a new node derived from a token, with a new token type. + * This is invoked from an imaginary node ref on right side of a + * rewrite rule as IMAG[$tokenLabel] or IMAG[$tokenLabel, "IMAG"]. + * + * This should invoke createToken(Token). + */ + function createFromToken(tokenType:int, fromToken:Token, text:String = null):Object; + + /** Create a new node derived from a token, with a new token type. + * This is invoked from an imaginary node ref on right side of a + * rewrite rule as IMAG["IMAG"]. + * + * This should invoke createToken(int,String). + */ + function createFromType(tokenType:int, text:String):Object; + + + // C o n t e n t + + /** For tree parsing, I need to know the token type of a node */ + function getType(t:Object):int; + + /** Node constructors can set the type of a node */ + function setType(t:Object, type:int):void; + + function getText(t:Object):String; + + /** Node constructors can set the text of a node */ + function setText(t:Object, text:String):void; + + /** Return the token object from which this node was created. + * Currently used only for printing an error message. + * The error display routine in BaseRecognizer needs to + * display where the input the error occurred. If your + * tree of limitation does not store information that can + * lead you to the token, you can create a token filled with + * the appropriate information and pass that back. See + * BaseRecognizer.getErrorMessage(). + */ + function getToken(t:Object):Token; + + /** Where are the bounds in the input token stream for this node and + * all children? Each rule that creates AST nodes will call this + * method right before returning. Flat trees (i.e., lists) will + * still usually have a nil root node just to hold the children list. + * That node would contain the start/stop indexes then. + */ + function setTokenBoundaries(t:Object, startToken:Token, stopToken:Token):void; + + /** Get the token start index for this subtree; return -1 if no such index */ + function getTokenStartIndex(t:Object):int; + + /** Get the token stop index for this subtree; return -1 if no such index */ + function getTokenStopIndex(t:Object):int; + + + // N a v i g a t i o n / T r e e P a r s i n g + + /** Get a child 0..n-1 node */ + function getChild(t:Object, i:int):Object; + + /** Set ith child (0..n-1) to t; t must be non-null and non-nil node */ + function setChild(t:Object, i:int, child:Object):void; + + /** Remove ith child and shift children down from right. */ + function deleteChild(t:Object, i:int):Object; + + /** How many children? If 0, then this is a leaf node */ + function getChildCount(t:Object):int; + + /** Who is the parent node of this node; if null, implies node is root. + * If your node type doesn't handle this, it's ok but the tree rewrites + * in tree parsers need this functionality. + */ + function getParent(t:Object):Object; + function setParent(t:Object, parent:Object):void; + + /** What index is this node in the child list? Range: 0..n-1 + * If your node type doesn't handle this, it's ok but the tree rewrites + * in tree parsers need this functionality. + */ + function getChildIndex(t:Object):int; + function setChildIndex(t:Object, index:int):void; + + /** Replace from start to stop child index of parent with t, which might + * be a list. Number of children may be different + * after this call. + * + * If parent is null, don't do anything; must be at root of overall tree. + * Can't replace whatever points to the parent externally. Do nothing. + */ + function replaceChildren(parent:Object, startChildIndex:int, stopChildIndex:int, t:Object):void; + + + // Code - generator support - TODO place in separate namespace + + /** + * Private method used by generated code. Based on type and number of arguments will call one of: + * + * * createWithPayload + * * createFromToken + * * createFromType + */ + function create(... args):Object; + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/TreeConstants.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/TreeConstants.as new file mode 100644 index 0000000..d9fad84 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/TreeConstants.as @@ -0,0 +1,8 @@ +package org.antlr.runtime.tree +{ + import org.antlr.runtime.TokenConstants; + + public class TreeConstants { + public static const INVALID_NODE:CommonTree = CommonTree.createFromToken(TokenConstants.INVALID_TOKEN); + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/TreeNodeStream.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/TreeNodeStream.as new file mode 100644 index 0000000..bd13cc8 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/TreeNodeStream.as @@ -0,0 +1,102 @@ +/* +[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. +*/ +package org.antlr.runtime.tree { + import org.antlr.runtime.IntStream; + import org.antlr.runtime.TokenStream; + + /** A stream of tree nodes, accessing nodes from a tree of some kind */ + public interface TreeNodeStream extends IntStream { + /** Get a tree node at an absolute index i; 0..n-1. + * If you don't want to buffer up nodes, then this method makes no + * sense for you. + */ + function getNode(i:int):Object; + + /** Get tree node at current input pointer + i ahead where i=1 is next node. + * i<0 indicates nodes in the past. So LT(-1) is previous node, but + * implementations are not required to provide results for k < -1. + * LT(0) is undefined. For i>=n, return null. + * Return null for LT(0) and any index that results in an absolute address + * that is negative. + * + * This is analogus to the LT() method of the TokenStream, but this + * returns a tree node instead of a token. Makes code gen identical + * for both parser and tree grammars. :) + */ + function LT(k:int):Object; + + /** Where is this stream pulling nodes from? This is not the name, but + * the object that provides node objects. + */ + function get treeSource():Object; + + /** If the tree associated with this stream was created from a TokenStream, + * you can specify it here. Used to do rule $text attribute in tree + * parser. Optional unless you use tree parser rule text attribute + * or output=template and rewrite=true options. + */ + function get tokenStream():TokenStream; + + /** What adaptor can tell me how to interpret/navigate nodes and + * trees. E.g., get text of a node. + */ + function get treeAdaptor():TreeAdaptor; + + /** As we flatten the tree, we use UP, DOWN nodes to represent + * the tree structure. When debugging we need unique nodes + * so we have to instantiate new ones. When doing normal tree + * parsing, it's slow and a waste of memory to create unique + * navigation nodes. Default should be false; + */ + function set hasUniqueNavigationNodes(uniqueNavigationNodes:Boolean):void; + + /** Return the text of all nodes from start to stop, inclusive. + * If the stream does not buffer all the nodes then it can still + * walk recursively from start until stop. You can always return + * null or "" too, but users should not access $ruleLabel.text in + * an action of course in that case. + */ + function toStringWithRange(start:Object, stop:Object):String; + + // REWRITING TREES (used by tree parser) + + /** Replace from start to stop child index of parent with t, which might + * be a list. Number of children may be different + * after this call. The stream is notified because it is walking the + * tree and might need to know you are monkeying with the underlying + * tree. Also, it might be able to modify the node stream to avoid + * restreaming for future phases. + * + * If parent is null, don't do anything; must be at root of overall tree. + * Can't replace whatever points to the parent externally. Do nothing. + */ + function replaceChildren(parent:Object, startChildIndex:int, stopChildIndex:int, t:Object):void; + + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/TreeParser.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/TreeParser.as new file mode 100644 index 0000000..cf0fb3b --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/TreeParser.as @@ -0,0 +1,159 @@ +/* + [The "BSD licence"] + Copyright (c) 2005-2007 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. +*/ +package org.antlr.runtime.tree { + import org.antlr.runtime.*; + + /** A parser for a stream of tree nodes. "tree grammars" result in a subclass + * of this. All the error reporting and recovery is shared with Parser via + * the BaseRecognizer superclass. + */ + public class TreeParser extends BaseRecognizer { + public static const DOWN:int = TokenConstants.DOWN; + public static const UP:int = TokenConstants.UP; + + protected var input:TreeNodeStream; + + public function TreeParser(input:TreeNodeStream, state:RecognizerSharedState = null) { + super(state); + treeNodeStream = input; + } + + public override function reset():void { + super.reset(); // reset all recognizer state variables + if ( input!=null ) { + input.seek(0); // rewind the input + } + } + + /** Set the input stream */ + public function set treeNodeStream(input:TreeNodeStream):void { + this.input = input; + } + + public function get treeNodeStream():TreeNodeStream { + return input; + } + + public override function get sourceName():String { + return input.sourceName; + } + + protected override function getCurrentInputSymbol(input:IntStream):Object { + return TreeNodeStream(input).LT(1); + } + + protected override function getMissingSymbol(input:IntStream, + e:RecognitionException, + expectedTokenType:int, + follow:BitSet):Object { + var tokenText:String = + "<missing "+tokenNames[expectedTokenType]+">"; + return CommonTree.createFromToken(new CommonToken(expectedTokenType, tokenText)); + } + + /** Match '.' in tree parser has special meaning. Skip node or + * entire tree if node has children. If children, scan until + * corresponding UP node. + */ + public function matchAny(ignore:IntStream):void { // ignore stream, copy of this.input + state.errorRecovery = false; + state.failed = false; + var look:Object = input.LT(1); + if ( input.treeAdaptor.getChildCount(look)==0 ) { + input.consume(); // not subtree, consume 1 node and return + return; + } + // current node is a subtree, skip to corresponding UP. + // must count nesting level to get right UP + var level:int=0; + var tokenType:int = input.treeAdaptor.getType(look); + while ( tokenType!=TokenConstants.EOF && !(tokenType==UP && level==0) ) { + input.consume(); + look = input.LT(1); + tokenType = input.treeAdaptor.getType(look); + if ( tokenType == DOWN ) { + level++; + } + else if ( tokenType == UP ) { + level--; + } + } + input.consume(); // consume UP + } + + /** We have DOWN/UP nodes in the stream that have no line info; override. + * plus we want to alter the exception type. Don't try to recover + * from tree parser errors inline... + */ + protected override function mismatch(input:IntStream, ttype:int, follow:BitSet):void { + throw new MismatchedTreeNodeException(ttype, TreeNodeStream(input)); + } + + /** Prefix error message with the grammar name because message is + * always intended for the programmer because the parser built + * the input tree not the user. + */ + public override function getErrorHeader(e:RecognitionException):String { + return grammarFileName+": node from "+ + (e.approximateLineInfo?"after ":"")+"line "+e.line+":"+e.charPositionInLine; + } + + /** Tree parsers parse nodes they usually have a token object as + * payload. Set the exception token and do the default behavior. + */ + public override function getErrorMessage(e:RecognitionException, tokenNames:Array):String { + if ( this is TreeParser ) { + var adaptor:TreeAdaptor = TreeNodeStream(e.input).treeAdaptor; + e.token = adaptor.getToken(e.node); + if ( e.token==null ) { // could be an UP/DOWN node + e.token = new CommonToken(adaptor.getType(e.node), + adaptor.getText(e.node)); + } + } + return super.getErrorMessage(e, tokenNames); + } + + public function set treeAdaptor(adaptor:TreeAdaptor):void { + // do nothing, implemented in generated code + } + + public function get treeAdaptor():TreeAdaptor { + // implementation provided in generated code + return null; + } + + public function traceIn(ruleName:String, ruleIndex:int):void { + super.traceInSymbol(ruleName, ruleIndex, input.LT(1)); + } + + public function traceOut(ruleName:String, ruleIndex:int):void { + super.traceOutSymbol(ruleName, ruleIndex, input.LT(1)); + } + } + +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/TreeRuleReturnScope.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/TreeRuleReturnScope.as new file mode 100644 index 0000000..57a89b7 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/tree/TreeRuleReturnScope.as @@ -0,0 +1,66 @@ +/* + [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. +*/ +package org.antlr.runtime.tree { + + import org.antlr.runtime.RuleReturnScope; + + /** This is identical to the ParserRuleReturnScope except that + * the start property is a tree nodes not Token object + * when you are parsing trees. To be generic the tree node types + * have to be Object. + */ + public class TreeRuleReturnScope extends RuleReturnScope { + + /** First node or root node of tree matched for this rule. */ + protected var _start:Object; + private var _tree:Object; // if output=AST this contains the tree + private var _values:Object = new Object(); // contains the return values + + /** Has a value potentially if output=AST; */ + public override function get tree():Object { + return _tree; + } + + public function set tree(tree:Object):void { + _tree = tree; + } + + public function get values():Object { + return _values; + } + + public override function get start():Object { + return _start; + } + + public function set start(value:Object):void { + _start = value; + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/version.as b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/version.as new file mode 100644 index 0000000..4363c69 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/src/org/antlr/runtime/version.as @@ -0,0 +1,3 @@ +package org.antlr.runtime { + public const version:String = "3.1.3"; +} diff --git a/antlr-3.1.3/runtime/ActionScript/project/test/Antlr3Test.mxml b/antlr-3.1.3/runtime/ActionScript/project/test/Antlr3Test.mxml new file mode 100644 index 0000000..b5b5b72 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/test/Antlr3Test.mxml @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="utf-8"?> +<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="onCreationComplete()"> + + <mx:Script> + <![CDATA[ + import org.antlr.runtime.test.AllTests; + import flexunit.junit.JUnitTestRunner; + + + [Bindable] + private var runner : JUnitTestRunner; + + private function onCreationComplete() : void + { + status.text = "Please wait running test suite..."; + + runner = new JUnitTestRunner(); + runner.run( new AllTests(), onTestComplete ); + } + + private function onTestComplete() : void + { + status.text = "Finished running test suite."; + + fscommand( "quit" ); + } + + ]]> + </mx:Script> + + <mx:Label id="status" /> + +</mx:Application> diff --git a/antlr-3.1.3/runtime/ActionScript/project/test/org/antlr/runtime/test/AllTests.as b/antlr-3.1.3/runtime/ActionScript/project/test/org/antlr/runtime/test/AllTests.as new file mode 100644 index 0000000..c19ab5b --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/test/org/antlr/runtime/test/AllTests.as @@ -0,0 +1,14 @@ +package org.antlr.runtime.test { + import flexunit.framework.TestSuite; + + + public class AllTests extends TestSuite { + + public function AllTests() { + addTest(new TestSuite(TestANTLRStringStream)); + addTest(new TestSuite(TestBitSet)); + addTest(new TestSuite(TestDFA)); + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/test/org/antlr/runtime/test/TestANTLRStringStream.as b/antlr-3.1.3/runtime/ActionScript/project/test/org/antlr/runtime/test/TestANTLRStringStream.as new file mode 100644 index 0000000..9cb5a7b --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/test/org/antlr/runtime/test/TestANTLRStringStream.as @@ -0,0 +1,131 @@ +package org.antlr.runtime.test { + import flexunit.framework.TestCase; + + import org.antlr.runtime.ANTLRStringStream; + import org.antlr.runtime.CharStream; + import org.antlr.runtime.CharStreamConstants; + + public class TestANTLRStringStream extends TestCase { + + public function TestANTLRStringStream() { + super(); + } + + public function testConsume():void { + var stream:CharStream = new ANTLRStringStream("abc"); + assertEquals(stream.size, 3); + assertEquals(stream.charPositionInLine, 0); + assertEquals(stream.line, 1); + assertEquals(stream.index, 0); + + for (var i:int = 0; i < stream.size; i++) { + stream.consume(); + + assertEquals(stream.size, 3); // invariant + assertEquals(stream.charPositionInLine, i + 1); + assertEquals(stream.line, 1); // invariant + assertEquals(stream.index, i + 1); + } + + // now consume past EOF for a few ticks, nothing should change + for (i = 0; i < 5; i++) { + stream.consume(); + + assertEquals(stream.size, 3); // invariant + assertEquals(stream.charPositionInLine, 3); + assertEquals(stream.line, 1); // invariant + assertEquals(stream.index, 3); + } + + } + + public function testLA():void { + var stream:CharStream = new ANTLRStringStream("abc"); + assertEquals(stream.LA(-1), CharStreamConstants.EOF); // should be EOF + assertEquals(stream.LA(0), 0); // should be 0 (undefined) + assertEquals(stream.LA(1), "a".charCodeAt(0)); + assertEquals(stream.LA(2), "b".charCodeAt(0)); + assertEquals(stream.LA(3), "c".charCodeAt(0)); + assertEquals(stream.LA(4), CharStreamConstants.EOF); + + // now consume() one byte and run some more tests. + stream.consume(); + assertEquals(stream.LA(-2), CharStreamConstants.EOF); + assertEquals(stream.LA(-1), "a".charCodeAt(0)); + assertEquals(stream.LA(0), 0); // should be 0 (undefined) + assertEquals(stream.LA(1), "b".charCodeAt(0)); + assertEquals(stream.LA(2), "c".charCodeAt(0)); + assertEquals(stream.LA(3), CharStreamConstants.EOF); + } + + public function testReset():void { + var stream:ANTLRStringStream = new ANTLRStringStream("abc"); + assertEquals(stream.size, 3); + assertEquals(stream.charPositionInLine, 0); + assertEquals(stream.line, 1); + assertEquals(stream.index, 0); + + stream.consume(); + stream.consume(); + + assertEquals(stream.size, 3); + assertEquals(stream.charPositionInLine, 2); + assertEquals(stream.line, 1); + assertEquals(stream.index, 2); + + stream.reset(); + + assertEquals(stream.size, 3); + assertEquals(stream.charPositionInLine, 0); + assertEquals(stream.line, 1); + assertEquals(stream.index, 0); + + } + + public function testMark():void { + var stream:ANTLRStringStream = new ANTLRStringStream("a\nbc"); + + // setup a couple of markers + var mark1:int = stream.mark(); + stream.consume(); + stream.consume(); + var mark2:int = stream.mark(); + stream.consume(); + + // make sure we are where we expect to be + assertEquals(stream.charPositionInLine, 1); + assertEquals(stream.line, 2); + assertEquals(stream.index, 3); + + assertEquals(mark1, 1); + assertTrue(mark1 != mark2); + + stream.rewindTo(mark2); + assertEquals(stream.charPositionInLine, 0); + assertEquals(stream.line, 2); + assertEquals(stream.index, 2); + + stream.rewindTo(mark1); + assertEquals(stream.index, 0); + assertEquals(stream.charPositionInLine, 0); + assertEquals(stream.line, 1); + + // test two-level rewind + mark1 = stream.mark(); + stream.consume(); + stream.consume(); + stream.mark(); + stream.consume(); + + // make sure we are where we expect to be + assertEquals(stream.charPositionInLine, 1); + assertEquals(stream.line, 2); + assertEquals(stream.index, 3); + + stream.rewindTo(mark1); + assertEquals(stream.index, 0); + assertEquals(stream.charPositionInLine, 0); + assertEquals(stream.line, 1); + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/test/org/antlr/runtime/test/TestBitSet.as b/antlr-3.1.3/runtime/ActionScript/project/test/org/antlr/runtime/test/TestBitSet.as new file mode 100644 index 0000000..de40634 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/test/org/antlr/runtime/test/TestBitSet.as @@ -0,0 +1,30 @@ +package org.antlr.runtime.test { + import flexunit.framework.TestCase; + + import org.antlr.runtime.BitSet; + + public class TestBitSet extends TestCase { + + public function testConstructor():void { + // empty + var bitSet:BitSet = new BitSet(); + + assertEquals(0, bitSet.numBits); + assertEquals(0, bitSet.toPackedArray().length); + assertEquals(0, bitSet.size); + assertTrue(bitSet.isNil); + assertEquals("{}", bitSet.toString()); + + bitSet = BitSet.of(0, 1, 2); + assertEquals(32, bitSet.numBits); + assertEquals(1, bitSet.toPackedArray().length); + //assertEquals(1, bitSet.size); + assertFalse(bitSet.isNil); + assertEquals(7, int(bitSet.toPackedArray()[0])); + assertEquals("{0,1,2}", bitSet.toString()); + + + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/ActionScript/project/test/org/antlr/runtime/test/TestDFA.as b/antlr-3.1.3/runtime/ActionScript/project/test/org/antlr/runtime/test/TestDFA.as new file mode 100644 index 0000000..f923bc9 --- /dev/null +++ b/antlr-3.1.3/runtime/ActionScript/project/test/org/antlr/runtime/test/TestDFA.as @@ -0,0 +1,29 @@ +package org.antlr.runtime.test { + import flexunit.framework.TestCase; + + import org.antlr.runtime.DFA; + + public class TestDFA extends TestCase { + + public function testUnpack():void { + // empty + var testVal:String = "\x01\x02\x03\x09"; + assertEquals(4, testVal.length); + assertEquals("2,9,9,9", DFA.unpackEncodedString("\x01\x02\x03\x09")); + + testVal = "\x03\u7fff"; + //testVal = String.fromCharCode(3, 0x7fff); + + assertEquals(2, testVal.length); + assertEquals("32767,32767,32767", DFA.unpackEncodedString(testVal)); + assertEquals("32767,32767,32767", DFA.unpackEncodedString(testVal, true)); + + testVal = "\x02\u80ff\xff"; + assertEquals(3, testVal.length); + assertEquals("-1,-1", DFA.unpackEncodedString(testVal)); + assertEquals("65535,65535", DFA.unpackEncodedString(testVal, true)); + + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/C/AUTHORS b/antlr-3.1.3/runtime/C/AUTHORS new file mode 100644 index 0000000..9195a9d --- /dev/null +++ b/antlr-3.1.3/runtime/C/AUTHORS @@ -0,0 +1,41 @@ +The ANTLR recognizer generator tool was written by (with many contributions) + + Prof. Terence Parr at USF + +The C runtime and related C code generation elements were written by + + Jim Idle (jimi idle ws s/ /./g) with no contributions at all because + nobody else was crazy enough to do it. + +The C runtime and the ANLTR tool itself are distributed under the BSD license +which basically gives the right to so anythign with it, so long as you +recognize the authors. See here: + + [The "BSD licence"] + Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC + http://www.temporal-wave.com + http://www.linkedin.com/in/jimidle + + 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. diff --git a/antlr-3.1.3/runtime/C/C.sln b/antlr-3.1.3/runtime/C/C.sln new file mode 100644 index 0000000..f841177 --- /dev/null +++ b/antlr-3.1.3/runtime/C/C.sln @@ -0,0 +1,53 @@ +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual Studio 2008 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "C", "C.vcproj", "{0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}" +EndProject +Global + GlobalSection(SourceCodeControl) = preSolution + SccNumberOfProjects = 2 + SccProjectName0 = Perforce\u0020Project + SccLocalPath0 = ..\\.. + SccProvider0 = MSSCCI:Perforce\u0020SCM + SccProjectFilePathRelativizedFromConnection0 = runtime\\C\\ + SccProjectUniqueName1 = C.vcproj + SccLocalPath1 = ..\\.. + SccProjectFilePathRelativizedFromConnection1 = runtime\\C\\ + EndGlobalSection + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + DebugDLL|Win32 = DebugDLL|Win32 + DebugDLL|x64 = DebugDLL|x64 + Deployment|Win32 = Deployment|Win32 + Deployment|x64 = Deployment|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + ReleaseDLL|Win32 = ReleaseDLL|Win32 + ReleaseDLL|x64 = ReleaseDLL|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Debug|Win32.ActiveCfg = Debug|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Debug|Win32.Build.0 = Debug|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Debug|x64.ActiveCfg = Debug|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Debug|x64.Build.0 = Debug|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.DebugDLL|Win32.ActiveCfg = DebugDLL|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.DebugDLL|Win32.Build.0 = DebugDLL|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.DebugDLL|x64.ActiveCfg = DebugDLL|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.DebugDLL|x64.Build.0 = DebugDLL|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Deployment|Win32.ActiveCfg = DebugDLL|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Deployment|Win32.Build.0 = DebugDLL|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Deployment|x64.ActiveCfg = DebugDLL|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Deployment|x64.Build.0 = DebugDLL|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Release|Win32.ActiveCfg = Release|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Release|Win32.Build.0 = Release|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Release|x64.ActiveCfg = Release|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Release|x64.Build.0 = Release|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.ReleaseDLL|Win32.ActiveCfg = ReleaseDLL|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.ReleaseDLL|Win32.Build.0 = ReleaseDLL|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.ReleaseDLL|x64.ActiveCfg = ReleaseDLL|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.ReleaseDLL|x64.Build.0 = ReleaseDLL|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/antlr-3.1.3/runtime/C/C.vcproj b/antlr-3.1.3/runtime/C/C.vcproj new file mode 100644 index 0000000..8b8cc05 --- /dev/null +++ b/antlr-3.1.3/runtime/C/C.vcproj @@ -0,0 +1,1064 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="9.00" + Name="C" + ProjectGUID="{0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}" + RootNamespace="C" + SccProjectName="Perforce Project" + SccLocalPath="..\.." + SccProvider="MSSCCI:Perforce SCM" + Keyword="Win32Proj" + TargetFrameworkVersion="131072" + > + <Platforms> + <Platform + Name="Win32" + /> + <Platform + Name="x64" + /> + </Platforms> + <ToolFiles> + </ToolFiles> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="4" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories=""$(SolutionDir)\include";"$(SolutionDir)\..\..\include"" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" + StringPooling="true" + MinimalRebuild="false" + BasicRuntimeChecks="3" + RuntimeLibrary="3" + StructMemberAlignment="0" + EnableFunctionLevelLinking="true" + EnableEnhancedInstructionSet="0" + FloatingPointModel="0" + FloatingPointExceptions="true" + DisableLanguageExtensions="false" + UsePrecompiledHeader="0" + ExpandAttributedSource="true" + AssemblerOutput="2" + BrowseInformation="1" + WarningLevel="4" + WarnAsError="false" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="0" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLibrarianTool" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)/antlr3cd.lib" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Debug|x64" + OutputDirectory="$(PlatformName)\$(ConfigurationName)" + IntermediateDirectory="$(PlatformName)\$(ConfigurationName)" + ConfigurationType="4" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + TargetEnvironment="3" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories=""$(SolutionDir)\include";"$(SolutionDir)\..\..\include"" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" + StringPooling="true" + MinimalRebuild="false" + BasicRuntimeChecks="3" + RuntimeLibrary="3" + StructMemberAlignment="0" + EnableFunctionLevelLinking="true" + EnableEnhancedInstructionSet="0" + FloatingPointModel="0" + FloatingPointExceptions="true" + DisableLanguageExtensions="false" + UsePrecompiledHeader="0" + ExpandAttributedSource="true" + AssemblerOutput="2" + BrowseInformation="1" + WarningLevel="4" + WarnAsError="false" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="0" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLibrarianTool" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)/antlr3cd.lib" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="4" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="3" + InlineFunctionExpansion="2" + EnableIntrinsicFunctions="true" + FavorSizeOrSpeed="1" + OmitFramePointers="true" + EnableFiberSafeOptimizations="true" + WholeProgramOptimization="true" + AdditionalIncludeDirectories=""$(SolutionDir)\include"" + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + StringPooling="true" + ExceptionHandling="1" + RuntimeLibrary="2" + BufferSecurityCheck="false" + EnableEnhancedInstructionSet="2" + FloatingPointModel="2" + DisableLanguageExtensions="false" + RuntimeTypeInfo="false" + UsePrecompiledHeader="0" + AssemblerListingLocation=".\asm\release" + WarningLevel="4" + WarnAsError="true" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="1" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLibrarianTool" + AdditionalOptions="/LTCG" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)/antlr3c.lib" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCXDCMakeTool" + ValidateIntelliSense="true" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Release|x64" + OutputDirectory="$(PlatformName)\$(ConfigurationName)" + IntermediateDirectory="$(PlatformName)\$(ConfigurationName)" + ConfigurationType="4" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + TargetEnvironment="3" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="3" + InlineFunctionExpansion="2" + EnableIntrinsicFunctions="true" + FavorSizeOrSpeed="1" + OmitFramePointers="true" + EnableFiberSafeOptimizations="true" + WholeProgramOptimization="true" + AdditionalIncludeDirectories=""$(SolutionDir)\include"" + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + StringPooling="true" + ExceptionHandling="1" + RuntimeLibrary="2" + BufferSecurityCheck="false" + EnableEnhancedInstructionSet="0" + FloatingPointModel="2" + RuntimeTypeInfo="false" + UsePrecompiledHeader="0" + AssemblerListingLocation=".\asm\release" + WarningLevel="4" + WarnAsError="true" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="1" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLibrarianTool" + AdditionalOptions="/LTCG" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)/antlr3c.lib" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCXDCMakeTool" + ValidateIntelliSense="true" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="ReleaseDLL|Win32" + OutputDirectory="$(ConfigurationName)" + IntermediateDirectory="$(ConfigurationName)" + ConfigurationType="2" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + WholeProgramOptimization="1" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="3" + InlineFunctionExpansion="2" + EnableIntrinsicFunctions="true" + FavorSizeOrSpeed="1" + OmitFramePointers="true" + EnableFiberSafeOptimizations="true" + WholeProgramOptimization="true" + AdditionalIncludeDirectories=""$(SolutionDir)\include"" + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + StringPooling="true" + ExceptionHandling="1" + RuntimeLibrary="2" + BufferSecurityCheck="false" + EnableEnhancedInstructionSet="2" + FloatingPointModel="2" + DisableLanguageExtensions="false" + RuntimeTypeInfo="false" + UsePrecompiledHeader="0" + AssemblerListingLocation=".\asm\release" + WarningLevel="4" + WarnAsError="true" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="0" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)\antlr3c.dll" + Version="3.1.1" + OptimizeReferences="2" + EnableCOMDATFolding="2" + RandomizedBaseAddress="1" + DataExecutionPrevention="0" + ImportLibrary="$(TargetDir)$(TargetName)_dll.lib" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + ValidateIntelliSense="true" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="ReleaseDLL|x64" + OutputDirectory="$(PlatformName)\$(ConfigurationName)" + IntermediateDirectory="$(PlatformName)\$(ConfigurationName)" + ConfigurationType="2" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + WholeProgramOptimization="1" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + TargetEnvironment="3" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="3" + InlineFunctionExpansion="2" + EnableIntrinsicFunctions="true" + FavorSizeOrSpeed="1" + OmitFramePointers="true" + EnableFiberSafeOptimizations="true" + WholeProgramOptimization="true" + AdditionalIncludeDirectories=""$(SolutionDir)\include"" + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + StringPooling="true" + ExceptionHandling="1" + RuntimeLibrary="2" + BufferSecurityCheck="false" + EnableEnhancedInstructionSet="0" + FloatingPointModel="2" + DisableLanguageExtensions="false" + RuntimeTypeInfo="false" + UsePrecompiledHeader="0" + AssemblerListingLocation=".\asm\release" + WarningLevel="4" + WarnAsError="true" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="0" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)\antlr3c64.dll" + Version="3.1.1" + OptimizeReferences="2" + EnableCOMDATFolding="2" + RandomizedBaseAddress="1" + DataExecutionPrevention="0" + ImportLibrary="$(TargetDir)$(TargetName)_dll.lib" + TargetMachine="17" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + ValidateIntelliSense="true" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="DebugDLL|Win32" + OutputDirectory="$(ConfigurationName)" + IntermediateDirectory="$(ConfigurationName)" + ConfigurationType="2" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + Outputs="$(TargetDir)$(TargetName)_dll.lib" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories=""$(SolutionDir)\include"" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" + StringPooling="true" + MinimalRebuild="false" + BasicRuntimeChecks="3" + RuntimeLibrary="3" + StructMemberAlignment="0" + EnableFunctionLevelLinking="true" + EnableEnhancedInstructionSet="0" + FloatingPointModel="0" + FloatingPointExceptions="true" + DisableLanguageExtensions="false" + UsePrecompiledHeader="0" + ExpandAttributedSource="true" + AssemblerOutput="2" + BrowseInformation="1" + WarningLevel="4" + WarnAsError="false" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="0" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)\antlr3cd.dll" + Version="3.1.1" + GenerateDebugInformation="true" + RandomizedBaseAddress="1" + DataExecutionPrevention="0" + ImportLibrary="$(TargetDir)$(TargetName)_dll.lib" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="DebugDLL|x64" + OutputDirectory="$(PlatformName)\$(ConfigurationName)" + IntermediateDirectory="$(PlatformName)\$(ConfigurationName)" + ConfigurationType="2" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + Outputs="$(TargetDir)$(TargetName)_dll.lib" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + TargetEnvironment="3" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories=""$(SolutionDir)\include"" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" + StringPooling="true" + MinimalRebuild="false" + BasicRuntimeChecks="3" + RuntimeLibrary="3" + StructMemberAlignment="0" + EnableFunctionLevelLinking="true" + EnableEnhancedInstructionSet="0" + FloatingPointModel="0" + FloatingPointExceptions="true" + DisableLanguageExtensions="false" + UsePrecompiledHeader="0" + ExpandAttributedSource="true" + AssemblerOutput="2" + BrowseInformation="1" + WarningLevel="4" + WarnAsError="false" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="0" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)\antlr3c64d.dll" + Version="3.1.1" + GenerateDebugInformation="true" + RandomizedBaseAddress="1" + DataExecutionPrevention="0" + ImportLibrary="$(TargetDir)$(TargetName)_dll.lib" + TargetMachine="17" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}" + > + <File + RelativePath=".\src\antlr3baserecognizer.c" + > + </File> + <File + RelativePath=".\src\antlr3basetree.c" + > + </File> + <File + RelativePath=".\src\antlr3basetreeadaptor.c" + > + </File> + <File + RelativePath=".\src\antlr3bitset.c" + > + </File> + <File + RelativePath=".\src\antlr3collections.c" + > + </File> + <File + RelativePath=".\src\antlr3commontoken.c" + > + </File> + <File + RelativePath=".\src\antlr3commontree.c" + > + </File> + <File + RelativePath=".\src\antlr3commontreeadaptor.c" + > + </File> + <File + RelativePath=".\src\antlr3commontreenodestream.c" + > + </File> + <File + RelativePath=".\src\antlr3convertutf.c" + > + </File> + <File + RelativePath=".\src\antlr3cyclicdfa.c" + > + </File> + <File + RelativePath=".\src\antlr3debughandlers.c" + > + </File> + <File + RelativePath=".\src\antlr3encodings.c" + > + </File> + <File + RelativePath=".\src\antlr3exception.c" + > + </File> + <File + RelativePath=".\src\antlr3filestream.c" + > + </File> + <File + RelativePath=".\src\antlr3inputstream.c" + > + </File> + <File + RelativePath=".\src\antlr3intstream.c" + > + </File> + <File + RelativePath=".\src\antlr3lexer.c" + > + </File> + <File + RelativePath=".\src\antlr3parser.c" + > + </File> + <File + RelativePath=".\src\antlr3rewritestreams.c" + > + </File> + <File + RelativePath=".\src\antlr3string.c" + > + </File> + <File + RelativePath=".\src\antlr3stringstream.c" + > + </File> + <File + RelativePath=".\src\antlr3tokenstream.c" + > + </File> + <File + RelativePath=".\src\antlr3treeparser.c" + > + </File> + <File + RelativePath=".\src\antlr3ucs2inputstream.c" + > + </File> + </Filter> + <Filter + Name="Header Files" + Filter="h;hpp;hxx;hm;inl;inc;xsd" + UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}" + > + <File + RelativePath=".\include\antlr3.h" + > + </File> + <File + RelativePath=".\include\antlr3baserecognizer.h" + > + </File> + <File + RelativePath=".\include\antlr3basetree.h" + > + </File> + <File + RelativePath=".\include\antlr3basetreeadaptor.h" + > + </File> + <File + RelativePath=".\include\antlr3bitset.h" + > + </File> + <File + RelativePath=".\include\antlr3collections.h" + > + </File> + <File + RelativePath=".\include\antlr3commontoken.h" + > + </File> + <File + RelativePath=".\include\antlr3commontree.h" + > + </File> + <File + RelativePath=".\include\antlr3commontreeadaptor.h" + > + </File> + <File + RelativePath=".\include\antlr3commontreenodestream.h" + > + </File> + <File + RelativePath=".\include\antlr3convertutf.h" + > + </File> + <File + RelativePath=".\include\antlr3cyclicdfa.h" + > + </File> + <File + RelativePath=".\include\antlr3debugeventlistener.h" + > + </File> + <File + RelativePath=".\include\antlr3defs.h" + > + </File> + <File + RelativePath=".\include\antlr3encodings.h" + > + </File> + <File + RelativePath=".\include\antlr3errors.h" + > + </File> + <File + RelativePath=".\include\antlr3exception.h" + > + </File> + <File + RelativePath=".\include\antlr3filestream.h" + > + </File> + <File + RelativePath=".\include\antlr3input.h" + > + </File> + <File + RelativePath=".\include\antlr3interfaces.h" + > + </File> + <File + RelativePath=".\include\antlr3intstream.h" + > + </File> + <File + RelativePath=".\include\antlr3lexer.h" + > + </File> + <File + RelativePath=".\include\antlr3memory.h" + > + </File> + <File + RelativePath=".\include\antlr3parser.h" + > + </File> + <File + RelativePath=".\include\antlr3parsetree.h" + > + </File> + <File + RelativePath=".\include\antlr3recognizersharedstate.h" + > + </File> + <File + RelativePath=".\include\antlr3rewritestreams.h" + > + </File> + <File + RelativePath=".\include\antlr3string.h" + > + </File> + <File + RelativePath=".\include\antlr3stringstream.h" + > + </File> + <File + RelativePath=".\include\antlr3tokenstream.h" + > + </File> + <File + RelativePath=".\include\antlr3treeparser.h" + > + </File> + </Filter> + <Filter + Name="Templates" + Filter=".stg" + > + <File + RelativePath="..\..\src\org\antlr\codegen\templates\C\AST.stg" + > + </File> + <File + RelativePath="..\..\src\org\antlr\codegen\templates\C\ASTDbg.stg" + > + </File> + <File + RelativePath="..\..\src\org\antlr\codegen\templates\C\ASTParser.stg" + > + </File> + <File + RelativePath="..\..\src\org\antlr\codegen\templates\C\ASTTreeParser.stg" + > + </File> + <File + RelativePath="..\..\src\org\antlr\codegen\templates\C\C.stg" + > + </File> + <File + RelativePath="..\..\src\org\antlr\codegen\templates\C\Dbg.stg" + > + </File> + <Filter + Name="Interfaces" + Filter="*.sti" + > + <File + RelativePath="..\..\src\org\antlr\codegen\templates\ANTLRCore.sti" + > + </File> + </Filter> + </Filter> + <Filter + Name="Java" + Filter="*.java" + > + <File + RelativePath="..\..\src\org\antlr\codegen\CTarget.java" + > + </File> + </Filter> + <Filter + Name="Doxygen" + > + <File + RelativePath=".\doxygen\atsections.dox" + > + </File> + <File + RelativePath=".\doxygen\build.dox" + > + </File> + <File + RelativePath=".\doxygen\buildrec.dox" + > + </File> + <File + RelativePath=".\doxygen\changes31.dox" + > + </File> + <File + RelativePath=".\doxygen\doxygengroups.dox" + > + </File> + <File + RelativePath=".\doxygen\generate.dox" + > + </File> + <File + RelativePath=".\doxygen\interop.dox" + > + </File> + <File + RelativePath=".\doxygen\mainpage.dox" + > + </File> + <File + RelativePath=".\doxygen\runtime.dox" + > + </File> + <File + RelativePath=".\doxygen\using.dox" + > + </File> + </Filter> + </Files> + <Globals> + <Global + Name="DevPartner_IsInstrumented" + Value="0" + /> + </Globals> +</VisualStudioProject> diff --git a/antlr-3.1.3/runtime/C/C.vcproj.vspscc b/antlr-3.1.3/runtime/C/C.vcproj.vspscc new file mode 100644 index 0000000..953a1b9 --- /dev/null +++ b/antlr-3.1.3/runtime/C/C.vcproj.vspscc @@ -0,0 +1,14 @@ +"" +{ +"FILE_VERSION" = "9237" +"ENLISTMENT_CHOICE" = "NEVER" +"PROJECT_FILE_RELATIVE_PATH" = "" +"NUMBER_OF_EXCLUDED_FILES" = "4" +"EXCLUDED_FILE0" = "debugdll\\antlr3cd_dll.lib" +"EXCLUDED_FILE1" = "releasedll\\antlr3c_dll.lib" +"EXCLUDED_FILE2" = "debugdll\\antlr3cd_dll.exp" +"EXCLUDED_FILE3" = "releasedll\\antlr3c_dll.exp" +"ORIGINAL_PROJECT_FILE_PATH" = "" +"NUMBER_OF_NESTED_PROJECTS" = "0" +"SOURCE_CONTROL_SETTINGS_PROVIDER" = "PROVIDER" +} diff --git a/antlr-3.1.3/runtime/C/C.vssscc b/antlr-3.1.3/runtime/C/C.vssscc new file mode 100644 index 0000000..6cb031b --- /dev/null +++ b/antlr-3.1.3/runtime/C/C.vssscc @@ -0,0 +1,10 @@ +"" +{ +"FILE_VERSION" = "9237" +"ENLISTMENT_CHOICE" = "NEVER" +"PROJECT_FILE_RELATIVE_PATH" = "" +"NUMBER_OF_EXCLUDED_FILES" = "0" +"ORIGINAL_PROJECT_FILE_PATH" = "" +"NUMBER_OF_NESTED_PROJECTS" = "0" +"SOURCE_CONTROL_SETTINGS_PROVIDER" = "PROJECT" +} diff --git a/antlr-3.1.3/runtime/C/COPYING b/antlr-3.1.3/runtime/C/COPYING new file mode 100644 index 0000000..f62f896 --- /dev/null +++ b/antlr-3.1.3/runtime/C/COPYING @@ -0,0 +1,29 @@ +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + diff --git a/antlr-3.1.3/runtime/C/ChangeLog b/antlr-3.1.3/runtime/C/ChangeLog new file mode 100644 index 0000000..c5540d9 --- /dev/null +++ b/antlr-3.1.3/runtime/C/ChangeLog @@ -0,0 +1,550 @@ +The following changes (change numbers refer to perforce) were +made from version 3.1.1 to 3.1.2 + +Runtime +------- + +Change 5641 on 2009/02/20 by jimi@jimi.jimi.antlr3 + + Release version 3.1.2 of the ANTLR C runtime. + + Updated documents and release notes will have to follow later. + +Change 5639 on 2009/02/20 by jimi@jimi.jimi.antlr3 + + Fixed: ANTLR-356 + + Ensure that code generation for C++ does not require casts + +Change 5577 on 2009/02/12 by jimi@jimi.jimi.antlr3 + + C Runtime - Bug fixes. + + o Having moved to use an extract directly from a vector for returning + tokens, it exposed a + bug whereby the EOF boudary calculation in tokLT was incorrectly + checking > rather than >=. + o Changing to API initialization of tokens rather than memcmp() + incorrectly forgot to set teh input stream pointer for the + manufactured tokens in the token factory; + o Rewrite streams for rewriting tree parsers did not check whether the + rewrite stream was ever assigned before trying to free it, it is now + in line with the ordinary parser code. + +Change 5576 on 2009/02/11 by jimi@jimi.jimi.antlr3 + + C Runtime: Ensure that when we manufacture a new token for a missing + token, that the user suplied custom information (if any) is copied + from the current token. + +Change 5575 on 2009/02/08 by jimi@jimi.jimi.antlr3 + + C Runtime - Vastly improve the reuse of allocated memory for nodes in + tree rewriting. + + A problem for all targets at the moment si that the rewrite logic + generated by ANTLR makes no attempt + to reuse any resources, it merely gurantees that the tree shape at the + end is correct. To some extent this is mitigated by the garbage + collection systems of Java and .Net, even thoguh it is still an overhead to + keep creating so many modes. + + This change implements the first of two C runtime changes that make + best efforst to track when a node has become orphaned and will never + be reused, based on inherent knowledge of the rewrite logic (which in + the long term is not a great soloution). + + Much of the rewrite logic consists of creating a niilnode into which + child nodes are appended. At: rulePost processing time; when a rewrite + stream is closed; and when becomeRoot is called, there are many situations + where the root of the tree that will be manipulted, or is finished with + (in the case of rewrtie streams), where the nilNode was just a temporary + creation for the sake of the rewrite itself. + + In these cases we can see that the nilNode would just be left ot rot in + the node factory that tracks all the tree nodes. + Rather than leave these in the factory to rot, we now keep a resuse + stck and always reuse any node on this + stack before claimin a new node from the factory pool. + + This single change alone reduces memory usage in the test case (20,604 + line C program and a GNU C parser) + from nearly a GB, to 276MB. This is still way more memory than we + shoudl need to do this operation, even on such a large input file, + but the reduction results in a huge performance increase and greatly + reduced system time spent on allocations. + + After this optimizatoin, comparison with gcc yeilds: + + time gcc -S a.c + a.c:1026: warning: conflicting types for built-in function ‘vsprintf’ + a.c:1030: warning: conflicting types for built-in function ‘vsnprintf’ + a.c:1041: warning: conflicting types for built-in function ‘vsscanf’ + 0.21user 0.01system 0:00.22elapsed 97%CPU (0avgtext+0avgdata 0maxresident)k + 0inputs+240outputs (0major+8345minor)pagefaults 0swaps + + and + + time ./jimi + Reading a.c + 0.28user 0.11system 0:00.39elapsed 98%CPU (0avgtext+0avgdata 0maxresident)k + 0inputs+0outputs (0major+66609minor)pagefaults 0swaps + + And we can now interpolate the fact that the only major differnce is + now the huge disparity in memory allocations. A + future optimization of vector pooling, to sepate node resue from vector + reuse, currently looks promising for further reuse of memory. + + Finally, a static analysis of the rewrte code, plus a realtime analysis + of the heap at runtime, may well give us a reasonable memory usage + pattern. In reality though, it is the generated rewrite logic + that must becom optional at not continuously rewriting things that it + need not, as it ascends the rule chain. + +Change 5563 on 2009/01/28 by jimi@jimi.jimi.antlr3 + + Allow rewrite streams to use the base adaptors vector factory and not + try to malloc new vectors themselves. + +Change 5562 on 2009/01/28 by jimi@jimi.jimi.antlr3 + + Don't use CALLOC to allocate tree pools, use malloc as there is no need + for calloc. + +Change 5561 on 2009/01/28 by jimi@jimi.jimi.antlr3 + + Prevent warnigsn about retval.stop not being initialized when a rule + returns eraly because it is in backtracking mode + +Change 5558 on 2009/01/28 by jimi@jimi.jimi.antlr3 + + Lots of optimizations (though the next one to be checked in is the huge + win) for AST building and vector factories. + + A large part of tree rewriting was the creation of vectors to hold AST + nodes. Although I had created a vector factory, for some reason I never got + around to creating a proper one, that pre-allocated the vectors in chunks and + so on. I guess I just forgot to. Hence a big win here is prevention of calling + malloc lots and lots of times to create vectors. + + A second inprovement was to change teh vector definition such that it + holds a certain number of elements wihtin the vector structure itself, rather + than malloc and freeing these. Currently this is set to 8, but may increase. + For AST construction, this is generally a big win because AST nodes don't often + have many individual children unless there has not been any shaping going on in + the parser. But if you are not shaping, then you don't really need a tree. + + Other perforamnce inprovements here include not calling functions + indirectly within token stream and common token stream. Hence tokens are + claimed directly from the vectors. Users can override these funcitons of course + and all this means is that if you override tokenstreams then you pretty much + have to provide all the mehtods, but then I think you woudl have to anyway (and + I don't know of anyone that has wanted to do this as you can carry your own + structure around with the tokens anyway and that is much easier). + +Change 5555 on 2009/01/26 by jimi@jimi.jimi.antlr3 + + Fixed: ANTLR-288 + Correct the interpretation of the skip token such that channel, start + index, char pos in lie, start line and text are correctly reset to the start of + the new token when the one that we just traversed was marked as being skipped. + + This correctly excludes the text that was matched as part of the + SKIP()ed token from the next token in the token stream and so has the side + effect that asking for $text of a rule no longer includes the text that shuodl + be skipped, but DOES include the text of tokens that were merely placed off the + default channel. + +Change 5551 on 2009/01/25 by jimi@jimi.jimi.antlr3 + + Fixed: ANTLR-287 + Most of the source files did not include the BSD license. THis might + not be that big a deal given that I don't care what people do with it + other than take my name off it, but having the license reproduced + everywhere + at least makes things perfectly clear. Hence this mass change of + sources and templates + to include the license. + +Change 5550 on 2009/01/25 by jimi@jimi.jimi.antlr3 + + Fixed: ANTLR-365 + Ensure that as soon as we known about an input stream on the lexer that + we borrow its string factroy adn use it in our EOF token in case + anyone tries to make it a string, such as in error messages for + instance. + +Change 5548 on 2009/01/25 by jimi@jimi.jimi.antlr3 + + Fixed: ANTLR-363 + At some point the Java runtime default changed from discarding offchannel + tokens to preserving them. The fix is to make the C runtime also + default to preserving off-channel tokens. + +Change 5544 on 2009/01/24 by jimi@jimi.jimi.antlr3 + + Fixed: ANTLR-360 + Ensure that the fillBuffer funtiion does not call any methods + that require the cached buffer size to be recorded before we + have actually recorded it. + +Change 5543 on 2009/01/24 by jimi@jimi.jimi.antlr3 + + Fixed: ANTLR-362 + Some users have started using string factories themselves and + exposed a flaw in the destroy method, that is intended to remove + a strng htat was created by the factory and is no longer needed. + The string was correctly removed from the vector that tracks them + but after the first one, all the remaining strings are then numbered + incorrectly. Hence the destroy method has been recoded to reindex + the strings in the factory after one is removed and everythig is once + more hunky dory. + User suggested fix rejected. + +Change 5542 on 2009/01/24 by jimi@jimi.jimi.antlr3 + + Fixed ANTLR-366 + The recognizer state now ensures that all fields are set to NULL upon +creation + and the reset does not overwrite the tokenname array + +Change 5527 on 2009/01/15 by jimi@jimi.jimi.antlr3 + + Add the C runtime for 3.1.2 beta2 to perforce + +Change 5526 on 2009/01/15 by jimi@jimi.jimivista.antlr3 + + Correctly define the MEMMOVE macro which was inadvertently left to be + memcpy. + +Change 5503 on 2008/12/12 by jimi@jimi.jimi.antlr3 + + Change C runtime release number to 3.1.2 beta + +Change 5473 on 2008/12/01 by jimi@jimi.jimivista.antlr3 + + Fixed: ANTLR-350 - C runtime use of memcpy + Prior change to use memcpy instead of memmove in all cases missed the + fact that the string factory can be in a situation where overlaps occur. We now + have ANTLR3_MEMCPY and ANTLR3_MEMMOVE and use the two appropriately. + +Change 5471 on 2008/12/01 by jimi@jimi.jimivista.antlr3 + + Fixed ANTLR-361 + - Ensure that ANTLR3_BOOLEAN is typedef'ed correctly when building for + MingW + +Templates +--------- + +Change 5637 on 2009/02/20 by jimi@jimi.jimi.antlr3 + + C rtunime - make sure that ADAPTOR results are cast to the tree type on + a rewrite + +Change 5620 on 2009/02/18 by jimi@jimi.jimi.antlr3 + + Rename/Move: + From: //depot/code/antlr/main/src/org/antlr/codegen/templates/... + To: //depot/code/antlr/main/src/main/resources/org/antlr/codegen/templates/... + + Relocate the code generating templates to exist in the directory set + that maven expects. + + When checking in your templates, you may find it easiest to make a copy + of what you have, revert the change in perforce, then just check out the + template in the new location, and copy the changes back over. Nobody has oore + than two files open at the moment. + +Change 5578 on 2009/02/12 by jimi@jimi.jimi.antlr3 + + Correct the string template escape sequences for generating scope + code in the C templates. + +Change 5577 on 2009/02/12 by jimi@jimi.jimi.antlr3 + + C Runtime - Bug fixes. + + o Having moved to use an extract directly from a vector for returning + tokens, it exposed a + bug whereby the EOF boudary calculation in tokLT was incorrectly + checking > rather than + >=. + o Changing to API initialization of tokens rather than memcmp() + incorrectly forgot to + set teh input stream pointer for the manufactured tokens in the + token factory; + o Rewrite streams for rewriting tree parsers did not check whether the + rewrite stream + was ever assigned before trying to free it, it is now in line with + the ordinary parser code. + +Change 5567 on 2009/01/29 by jimi@jimi.jimi.antlr3 + + C Runtime - Further Optimizations + + Within grammars that used scopes and were intended to parse large + inputs with many rule nests, + the creation anf deletion of the scopes themselves became significant. + Careful analysis shows that + for most grammars, while a parse could create and delete 20,000 scopes, + the maxium depth of + any scope was only 8. + + This change therefore changes the scope implementation so that it does + not free scope memory when + it is popped but just tracks it in a C runtime stack, eventually + freeing it when the stack is freed. This change + caused the allocation of only 12 scope structures instead of 20,000 for + the extreme example case. + + This change means that scope users must be carefule (as ever in C) to + initializae their scope elements + correctly as: + + 1) If not you may inherit values from a prior use of the scope + structure; + 2) SCope structure are now allocated with malloc and not calloc; + + Also, when using a custom free function to clean a scope when it is + popped, it is probably a good idea + to set any free'd pointers to NULL (this is generally good C programmig + practice in any case) + +Change 5566 on 2009/01/29 by jimi@jimi.jimi.antlr3 + + Remove redundant BACKTRACK checking so that MSVC9 does not get confused + about possibly uninitialized variables + +Change 5565 on 2009/01/28 by jimi@jimi.jimi.antlr3 + + Use malloc rather than calloc to allocate memory for new scopes. Note + that this means users will have to be careful to initialize any values in their + scopes that they expect to be 0 or NULL and I must document this. + +Change 5564 on 2009/01/28 by jimi@jimi.jimi.antlr3 + + Use malloc rather than calloc for copying list lable tokens for + rewrites. + +Change 5561 on 2009/01/28 by jimi@jimi.jimi.antlr3 + + Prevent warnigsn about retval.stop not being initialized when a rule + returns eraly because it is in backtracking mode + +Change 5560 on 2009/01/28 by jimi@jimi.jimi.antlr3 + + Add a NULL check before freeing rewrite streams used in AST rewrites + rather than auto-rewrites. + + While the NULL check is redundant as the free cannot be called unless + it is assigned, Visual Studio C 2008 + gets it wrong and thinks that there is a PATH than can arrive at the + free wihtout it being assigned and that is too annoying to ignore. + +Change 5559 on 2009/01/28 by jimi@jimi.jimi.antlr3 + + C target Tree rewrite optimization + + There is only one optimization in this change, but it is a huge one. + + The code generation templates were set up so that at the start of a rule, + any rewrite streams mentioned in the rule wer pre-created. However, this + is a massive overhead for rules where only one or two of the streams are + actually used, as we create them then free them without ever using them. + This was copied from the Java templates basically. + This caused literally millions of extra calls and vector allocations + in the case of the GNU C parser given to me for testing with a 20,000 + line program. + + After this change, the following comparison is avaiable against the gcc + compiler: + + Before (different machines here so use the relative difference for + comparison): + + gcc: + + real 0m0.425s + user 0m0.384s + sys 0m0.036s + + ANTLR C + real 0m1.958s + user 0m1.284s + sys 0m0.656s + + After the previous optimizations for vector pooling via a factory, + plus this huge win in removing redundant code, we have the following + (different machine to the one above): + + gcc: + 0.21user 0.01system 0:00.23elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k + 0inputs+328outputs (0major+9922minor)pagefaults 0swaps + + ANTLR C: + + 0.37user 0.26system 0:00.64elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k + 0inputs+0outputs (0major+130944minor)pagefaults 0swaps + + The extra system time coming from the fact that although the tree + rewriting is now optimal in terms of not allocating things it does + not need, there is still a lot more overhead in a parser that is generated + for generic use, including much more use of structures for tokens and extra + copying and so on. I will + continue to work on improviing things where I can, but the next big + improvement will come from Ter's optimization of the actual code structures we + generate including not doing things with rewrite streams that we do not need to + do at all. + + The second machine I used is about twice as fast CPU wise as the system + that was used originally by the user that asked about this performance. + +Change 5558 on 2009/01/28 by jimi@jimi.jimi.antlr3 + + Lots of optimizations (though the next one to be checked in is the huge + win) for AST building and vector factories. + + A large part of tree rewriting was the creation of vectors to hold AST + nodes. Although I had created a vector factory, for some reason I never got + around to creating a proper one, that pre-allocated the vectors in chunks and + so on. I guess I just forgot to. Hence a big win here is prevention of calling + malloc lots and lots of times to create vectors. + + A second inprovement was to change teh vector definition such that it + holds a certain number of elements wihtin the vector structure itself, rather + than malloc and freeing these. Currently this is set to 8, but may increase. + For AST construction, this is generally a big win because AST nodes don't often + have many individual children unless there has not been any shaping going on in + the parser. But if you are not shaping, then you don't really need a tree. + + Other perforamnce inprovements here include not calling functions + indirectly within token stream and common token stream. Hence tokens are + claimed directly from the vectors. Users can override these funcitons of course + and all this means is that if you override tokenstreams then you pretty much + have to provide all the mehtods, but then I think you woudl have to anyway (and + I don't know of anyone that has wanted to do this as you can carry your own + structure around with the tokens anyway and that is much easier). + +Change 5554 on 2009/01/26 by jimi@jimi.jimi.antlr3 + + Fixed: ANTLR-379 + For some reason in the past, the ruleMemozation() template had required + that the name parameter be set to the rule name. This does not seem to be a + requirement any more. The name=xxx override when invoking the template was + causing all the scope names derived when cleaning up in memoization to be + called after the rule name, which was not correct. Howver, this only affected + the output when in output=AST mode. + + This template invocation is now corrected. + +Change 5553 on 2009/01/26 by jimi@jimi.jimi.antlr3 + + Fixed: ANTLR-330 + Managed to get the one rule that could not see the ASTLabelType to call + back in to the super template C.stg and ask it to construct hte name. I am not + 100% sure that this fixes all cases, but I cannot find any that fail. PLease + let me know if you find any exampoles of being unable to default the + ASTLabelType option in the C target. + +Change 5552 on 2009/01/25 by jimi@jimi.jimi.antlr3 + + Progress: ANTLR-327 + Fix debug code generation templates when output=AST such that code + can at least be generated and I can debug the output code correctly. + Note that this checkin does not implement the debugging requirements + for tree generating parsers. + +Change 5551 on 2009/01/25 by jimi@jimi.jimi.antlr3 + + Fixed: ANTLR-287 + Most of the source files did not include the BSD license. THis might + not be that big a deal given that I don't care what people do with it + other than take my name off it, but having the license reproduced + everywhere at least makes things perfectly clear. Hence this mass change of + sources and templates to include the license. + +Change 5549 on 2009/01/25 by jimi@jimi.jimi.antlr3 + + Fixed: ANTLR-354 + Using 0.0D as the default initialize value for a double caused + VS 2003 C compiler to bomb out. There seesm to be no reason other + than force of habit to set this to 0.0D so I have dropped the D so + that older compilers do not complain. + +Change 5547 on 2009/01/25 by jimi@jimi.jimi.antlr3 + + Fixed: ANTLR-282 + All references are now unadorned with any type of NULL check for the + following reasons: + + 1) A NULL reference means that there is a problem with the + grammar and we need the program to fail immediately so + that the programmer can work out where the problem occured; + 2) Most of the time, the only sensible value that can be + returned is NULL or 0 which + obviates the NULL check in the first place; + 3) If we replace a NULL reference with some value such as 0, + then the program may blithely continue but just do something + logically wrong, which will be very difficult for the + grammar programmer to detect and correct. + +Change 5545 on 2009/01/24 by jimi@jimi.jimi.antlr3 + + Fixed: ANTLR-357 + The bug report was correct in that the types of references to things + like $start were being incorrectly cast as they wer not changed from + Java style casts (and the casts are unneccessary). this is now fixed + and references are referencing the correct, uncast, types. + However, the bug report was wrong in that the reference in the bok to + $start.pos will only work for Java and really, it is incorrect in the + book because it shoudl not access the .pos member directly but shudl + be using $start.getCharPositionInLine(). + Because there is no access qualification in C, one could use + $start.charPosition, however + really this should be $start->getCharPositionInLine($start); + +Change 5541 on 2009/01/24 by jimi@jimi.jimi.antlr3 + + Fixed - ANTLR-367 + The code generation for the free method of a recognizer was not + distinguishing tree parsers from parsers when it came to calling delegate free + functions. + This is now corrected. + +Change 5540 on 2009/01/24 by jimi@jimi.jimi.antlr3 + + Fixed ANTLR-355 + Ensure that we do not attempt to free any memory that we did not + actually allocate because the parser rule was being executed in + backtracking mode. + +Change 5539 on 2009/01/24 by jimi@jimi.jimivista.antlr3 + + Fixed: ANTLR-355 + When a C targetted parser is producing in backtracking mode, then the + creation of new stream rewrite structures shoudl not happen if the rule is + currently backtracking + +Change 5502 on 2008/12/11 by jimi@jimi.jimi.antlr3 + + Fixed: ANTLR-349 Ensure that all marker labels in the lexer are 64 bit + compatible + +Change 5473 on 2008/12/01 by jimi@jimi.jimivista.antlr3 + + Fixed: ANTLR-350 - C runtime use of memcpy + Prior change to use memcpy instead of memmove in all cases missed the + fact that the string factory can be in a situation where overlaps occur. We now + have ANTLR3_MEMCPY and ANTLR3_MEMMOVE and use the two appropriately. + +Change 5387 on 2008/11/05 by parrt@parrt.spork + + Fixed x+=. issue with tree grammars; added unit test + +Change 5325 on 2008/10/23 by parrt@parrt.spork + + We were all ref'ing backtracking==0 hardcoded instead checking the + @synpredgate action. + + diff --git a/antlr-3.1.3/runtime/C/Cvs2005.sln b/antlr-3.1.3/runtime/C/Cvs2005.sln new file mode 100644 index 0000000..43050c7 --- /dev/null +++ b/antlr-3.1.3/runtime/C/Cvs2005.sln @@ -0,0 +1,53 @@ +Microsoft Visual Studio Solution File, Format Version 9.00 +# Visual Studio 2005 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "C", "Cvs2005.vcproj", "{0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}" +EndProject +Global + GlobalSection(SourceCodeControl) = preSolution + SccNumberOfProjects = 2 + SccProjectName0 = Perforce\u0020Project + SccLocalPath0 = ..\\.. + SccProvider0 = MSSCCI:Perforce\u0020SCM + SccProjectFilePathRelativizedFromConnection0 = runtime\\C\\ + SccProjectUniqueName1 = Cvs2005.vcproj + SccLocalPath1 = ..\\.. + SccProjectFilePathRelativizedFromConnection1 = runtime\\C\\ + EndGlobalSection + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + DebugDLL|Win32 = DebugDLL|Win32 + DebugDLL|x64 = DebugDLL|x64 + Deployment|Win32 = Deployment|Win32 + Deployment|x64 = Deployment|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + ReleaseDLL|Win32 = ReleaseDLL|Win32 + ReleaseDLL|x64 = ReleaseDLL|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Debug|Win32.ActiveCfg = Debug|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Debug|Win32.Build.0 = Debug|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Debug|x64.ActiveCfg = Debug|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Debug|x64.Build.0 = Debug|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.DebugDLL|Win32.ActiveCfg = DebugDLL|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.DebugDLL|Win32.Build.0 = DebugDLL|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.DebugDLL|x64.ActiveCfg = DebugDLL|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.DebugDLL|x64.Build.0 = DebugDLL|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Deployment|Win32.ActiveCfg = DebugDLL|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Deployment|Win32.Build.0 = DebugDLL|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Deployment|x64.ActiveCfg = DebugDLL|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Deployment|x64.Build.0 = DebugDLL|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Release|Win32.ActiveCfg = Release|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Release|Win32.Build.0 = Release|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Release|x64.ActiveCfg = Release|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.Release|x64.Build.0 = Release|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.ReleaseDLL|Win32.ActiveCfg = ReleaseDLL|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.ReleaseDLL|Win32.Build.0 = ReleaseDLL|Win32 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.ReleaseDLL|x64.ActiveCfg = ReleaseDLL|x64 + {0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}.ReleaseDLL|x64.Build.0 = ReleaseDLL|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/antlr-3.1.3/runtime/C/Cvs2005.vcproj b/antlr-3.1.3/runtime/C/Cvs2005.vcproj new file mode 100644 index 0000000..a00807c --- /dev/null +++ b/antlr-3.1.3/runtime/C/Cvs2005.vcproj @@ -0,0 +1,1067 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="8.00" + Name="C" + ProjectGUID="{0F0FE03A-78F3-4B34-9DCE-0CDFF1FB5C40}" + RootNamespace="C" + SccProjectName="Perforce Project" + SccLocalPath="..\.." + SccProvider="MSSCCI:Perforce SCM" + Keyword="Win32Proj" + > + <Platforms> + <Platform + Name="Win32" + /> + <Platform + Name="x64" + /> + </Platforms> + <ToolFiles> + </ToolFiles> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="4" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories=""$(SolutionDir)\include";"$(SolutionDir)\..\..\include"" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" + StringPooling="true" + MinimalRebuild="false" + BasicRuntimeChecks="3" + RuntimeLibrary="3" + StructMemberAlignment="0" + EnableFunctionLevelLinking="true" + EnableEnhancedInstructionSet="0" + FloatingPointModel="0" + FloatingPointExceptions="true" + DisableLanguageExtensions="false" + UsePrecompiledHeader="0" + ExpandAttributedSource="true" + AssemblerOutput="2" + BrowseInformation="1" + WarningLevel="4" + WarnAsError="false" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="0" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLibrarianTool" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)/antlr3cd.lib" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Debug|x64" + OutputDirectory="$(PlatformName)\$(ConfigurationName)" + IntermediateDirectory="$(PlatformName)\$(ConfigurationName)" + ConfigurationType="4" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + TargetEnvironment="3" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories=""$(SolutionDir)\include";"$(SolutionDir)\..\..\include"" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" + StringPooling="true" + MinimalRebuild="false" + BasicRuntimeChecks="3" + RuntimeLibrary="3" + StructMemberAlignment="0" + EnableFunctionLevelLinking="true" + EnableEnhancedInstructionSet="0" + FloatingPointModel="0" + FloatingPointExceptions="true" + DisableLanguageExtensions="false" + UsePrecompiledHeader="0" + ExpandAttributedSource="true" + AssemblerOutput="2" + BrowseInformation="1" + WarningLevel="4" + WarnAsError="false" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="0" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLibrarianTool" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)/antlr3cd.lib" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="4" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="3" + InlineFunctionExpansion="2" + EnableIntrinsicFunctions="true" + FavorSizeOrSpeed="1" + OmitFramePointers="true" + EnableFiberSafeOptimizations="true" + WholeProgramOptimization="true" + AdditionalIncludeDirectories=""$(SolutionDir)\include"" + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + StringPooling="true" + ExceptionHandling="1" + RuntimeLibrary="2" + BufferSecurityCheck="false" + EnableEnhancedInstructionSet="2" + FloatingPointModel="2" + DisableLanguageExtensions="false" + RuntimeTypeInfo="false" + UsePrecompiledHeader="0" + AssemblerListingLocation=".\asm\release" + WarningLevel="4" + WarnAsError="true" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="1" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLibrarianTool" + AdditionalOptions="/LTCG" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)/antlr3c.lib" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCXDCMakeTool" + ValidateIntelliSense="true" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Release|x64" + OutputDirectory="$(PlatformName)\$(ConfigurationName)" + IntermediateDirectory="$(PlatformName)\$(ConfigurationName)" + ConfigurationType="4" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + TargetEnvironment="3" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="3" + InlineFunctionExpansion="2" + EnableIntrinsicFunctions="true" + FavorSizeOrSpeed="1" + OmitFramePointers="true" + EnableFiberSafeOptimizations="true" + WholeProgramOptimization="true" + AdditionalIncludeDirectories=""$(SolutionDir)\include"" + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + StringPooling="true" + ExceptionHandling="1" + RuntimeLibrary="2" + BufferSecurityCheck="false" + EnableEnhancedInstructionSet="0" + FloatingPointModel="2" + RuntimeTypeInfo="false" + UsePrecompiledHeader="0" + AssemblerListingLocation=".\asm\release" + WarningLevel="4" + WarnAsError="true" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="1" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLibrarianTool" + AdditionalOptions="/LTCG" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)/antlr3c.lib" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCXDCMakeTool" + ValidateIntelliSense="true" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="ReleaseDLL|Win32" + OutputDirectory="$(ConfigurationName)" + IntermediateDirectory="$(ConfigurationName)" + ConfigurationType="2" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + WholeProgramOptimization="1" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="3" + InlineFunctionExpansion="2" + EnableIntrinsicFunctions="true" + FavorSizeOrSpeed="1" + OmitFramePointers="true" + EnableFiberSafeOptimizations="true" + WholeProgramOptimization="true" + AdditionalIncludeDirectories=""$(SolutionDir)\include"" + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + StringPooling="true" + ExceptionHandling="1" + RuntimeLibrary="2" + BufferSecurityCheck="false" + EnableEnhancedInstructionSet="2" + FloatingPointModel="2" + DisableLanguageExtensions="false" + RuntimeTypeInfo="false" + UsePrecompiledHeader="0" + AssemblerListingLocation=".\asm\release" + WarningLevel="4" + WarnAsError="true" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="0" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)\antlr3c.dll" + Version="3.0b4" + OptimizeReferences="2" + EnableCOMDATFolding="2" + ImportLibrary="$(TargetDir)$(TargetName)_dll.lib" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + ValidateIntelliSense="true" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCWebDeploymentTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="ReleaseDLL|x64" + OutputDirectory="$(PlatformName)\$(ConfigurationName)" + IntermediateDirectory="$(PlatformName)\$(ConfigurationName)" + ConfigurationType="2" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + WholeProgramOptimization="1" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + TargetEnvironment="3" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="3" + InlineFunctionExpansion="2" + EnableIntrinsicFunctions="true" + FavorSizeOrSpeed="1" + OmitFramePointers="true" + EnableFiberSafeOptimizations="true" + WholeProgramOptimization="true" + AdditionalIncludeDirectories=""$(SolutionDir)\include"" + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + StringPooling="true" + ExceptionHandling="1" + RuntimeLibrary="2" + BufferSecurityCheck="false" + EnableEnhancedInstructionSet="0" + FloatingPointModel="2" + DisableLanguageExtensions="false" + RuntimeTypeInfo="false" + UsePrecompiledHeader="0" + AssemblerListingLocation=".\asm\release" + WarningLevel="4" + WarnAsError="true" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="0" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)\antlr3c64.dll" + Version="3.0.0.1" + OptimizeReferences="2" + EnableCOMDATFolding="2" + ImportLibrary="$(TargetDir)$(TargetName)_dll.lib" + TargetMachine="17" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + ValidateIntelliSense="true" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCWebDeploymentTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="DebugDLL|Win32" + OutputDirectory="$(ConfigurationName)" + IntermediateDirectory="$(ConfigurationName)" + ConfigurationType="2" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + Outputs="$(TargetDir)$(TargetName)_dll.lib" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories=""$(SolutionDir)\include"" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" + StringPooling="true" + MinimalRebuild="false" + BasicRuntimeChecks="3" + RuntimeLibrary="3" + StructMemberAlignment="0" + EnableFunctionLevelLinking="true" + EnableEnhancedInstructionSet="0" + FloatingPointModel="0" + FloatingPointExceptions="true" + DisableLanguageExtensions="false" + UsePrecompiledHeader="0" + ExpandAttributedSource="true" + AssemblerOutput="2" + BrowseInformation="1" + WarningLevel="4" + WarnAsError="false" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="0" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)\antlr3cd.dll" + Version="1.0b4" + GenerateDebugInformation="true" + ImportLibrary="$(TargetDir)$(TargetName)_dll.lib" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCWebDeploymentTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="DebugDLL|x64" + OutputDirectory="$(PlatformName)\$(ConfigurationName)" + IntermediateDirectory="$(PlatformName)\$(ConfigurationName)" + ConfigurationType="2" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + Outputs="$(TargetDir)$(TargetName)_dll.lib" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + TargetEnvironment="3" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories=""$(SolutionDir)\include"" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" + StringPooling="true" + MinimalRebuild="false" + BasicRuntimeChecks="3" + RuntimeLibrary="3" + StructMemberAlignment="0" + EnableFunctionLevelLinking="true" + EnableEnhancedInstructionSet="0" + FloatingPointModel="0" + FloatingPointExceptions="true" + DisableLanguageExtensions="false" + UsePrecompiledHeader="0" + ExpandAttributedSource="true" + AssemblerOutput="2" + BrowseInformation="1" + WarningLevel="4" + WarnAsError="false" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + CallingConvention="0" + CompileAs="0" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalDependencies="Ws2_32.lib" + OutputFile="$(OutDir)\antlr3c64d.dll" + Version="3.0.0.1" + GenerateDebugInformation="true" + ImportLibrary="$(TargetDir)$(TargetName)_dll.lib" + TargetMachine="17" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCWebDeploymentTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}" + > + <File + RelativePath=".\src\antlr3baserecognizer.c" + > + </File> + <File + RelativePath=".\src\antlr3basetree.c" + > + </File> + <File + RelativePath=".\src\antlr3basetreeadaptor.c" + > + </File> + <File + RelativePath=".\src\antlr3bitset.c" + > + </File> + <File + RelativePath=".\src\antlr3collections.c" + > + </File> + <File + RelativePath=".\src\antlr3commontoken.c" + > + </File> + <File + RelativePath=".\src\antlr3commontree.c" + > + </File> + <File + RelativePath=".\src\antlr3commontreeadaptor.c" + > + </File> + <File + RelativePath=".\src\antlr3commontreenodestream.c" + > + </File> + <File + RelativePath=".\src\antlr3convertutf.c" + > + </File> + <File + RelativePath=".\src\antlr3cyclicdfa.c" + > + </File> + <File + RelativePath=".\src\antlr3debughandlers.c" + > + </File> + <File + RelativePath=".\src\antlr3encodings.c" + > + </File> + <File + RelativePath=".\src\antlr3exception.c" + > + </File> + <File + RelativePath=".\src\antlr3filestream.c" + > + </File> + <File + RelativePath=".\src\antlr3inputstream.c" + > + </File> + <File + RelativePath=".\src\antlr3intstream.c" + > + </File> + <File + RelativePath=".\src\antlr3lexer.c" + > + </File> + <File + RelativePath=".\src\antlr3parser.c" + > + </File> + <File + RelativePath=".\src\antlr3rewritestreams.c" + > + </File> + <File + RelativePath=".\src\antlr3string.c" + > + </File> + <File + RelativePath=".\src\antlr3stringstream.c" + > + </File> + <File + RelativePath=".\src\antlr3tokenstream.c" + > + </File> + <File + RelativePath=".\src\antlr3treeparser.c" + > + </File> + <File + RelativePath=".\src\antlr3ucs2inputstream.c" + > + </File> + </Filter> + <Filter + Name="Header Files" + Filter="h;hpp;hxx;hm;inl;inc;xsd" + UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}" + > + <File + RelativePath=".\include\antlr3.h" + > + </File> + <File + RelativePath=".\include\antlr3baserecognizer.h" + > + </File> + <File + RelativePath=".\include\antlr3basetree.h" + > + </File> + <File + RelativePath=".\include\antlr3basetreeadaptor.h" + > + </File> + <File + RelativePath=".\include\antlr3bitset.h" + > + </File> + <File + RelativePath=".\include\antlr3collections.h" + > + </File> + <File + RelativePath=".\include\antlr3commontoken.h" + > + </File> + <File + RelativePath=".\include\antlr3commontree.h" + > + </File> + <File + RelativePath=".\include\antlr3commontreeadaptor.h" + > + </File> + <File + RelativePath=".\include\antlr3commontreenodestream.h" + > + </File> + <File + RelativePath=".\include\antlr3convertutf.h" + > + </File> + <File + RelativePath=".\include\antlr3cyclicdfa.h" + > + </File> + <File + RelativePath=".\include\antlr3debugeventlistener.h" + > + </File> + <File + RelativePath=".\include\antlr3defs.h" + > + </File> + <File + RelativePath=".\include\antlr3encodings.h" + > + </File> + <File + RelativePath=".\include\antlr3errors.h" + > + </File> + <File + RelativePath=".\include\antlr3exception.h" + > + </File> + <File + RelativePath=".\include\antlr3filestream.h" + > + </File> + <File + RelativePath=".\include\antlr3input.h" + > + </File> + <File + RelativePath=".\include\antlr3interfaces.h" + > + </File> + <File + RelativePath=".\include\antlr3intstream.h" + > + </File> + <File + RelativePath=".\include\antlr3lexer.h" + > + </File> + <File + RelativePath=".\include\antlr3memory.h" + > + </File> + <File + RelativePath=".\include\antlr3parser.h" + > + </File> + <File + RelativePath=".\include\antlr3parsetree.h" + > + </File> + <File + RelativePath=".\include\antlr3recognizersharedstate.h" + > + </File> + <File + RelativePath=".\include\antlr3rewritestreams.h" + > + </File> + <File + RelativePath=".\include\antlr3string.h" + > + </File> + <File + RelativePath=".\include\antlr3stringstream.h" + > + </File> + <File + RelativePath=".\include\antlr3tokenstream.h" + > + </File> + <File + RelativePath=".\include\antlr3treeparser.h" + > + </File> + </Filter> + <Filter + Name="Templates" + Filter=".stg" + > + <File + RelativePath="..\..\src\org\antlr\codegen\templates\C\AST.stg" + > + </File> + <File + RelativePath="..\..\src\org\antlr\codegen\templates\C\ASTDbg.stg" + > + </File> + <File + RelativePath="..\..\src\org\antlr\codegen\templates\C\ASTParser.stg" + > + </File> + <File + RelativePath="..\..\src\org\antlr\codegen\templates\C\ASTTreeParser.stg" + > + </File> + <File + RelativePath="..\..\src\org\antlr\codegen\templates\C\C.stg" + > + </File> + <File + RelativePath="..\..\src\org\antlr\codegen\templates\C\Dbg.stg" + > + </File> + <Filter + Name="Interfaces" + Filter="*.sti" + > + <File + RelativePath="..\..\src\org\antlr\codegen\templates\ANTLRCore.sti" + > + </File> + </Filter> + </Filter> + <Filter + Name="Java" + Filter="*.java" + > + <File + RelativePath="..\..\src\org\antlr\codegen\CTarget.java" + > + </File> + </Filter> + <Filter + Name="Doxygen" + > + <File + RelativePath=".\doxygen\atsections.dox" + > + </File> + <File + RelativePath=".\doxygen\build.dox" + > + </File> + <File + RelativePath=".\doxygen\buildrec.dox" + > + </File> + <File + RelativePath=".\doxygen\changes31.dox" + > + </File> + <File + RelativePath=".\doxygen\doxygengroups.dox" + > + </File> + <File + RelativePath=".\doxygen\generate.dox" + > + </File> + <File + RelativePath=".\doxygen\interop.dox" + > + </File> + <File + RelativePath=".\doxygen\mainpage.dox" + > + </File> + <File + RelativePath=".\doxygen\runtime.dox" + > + </File> + <File + RelativePath=".\doxygen\using.dox" + > + </File> + </Filter> + </Files> + <Globals> + <Global + Name="DevPartner_IsInstrumented" + Value="0" + /> + </Globals> +</VisualStudioProject> diff --git a/antlr-3.1.3/runtime/C/INSTALL b/antlr-3.1.3/runtime/C/INSTALL new file mode 100644 index 0000000..d3c5b40 --- /dev/null +++ b/antlr-3.1.3/runtime/C/INSTALL @@ -0,0 +1,237 @@ +Installation Instructions +************************* + +Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005, +2006, 2007 Free Software Foundation, Inc. + +This file is free documentation; the Free Software Foundation gives +unlimited permission to copy, distribute and modify it. + +Basic Installation +================== + +Briefly, the shell commands `./configure; make; make install' should +configure, build, and install this package. The following +more-detailed instructions are generic; see the `README' file for +instructions specific to this package. + + The `configure' shell script attempts to guess correct values for +various system-dependent variables used during compilation. It uses +those values to create a `Makefile' in each directory of the package. +It may also create one or more `.h' files containing system-dependent +definitions. Finally, it creates a shell script `config.status' that +you can run in the future to recreate the current configuration, and a +file `config.log' containing compiler output (useful mainly for +debugging `configure'). + + It can also use an optional file (typically called `config.cache' +and enabled with `--cache-file=config.cache' or simply `-C') that saves +the results of its tests to speed up reconfiguring. Caching is +disabled by default to prevent problems with accidental use of stale +cache files. + + If you need to do unusual things to compile the package, please try +to figure out how `configure' could check whether to do them, and mail +diffs or instructions to the address given in the `README' so they can +be considered for the next release. If you are using the cache, and at +some point `config.cache' contains results you don't want to keep, you +may remove or edit it. + + The file `configure.ac' (or `configure.in') is used to create +`configure' by a program called `autoconf'. You need `configure.ac' if +you want to change it or regenerate `configure' using a newer version +of `autoconf'. + +The simplest way to compile this package is: + + 1. `cd' to the directory containing the package's source code and type + `./configure' to configure the package for your system. + + Running `configure' might take a while. While running, it prints + some messages telling which features it is checking for. + + 2. Type `make' to compile the package. + + 3. Optionally, type `make check' to run any self-tests that come with + the package. + + 4. Type `make install' to install the programs and any data files and + documentation. + + 5. You can remove the program binaries and object files from the + source code directory by typing `make clean'. To also remove the + files that `configure' created (so you can compile the package for + a different kind of computer), type `make distclean'. There is + also a `make maintainer-clean' target, but that is intended mainly + for the package's developers. If you use it, you may have to get + all sorts of other programs in order to regenerate files that came + with the distribution. + + 6. Often, you can also type `make uninstall' to remove the installed + files again. + +Compilers and Options +===================== + +Some systems require unusual options for compilation or linking that the +`configure' script does not know about. Run `./configure --help' for +details on some of the pertinent environment variables. + + You can give `configure' initial values for configuration parameters +by setting variables in the command line or in the environment. Here +is an example: + + ./configure CC=c99 CFLAGS=-g LIBS=-lposix + + *Note Defining Variables::, for more details. + +Compiling For Multiple Architectures +==================================== + +You can compile the package for more than one kind of computer at the +same time, by placing the object files for each architecture in their +own directory. To do this, you can use GNU `make'. `cd' to the +directory where you want the object files and executables to go and run +the `configure' script. `configure' automatically checks for the +source code in the directory that `configure' is in and in `..'. + + With a non-GNU `make', it is safer to compile the package for one +architecture at a time in the source code directory. After you have +installed the package for one architecture, use `make distclean' before +reconfiguring for another architecture. + +Installation Names +================== + +By default, `make install' installs the package's commands under +`/usr/local/bin', include files under `/usr/local/include', etc. You +can specify an installation prefix other than `/usr/local' by giving +`configure' the option `--prefix=PREFIX'. + + You can specify separate installation prefixes for +architecture-specific files and architecture-independent files. If you +pass the option `--exec-prefix=PREFIX' to `configure', the package uses +PREFIX as the prefix for installing programs and libraries. +Documentation and other data files still use the regular prefix. + + In addition, if you use an unusual directory layout you can give +options like `--bindir=DIR' to specify different values for particular +kinds of files. Run `configure --help' for a list of the directories +you can set and what kinds of files go in them. + + If the package supports it, you can cause programs to be installed +with an extra prefix or suffix on their names by giving `configure' the +option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. + +Optional Features +================= + +Some packages pay attention to `--enable-FEATURE' options to +`configure', where FEATURE indicates an optional part of the package. +They may also pay attention to `--with-PACKAGE' options, where PACKAGE +is something like `gnu-as' or `x' (for the X Window System). The +`README' should mention any `--enable-' and `--with-' options that the +package recognizes. + + For packages that use the X Window System, `configure' can usually +find the X include and library files automatically, but if it doesn't, +you can use the `configure' options `--x-includes=DIR' and +`--x-libraries=DIR' to specify their locations. + +Specifying the System Type +========================== + +There may be some features `configure' cannot figure out automatically, +but needs to determine by the type of machine the package will run on. +Usually, assuming the package is built to be run on the _same_ +architectures, `configure' can figure that out, but if it prints a +message saying it cannot guess the machine type, give it the +`--build=TYPE' option. TYPE can either be a short name for the system +type, such as `sun4', or a canonical name which has the form: + + CPU-COMPANY-SYSTEM + +where SYSTEM can have one of these forms: + + OS KERNEL-OS + + See the file `config.sub' for the possible values of each field. If +`config.sub' isn't included in this package, then this package doesn't +need to know the machine type. + + If you are _building_ compiler tools for cross-compiling, you should +use the option `--target=TYPE' to select the type of system they will +produce code for. + + If you want to _use_ a cross compiler, that generates code for a +platform different from the build platform, you should specify the +"host" platform (i.e., that on which the generated programs will +eventually be run) with `--host=TYPE'. + +Sharing Defaults +================ + +If you want to set default values for `configure' scripts to share, you +can create a site shell script called `config.site' that gives default +values for variables like `CC', `cache_file', and `prefix'. +`configure' looks for `PREFIX/share/config.site' if it exists, then +`PREFIX/etc/config.site' if it exists. Or, you can set the +`CONFIG_SITE' environment variable to the location of the site script. +A warning: not all `configure' scripts look for a site script. + +Defining Variables +================== + +Variables not defined in a site shell script can be set in the +environment passed to `configure'. However, some packages may run +configure again during the build, and the customized values of these +variables may be lost. In order to avoid this problem, you should set +them in the `configure' command line, using `VAR=value'. For example: + + ./configure CC=/usr/local2/bin/gcc + +causes the specified `gcc' to be used as the C compiler (unless it is +overridden in the site shell script). + +Unfortunately, this technique does not work for `CONFIG_SHELL' due to +an Autoconf bug. Until the bug is fixed you can use this workaround: + + CONFIG_SHELL=/bin/bash /bin/bash ./configure CONFIG_SHELL=/bin/bash + +`configure' Invocation +====================== + +`configure' recognizes the following options to control how it operates. + +`--help' +`-h' + Print a summary of the options to `configure', and exit. + +`--version' +`-V' + Print the version of Autoconf used to generate the `configure' + script, and exit. + +`--cache-file=FILE' + Enable the cache: use and save the results of the tests in FILE, + traditionally `config.cache'. FILE defaults to `/dev/null' to + disable caching. + +`--config-cache' +`-C' + Alias for `--cache-file=config.cache'. + +`--quiet' +`--silent' +`-q' + Do not print messages saying which checks are being made. To + suppress all normal output, redirect it to `/dev/null' (any error + messages will still be shown). + +`--srcdir=DIR' + Look for the package's source code in directory DIR. Usually + `configure' can determine that directory automatically. + +`configure' also accepts some other, not widely useful, options. Run +`configure --help' for more details. + diff --git a/antlr-3.1.3/runtime/C/Makefile.am b/antlr-3.1.3/runtime/C/Makefile.am new file mode 100644 index 0000000..934c18c --- /dev/null +++ b/antlr-3.1.3/runtime/C/Makefile.am @@ -0,0 +1,81 @@ +AUTOMAKE_OPTIONS = gnu +AM_LIBTOOLFLAGS = +## --silent + +lib_LTLIBRARIES = libantlr3c.la + +LIBSOURCES = src/antlr3baserecognizer.c \ + src/antlr3basetree.c \ + src/antlr3basetreeadaptor.c \ + src/antlr3bitset.c \ + src/antlr3collections.c \ + src/antlr3commontoken.c \ + src/antlr3commontree.c \ + src/antlr3commontreeadaptor.c \ + src/antlr3commontreenodestream.c \ + src/antlr3convertutf.c \ + src/antlr3cyclicdfa.c \ + src/antlr3debughandlers.c \ + src/antlr3encodings.c \ + src/antlr3exception.c \ + src/antlr3filestream.c \ + src/antlr3inputstream.c \ + src/antlr3intstream.c \ + src/antlr3lexer.c \ + src/antlr3parser.c \ + src/antlr3rewritestreams.c \ + src/antlr3string.c \ + src/antlr3stringstream.c \ + src/antlr3tokenstream.c \ + src/antlr3treeparser.c \ + src/antlr3ucs2inputstream.c + +libantlr3c_la_SOURCES = $(LIBSOURCES) + +include_HEADERS = include/antlr3.h \ + include/antlr3baserecognizer.h \ + include/antlr3basetree.h \ + include/antlr3basetreeadaptor.h \ + include/antlr3bitset.h \ + include/antlr3collections.h \ + include/antlr3commontoken.h \ + include/antlr3commontree.h \ + include/antlr3commontreeadaptor.h \ + include/antlr3commontreenodestream.h \ + include/antlr3convertutf.h \ + include/antlr3cyclicdfa.h \ + include/antlr3debugeventlistener.h \ + include/antlr3defs.h \ + include/antlr3encodings.h \ + include/antlr3errors.h \ + include/antlr3exception.h \ + include/antlr3filestream.h \ + include/antlr3input.h \ + include/antlr3interfaces.h \ + include/antlr3intstream.h \ + include/antlr3lexer.h \ + include/antlr3memory.h \ + include/antlr3parser.h \ + include/antlr3parsetree.h \ + include/antlr3recognizersharedstate.h \ + include/antlr3rewritestreams.h \ + include/antlr3string.h \ + include/antlr3stringstream.h \ + include/antlr3tokenstream.h \ + include/antlr3treeparser.h \ + antlr3config.h + +libantlr3c_la_LDFLAGS = -avoid-version + +INCLUDES = -Iinclude -Iinclude + +EXTRA_DIST = \ + vsrulefiles/antlr3lexerandparser.rules \ + vsrulefiles/antlr3lexer.rules \ + vsrulefiles/antlr3parser.rules \ + vsrulefiles/antlr3treeparser.rules \ + C.sln C.vcproj C.vcproj.vspscc \ + C.vssscc + +export OBJECT_MODE + diff --git a/antlr-3.1.3/runtime/C/NEWS b/antlr-3.1.3/runtime/C/NEWS new file mode 100644 index 0000000..aea82ff --- /dev/null +++ b/antlr-3.1.3/runtime/C/NEWS @@ -0,0 +1,2 @@ +See www.antlr.org and the associated email forums for release dates and +other announcements. diff --git a/antlr-3.1.3/runtime/C/README b/antlr-3.1.3/runtime/C/README new file mode 100644 index 0000000..a1c0555 --- /dev/null +++ b/antlr-3.1.3/runtime/C/README @@ -0,0 +1,1924 @@ +ANTLR v3.0.1 C Runtime +ANTLR 3.0.1 +January 1, 2008 + +At the moment, the use of the C runtime engine for the parser is not generally +for the inexperienced C programmer. However this is mainly because of the lack +of documentation on use, which will be corrected shortly. The C runtime +code itself is however well documented with doxygen style comments and a +reasonably experienced C programmer should be able to piece it together. You +can visit the documentation at: http://www.antlr.org/api/C/index.html + +The general make up is that everything is implemented as a pseudo class/object +initialized with pointers to its 'member' functions and data. All objects are +(usually) created by factories, which auto manage the memory allocation and +release and generally make life easier. If you remember this rule, everything +should fall in to place. + +Jim Idle - Portland Oregon, Jan 2008 +jimi idle ws + +=============================================================================== + +Terence Parr, parrt at cs usfca edu +ANTLR project lead and supreme dictator for life +University of San Francisco + +INTRODUCTION + +Welcome to ANTLR v3! I've been working on this for nearly 4 years and it's +almost ready! I plan no feature additions between this beta and first +3.0 release. I have lots of features to add later, but this will be +the first set. Ultimately, I need to rewrite ANTLR v3 in itself (it's +written in 2.7.7 at the moment and also needs StringTemplate 3.0 or +later). + +You should use v3 in conjunction with ANTLRWorks: + + http://www.antlr.org/works/index.html + +WARNING: We have bits of documentation started, but nothing super-complete +yet. The book will be printed May 2007: + +http://www.pragmaticprogrammer.com/titles/tpantlr/index.html + +but we should have a beta PDF available on that page in Feb 2007. + +You also have the examples plus the source to guide you. + +See the new wiki FAQ: + + http://www.antlr.org/wiki/display/ANTLR3/ANTLR+v3+FAQ + +and general doc root: + + http://www.antlr.org/wiki/display/ANTLR3/ANTLR+3+Wiki+Home + +Please help add/update FAQ entries. + +I have made very little effort at this point to deal well with +erroneous input (e.g., bad syntax might make ANTLR crash). I will clean +this up after I've rewritten v3 in v3. + +Per the license in LICENSE.txt, this software is not guaranteed to +work and might even destroy all life on this planet: + +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. + +EXAMPLES + +ANTLR v3 sample grammars: + + http://www.antlr.org/download/examples-v3.tar.gz + +contains the following examples: LL-star, cminus, dynamic-scope, +fuzzy, hoistedPredicates, island-grammar, java, python, scopes, +simplecTreeParser, treeparser, tweak, xmlLexer. + +Also check out Mantra Programming Language for a prototype (work in +progress) using v3: + + http://www.linguamantra.org/ + +---------------------------------------------------------------------- + +What is ANTLR? + +ANTLR stands for (AN)other (T)ool for (L)anguage (R)ecognition and was +originally known as PCCTS. ANTLR is a language tool that provides a +framework for constructing recognizers, compilers, and translators +from grammatical descriptions containing actions. Target language list: + +http://www.antlr.org/wiki/display/ANTLR3/Code+Generation+Targets + +---------------------------------------------------------------------- + +How is ANTLR v3 different than ANTLR v2? + +See migration guide: + http://www.antlr.org/wiki/display/ANTLR3/Migrating+from+ANTLR+2+to+ANTLR+3 + +ANTLR v3 has a far superior parsing algorithm called LL(*) that +handles many more grammars than v2 does. In practice, it means you +can throw almost any grammar at ANTLR that is non-left-recursive and +unambiguous (same input can be matched by multiple rules); the cost is +perhaps a tiny bit of backtracking, but with a DFA not a full parser. +You can manually set the max lookahead k as an option for any decision +though. The LL(*) algorithm ramps up to use more lookahead when it +needs to and is much more efficient than normal LL backtracking. There +is support for syntactic predicate (full LL backtracking) when LL(*) +fails. + +Lexers are much easier due to the LL(*) algorithm as well. Previously +these two lexer rules would cause trouble because ANTLR couldn't +distinguish between them with finite lookahead to see the decimal +point: + +INT : ('0'..'9')+ ; +FLOAT : INT '.' INT ; + +The syntax is almost identical for features in common, but you should +note that labels are always '=' not ':'. So do id=ID not id:ID. + +You can do combined lexer/parser grammars again (ala PCCTS) both lexer +and parser rules are defined in the same file. See the examples. +Really nice. You can reference strings and characters in the grammar +and ANTLR will generate the lexer for you. + +The attribute structure has been enhanced. Rules may have multiple +return values, for example. Further, there are dynamically scoped +attributes whereby a rule may define a value usable by any rule it +invokes directly or indirectly w/o having to pass a parameter all the +way down. + +ANTLR v3 tree construction is far superior--it provides tree rewrite +rules where the right hand side is simply the tree grammar fragment +describing the tree you want to build: + +formalArgs + : typename declarator (',' typename declarator )* + -> ^(ARG typename declarator)+ + ; + +That builds tree sequences like: + +^(ARG int v1) ^(ARG int v2) + +ANTLR v3 also incorporates StringTemplate: + + http://www.stringtemplate.org + +just like AST support. It is useful for generating output. For +example this rule creates a template called 'import' for each import +definition found in the input stream: + +grammar Java; +options { + output=template; +} +... +importDefinition + : 'import' identifierStar SEMI + -> import(name={$identifierStar.st}, + begin={$identifierStar.start}, + end={$identifierStar.stop}) + ; + +The attributes are set via assignments in the argument list. The +arguments are actions with arbitrary expressions in the target +language. The .st label property is the result template from a rule +reference. There is a nice shorthand in actions too: + + %foo(a={},b={},...) ctor + %({name-expr})(a={},...) indirect template ctor reference + %{string-expr} anonymous template from string expr + %{expr}.y = z; template attribute y of StringTemplate-typed expr to z + %x.y = z; set template attribute y of x (always set never get attr) + to z [languages like python without ';' must still use the + ';' which the code generator is free to remove during code gen] + Same as '(x).setAttribute("y", z);' + +For ANTLR v3 I decided to make the most common tasks easy by default +rather. This means that some of the basic objects are heavier weight +than some speed demons would like, but they are free to pare it down +leaving most programmers the luxury of having it "just work." For +example, to read in some input, tweak it, and write it back out +preserving whitespace, is easy in v3. + +The ANTLR source code is much prettier. You'll also note that the +run-time classes are conveniently encapsulated in the +org.antlr.runtime package. + +---------------------------------------------------------------------- + +How do I install this damn thing? + +Just untar and you'll get: + +antlr-3.0b6/README.txt (this file) +antlr-3.0b6/LICENSE.txt +antlr-3.0b6/src/org/antlr/... +antlr-3.0b6/lib/stringtemplate-3.0.jar (3.0b6 needs 3.0) +antlr-3.0b6/lib/antlr-2.7.7.jar +antlr-3.0b6/lib/antlr-3.0b6.jar + +Then you need to add all the jars in lib to your CLASSPATH. + +---------------------------------------------------------------------- + +How do I use ANTLR v3? + +[I am assuming you are only using the command-line (and not the +ANTLRWorks GUI)]. + +Running ANTLR with no parameters shows you: + +ANTLR Parser Generator Early Access Version 3.0b6 (Jan 31, 2007) 1989-2007 +usage: java org.antlr.Tool [args] file.g [file2.g file3.g ...] + -o outputDir specify output directory where all output is generated + -lib dir specify location of token files + -report print out a report about the grammar(s) processed + -print print out the grammar without actions + -debug generate a parser that emits debugging events + -profile generate a parser that computes profiling information + -nfa generate an NFA for each rule + -dfa generate a DFA for each decision point + -message-format name specify output style for messages + -X display extended argument list + +For example, consider how to make the LL-star example from the examples +tarball you can get at http://www.antlr.org/download/examples-v3.tar.gz + +$ cd examples/java/LL-star +$ java org.antlr.Tool simplec.g +$ jikes *.java + +For input: + +char c; +int x; +void bar(int x); +int foo(int y, char d) { + int i; + for (i=0; i<3; i=i+1) { + x=3; + y=5; + } +} + +you will see output as follows: + +$ java Main input +bar is a declaration +foo is a definition + +What if I want to test my parser without generating code? Easy. Just +run ANTLR in interpreter mode. It can't execute your actions, but it +can create a parse tree from your input to show you how it would be +matched. Use the org.antlr.tool.Interp main class. In the following, +I interpret simplec.g on t.c, which contains "int x;" + +$ java org.antlr.tool.Interp simplec.g WS program t.c +( <grammar SimpleC> + ( program + ( declaration + ( variable + ( type [@0,0:2='int',<14>,1:0] ) + ( declarator [@2,4:4='x',<2>,1:4] ) + [@3,5:5=';',<5>,1:5] + ) + ) + ) +) + +where I have formatted the output to make it more readable. I have +told it to ignore all WS tokens. + +---------------------------------------------------------------------- + +How do I rebuild ANTLR v3? + +Make sure the following two jars are in your CLASSPATH + +antlr-3.0b6/lib/stringtemplate-3.0.jar +antlr-3.0b6/lib/antlr-2.7.7.jar +junit.jar [if you want to build the test directories] + +then jump into antlr-3.0b6/src directory and then type: + +$ javac -d . org/antlr/Tool.java org/antlr/*/*.java org/antlr/*/*/*.java + +Takes 9 seconds on my 1Ghz laptop or 4 seconds with jikes. Later I'll +have a real build mechanism, though I must admit the one-liner appeals +to me. I use Intellij so I never type anything actually to build. + +There is also an ANT build.xml file, but I know nothing of ANT; contributed +by others (I'm opposed to any tool with an XML interface for Humans). + +----------------------------------------------------------------------- +C# Target Notes + +1. Auto-generated lexers do not inherit parent parser's @namespace + {...} value. Use @lexer::namespace{...}. + +----------------------------------------------------------------------- + +CHANGES + +March 17, 2007 + +* Jonathan DeKlotz updated C# templates to be 3.0b6 current + +March 14, 2007 + +* Manually-specified (...)=> force backtracking eval of that predicate. + backtracking=true mode does not however. Added unit test. + +March 14, 2007 + +* Fixed bug in lexer where ~T didn't compute the set from rule T. + +* Added -Xnoinlinedfa make all DFA with tables; no inline prediction with IFs + +* Fixed http://www.antlr.org:8888/browse/ANTLR-80. + Sem pred states didn't define lookahead vars. + +* Fixed http://www.antlr.org:8888/browse/ANTLR-91. + When forcing some acyclic DFA to be state tables, they broke. + Forcing all DFA to be state tables should give same results. + +March 12, 2007 + +* setTokenSource in CommonTokenStream didn't clear tokens list. + setCharStream calls reset in Lexer. + +* Altered -depend. No longer printing grammar files for multiple input + files with -depend. Doesn't show T__.g temp file anymore. Added + TLexer.tokens. Added .h files if defined. + +February 11, 2007 + +* Added -depend command-line option that, instead of processing files, + it shows you what files the input grammar(s) depend on and what files + they generate. For combined grammar T.g: + + $ java org.antlr.Tool -depend T.g + + You get: + + TParser.java : T.g + T.tokens : T.g + T__.g : T.g + + Now, assuming U.g is a tree grammar ref'd T's tokens: + + $ java org.antlr.Tool -depend T.g U.g + + TParser.java : T.g + T.tokens : T.g + T__.g : T.g + U.g: T.tokens + U.java : U.g + U.tokens : U.g + + Handles spaces by escaping them. Pays attention to -o, -fo and -lib. + Dir 'x y' is a valid dir in current dir. + + $ java org.antlr.Tool -depend -lib /usr/local/lib -o 'x y' T.g U.g + x\ y/TParser.java : T.g + x\ y/T.tokens : T.g + x\ y/T__.g : T.g + U.g: /usr/local/lib/T.tokens + x\ y/U.java : U.g + x\ y/U.tokens : U.g + + You have API access via org.antlr.tool.BuildDependencyGenerator class: + getGeneratedFileList(), getDependenciesFileList(). You can also access + the output template: getDependencies(). The file + org/antlr/tool/templates/depend.stg contains the template. You can + modify as you want. File objects go in so you can play with path etc... + +February 10, 2007 + +* no more .gl files generated. All .g all the time. + +* changed @finally to be @after and added a finally clause to the + exception stuff. I also removed the superfluous "exception" + keyword. Here's what the new syntax looks like: + + a + @after { System.out.println("ick"); } + : 'a' + ; + catch[RecognitionException e] { System.out.println("foo"); } + catch[IOException e] { System.out.println("io"); } + finally { System.out.println("foobar"); } + + @after executes after bookkeeping to set $rule.stop, $rule.tree but + before scopes pop and any memoization happens. Dynamic scopes and + memoization are still in generated finally block because they must + exec even if error in rule. The @after action and tree setting + stuff can technically be skipped upon syntax error in rule. [Later + we might add something to finally to stick an ERROR token in the + tree and set the return value.] Sequence goes: set $stop, $tree (if + any), @after (if any), pop scopes (if any), memoize (if needed), + grammar finally clause. Last 3 are in generated code's finally + clause. + +3.0b6 - January 31, 2007 + +January 30, 2007 + +* Fixed bug in IntervalSet.and: it returned the same empty set all the time + rather than new empty set. Code altered the same empty set. + +* Made analysis terminate faster upon a decision that takes too long; + it seemed to keep doing work for a while. Refactored some names + and updated comments. Also made it terminate when it realizes it's + non-LL(*) due to recursion. just added terminate conditions to loop + in convert(). + +* Sometimes fatal non-LL(*) messages didn't appear; instead you got + "antlr couldn't analyze", which is actually untrue. I had the + order of some prints wrong in the DecisionProbe. + +* The code generator incorrectly detected when it could use a fixed, + acyclic inline DFA (i.e., using an IF). Upon non-LL(*) decisions + with predicates, analysis made cyclic DFA. But this stops + the computation detecting whether they are cyclic. I just added + a protection in front of the acyclic DFA generator to avoid if + non-LL(*). Updated comments. + +January 23, 2007 + +* Made tree node streams use adaptor to create navigation nodes. + Thanks to Emond Papegaaij. + +January 22, 2007 + +* Added lexer rule properties: start, stop + +January 1, 2007 + +* analysis failsafe is back on; if a decision takes too long, it bails out + and uses k=1 + +January 1, 2007 + +* += labels for rules only work for output option; previously elements + of list were the return value structs, but are now either the tree or + StringTemplate return value. You can label different rules now + x+=a x+=b. + +December 30, 2006 + +* Allow \" to work correctly in "..." template. + +December 28, 2006 + +* errors that are now warnings: missing AST label type in trees. + Also "no start rule detected" is warning. + +* tree grammars also can do rewrite=true for output=template. + Only works for alts with single node or tree as alt elements. + If you are going to use $text in a tree grammar or do rewrite=true + for templates, you must use in your main: + + nodes.setTokenStream(tokens); + +* You get a warning for tree grammars that do rewrite=true and + output=template and have -> for alts that are not simple nodes + or simple trees. new unit tests in TestRewriteTemplates at end. + +December 27, 2006 + +* Error message appears when you use -> in tree grammar with + output=template and rewrite=true for alt that is not simple + node or tree ref. + +* no more $stop attribute for tree parsers; meaningless/useless. + Removed from TreeRuleReturnScope also. + +* rule text attribute in tree parser must pull from token buffer. + Makes no sense otherwise. added getTokenStream to TreeNodeStream + so rule $text attr works. CommonTreeNodeStream etc... now let + you set the token stream so you can access later from tree parser. + $text is not well-defined for rules like + + slist : stat+ ; + + because stat is not a single node nor rooted with a single node. + $slist.text will get only first stat. I need to add a warning about + this... + +* Fixed http://www.antlr.org:8888/browse/ANTLR-76 for Java. + Enhanced TokenRewriteStream so it accepts any object; converts + to string at last second. Allows you to rewrite with StringTemplate + templates now :) + +* added rewrite option that makes -> template rewrites do replace ops for + TokenRewriteStream input stream. In output=template and rewrite=true mode + same as before 'cept that the parser does + + ((TokenRewriteStream)input).replace( + ((Token)retval.start).getTokenIndex(), + input.LT(-1).getTokenIndex(), + retval.st); + + after each rewrite so that the input stream is altered. Later refs to + $text will have rewrites. Here's a sample test program for grammar Rew. + + FileReader groupFileR = new FileReader("Rew.stg"); + StringTemplateGroup templates = new StringTemplateGroup(groupFileR); + ANTLRInputStream input = new ANTLRInputStream(System.in); + RewLexer lexer = new RewLexer(input); + TokenRewriteStream tokens = new TokenRewriteStream(lexer); + RewParser parser = new RewParser(tokens); + parser.setTemplateLib(templates); + parser.program(); + System.out.println(tokens.toString()); + groupFileR.close(); + +December 26, 2006 + +* BaseTree.dupTree didn't dup recursively. + +December 24, 2006 + +* Cleaned up some comments and removed field treeNode + from MismatchedTreeNodeException class. It is "node" in + RecognitionException. + +* Changed type from Object to BitSet for expecting fields in + MismatchedSetException and MismatchedNotSetException + +* Cleaned up error printing in lexers and the messages that it creates. + +* Added this to TreeAdaptor: + /** Return the token object from which this node was created. + * Currently used only for printing an error message. + * The error display routine in BaseRecognizer needs to + * display where the input the error occurred. If your + * tree of limitation does not store information that can + * lead you to the token, you can create a token filled with + * the appropriate information and pass that back. See + * BaseRecognizer.getErrorMessage(). + */ + public Token getToken(Object t); + +December 23, 2006 + +* made BaseRecognizer.displayRecognitionError nonstatic so people can + override it. Not sure why it was static before. + +* Removed state/decision message that comes out of no + viable alternative exceptions, as that was too much. + removed the decision number from the early exit exception + also. During development, you can simply override + displayRecognitionError from BaseRecognizer to add the stuff + back in if you want. + +* made output go to an output method you can override: emitErrorMessage() + +* general cleanup of the error emitting code in BaseRecognizer. Lots + more stuff you can override: getErrorHeader, getTokenErrorDisplay, + emitErrorMessage, getErrorMessage. + +December 22, 2006 + +* Altered Tree.Parser.matchAny() so that it skips entire trees if + node has children otherwise skips one node. Now this works to + skip entire body of function if single-rooted subtree: + ^(FUNC name=ID arg=ID .) + +* Added "reverse index" from node to stream index. Override + fillReverseIndex() in CommonTreeNodeStream if you want to change. + Use getNodeIndex(node) to find stream index for a specific tree node. + See getNodeIndex(), reverseIndex(Set tokenTypes), + reverseIndex(int tokenType), fillReverseIndex(). The indexing + costs time and memory to fill, but pulling stuff out will be lots + faster as it can jump from a node ptr straight to a stream index. + +* Added TreeNodeStream.get(index) to make it easier for interpreters to + jump around in tree node stream. + +* New CommonTreeNodeStream buffers all nodes in stream for fast jumping + around. It now has push/pop methods to invoke other locations in + the stream for building interpreters. + +* Moved CommonTreeNodeStream to UnBufferedTreeNodeStream and removed + Iterator implementation. moved toNodesOnlyString() to TestTreeNodeStream + +* [BREAKS ANY TREE IMPLEMENTATION] + made CommonTreeNodeStream work with any tree node type. TreeAdaptor + now implements isNil so must add; trivial, but does break back + compatibility. + +December 17, 2006 + +* Added traceIn/Out methods to recognizers so that you can override them; + previously they were in-line print statements. The message has also + been slightly improved. + +* Factored BuildParseTree into debug package; cleaned stuff up. Fixed + unit tests. + +December 15, 2006 + +* [BREAKS ANY TREE IMPLEMENTATION] + org.antlr.runtime.tree.Tree; needed to add get/set for token start/stop + index so CommonTreeAdaptor can assume Tree interface not CommonTree + implementation. Otherwise, no way to create your own nodes that satisfy + Tree because CommonTreeAdaptor was doing + + public int getTokenStartIndex(Object t) { + return ((CommonTree)t).startIndex; + } + + Added to Tree: + + /** What is the smallest token index (indexing from 0) for this node + * and its children? + */ + int getTokenStartIndex(); + + void setTokenStartIndex(int index); + + /** What is the largest token index (indexing from 0) for this node + * and its children? + */ + int getTokenStopIndex(); + + void setTokenStopIndex(int index); + +December 13, 2006 + +* Added org.antlr.runtime.tree.DOTTreeGenerator so you can generate DOT + diagrams easily from trees. + + CharStream input = new ANTLRInputStream(System.in); + TLexer lex = new TLexer(input); + CommonTokenStream tokens = new CommonTokenStream(lex); + TParser parser = new TParser(tokens); + TParser.e_return r = parser.e(); + Tree t = (Tree)r.tree; + System.out.println(t.toStringTree()); + DOTTreeGenerator gen = new DOTTreeGenerator(); + StringTemplate st = gen.toDOT(t); + System.out.println(st); + +* Changed the way mark()/rewind() work in CommonTreeNode stream to mirror + more flexible solution in ANTLRStringStream. Forgot to set lastMarker + anyway. Now you can rewind to non-most-recent marker. + +December 12, 2006 + +* Temp lexer now end in .gl (T__.gl, for example) + +* TreeParser suffix no longer generated for tree grammars + +* Defined reset for lexer, parser, tree parser; rewinds the input stream also + +December 10, 2006 + +* Made Grammar.abortNFAToDFAConversion() abort in middle of a DFA. + +December 9, 2006 + +* fixed bug in OrderedHashSet.add(). It didn't track elements correctly. + +December 6, 2006 + +* updated build.xml for future Ant compatibility, thanks to Matt Benson. + +* various tests in TestRewriteTemplate and TestSyntacticPredicateEvaluation + were using the old 'channel' vs. new '$channel' notation. + TestInterpretedParsing didn't pick up an earlier change to CommonToken. + Reported by Matt Benson. + +* fixed platform dependent test failures in TestTemplates, supplied by Matt + Benson. + +November 29, 2006 + +* optimized semantic predicate evaluation so that p||!p yields true. + +November 22, 2006 + +* fixed bug that prevented var = $rule.some_retval from working in anything + but the first alternative of a rule or subrule. + +* attribute names containing digits were not allowed, this is now fixed, + allowing attributes like 'name1' but not '1name1'. + +November 19, 2006 + +* Removed LeftRecursionMessage and apparatus because it seems that I check + for left recursion upfront before analysis and everything gets specified as + recursion cycles at this point. + +November 16, 2006 + +* TokenRewriteStream.replace was not passing programName to next method. + +November 15, 2006 + +* updated DOT files for DFA generation to make smaller circles. + +* made epsilon edges italics in the NFA diagrams. + +3.0b5 - November 15, 2006 + +The biggest thing is that your grammar file names must match the grammar name +inside (your generated class names will also be different) and we use +$channel=HIDDEN now instead of channel=99 inside lexer actions. +Should be compatible other than that. Please look at complete list of +changes. + +November 14, 2006 + +* Force token index to be -1 for CommonIndex in case not set. + +November 11, 2006 + +* getUniqueID for TreeAdaptor now uses identityHashCode instead of hashCode. + +November 10, 2006 + +* No grammar nondeterminism warning now when wildcard '.' is final alt. + Examples: + + a : A | B | . ; + + A : 'a' + | . + ; + + SL_COMMENT + : '//' (options {greedy=false;} : .)* '\r'? '\n' + ; + + SL_COMMENT2 + : '//' (options {greedy=false;} : 'x'|.)* '\r'? '\n' + ; + + +November 8, 2006 + +* Syntactic predicates did not get hoisting properly upon non-LL(*) decision. Other hoisting issues fixed. Cleaned up code. + +* Removed failsafe that check to see if I'm spending too much time on a single DFA; I don't think we need it anymore. + +November 3, 2006 + +* $text, $line, etc... were not working in assignments. Fixed and added + test case. + +* $label.text translated to label.getText in lexer even if label was on a char + +November 2, 2006 + +* Added error if you don't specify what the AST type is; actions in tree + grammar won't work without it. + + $ cat x.g + tree grammar x; + a : ID {String s = $ID.text;} ; + + ANTLR Parser Generator Early Access Version 3.0b5 (??, 2006) 1989-2006 + error: x.g:0:0: (152) tree grammar x has no ASTLabelType option + +November 1, 2006 + +* $text, $line, etc... were not working properly within lexer rule. + +October 32, 2006 + +* Finally actions now execute before dynamic scopes are popped it in the + rule. Previously was not possible to access the rules scoped variables + in a finally action. + +October 29, 2006 + +* Altered ActionTranslator to emit errors on setting read-only attributes + such as $start, $stop, $text in a rule. Also forbid setting any attributes + in rules/tokens referenced by a label or name. + Setting dynamic scopes's attributes and your own parameter attributes + is legal. + +October 27, 2006 + +* Altered how ANTLR figures out what decision is associated with which + block of grammar. Makes ANTLRWorks correctly find DFA for a block. + +October 26, 2006 + +* Fixed bug where EOT transitions led to no NFA configs in a DFA state, + yielding an error in DFA table generation. + +* renamed action.g to ActionTranslator.g + the ActionTranslator class is now called ActionTranslatorLexer, as ANTLR + generates this classname now. Fixed rest of codebase accordingly. + +* added rules recognizing setting of scopes' attributes to ActionTranslator.g + the Objective C target needed access to the right-hand side of the assignment + in order to generate correct code + +* changed ANTLRCore.sti to reflect the new mandatory templates to support the above + namely: scopeSetAttributeRef, returnSetAttributeRef and the ruleSetPropertyRef_* + templates, with the exception of ruleSetPropertyRef_text. we cannot set this attribute + +October 19, 2006 + +* Fixed 2 bugs in DFA conversion that caused exceptions. + altered functionality of getMinElement so it ignores elements<0. + +October 18, 2006 + +* moved resetStateNumbersToBeContiguous() to after issuing of warnings; + an internal error in that routine should make more sense as issues + with decision will appear first. + +* fixed cut/paste bug I introduced when fixed EOF in min/max + bug. Prevented C grammar from working briefly. + +October 17, 2006 + +* Removed a failsafe that seems to be unnecessary that ensure DFA didn't + get too big. It was resulting in some failures in code generation that + led me on quite a strange debugging trip. + +October 16, 2006 + +* Use channel=HIDDEN not channel=99 to put tokens on hidden channel. + +October 12, 2006 + +* ANTLR now has a customizable message format for errors and warnings, + to make it easier to fulfill requirements by IDEs and such. + The format to be used can be specified via the '-message-format name' + command line switch. The default for name is 'antlr', also available + at the moment is 'gnu'. This is done via StringTemplate, for details + on the requirements look in org/antlr/tool/templates/messages/formats/ + +* line numbers for lexers in combined grammars are now reported correctly. + +September 29, 2006 + +* ANTLRReaderStream improperly checked for end of input. + +September 28, 2006 + +* For ANTLRStringStream, LA(-1) was off by one...gave you LA(-2). + +3.0b4 - August 24, 2006 + +* error when no rules in grammar. doesn't crash now. + +* Token is now an interface. + +* remove dependence on non runtime classes in runtime package. + +* filename and grammar name must be same Foo in Foo.g. Generates FooParser, + FooLexer, ... Combined grammar Foo generates Foo$Lexer.g which generates + FooLexer.java. tree grammars generate FooTreeParser.java + +August 24, 2006 + +* added C# target to lib, codegen, templates + +August 11, 2006 + +* added tree arg to navigation methods in treeadaptor + +August 07, 2006 + +* fixed bug related to (a|)+ on end of lexer rules. crashed instead + of warning. + +* added warning that interpreter doesn't do synpreds yet + +* allow different source of classloader: +ClassLoader cl = Thread.currentThread().getContextClassLoader(); +if ( cl==null ) { + cl = this.getClass().getClassLoader(); +} + + +July 26, 2006 + +* compressed DFA edge tables significantly. All edge tables are + unique. The transition table can reuse arrays. Look like this now: + + public static readonly DFA30_transition0 = + new short[] { 46, 46, -1, 46, 46, -1, -1, -1, -1, -1, -1, -1,...}; + public static readonly DFA30_transition1 = + new short[] { 21 }; + public static readonly short[][] DFA30_transition = { + DFA30_transition0, + DFA30_transition0, + DFA30_transition1, + ... + }; + +* If you defined both a label like EQ and '=', sometimes the '=' was + used instead of the EQ label. + +* made headerFile template have same arg list as outputFile for consistency + +* outputFile, lexer, genericParser, parser, treeParser templates + reference cyclicDFAs attribute which was no longer used after I + started the new table-based DFA. I made cyclicDFADescriptors + argument to outputFile and headerFile (only). I think this is + correct as only OO languages will want the DFA in the recognizer. + At the top level, C and friends can use it. Changed name to use + cyclicDFAs again as it's a better name probably. Removed parameter + from the lexer, ... For example, my parser template says this now: + + <cyclicDFAs:cyclicDFA()> <! dump tables for all DFA !> + +* made all token ref token types go thru code gen's + getTokenTypeAsTargetLabel() + +* no more computing DFA transition tables for acyclic DFA. + +July 25, 2006 + +* fixed a place where I was adding syn predicates into rewrite stuff. + +* turned off invalid token index warning in AW support; had a problem. + +* bad location event generated with -debug for synpreds in autobacktrack mode. + +July 24, 2006 + +* changed runtime.DFA so that it treats all chars and token types as + char (unsigned 16 bit int). -1 becomes '\uFFFF' then or 65535. + +* changed MAX_STATE_TRANSITIONS_FOR_TABLE to be 65534 by default + now. This means that all states can use a table to do transitions. + +* was not making synpreds on (C)* type loops with backtrack=true + +* was copying tree stuff and actions into synpreds with backtrack=true + +* was making synpreds on even single alt rules / blocks with backtrack=true + +3.0b3 - July 21, 2006 + +* ANTLR fails to analyze complex decisions much less frequently. It + turns out that the set of decisions for which ANTLR fails (times + out) is the same set (so far) of non-LL(*) decisions. Morever, I'm + able to detect this situation quickly and report rather than timing + out. Errors look like: + + java.g:468:23: [fatal] rule concreteDimensions has non-LL(*) + decision due to recursive rule invocations in alts 1,2. Resolve + by left-factoring or using syntactic predicates with fixed k + lookahead or use backtrack=true option. + + This message only appears when k=*. + +* Shortened no viable alt messages to not include decision + description: + +[compilationUnit, declaration]: line 8:8 decision=<<67:1: declaration +: ( ( fieldDeclaration )=> fieldDeclaration | ( methodDeclaration )=> +methodDeclaration | ( constructorDeclaration )=> +constructorDeclaration | ( classDeclaration )=> classDeclaration | ( +interfaceDeclaration )=> interfaceDeclaration | ( blockDeclaration )=> +blockDeclaration | emptyDeclaration );>> state 3 (decision=14) no +viable alt; token=[@1,184:187='java',<122>,8:8] + + too long and hard to read. + +July 19, 2006 + +* Code gen bug: states with no emanating edges were ignored by ST. + Now an empty list is used. + +* Added grammar parameter to recognizer templates so they can access + properties like getName(), ... + +July 10, 2006 + +* Fixed the gated pred merged state bug. Added unit test. + +* added new method to Target: getTokenTypeAsTargetLabel() + +July 7, 2006 + +* I was doing an AND instead of OR in the gated predicate stuff. + Thanks to Stephen Kou! + +* Reduce op for combining predicates was insanely slow sometimes and + didn't actually work well. Now it's fast and works. + +* There is a bug in merging of DFA stop states related to gated + preds...turned it off for now. + +3.0b2 - July 5, 2006 + +July 5, 2006 + +* token emission not properly protected in lexer filter mode. + +* EOT, EOT DFA state transition tables should be init'd to -1 (only + was doing this for compressed tables). Fixed. + +* in trace mode, exit method not shown for memoized rules + +* added -Xmaxdfaedges to allow you to increase number of edges allowed + for a single DFA state before it becomes "special" and can't fit in + a simple table. + +* Bug in tables. Short are signed so min/max tables for DFA are now + char[]. Bizarre. + +July 3, 2006 + +* Added a method to reset the tool error state for current thread. + See ErrorManager.java + +* [Got this working properly today] backtrack mode that let's you type + in any old crap and ANTLR will backtrack if it can't figure out what + you meant. No errors are reported by antlr during analysis. It + implicitly adds a syn pred in front of every production, using them + only if static grammar LL(*) analysis fails. Syn pred code is not + generated if the pred is not used in a decision. + + This is essentially a rapid prototyping mode. + +* Added backtracking report to the -report option + +* Added NFA->DFA conversion early termination report to the -report option + +* Added grammar level k and backtrack options to -report + +* Added a dozen unit tests to test autobacktrack NFA construction. + +* If you are using filter mode, you must manually use option + memoize=true now. + +July 2, 2006 + +* Added k=* option so you can set k=2, for example, on whole grammar, + but an individual decision can be LL(*). + +* memoize option for grammars, rules, blocks. Remove -nomemo cmd-line option + +* but in DOT generator for DFA; fixed. + +* runtime.DFA reported errors even when backtracking + +July 1, 2006 + +* Added -X option list to help + +* Syn preds were being hoisted into other rules, causing lots of extra + backtracking. + +June 29, 2006 + +* unnecessary files removed during build. + +* Matt Benson updated build.xml + +* Detecting use of synpreds in analysis now instead of codegen. In + this way, I can avoid analyzing decisions in synpreds for synpreds + not used in a DFA for a real rule. This is used to optimize things + for backtrack option. + +* Code gen must add _fragment or whatever to end of pred name in + template synpredRule to avoid having ANTLR know anything about + method names. + +* Added -IdbgST option to emit ST delimiters at start/stop of all + templates spit out. + +June 28, 2006 + +* Tweaked message when ANTLR cannot handle analysis. + +3.0b1 - June 27, 2006 + +June 24, 2006 + +* syn preds no longer generate little static classes; they also don't + generate a whole bunch of extra crap in the rules built to test syn + preds. Removed GrammarFragmentPointer class from runtime. + +June 23-24, 2006 + +* added output option to -report output. + +* added profiling info: + Number of rule invocations in "guessing" mode + number of rule memoization cache hits + number of rule memoization cache misses + +* made DFA DOT diagrams go left to right not top to bottom + +* I try to recursive overflow states now by resolving these states + with semantic/syntactic predicates if they exist. The DFA is then + deterministic rather than simply resolving by choosing first + nondeterministic alt. I used to generated errors: + +~/tmp $ java org.antlr.Tool -dfa t.g +ANTLR Parser Generator Early Access Version 3.0b2 (July 5, 2006) 1989-2006 +t.g:2:5: Alternative 1: after matching input such as A A A A A decision cannot predict what comes next due to recursion overflow to b from b +t.g:2:5: Alternative 2: after matching input such as A A A A A decision cannot predict what comes next due to recursion overflow to b from b + + Now, I uses predicates if available and emits no warnings. + +* made sem preds share accept states. Previously, multiple preds in a +decision forked new accepts each time for each nondet state. + +June 19, 2006 + +* Need parens around the prediction expressions in templates. + +* Referencing $ID.text in an action forced bad code gen in lexer rule ID. + +* Fixed a bug in how predicates are collected. The definition of + "last predicated alternative" was incorrect in the analysis. Further, + gated predicates incorrectly missed a case where an edge should become + true (a tautology). + +* Removed an unnecessary input.consume() reference in the runtime/DFA class. + +June 14, 2006 + +* -> ($rulelabel)? didn't generate proper code for ASTs. + +* bug in code gen (did not compile) +a : ID -> ID + | ID -> ID + ; +Problem is repeated ref to ID from left side. Juergen pointed this out. + +* use of tokenVocab with missing file yielded exception + +* (A|B)=> foo yielded an exception as (A|B) is a set not a block. Fixed. + +* Didn't set ID1= and INT1= for this alt: + | ^(ID INT+ {System.out.print(\"^(\"+$ID+\" \"+$INT+\")\");}) + +* Fixed so repeated dangling state errors only occur once like: +t.g:4:17: the decision cannot distinguish between alternative(s) 2,1 for at least one input sequence + +* tracking of rule elements was on (making list defs at start of + method) with templates instead of just with ASTs. Turned off. + +* Doesn't crash when you give it a missing file now. + +* -report: add output info: how many LL(1) decisions. + +June 13, 2006 + +* ^(ROOT ID?) Didn't work; nor did any other nullable child list such as + ^(ROOT ID* INT?). Now, I check to see if child list is nullable using + Grammar.LOOK() and, if so, I generate an "IF lookahead is DOWN" gate + around the child list so the whole thing is optional. + +* Fixed a bug in LOOK that made it not look through nullable rules. + +* Using AST suffixes or -> rewrite syntax now gives an error w/o a grammar + output option. Used to crash ;) + +* References to EOF ended up with improper -1 refs instead of EOF in output. + +* didn't warn of ambig ref to $expr in rewrite; fixed. +list + : '[' expr 'for' type ID 'in' expr ']' + -> comprehension(expr={$expr.st},type={},list={},i={}) + ; + +June 12, 2006 + +* EOF works in the parser as a token name. + +* Rule b:(A B?)*; didn't display properly in AW due to the way ANTLR + generated NFA. + +* "scope x;" in a rule for unknown x gives no error. Fixed. Added unit test. + +* Label type for refs to start/stop in tree parser and other parsers were + not used. Lots of casting. Ick. Fixed. + +* couldn't refer to $tokenlabel in isolation; but need so we can test if + something was matched. Fixed. + +* Lots of little bugs fixed in $x.y, %... translation due to new + action translator. + +* Improperly tracking block nesting level; result was that you couldn't + see $ID in action of rule "a : A+ | ID {Token t = $ID;} | C ;" + +* a : ID ID {$ID.text;} ; did not get a warning about ambiguous $ID ref. + +* No error was found on $COMMENT.text: + +COMMENT + : '/*' (options {greedy=false;} : . )* '*/' + {System.out.println("found method "+$COMMENT.text);} + ; + + $enclosinglexerrule scope does not exist. Use text or setText() here. + +June 11, 2006 + +* Single return values are initialized now to default or to your spec. + +* cleaned up input stream stuff. Added ANTLRReaderStream, ANTLRInputStream + and refactored. You can specify encodings now on ANTLRFileStream (and + ANTLRInputStream) now. + +* You can set text local var now in a lexer rule and token gets that text. + start/stop indexes are still set for the token. + +* Changed lexer slightly. Calling a nonfragment rule from a + nonfragment rule does not set the overall token. + +June 10, 2006 + +* Fixed bug where unnecessary escapes yield char==0 like '\{'. + +* Fixed analysis bug. This grammar didn't report a recursion warning: +x : y X + | y Y + ; +y : L y R + | B + ; + The DFAState.equals() method was messed up. + +* Added @synpredgate {...} action so you can tell ANTLR how to gate actions + in/out during syntactic predicate evaluation. + +* Fuzzy parsing should be more efficient. It should backtrack over a rule + and then rewind and do it again "with feeling" to exec actions. It was + actually doing it 3x not 2x. + +June 9, 2006 + +* Gutted and rebuilt the action translator for $x.y, $x::y, ... + Uses ANTLR v3 now for the first time inside v3 source. :) + ActionTranslator.java + +* Fixed a bug where referencing a return value on a rule didn't work + because later a ref to that rule's predefined properties didn't + properly force a return value struct to be built. Added unit test. + +June 6, 2006 + +* New DFA mechanisms. Cyclic DFA are implemented as state tables, + encoded via strings as java cannot handle large static arrays :( + States with edges emanating that have predicates are specially + treated. A method is generated to do these states. The DFA + simulation routine uses the "special" array to figure out if the + state is special. See March 25, 2006 entry for description: + http://www.antlr.org/blog/antlr3/codegen.tml. analysis.DFA now has + all the state tables generated for code gen. CyclicCodeGenerator.java + disappeared as it's unneeded code. :) + +* Internal general clean up of the DFA.states vs uniqueStates thing. + Fixed lookahead decisions no longer fill uniqueStates. Waste of + time. Also noted that when adding sem pred edges, I didn't check + for state reuse. Fixed. + +June 4, 2006 + +* When resolving ambig DFA states predicates, I did not add the new states + to the list of unique DFA states. No observable effect on output except + that DFA state numbers were not always contiguous for predicated decisions. + I needed this fix for new DFA tables. + +3.0ea10 - June 2, 2006 + +June 2, 2006 + +* Improved grammar stats and added syntactic pred tracking. + +June 1, 2006 + +* Due to a type mismatch, the DebugParser.recoverFromMismatchedToken() + method was not called. Debug events for mismatched token error + notification were not sent to ANTLRWorks probably + +* Added getBacktrackingLevel() for any recognizer; needed for profiler. + +* Only writes profiling data for antlr grammar analysis with -profile set + +* Major update and bug fix to (runtime) Profiler. + +May 27, 2006 + +* Added Lexer.skip() to force lexer to ignore current token and look for + another; no token is created for current rule and is not passed on to + parser (or other consumer of the lexer). + +* Parsers are much faster now. I removed use of java.util.Stack for pushing + follow sets and use a hardcoded array stack instead. Dropped from + 5900ms to 3900ms for parse+lex time parsing entire java 1.4.2 source. Lex + time alone was about 1500ms. Just looking at parse time, we get about 2x + speed improvement. :) + +May 26, 2006 + +* Fixed NFA construction so it generates NFA for (A*)* such that ANTLRWorks + can display it properly. + +May 25, 2006 + +* added abort method to Grammar so AW can terminate the conversion if it's + taking too long. + +May 24, 2006 + +* added method to get left recursive rules from grammar without doing full + grammar analysis. + +* analysis, code gen not attempted if serious error (like + left-recursion or missing rule definition) occurred while reading + the grammar in and defining symbols. + +* added amazing optimization; reduces analysis time by 90% for java + grammar; simple IF statement addition! + +3.0ea9 - May 20, 2006 + +* added global k value for grammar to limit lookahead for all decisions unless +overridden in a particular decision. + +* added failsafe so that any decision taking longer than 2 seconds to create +the DFA will fall back on k=1. Use -ImaxtimeforDFA n (in ms) to set the time. + +* added an option (turned off for now) to use multiple threads to +perform grammar analysis. Not much help on a 2-CPU computer as +garbage collection seems to peg the 2nd CPU already. :( Gotta wait for +a 4 CPU box ;) + +* switched from #src to // $ANTLR src directive. + +* CommonTokenStream.getTokens() looked past end of buffer sometimes. fixed. + +* unicode literals didn't really work in DOT output and generated code. fixed. + +* fixed the unit test rig so it compiles nicely with Java 1.5 + +* Added ant build.xml file (reads build.properties file) + +* predicates sometimes failed to compile/eval properly due to missing (...) + in IF expressions. Forced (..) + +* (...)? with only one alt were not optimized. Was: + + // t.g:4:7: ( B )? + int alt1=2; + int LA1_0 = input.LA(1); + if ( LA1_0==B ) { + alt1=1; + } + else if ( LA1_0==-1 ) { + alt1=2; + } + else { + NoViableAltException nvae = + new NoViableAltException("4:7: ( B )?", 1, 0, input); + throw nvae; + } + +is now: + + // t.g:4:7: ( B )? + int alt1=2; + int LA1_0 = input.LA(1); + if ( LA1_0==B ) { + alt1=1; + } + + Smaller, faster and more readable. + +* Allow manual init of return values now: + functionHeader returns [int x=3*4, char (*f)()=null] : ... ; + +* Added optimization for DFAs that fixed a codegen bug with rules in lexer: + EQ : '=' ; + ASSIGNOP : '=' | '+=' ; + EQ is a subset of other rule. It did not given an error which is + correct, but generated bad code. + +* ANTLR was sending column not char position to ANTLRWorks. + +* Bug fix: location 0, 0 emitted for synpreds and empty alts. + +* debugging event handshake how sends grammar file name. Added getGrammarFileName() to recognizers. Java.stg generates it: + + public String getGrammarFileName() { return "<fileName>"; } + +* tree parsers can do arbitrary lookahead now including backtracking. I + updated CommonTreeNodeStream. + +* added events for debugging tree parsers: + + /** Input for a tree parser is an AST, but we know nothing for sure + * about a node except its type and text (obtained from the adaptor). + * This is the analog of the consumeToken method. Again, the ID is + * the hashCode usually of the node so it only works if hashCode is + * not implemented. + */ + public void consumeNode(int ID, String text, int type); + + /** The tree parser looked ahead */ + public void LT(int i, int ID, String text, int type); + + /** The tree parser has popped back up from the child list to the + * root node. + */ + public void goUp(); + + /** The tree parser has descended to the first child of a the current + * root node. + */ + public void goDown(); + +* Added DebugTreeNodeStream and DebugTreeParser classes + +* Added ctor because the debug tree node stream will need to ask quesitons about nodes and since nodes are just Object, it needs an adaptor to decode the nodes and get text/type info for the debugger. + +public CommonTreeNodeStream(TreeAdaptor adaptor, Tree tree); + +* added getter to TreeNodeStream: + public TreeAdaptor getTreeAdaptor(); + +* Implemented getText/getType in CommonTreeAdaptor. + +* Added TraceDebugEventListener that can dump all events to stdout. + +* I broke down and make Tree implement getText + +* tree rewrites now gen location debug events. + +* added AST debug events to listener; added blank listener for convenience + +* updated debug events to send begin/end backtrack events for debugging + +* with a : (b->b) ('+' b -> ^(PLUS $a b))* ; you get b[0] each time as + there is no loop in rewrite rule itself. Need to know context that + the -> is inside the rule and hence b means last value of b not all + values. + +* Bug in TokenRewriteStream; ops at indexes < start index blocked proper op. + +* Actions in ST rewrites "-> ({$op})()" were not translated + +* Added new action name: + +@rulecatch { +catch (RecognitionException re) { + reportError(re); + recover(input,re); +} +catch (Throwable t) { + System.err.println(t); +} +} +Overrides rule catch stuff. + +* Isolated $ refs caused exception + +3.0ea8 - March 11, 2006 + +* added @finally {...} action like @init for rules. Executes in + finally block (java target) after all other stuff like rule memoization. + No code changes needs; ST just refs a new action: + <ruleDescriptor.actions.finally> + +* hideous bug fixed: PLUS='+' didn't result in '+' rule in lexer + +* TokenRewriteStream didn't do toString() right when no rewrites had been done. + +* lexer errors in interpreter were not printed properly + +* bitsets are dumped in hex not decimal now for FOLLOW sets + +* /* epsilon */ is not printed now when printing out grammars with empty alts + +* Fixed another bug in tree rewrite stuff where it was checking that elements + had at least one element. Strange...commented out for now to see if I can remember what's up. + +* Tree rewrites had problems when you didn't have x+=FOO variables. Rules + like this work now: + + a : (x=ID)? y=ID -> ($x $y)?; + +* filter=true for lexers turns on k=1 and backtracking for every token + alternative. Put the rules in priority order. + +* added getLine() etc... to Tree to support better error reporting for + trees. Added MismatchedTreeNodeException. + +* $templates::foo() is gone. added % as special template symbol. + %foo(a={},b={},...) ctor (even shorter than $templates::foo(...)) + %({name-expr})(a={},...) indirect template ctor reference + + The above are parsed by antlr.g and translated by codegen.g + The following are parsed manually here: + + %{string-expr} anonymous template from string expr + %{expr}.y = z; template attribute y of StringTemplate-typed expr to z + %x.y = z; set template attribute y of x (always set never get attr) + to z [languages like python without ';' must still use the + ';' which the code generator is free to remove during code gen] + +* -> ({expr})(a={},...) notation for indirect template rewrite. + expr is the name of the template. + +* $x[i]::y and $x[-i]::y notation for accesssing absolute scope stack + indexes and relative negative scopes. $x[-1]::y is the y attribute + of the previous scope (stack top - 1). + +* filter=true mode for lexers; can do this now...upon mismatch, just + consumes a char and tries again: +lexer grammar FuzzyJava; +options {filter=true;} + +FIELD + : TYPE WS? name=ID WS? (';'|'=') + {System.out.println("found var "+$name.text);} + ; + +* refactored char streams so ANTLRFileStream is now a subclass of + ANTLRStringStream. + +* char streams for lexer now allowed nested backtracking in lexer. + +* added TokenLabelType for lexer/parser for all token labels + +* line numbers for error messages were not updated properly in antlr.g + for strings, char literals and <<...>> + +* init action in lexer rules was before the type,start,line,... decls. + +* Tree grammars can now specify output; I've only tested output=templat + though. + +* You can reference EOF now in the parser and lexer. It's just token type + or char value -1. + +* Bug fix: $ID refs in the *lexer* were all messed up. Cleaned up the + set of properties available... + +* Bug fix: .st not found in rule ref when rule has scope: +field +scope { + StringTemplate funcDef; +} + : ... + {$field::funcDef = $field.st;} + ; +it gets field_stack.st instead + +* return in backtracking must return retval or null if return value. + +* $property within a rule now works like $text, $st, ... + +* AST/Template Rewrites were not gated by backtracking==0 so they + executed even when guessing. Auto AST construction is now gated also. + +* CommonTokenStream was somehow returning tokens not text in toString() + +* added useful methods to runtime.BitSet and also to CommonToken so you can + update the text. Added nice Token stream method: + + /** Given a start and stop index, return a List of all tokens in + * the token type BitSet. Return null if no tokens were found. This + * method looks at both on and off channel tokens. + */ + public List getTokens(int start, int stop, BitSet types); + +* literals are now passed in the .tokens files so you can ref them in + tree parses, for example. + +* added basic exception handling; no labels, just general catches: + +a : {;}A | B ; + exception + catch[RecognitionException re] { + System.out.println("recog error"); + } + catch[Exception e] { + System.out.println("error"); + } + +* Added method to TokenStream: + public String toString(Token start, Token stop); + +* antlr generates #src lines in lexer grammars generated from combined grammars + so error messages refer to original file. + +* lexers generated from combined grammars now use originally formatting. + +* predicates have $x.y stuff translated now. Warning: predicates might be + hoisted out of context. + +* return values in return val structs are now public. + +* output=template with return values on rules was broken. I assume return values with ASTs was broken too. Fixed. + +3.0ea7 - December 14, 2005 + +* Added -print option to print out grammar w/o actions + +* Renamed BaseParser to be BaseRecognizer and even made Lexer derive from + this; nice as it now shares backtracking support code. + +* Added syntactic predicates (...)=>. See December 4, 2005 entry: + + http://www.antlr.org/blog/antlr3/lookahead.tml + + Note that we have a new option for turning off rule memoization during + backtracking: + + -nomemo when backtracking don't generate memoization code + +* Predicates are now tested in order that you specify the alts. If you + leave the last alt "naked" (w/o pred), it will assume a true pred rather + than union of other preds. + +* Added gated predicates "{p}?=>" that literally turn off a production whereas +disambiguating predicates are only hoisted into the predictor when syntax alone +is not sufficient to uniquely predict alternatives. + +A : {p}? => "a" ; +B : {!p}? => ("a"|"b")+ ; + +* bug fixed related to predicates in predictor +lexer grammar w; +A : {p}? "a" ; +B : {!p}? ("a"|"b")+ ; +DFA is correct. A state splits for input "a" on the pred. +Generated code though was hosed. No pred tests in prediction code! +I added testLexerPreds() and others in TestSemanticPredicateEvaluation.java + +* added execAction template in case we want to do something in front of + each action execution or something. + +* left-recursive cycles from rules w/o decisions were not detected. + +* undefined lexer rules were not announced! fixed. + +* unreachable messages for Tokens rule now indicate rule name not alt. E.g., + + Ruby.lexer.g:24:1: The following token definitions are unreachable: IVAR + +* nondeterminism warnings improved for Tokens rule: + +Ruby.lexer.g:10:1: Multiple token rules can match input such as ""0".."9"": INT, FLOAT +As a result, tokens(s) FLOAT were disabled for that input + + +* DOT diagrams didn't show escaped char properly. + +* Char/string literals are now all 'abc' not "abc". + +* action syntax changed "@scope::actionname {action}" where scope defaults + to "parser" if parser grammar or combined grammar, "lexer" if lexer grammar, + and "treeparser" if tree grammar. The code generation targets decide + what scopes are available. Each "scope" yields a hashtable for use in + the output templates. The scopes full of actions are sent to all output + file templates (currently headerFile and outputFile) as attribute actions. + Then you can reference <actions.scope> to get the map of actions associated + with scope and <actions.parser.header> to get the parser's header action + for example. This should be very flexible. The target should only have + to define which scopes are valid, but the action names should be variable + so we don't have to recompile ANTLR to add actions to code gen templates. + + grammar T; + options {language=Java;} + @header { package foo; } + @parser::stuff { int i; } // names within scope not checked; target dependent + @members { int i; } + @lexer::header {head} + @lexer::members { int j; } + @headerfile::blort {...} // error: this target doesn't have headerfile + @treeparser::members {...} // error: this is not a tree parser + a + @init {int i;} + : ID + ; + ID : 'a'..'z'; + + For now, the Java target uses members and header as a valid name. Within a + rule, the init action name is valid. + +* changed $dynamicscope.value to $dynamicscope::value even if value is defined + in same rule such as $function::name where rule function defines name. + +* $dynamicscope gets you the stack + +* rule scopes go like this now: + + rule + scope {...} + scope slist,Symbols; + : ... + ; + +* Created RuleReturnScope as a generic rule return value. Makes it easier + to do this: + RuleReturnScope r = parser.program(); + System.out.println(r.getTemplate().toString()); + +* $template, $tree, $start, etc... + +* $r.x in current rule. $r is ignored as fully-qualified name. $r.start works too + +* added warning about $r referring to both return value of rule and dynamic scope of rule + +* integrated StringTemplate in a very simple manner + +Syntax: +-> template(arglist) "..." +-> template(arglist) <<...>> +-> namedTemplate(arglist) +-> {free expression} +-> // empty + +Predicate syntax: +a : A B -> {p1}? foo(a={$A.text}) + -> {p2}? foo(a={$B.text}) + -> // return nothing + +An arg list is just a list of template attribute assignments to actions in curlies. + +There is a setTemplateLib() method for you to use with named template rewrites. + +Use a new option: + +grammar t; +options {output=template;} +... + +This all should work for tree grammars too, but I'm still testing. + +* fixed bugs where strings were improperly escaped in exceptions, comments, etc.. For example, newlines came out as newlines not the escaped version + +3.0ea6 - November 13, 2005 + +* turned off -debug/-profile, which was on by default + +* completely refactored the output templates; added some missing templates. + +* dramatically improved infinite recursion error messages (actually + left-recursion never even was printed out before). + +* wasn't printing dangling state messages when it reanalyzes with k=1. + +* fixed a nasty bug in the analysis engine dealing with infinite recursion. + Spent all day thinking about it and cleaned up the code dramatically. + Bug fixed and software is more powerful and I understand it better! :) + +* improved verbose DFA nodes; organized by alt + +* got much better random phrase generation. For example: + + $ java org.antlr.tool.RandomPhrase simple.g program + int Ktcdn ';' method wh '(' ')' '{' return 5 ';' '}' + +* empty rules like "a : ;" generated code that didn't compile due to + try/catch for RecognitionException. Generated code couldn't possibly + throw that exception. + +* when printing out a grammar, such as in comments in generated code, + ANTLR didn't print ast suffix stuff back out for literals. + +* This never exited loop: + DATA : (options {greedy=false;}: .* '\n' )* '\n' '.' ; + and now it works due to new default nongreedy .* Also this works: + DATA : (options {greedy=false;}: .* '\n' )* '.' ; + +* Dot star ".*" syntax didn't work; in lexer it is nongreedy by + default. In parser it is on greedy but also k=1 by default. Added + unit tests. Added blog entry to describe. + +* ~T where T is the only token yielded an empty set but no error + +* Used to generate unreachable message here: + + parser grammar t; + a : ID a + | ID + ; + + z.g:3:11: The following alternatives are unreachable: 2 + + In fact it should really be an error; now it generates: + + no start rule in grammar t (no rule can obviously be followed by EOF) + + Per next change item, ANTLR cannot know that EOF follows rule 'a'. + +* added error message indicating that ANTLR can't figure out what your + start rule is. Required to properly generate code in some cases. + +* validating semantic predicates now work (if they are false, they + throw a new FailedPredicateException + +* two hideous bug fixes in the IntervalSet, which made analysis go wrong + in a few cases. Thanks to Oliver Zeigermann for finding lots of bugs + and making suggested fixes (including the next two items)! + +* cyclic DFAs are now nonstatic and hence can access instance variables + +* labels are now allowed on lexical elements (in the lexer) + +* added some internal debugging options + +* ~'a'* and ~('a')* were not working properly; refactored antlr.g grammar + +3.0ea5 - July 5, 2005 + +* Using '\n' in a parser grammar resulted in a nonescaped version of '\n' in the token names table making compilation fail. I fixed this by reorganizing/cleaning up portion of ANTLR that deals with literals. See comment org.antlr.codegen.Target. + +* Target.getMaxCharValue() did not use the appropriate max value constant. + +* ALLCHAR was a constant when it should use the Target max value def. set complement for wildcard also didn't use the Target def. Generally cleaned up the max char value stuff. + +* Code gen didn't deal with ASTLabelType properly...I think even the 3.0ea7 example tree parser was broken! :( + +* Added a few more unit tests dealing with escaped literals + +3.0ea4 - June 29, 2005 + +* tree parsers work; added CommonTreeNodeStream. See simplecTreeParser + example in examples-v3 tarball. + +* added superClass and ASTLabelType options + +* refactored Parser to have a BaseParser and added TreeParser + +* bug fix: actions being dumped in description strings; compile errors + resulted + +3.0ea3 - June 23, 2005 + +Enhancements + +* Automatic tree construction operators are in: ! ^ ^^ + +* Tree construction rewrite rules are in + -> {pred1}? rewrite1 + -> {pred2}? rewrite2 + ... + -> rewriteN + + The rewrite rules may be elements like ID, expr, $label, {node expr} + and trees ^( <root> <children> ). You have have (...)?, (...)*, (...)+ + subrules as well. + + You may have rewrites in subrules not just at outer level of rule, but + any -> rewrite forces auto AST construction off for that alternative + of that rule. + + To avoid cycles, copy semantics are used: + + r : INT -> INT INT ; + + means make two new nodes from the same INT token. + + Repeated references to a rule element implies a copy for at least one + tree: + + a : atom -> ^(atom atom) ; // NOT CYCLE! (dup atom tree) + +* $ruleLabel.tree refers to tree created by matching the labeled element. + +* A description of the blocks/alts is generated as a comment in output code + +* A timestamp / signature is put at top of each generated code file + +3.0ea2 - June 12, 2005 + +Bug fixes + +* Some error messages were missing the stackTrace parameter + +* Removed the file locking mechanism as it's not cross platform + +* Some absolute vs relative path name problems with writing output + files. Rules are now more concrete. -o option takes precedence + // -o /tmp /var/lib/t.g => /tmp/T.java + // -o subdir/output /usr/lib/t.g => subdir/output/T.java + // -o . /usr/lib/t.g => ./T.java + // -o /tmp subdir/t.g => /tmp/subdir/t.g + // If they didn't specify a -o dir so just write to location + // where grammar is, absolute or relative + +* does error checking on unknown option names now + +* Using just language code not locale name for error message file. I.e., + the default (and for any English speaking locale) is en.stg not en_US.stg + anymore. + +* The error manager now asks the Tool to panic rather than simply doing + a System.exit(). + +* Lots of refactoring concerning grammar, rule, subrule options. Now + detects invalid options. + +3.0ea1 - June 1, 2005 + +Initial early access release + diff --git a/antlr-3.1.3/runtime/C/configure.ac b/antlr-3.1.3/runtime/C/configure.ac new file mode 100644 index 0000000..71b6149 --- /dev/null +++ b/antlr-3.1.3/runtime/C/configure.ac @@ -0,0 +1,215 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_INIT(libantlr3c, 3.1.3, jimi@idle.ws) +AC_PREREQ(2.60) +AC_COPYRIGHT([ + (The "BSD licence") + Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC + http://www.temporal-wave.com + http://www.linkedin.com/in/jimidle + 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. +]) +AC_CONFIG_SRCDIR(src/antlr3parser.c) + + +AC_ARG_ENABLE([debuginfo], + [AS_HELP_STRING([--enable-debuginfo], [Compiles debug info into the library (default --disable-debuginfo)])], + [], [enable_debuginfo=no]) + +AC_ARG_ENABLE([64bit], + [AS_HELP_STRING([--enable-64bit], [Turns on flags that produce 64 bit object code if any are required (default --disable-64bit)])], + [], [enable_64bit=no]) + +AC_ARG_ENABLE([abiflags], + [AS_HELP_STRING([--disable-abiflags], [Does not add ABI flags -m32 or -m64 for older versions of gcc, such as itanium 3.x (default --enable-abiflags=yes)])], + [], [enable_abiflags=yes]) + +AC_ARG_ENABLE([antlrdebug], + [AS_HELP_STRING([--disable-antlrdebug], [Turns off default flags that include the antlr debugger in the runtime. Specify to remove debugger and the socket dependancies (default --enable-antlrdebug)])], + [], [enable_antlrdebug=yes]) + +AM_INIT_AUTOMAKE(foreign) +AC_LANG(C) +AC_PROG_CC([xlc aCC gcc cc]) +AM_MAINTAINER_MODE +AM_PROG_LIBTOOL + +AC_CANONICAL_BUILD +AC_CANONICAL_HOST + +OBJECT_MODE= +# Checks for programs. +AC_MSG_CHECKING([compiler flags required for compiling ANTLR with $CC C compiler on host $host]) +WARNFLAGS= +case $CC in +xlc*) + CPPCMNTFLAGS="-qcpluscmt" + if test x"$enable_64bit" = xyes; then + CCFLAGS64="-q64 -Wl,-b64" + OBJECT_MODE="64" + else + OBJECT_MODE="32" + fi + OPTIMFLAGS="-O2" + if test x"$enable_debuginfo" = xyes; then + DEBUGFLAGS="-g" + fi + ;; + +aCC*) + CPPCMNTFLAGS= + if test x"$enable_64bit" = xyes; then + CCFLAGS64="+DD64" + fi + OPTIMFLAGS="-O" + if test $DEBUGINF = 1; then + DEBUGFLAGS="-g" + fi + ;; + +gcc*) + CPPCMNTFLAGS= + if test x"$enable_64bit" = xyes; then + GCCABIFLAGS="-m64" + else + GCCABIFLAGS="-m32" + fi + if test x"$enable_abiflags" = xyes; then + CCFLAGS64=$GCCABIFLAGS + fi + OPTIMFLAGS="-O2" + if test x"$enable_debuginfo" = xyes; then + DEBUGFLAGS="-g" + fi + WARNFLAGS=-Wall + ;; + +*) + +case $host in +sparc*-*solaris*) + CPPCMNTFLAGS= + if test x"$enable_64bit" = xyes; then + CCFLAGS64="-fast -xtarget=ultra4 -m64 -xarch=sparcvis" + fi + OPTIMFLAGS="-O" + if test x"$enable_debuginfo" = xyes; then + DEBUGFLAGS='-g' + fi + ;; + +*) + CPPCMNTFLAGS= + CCFLAGS64= + OPTIMFLAGS="-O" + if test x"$enable_debuginfo" = xyes; then + DEBUGFLAGS='-g' + fi + ;; +esac + + ;; +esac + +CFLAGS="$CCFLAGS64 $CPPCMNTFLAGS $OPTIMFLAGS $DEBUGFLAGS $WARNFLAGS" +AC_MSG_RESULT([$CFLAGS]) +AC_SUBST([OBJECT_MODE]) + +AS_IF([test "x$enable_antlrdebug" = xno], [AC_DEFINE([ANTLR3_NODEBUGGER], [1], [Define if ANTLR debugger not required])], []) +AS_IF([test x"$enable_64bit" = xyes], [AC_DEFINE([ANTLR3_USE_64BIT], [1], [Define if 64 bit mode required])], []) + +AC_PROG_INSTALL +AC_PROG_LN_S +AC_PROG_MAKE_SET + +# Checks for libraries. + +# Checks for header files. +AC_INCLUDES_DEFAULT() +AC_HEADER_RESOLV +AC_CHECK_HEADERS([sys/malloc.h malloc.h], [], [], +[[#ifdef HAVE_SYS_MALLOC_H +#include <sys/malloc.h> +#endif +#ifdef HAVE_MALLOC_H +#include <malloc.h> +#endif +]]) +AC_CHECK_HEADERS([stdarg.h], [], [], +[[#ifdef HAVE_STDARG_H +#include <stdarg.h> +#endif +]]) + +AC_CHECK_HEADERS([sys/stat.h], [], [], +[[#ifdef HAVE_SYS_STAT_H +#include <sys/stat.h> +#endif +]]) + +AC_CHECK_HEADERS([ctype.h], [], [], +[[#ifdef HAVE_CTYPE_H +#include <ctype.h> +#endif +]]) + +AC_CHECK_HEADERS([netinet/tcp.h], [], [], +[[#ifdef HAVE_NETINET_TCP_H +#include <netinet/tcp.h> +#endif +]]) + +AC_CHECK_HEADERS([sys/socket.h socket.h], [], [], +[[#ifdef HAVE_SYS_SOCKET_H +#include <sys/socket.h> +#endif +#ifdef HAVE_SOCKET_H +#include <socket.h> +#endif +]]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_C_CONST +AC_TYPE_SIZE_T +AC_TYPE_INT8_T +AC_TYPE_INT16_T +AC_TYPE_INT32_T +AC_TYPE_INT64_T +AC_TYPE_INTPTR_T +AC_TYPE_UINT8_T +AC_TYPE_UINT16_T +AC_TYPE_UINT32_T +AC_TYPE_UINT64_T +AC_TYPE_UINTPTR_T +AC_C_INLINE + + +# Checks for library functions. +AC_CHECK_FUNCS([memmove memset strdup accept]) + +AC_CONFIG_HEADERS(antlr3config.h) +AC_CONFIG_FILES([Makefile]) +AC_OUTPUT diff --git a/antlr-3.1.3/runtime/C/dist/libantlr3c-3.0.1.tar.gz b/antlr-3.1.3/runtime/C/dist/libantlr3c-3.0.1.tar.gz new file mode 100644 index 0000000..15461ba Binary files /dev/null and b/antlr-3.1.3/runtime/C/dist/libantlr3c-3.0.1.tar.gz differ diff --git a/antlr-3.1.3/runtime/C/dist/libantlr3c-3.1.1.tar.gz b/antlr-3.1.3/runtime/C/dist/libantlr3c-3.1.1.tar.gz new file mode 100644 index 0000000..56554ab Binary files /dev/null and b/antlr-3.1.3/runtime/C/dist/libantlr3c-3.1.1.tar.gz differ diff --git a/antlr-3.1.3/runtime/C/dist/libantlr3c-3.1.2.tar.gz b/antlr-3.1.3/runtime/C/dist/libantlr3c-3.1.2.tar.gz new file mode 100644 index 0000000..aaa6c97 Binary files /dev/null and b/antlr-3.1.3/runtime/C/dist/libantlr3c-3.1.2.tar.gz differ diff --git a/antlr-3.1.3/runtime/C/dist/libantlr3c-3.1.3.tar.gz b/antlr-3.1.3/runtime/C/dist/libantlr3c-3.1.3.tar.gz new file mode 100644 index 0000000..113ef24 Binary files /dev/null and b/antlr-3.1.3/runtime/C/dist/libantlr3c-3.1.3.tar.gz differ diff --git a/antlr-3.1.3/runtime/C/dist/libantlr3c-3.1.tar.gz b/antlr-3.1.3/runtime/C/dist/libantlr3c-3.1.tar.gz new file mode 100644 index 0000000..8cbc0ea Binary files /dev/null and b/antlr-3.1.3/runtime/C/dist/libantlr3c-3.1.tar.gz differ diff --git a/antlr-3.1.3/runtime/C/doxyfile b/antlr-3.1.3/runtime/C/doxyfile new file mode 100644 index 0000000..16dd677 --- /dev/null +++ b/antlr-3.1.3/runtime/C/doxyfile @@ -0,0 +1,265 @@ +# Doxyfile 1.5.2 + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- +DOXYFILE_ENCODING = UTF-8 +PROJECT_NAME = ANTLR3C +PROJECT_NUMBER = 3.1.2 +OUTPUT_DIRECTORY = api +CREATE_SUBDIRS = NO +OUTPUT_LANGUAGE = English +BRIEF_MEMBER_DESC = YES +REPEAT_BRIEF = YES +ABBREVIATE_BRIEF = "The $name class" \ + "The $name widget" \ + "The $name file" \ + is \ + provides \ + specifies \ + contains \ + represents \ + a \ + an \ + the +ALWAYS_DETAILED_SEC = NO +INLINE_INHERITED_MEMB = NO +FULL_PATH_NAMES = YES +STRIP_FROM_PATH = +STRIP_FROM_INC_PATH = +SHORT_NAMES = NO +JAVADOC_AUTOBRIEF = YES +MULTILINE_CPP_IS_BRIEF = NO +DETAILS_AT_TOP = YES +INHERIT_DOCS = YES +SEPARATE_MEMBER_PAGES = NO +TAB_SIZE = 4 +ALIASES = +OPTIMIZE_OUTPUT_FOR_C = YES +OPTIMIZE_OUTPUT_JAVA = NO +BUILTIN_STL_SUPPORT = NO +CPP_CLI_SUPPORT = NO +DISTRIBUTE_GROUP_DOC = NO +SUBGROUPING = YES +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- +EXTRACT_ALL = YES +EXTRACT_PRIVATE = YES +EXTRACT_STATIC = YES +EXTRACT_LOCAL_CLASSES = YES +EXTRACT_LOCAL_METHODS = NO +HIDE_UNDOC_MEMBERS = NO +HIDE_UNDOC_CLASSES = NO +HIDE_FRIEND_COMPOUNDS = NO +HIDE_IN_BODY_DOCS = NO +INTERNAL_DOCS = NO +CASE_SENSE_NAMES = NO +HIDE_SCOPE_NAMES = NO +SHOW_INCLUDE_FILES = YES +INLINE_INFO = YES +SORT_MEMBER_DOCS = YES +SORT_BRIEF_DOCS = YES +SORT_BY_SCOPE_NAME = NO +GENERATE_TODOLIST = YES +GENERATE_TESTLIST = YES +GENERATE_BUGLIST = YES +GENERATE_DEPRECATEDLIST= YES +ENABLED_SECTIONS = +MAX_INITIALIZER_LINES = 30 +SHOW_USED_FILES = YES +SHOW_DIRECTORIES = YES +FILE_VERSION_FILTER = +#--------------------------------------------------------------------------- +# configuration options related to warning and progress messages +#--------------------------------------------------------------------------- +QUIET = NO +WARNINGS = YES +WARN_IF_UNDOCUMENTED = YES +WARN_IF_DOC_ERROR = YES +WARN_NO_PARAMDOC = YES +WARN_FORMAT = "$file:$line: $text" +WARN_LOGFILE = docwarn.txt +#--------------------------------------------------------------------------- +# configuration options related to the input files +#--------------------------------------------------------------------------- +INPUT = src \ + include \ + doxygen +INPUT_ENCODING = UTF-8 +FILE_PATTERNS = *.c \ + *.cc \ + *.cxx \ + *.cpp \ + *.c++ \ + *.d \ + *.java \ + *.ii \ + *.ixx \ + *.ipp \ + *.i++ \ + *.inl \ + *.h \ + *.hh \ + *.hxx \ + *.hpp \ + *.h++ \ + *.idl \ + *.odl \ + *.cs \ + *.php \ + *.php3 \ + *.inc \ + *.m \ + *.mm \ + *.dox \ + *.py +RECURSIVE = YES +EXCLUDE = +EXCLUDE_SYMLINKS = NO +EXCLUDE_PATTERNS = +EXCLUDE_SYMBOLS = +EXAMPLE_PATH = +EXAMPLE_PATTERNS = * +EXAMPLE_RECURSIVE = NO +IMAGE_PATH = +INPUT_FILTER = +FILTER_PATTERNS = +FILTER_SOURCE_FILES = NO +#--------------------------------------------------------------------------- +# configuration options related to source browsing +#--------------------------------------------------------------------------- +SOURCE_BROWSER = NO +INLINE_SOURCES = NO +STRIP_CODE_COMMENTS = YES +REFERENCED_BY_RELATION = YES +REFERENCES_RELATION = YES +REFERENCES_LINK_SOURCE = YES +USE_HTAGS = NO +VERBATIM_HEADERS = NO +#--------------------------------------------------------------------------- +# configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- +ALPHABETICAL_INDEX = YES +COLS_IN_ALPHA_INDEX = 5 +IGNORE_PREFIX = +#--------------------------------------------------------------------------- +# configuration options related to the HTML output +#--------------------------------------------------------------------------- +GENERATE_HTML = YES +HTML_OUTPUT = . +HTML_FILE_EXTENSION = .html +HTML_HEADER = +HTML_FOOTER = +HTML_STYLESHEET = +HTML_ALIGN_MEMBERS = YES +GENERATE_HTMLHELP = NO +CHM_FILE = +HHC_LOCATION = +GENERATE_CHI = NO +BINARY_TOC = NO +TOC_EXPAND = NO +DISABLE_INDEX = NO +ENUM_VALUES_PER_LINE = 4 +GENERATE_TREEVIEW = YES +TREEVIEW_WIDTH = 300 +#--------------------------------------------------------------------------- +# configuration options related to the LaTeX output +#--------------------------------------------------------------------------- +GENERATE_LATEX = NO +LATEX_OUTPUT = latex +LATEX_CMD_NAME = latex +MAKEINDEX_CMD_NAME = makeindex +COMPACT_LATEX = NO +PAPER_TYPE = a4wide +EXTRA_PACKAGES = +LATEX_HEADER = +PDF_HYPERLINKS = NO +USE_PDFLATEX = NO +LATEX_BATCHMODE = NO +LATEX_HIDE_INDICES = NO +#--------------------------------------------------------------------------- +# configuration options related to the RTF output +#--------------------------------------------------------------------------- +GENERATE_RTF = NO +RTF_OUTPUT = rtf +COMPACT_RTF = NO +RTF_HYPERLINKS = NO +RTF_STYLESHEET_FILE = +RTF_EXTENSIONS_FILE = +#--------------------------------------------------------------------------- +# configuration options related to the man page output +#--------------------------------------------------------------------------- +GENERATE_MAN = YES +MAN_OUTPUT = man +MAN_EXTENSION = .3 +MAN_LINKS = YES +#--------------------------------------------------------------------------- +# configuration options related to the XML output +#--------------------------------------------------------------------------- +GENERATE_XML = NO +XML_OUTPUT = xml +XML_SCHEMA = +XML_DTD = +XML_PROGRAMLISTING = YES +#--------------------------------------------------------------------------- +# configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- +GENERATE_AUTOGEN_DEF = NO +#--------------------------------------------------------------------------- +# configuration options related to the Perl module output +#--------------------------------------------------------------------------- +GENERATE_PERLMOD = NO +PERLMOD_LATEX = NO +PERLMOD_PRETTY = YES +PERLMOD_MAKEVAR_PREFIX = +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- +ENABLE_PREPROCESSING = YES +MACRO_EXPANSION = NO +EXPAND_ONLY_PREDEF = NO +SEARCH_INCLUDES = YES +INCLUDE_PATH = +INCLUDE_FILE_PATTERNS = +PREDEFINED = +EXPAND_AS_DEFINED = +SKIP_FUNCTION_MACROS = YES +#--------------------------------------------------------------------------- +# Configuration::additions related to external references +#--------------------------------------------------------------------------- +TAGFILES = +GENERATE_TAGFILE = +ALLEXTERNALS = NO +EXTERNAL_GROUPS = YES +PERL_PATH = /usr/bin/perl +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- +CLASS_DIAGRAMS = YES +MSCGEN_PATH = +HIDE_UNDOC_RELATIONS = YES +HAVE_DOT = YES +CLASS_GRAPH = YES +COLLABORATION_GRAPH = YES +GROUP_GRAPHS = YES +UML_LOOK = NO +TEMPLATE_RELATIONS = NO +INCLUDE_GRAPH = YES +INCLUDED_BY_GRAPH = YES +CALL_GRAPH = YES +CALLER_GRAPH = YES +GRAPHICAL_HIERARCHY = YES +DIRECTORY_GRAPH = YES +DOT_IMAGE_FORMAT = png +DOT_PATH = +DOTFILE_DIRS = +DOT_GRAPH_MAX_NODES = 50 +DOT_TRANSPARENT = NO +DOT_MULTI_TARGETS = YES +GENERATE_LEGEND = YES +DOT_CLEANUP = YES +#--------------------------------------------------------------------------- +# Configuration::additions related to the search engine +#--------------------------------------------------------------------------- +SEARCHENGINE = NO diff --git a/antlr-3.1.3/runtime/C/doxygen/atsections.dox b/antlr-3.1.3/runtime/C/doxygen/atsections.dox new file mode 100644 index 0000000..bd4ea12 --- /dev/null +++ b/antlr-3.1.3/runtime/C/doxygen/atsections.dox @@ -0,0 +1,143 @@ +/// \page atsections Using Sections Within Grammar Files +/// +/// \section intro Introduction +/// +/// A C targeted grammar can make use of special annotations within a grammar +/// file, which are prefixed with the <b>\@</b> character. These sections cause the +/// the placement of their contents within the generated code at defined points +/// such as within the generated C header file. +/// +/// The general form of these annotations is: +/// +/// \code +/// section +/// : '@' (( 'parser' | 'lexer' ) '::')? SECTIONNAME '{' yourcode '}' +/// ; +/// \endcode +/// +/// If the 'parser' or lexer keywords are left out of the specification, then the +/// ANTLR tool assumes a lexer target for a lexer grammar, a parser target for a parser +/// or tree parser grammar, and a parser target for a combined lexer/parser grammar. You +/// are advised as a matter of course to include the parser or lexer target keyword. +/// +/// Documentation regarding the \@sections available for a grammar targeted at C now +/// follows. +/// +/// \subsection psrinit Sections \@init and \@declarations +/// +/// Java targeted grammars allow the special section <code>\@init</code> to be placed after the declaration +/// of a rule (lexer, parser and tree parser rules). This allows you to both declare and initialize +/// variables that are local to the code generated for that rule. You can then reference them within +/// your rule action code. +/// +/// With the C target, the generated code is subject to the restrictions of C semantics and this +/// means that you must declare any local variables, then assign to them afterwards. As well as the +/// <code>\@init</code> section, which C programmers should use to initialize their local variables, the C +/// target provides the <code>\@declarations</code> section, which is also a rule based section. This section +/// is where the C programmer should declare the local variables, thus separating their declaration +/// from their initialization. Here is an example: +/// +/// \code +/// translation_unit +/// @declarations +/// { +/// pANTLR3_BOOLEAN hasUsing; +/// } +/// @init +/// { +/// +/// // Assume no Using directives +/// // +/// hasUsing = ANTLR3_FALSE; +/// +/// } +/// : rulea ruleb ... +/// +/// \endcode +/// +/// Using the <code>\@declarations</code> and <code>\@init</code> sections guarantees that your generated code will +/// compile correctly on any standard C compiler (assuming, of course, that you type in valid C code.) +/// +/// \subsection psrheader \@header section. +/// +/// The <code>\@parser::header</code> or <code>\@lexer::header</code> annotations cause the code they encapsulate +/// to be placed at the start of each generated file, regardless of whether it is a .c or .h file. This can +/// be useful for inserting copyright information and so on in all your generated files. +/// +/// \bNOTE: Be careful not to confuse this concept with placing code in the generated .h header file. The name choice is +/// unfortunate, but was already used in the Java target to allow the placement of \c imports statements +/// in generated java classes. We have therefore kept the intent of this section the same. +/// +/// Here is an example: +//// +/// \code +/// @lexer::header +/// { +/// // Copyright (c) Jim Idle 2007 - All your grammar are belong to us. +/// } +/// +/// @parser::header +/// { +/// // Copyright (c) Jim Idle 2007 - All your grammar are belong to us. +/// } +/// \endcode +/// +/// +/// \subsection hdrinclude \@includes section +/// +/// The <code>\@parser::includes</code> or <code>\@lexer::includes</code> annotations cause +/// the code they encapsulate to be placed in the generated .h file, \b after the standard +/// includes required by the ANTLR generated code. +/// +/// Here you could for instance place a <code>\#include</code> +/// statement to cause your grammar code to include some standard definitions. Because you +/// may use multiple parsers and lexers in your solution, you should probably not place +/// <code>#define</code> statements here, but in the <code>\@postinclude</code> section. Then you +/// may create different <code>\#defines</code> for different recognizers. +/// +/// Here is an example: +//// +/// \code +/// @lexer::includes +/// { +/// #include "myprojectcommondefs.h" +/// } +/// +/// @parser::includes +/// { +/// #include "myprojectcommondefs.h" +/// } +/// \endcode +/// +/// +/// \subsection hdrpreinclude \@preincludes section +/// +/// The <code>\@parser::preincludes</code> or <code>\@lexer::preincludes</code> annotations cause +/// the code they encapsulate to be placed in the generated .h file, \b before the standard +/// includes required by the ANTLR generated code. +/// +/// You should use this section when you wish to place #defines and other definitions +/// in the code before the standard ANTLR runtime includes defined them. This allows you +/// to override any predefined symbols and options that the includes otherwise take +/// defaults for. For instance, if you have built a version of the runtime with a +/// special version of malloc, you can <code>\#define</code> #ANTLR3_MALLOC to match the definition +/// you used for the ANTLR runtime library. +/// +/// \subsection hdrpostinclude \@postinclude section +/// +/// The <code>\@parser::postinclude</code> or <code>\@lexer::postinclude</code> annotations cause +/// the code they encapsulate to be placed in the generated <b>.C</b> file, after the generated include +/// file (which includes the standard ANTLR3C library includes. +/// +/// Code you place here then will be subject to any macros defined by your own includes, by the +/// generated include and by the standard ANTLR3 includes. This is a good place to <code>\#undef</code> +/// anything that you don;t like the default values of, but cannot override before the includes +/// define them. +/// +/// This is also a good place to <code>#define</code> any macros you may wish to use in the generated +/// .c file. As you can include multiple parsers in your projects, you will need to include the +/// generated .h file of each of them, possibly globally, but almost certainly in a context where you +/// are including more than one .h file simultaneously. Hence if you commonly use the same macro +/// names for accessing structures and so on, and they change from grammar to grammar, you should +/// define them here to avoid creating conflicting definitions in the header files. +/// \ No newline at end of file diff --git a/antlr-3.1.3/runtime/C/doxygen/build.dox b/antlr-3.1.3/runtime/C/doxygen/build.dox new file mode 100644 index 0000000..05c3c66 --- /dev/null +++ b/antlr-3.1.3/runtime/C/doxygen/build.dox @@ -0,0 +1,207 @@ +/// \page build Building From Source +/// +/// The C runtime is provided in source code form only as there are too many binary +/// versions to sensibly maintain binaries on www.antlr.org. +/// +/// The runtime code is provided with .sln and .vcproj files for Visual Studio 2005 and 2008, +/// and \b configure files for building and installation on UNIX or other systems that support this tool. If your +/// system is neither Windows nor \b configure compatible, then you should find it +/// reasonable to build the code manually (see section "Building Manually".) +/// +/// \section src Source Code Organization +/// +/// The source code expands from a tar/zip file to give you the following +/// directories: +/// +/// - <b>./</b> The location of the configure script and the antlr3config.h file +/// generated by the running the configure script.This directory also +/// contains the solution and project files for visual studio 2005 and +/// 2008. +/// - <b>./src</b> The location of all the C files in the project. +/// - <b>./include</b> The location of all the header files for the project +/// - <b>./doxygen</b> The location of documentation files such as the one that generates this page +/// - Other ancillary directories used by the build or documentation process. +/// +/// \section winbuild Building for Windows +/// +/// If you are building for Cygwin, or a similar UNIX on Windows System, follow the "Building With Configure" instructions below. +/// +/// Note that the runtime is no longer compatible with the VC6 Microsoft compiler. If you absolutely need to build with +/// this compiler, you can probably hack the source code to deall with the pieces that VC6 cannot handle such as the +/// ULL suffix for constants. +/// +/// If you wish to build the binaries for Windows using Visual Studio 2005, or 2008 you may build using the IDE: +/// -# Open the C.sln file +/// -# Select batch Build from the Build menu +/// -# Select all configurations and press the build button. +/// +/// If you wish or need to build the libraries from the command line, then you must +/// use a Windows command shell configured for access to VS2005/VS2008 compilers, such as the one that is +/// started from: +/// +/// <i>Start->Microsoft Visual Studio 2005->Visual Studio Tools->Visual Studio 2005 Command Prompt</i> +/// +/// There appears to be no way to build all targets at once in a batch mode from the command line, +/// so you may build one or all of the following: +/// \verbatim + C:\antlrsrc\code\antlr\main\runtime\C> DEVENV C.sln /Build ReleaseDLL + C:\antlrsrc\code\antlr\main\runtime\C> DEVENV C.sln /Build Release + C:\antlrsrc\code\antlr\main\runtime\C> DEVENV C.sln /Build DebugDLL + C:\antlrsrc\code\antlr\main\runtime\C> DEVENV C.sln /Build Debug +\endverbatim +/// +/// After the build is complete you will find the \c.\cDLL and \c.\cLIB files under the directory containing C.sln, +/// in a subdirectory named after the /Build target. In the Release and Debug targets, you will find that there is only a \c.\cLIB archive file, +/// which you can link directly into your own projects if you wish to avoid the DLL. In \c ReleaseDLL and \c DebugDLL you will find both a +/// \c .LIB file which you should link your projects with and a DLL. The library and names on Windows are as follows: +/// +/// \verbatim + - ReleaseDLL : ANTLR3C.DLL and ANTLR3C_DLL.LIB + - DebugDLL : ANTLR3CD.DLL and ANTLR3CD_DLL.LIB + - Release : ANTLR3C.LIB + - Debug : ANTLR3CD.LIB +\endverbatim +/// +/// There currently no .msi modules or other installs built for Windows, so you must place the DLLs in a directory referenced +/// by the PATH environment variable and make the include directory available to your project configurations. +/// +/// +/// \section configure Building with configure +/// +/// Before starting, make sure that you are using a source code distribution and not the source code directly from the +/// Perforce repository. If you use the source from the perforce tree directly, you will find that there is no configure +/// script as this is generated as part of the distribution build by the maintainers. If you feel the need to build from +/// the distribution tree then you must have all the autobuild packages available on your system and can generate the +/// configure script using autoreconf. If you are not familiar with these tools, then please use the tgz files in the +/// dist subdirectory (or downloaded from the ANTLR web site). +/// +/// The source code file should be expanded in a directory of your choice (probably your working directory) using the command: +/// +/// \verbatim +gzip -dc antlrtgzname.tar.gz | tar xvf - +\endverbatim +/// +/// Where: <b>antlrtgzname.tar.gz</b> is of course the name of the tar when you downloaded it. You should find a \b configure script in the sub directory thus created. +/// +/// The configure script accepts the usual options, such as --prefix= but the default is to build in the source directory and to place libraries in +/// <b>/usr/local/lib</b> and include files (for building your recognizers) in <b>/usr/local/include</b>. There are also a number of antlr specific options, which you may wish to utilize. The command: +/// \verbatim +./configure --help +\endverbatim +/// +/// Will document the latest incarnations of these options in case this documentation is ever out of date. At this time the options are: +/// +/// \verbatim + --enable-debuginfo Compiles debug info into the library (default no) + --enable-64bit Turns on flags that produce 64 bit object code if + any are required (default no) +\endverbatim +/// +/// Unless you need 64 bit builds, or a change in library types, you will generally use the configure command without options: +/// +/// Here is a sample configure output: +/// +/// \verbatim +[jimi@localhost dist]$ tar zvxf libantlr3c-3.0.0-rc8.tar.gz + +libantlr3c-3.0.0-rc8/ +libantlr3c-3.0.0-rc8/antlr3config.h +libantlr3c-3.0.0-rc8/src/ +libantlr3c-3.0.0-rc8/src/antlr3stringstream.c +... +libantlr3c-3.0.0-rc8/antlr3config.h.in +\endverbatim +/// \verbatim +[jimi@localhost dist]$ cd libantlr3c-3.0.0-rc +\endverbatim +/// \verbatim +[jimi@localhost libantlr3c-3.0.0-rc8]$ ./configure + +checking for a BSD-compatible install... /usr/bin/install -c +checking whether build environment is sane... yes +checking for a thread-safe mkdir -p... /bin/mkdir -p +checking for gawk... gawk +checking whether make sets $(MAKE)... yes +checking for xlc... no +checking for aCC... no +checking for gcc... gcc +... +checking for strdup... yes +configure: creating ./config.status +config.status: creating Makefile +config.status: creating antlr3config.h +config.status: antlr3config.h is unchanged +config.status: executing depfiles commands +\endverbatim +/// +/// Having configured the library successfully, you need only make it, and install it: +/// +/// \verbatim +[jimi@localhost libantlr3c-3.0.0-rc8]$ make +\endverbatim +/// \verbatim +make all-am +make[1]: Entering directory `/home/jimi/antlrsrc/code/antlr/main/runtime/C/dist/libantlr3c-3.0.0-rc8' +/bin/sh ./libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -Iinclude -Iinclude -O2 -MT antlr3baserecognizer.lo -MD -MP -MF .deps/antlr3baserecognizer.Tpo -c -o antlr3baserecognizer.lo `test -f 'src/antlr3baserecognizer.c' || echo './'`src/antlr3baserecognizer.c +... +gcc -shared .libs/antlr3baserecognizer.o .libs/antlr3basetree.o .libs/antlr3basetreeadaptor.o .libs/antlr3bitset.o .libs/antlr3collections.o .libs/antlr3commontoken.o .libs/antlr3commontree.o .libs/antlr3commontreeadaptor.o .libs/antlr3commontreenodestream.o .libs/antlr3cyclicdfa.o .libs/antlr3encodings.o .libs/antlr3exception.o .libs/antlr3filestream.o .libs/antlr3inputstream.o .libs/antlr3intstream.o .libs/antlr3lexer.o .libs/antlr3parser.o .libs/antlr3string.o .libs/antlr3stringstream.o .libs/antlr3tokenstream.o .libs/antlr3treeparser.o .libs/antlr3rewritestreams.o .libs/antlr3ucs2inputstream.o -Wl,-soname -Wl,libantlr3c.so -o .libs/libantlr3c.so +ar cru .libs/libantlr3c.a antlr3baserecognizer.o antlr3basetree.o antlr3basetreeadaptor.o antlr3bitset.o antlr3collections.o antlr3commontoken.o antlr3commontree.o antlr3commontreeadaptor.o antlr3commontreenodestream.o antlr3cyclicdfa.o antlr3encodings.o antlr3exception.o antlr3filestream.o antlr3inputstream.o antlr3intstream.o antlr3lexer.o antlr3parser.o antlr3string.o antlr3stringstream.o antlr3tokenstream.o antlr3treeparser.o antlr3rewritestreams.o antlr3ucs2inputstream.o +ranlib .libs/libantlr3c.a +creating libantlr3c.la + +(cd .libs && rm -f libantlr3c.la && ln -s ../libantlr3c.la libantlr3c.la) +make[1]: Leaving directory `/home/jimi/antlrsrc/code/antlr/main/runtime/C/dist/libantlr3c-3.0.0-rc8' +\endverbatim +/// \verbatim +[jimi@localhost libantlr3c-3.0.0-rc8]$ sudo make install +\endverbatim +/// \verbatim +make[1]: Entering directory `/home/jimi/antlrsrc/code/antlr/main/runtime/C/dist/libantlr3c-3.0.0-rc8' +test -z "/usr/local/lib" || /bin/mkdir -p "/usr/local/lib" + /bin/sh ./libtool --mode=install /usr/bin/install -c 'libantlr3c.la' '/usr/local/lib/libantlr3c.la' +/usr/bin/install -c .libs/libantlr3c.so /usr/local/lib/libantlr3c.so +/usr/bin/install -c .libs/libantlr3c.lai /usr/local/lib/libantlr3c.la +/usr/bin/install -c .libs/libantlr3c.a /usr/local/lib/libantlr3c.a +... + /usr/bin/install -c -m 644 'include/antlr3stringstream.h' '/usr/local/include/antlr3stringstream.h' +... + /usr/bin/install -c -m 644 'antlr3config.h' '/usr/local/include/antlr3config.h' +make[1]: Leaving directory `/home/jimi/antlrsrc/code/antlr/main/runtime/C/dist/libantlr3c-3.0.0-rc8' + +[jimi@localhost libantlr3c-3.0.0-rc8]$ +\endverbatim +/// +/// You are now ready to generate C recognizers and compile and link them with the ANTLR 3 C Runtime. +/// +/// +/// \section buildman Building Manually +/// +/// The only step that configure performs that cannot be done +/// manually (without effort) is to produce the header file +/// \c antlr3config.h, which contains typedefs of the fundamental types +/// that your local C compiler supports. The easiest way to produce +/// this file for your system, if you cannot port \b automake and \b configure +/// to the system is: +/// +/// -# Run configure on a system that does support configure +/// -# Copy the generated \c antlr3config.h file to the target system +/// -# Edit the file locally and change any types that differ on this +/// system to the target systems. There are only a few types and you should +/// find this relatively easy. +/// +/// Having produced a compatible antlr3config.h file, then you should be able to +/// compile the source files in the \c ./src subdirectory, providing an include path +/// to the location of \c antlr3config.h and the \c ./include subdirectory. Something akin +/// to: +/// \verbatim + +~/C/src: cc -c -O -I.. -I../include *.c + +\endverbatim +/// +/// Having produced the .o (or equivalent) files for the local system you can then +/// build an archive or shared library for the C runtime. +/// +/// When you wish to build and link with the C runtime, specify the path to the +/// supplied header files, and the path to the library that you built. +/// \ No newline at end of file diff --git a/antlr-3.1.3/runtime/C/doxygen/buildrec.dox b/antlr-3.1.3/runtime/C/doxygen/buildrec.dox new file mode 100644 index 0000000..1c19c36 --- /dev/null +++ b/antlr-3.1.3/runtime/C/doxygen/buildrec.dox @@ -0,0 +1,269 @@ +/// \page buildrec How to build Generated C Code +/// +/// \section generated Generated Files +/// +/// The antlr tool jar, run against a grammar file that targets the C language, will generate the following files +/// according to whether your grammar file contains a lexer, parser, combined or treeparser specification. +/// Your grammar file name and the subject of the grammar line in your file are expected to match. Here the generic name G is used: +/// +/// <table> +/// <tr> +/// <th> Suffix </th> +/// <th> Generated files </th> +/// </tr> +/// <tr> +/// <td> lexer grammar (G.g3l) </td> +/// <td> GLexer.c GLexer.h</td> +/// </tr> +/// <tr> +/// <td> parser grammar (G.g3p) </td> +/// <td> GParser.c GParser.h </td> +/// </tr> +/// <tr> +/// <td> grammar G (G.g3pl) </td> +/// <td> GParser.c GParser.h GLexer.c GLexer.h</td> +/// </tr> +/// <tr> +/// <td> tree grammar G; (G.g3t) </td> +/// <td> G.c G.h </td> +/// </tr> +/// </table> +/// +/// The generated .c files reference the .h files using <G.h>, so you must use <code>-I.</code> on your compiler command line +/// (or include the current directory in your include paths in Visual Studio). Additionally, the generated .h files reference +/// <code>antlr3.h</code>, so you must use <code>-I/path/to/antlr/include</code> (E.g. <code>-I /usr/local/include</code>) to reference the standard ANTLR include files. +/// +/// In order to reference the library file at compile time (you can/should only reference one) you need to use the +/// <code>-L/path/to/antlr/lib</code> (E.g. <code>-L /usr/local/lib</code>) on Unix, or add the path to your "Additional Library Path" in +/// Visual Studio. You also need to specify the library using <code>-L</code> on Unix (E.g. <code>-L /usr/local/lib -l antlr3c</code>) or add <code>antlr3c_dll.lib</code> +/// to your Additional Library Dependencies in Visual Studio. +/// +/// In case it isn't obvious, the generated files may be used to produce either a library or an executable (.EXE on Windows) file. +/// +/// If you use the shared version of the libraries, DLL or .so/.so/.a then you must ship the library with your +/// application must run in an environment whereby the library can be found by the runtime linker/loader. +/// This usually involves specifying the directory in which the library lives to an environment variable. +/// On Windows, X:{yourwininstalldir}\system32 will be searched automatically. +/// +/// \section invoke Invoking Your Generated Recognizer +/// +/// In order to run your lexer/parser/tree parser combination, you will need a small function (or main) +/// function that controls the sequence of events, from reading the input file or string, through to +/// invoking the tree parser(s) and retrieving the results. See "Using the ANTLR3C C Target" for more +/// detailed instructions, but if you just want to get going as fast as possible, study the following +/// code example. +/// +/// \code +/// +/// // You may adopt your own practices by all means, but in general it is best +/// // to create a single include for your project, that will include the ANTLR3 C +/// // runtime header files, the generated header files (all of which are safe to include +/// // multiple times) and your own project related header files. Use <> to include and +/// // -I on the compile line (which vs2005 now handles, where vs2003 did not). +/// // +/// #include <treeparser.h> +/// +/// // Main entry point for this example +/// // +/// int ANTLR3_CDECL +/// main (int argc, char *argv[]) +/// { +/// // Now we declare the ANTLR related local variables we need. +/// // Note that unless you are convinced you will never need thread safe +/// // versions for your project, then you should always create such things +/// // as instance variables for each invocation. +/// // ------------------- +/// +/// // Name of the input file. Note that we always use the abstract type pANTLR3_UINT8 +/// // for ASCII/8 bit strings - the runtime library guarantees that this will be +/// // good on all platforms. This is a general rule - always use the ANTLR3 supplied +/// // typedefs for pointers/types/etc. +/// // +/// pANTLR3_UINT8 fName; +/// +/// // The ANTLR3 character input stream, which abstracts the input source such that +/// // it is easy to privide inpput from different sources such as files, or +/// // memory strings. +/// // +/// // For an ASCII/latin-1 memory string use: +/// // input = antlr3NewAsciiStringInPlaceStream (stringtouse, (ANTLR3_UINT32) length, NULL); +/// // +/// // For a UCS2 (16 bit) memory string use: +/// // input = antlr3NewUCS2StringInPlaceStream (stringtouse, (ANTLR3_UINT32) length, NULL); +/// // +/// // For input from a file, see code below +/// // +/// // Note that this is essentially a pointer to a structure containing pointers to functions. +/// // You can create your own input stream type (copy one of the existing ones) and override any +/// // individual function by installing your own pointer after you have created the standard +/// // version. +/// // +/// pANTLR3_INPUT_STREAM input; +/// +/// // The lexer is of course generated by ANTLR, and so the lexer type is not upper case. +/// // The lexer is supplied with a pANTLR3_INPUT_STREAM from whence it consumes its +/// // input and generates a token stream as output. This is the ctx (CTX macro) pointer +/// // for your lexer. +/// // +/// pLangLexer lxr; +/// +/// // The token stream is produced by the ANTLR3 generated lexer. Again it is a structure based +/// // API/Object, which you can customise and override methods of as you wish. a Token stream is +/// // supplied to the generated parser, and you can write your own token stream and pass this in +/// // if you wish. +/// // +/// pANTLR3_COMMON_TOKEN_STREAM tstream; +/// +/// // The Lang parser is also generated by ANTLR and accepts a token stream as explained +/// // above. The token stream can be any source in fact, so long as it implements the +/// // ANTLR3_TOKEN_SOURCE interface. In this case the parser does not return anything +/// // but it can of course specify any kind of return type from the rule you invoke +/// // when calling it. This is the ctx (CTX macro) pointer for your parser. +/// // +/// pLangParser psr; +/// +/// // The parser produces an AST, which is returned as a member of the return type of +/// // the starting rule (any rule can start first of course). This is a generated type +/// // based upon the rule we start with. +/// // +/// LangParser_decl_return langAST; +/// +/// +/// // The tree nodes are managed by a tree adaptor, which doles +/// // out the nodes upon request. You can make your own tree types and adaptors +/// // and override the built in versions. See runtime source for details and +/// // eventually the wiki entry for the C target. +/// // +/// pANTLR3_COMMON_TREE_NODE_STREAM nodes; +/// +/// // Finally, when the parser runs, it will produce an AST that can be traversed by the +/// // the tree parser: c.f. LangDumpDecl.g3t This is the ctx (CTX macro) pointer for your +/// // tree parser. +/// // +/// pLangDumpDecl treePsr; +/// +/// // Create the input stream based upon the argument supplied to us on the command line +/// // for this example, the input will always default to ./input if there is no explicit +/// // argument. +/// // +/// if (argc < 2 || argv[1] == NULL) +/// { +/// fName =(pANTLR3_UINT8)"./input"; // Note in VS2005 debug, working directory must be configured +/// } +/// else +/// { +/// fName = (pANTLR3_UINT8)argv[1]; +/// } +/// +/// // Create the input stream using the supplied file name +/// // (Use antlr3AsciiFileStreamNew for UCS2/16bit input). +/// // +/// input = antlr3AsciiFileStreamNew(fName); +/// +/// // The input will be created successfully, providing that there is enough +/// // memory and the file exists etc +/// // +/// if ( input == NULL ) +/// { +/// ANTLR3_FPRINTF(stderr, "Unable to open file %s due to malloc() failure1\n", (char *)fName); +/// } +/// +/// // Our input stream is now open and all set to go, so we can create a new instance of our +/// // lexer and set the lexer input to our input stream: +/// // (file | memory | ?) --> inputstream -> lexer --> tokenstream --> parser ( --> treeparser )? +/// // +/// lxr = LangLexerNew(input); // CLexerNew is generated by ANTLR +/// +/// // Need to check for errors +/// // +/// if ( lxr == NULL ) +/// { +/// ANTLR3_FPRINTF(stderr, "Unable to create the lexer due to malloc() failure1\n"); +/// exit(ANTLR3_ERR_NOMEM); +/// } +/// +/// // Our lexer is in place, so we can create the token stream from it +/// // NB: Nothing happens yet other than the file has been read. We are just +/// // connecting all these things together and they will be invoked when we +/// // call the parser rule. ANTLR3_SIZE_HINT can be left at the default usually +/// // unless you have a very large token stream/input. Each generated lexer +/// // provides a token source interface, which is the second argument to the +/// // token stream creator. +/// // Note tha even if you implement your own token structure, it will always +/// // contain a standard common token within it and this is the pointer that +/// // you pass around to everything else. A common token as a pointer within +/// // it that should point to your own outer token structure. +/// // +/// tstream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, lxr->pLexer->tokSource); +/// +/// if (tstream == NULL) +/// { +/// ANTLR3_FPRINTF(stderr, "Out of memory trying to allocate token stream\n"); +/// exit(ANTLR3_ERR_NOMEM); +/// } +/// +/// // Finally, now that we have our lexer constructed, we can create the parser +/// // +/// psr = LangParserNew(tstream); // CParserNew is generated by ANTLR3 +/// +/// if (psr == NULL) +/// { +/// ANTLR3_FPRINTF(stderr, "Out of memory trying to allocate parser\n"); +/// exit(ANTLR3_ERR_NOMEM); +/// } +/// +/// // We are all ready to go. Though that looked complicated at first glance, +/// // I am sure, you will see that in fact most of the code above is dealing +/// // with errors and there isn;t really that much to do (isn;t this always the +/// // case in C? ;-). +/// // +/// // So, we now invoke the parser. All elements of ANTLR3 generated C components +/// // as well as the ANTLR C runtime library itself are pseudo objects. This means +/// // that they are represented as pointers to structures, which contain any +/// // instance data they need, and a set of pointers to other interfaces or +/// // 'methods'. Note that in general, these few pointers we have created here are +/// // the only things you will ever explicitly free() as everything else is created +/// // via factories, that allocate memory efficiently and free() everything they use +/// // automatically when you close the parser/lexer/etc. +/// // +/// // Note that this means only that the methods are always called via the object +/// // pointer and the first argument to any method, is a pointer to the structure itself. +/// // It also has the side advantage, if you are using an IDE such as VS2005 that can do it +/// // that when you type ->, you will see a list of all the methods the object supports. +/// // +/// langAST = psr->decl(psr); +/// +/// // If the parser ran correctly, we will have a tree to parse. In general I recommend +/// // keeping your own flags as part of the error trapping, but here is how you can +/// // work out if there were errors if you are using the generic error messages +/// // +/// if (psr->pParser->rec->errorCount > 0) +/// { +/// ANTLR3_FPRINTF(stderr, "The parser returned %d errors, tree walking aborted.\n", psr->pParser->rec->errorCount); +/// +/// } +/// else +/// { +/// nodes = antlr3CommonTreeNodeStreamNewTree(langAST.tree, ANTLR3_SIZE_HINT); // sIZE HINT WILL SOON BE DEPRECATED!! +/// +/// // Tree parsers are given a common tree node stream (or your override) +/// // +/// treePsr = LangDumpDeclNew(nodes); +/// +/// treePsr->decl(treePsr); +/// nodes ->free (nodes); nodes = NULL; +/// treePsr ->free (treePsr); treePsr = NULL; +/// } +/// +/// // We did not return anything from this parser rule, so we can finish. It only remains +/// // to close down our open objects, in the reverse order we created them +/// // +/// psr ->free (psr); psr = NULL; +/// tstream ->free (tstream); tstream = NULL; +/// lxr ->free (lxr); lxr = NULL; +/// input ->close (input); input = NULL; +/// +/// return 0; +/// } +/// \endcode +/// diff --git a/antlr-3.1.3/runtime/C/doxygen/changes31.dox b/antlr-3.1.3/runtime/C/doxygen/changes31.dox new file mode 100644 index 0000000..d1793db --- /dev/null +++ b/antlr-3.1.3/runtime/C/doxygen/changes31.dox @@ -0,0 +1,56 @@ +/// \page changes31 Changes in 3.1 from 3.0 +/// +/// The following changes have taken place from 3.0 to 3.1. Some of +/// them may require minor changes to your grammar files or the +/// programs that invoke your grammar. Please take the time to read +/// through this list as it may save you time later. +/// +/// \section returns Constructor Return Values +/// +/// In previous releases the return value from both the generated constructors and +/// built in constructor functions would return a value of -1 or -2 if a problem +/// occurred. However, the only problem that can really occur is lack of memory, +/// hence to avoid the remote change that some memory allocation scheme would return +/// an address of -1 for a pointer, the return address is now NULL if there was +/// no memory available. The old macros for this mechanism have been removed which +/// will force you to read this information. You now need only check the return +/// address for NULL, or you could not bother doing that and join with 95% of the world's +/// C code. +/// +/// \section trees Tree Parser Rewrites +/// +/// The 3.1 runtime now supports tree rewrites from tree parsers. See the main ANTLR +/// documentation for more details. This beta version contains \subpage knownissues regarding +/// the release of mmeory allocated to tree nodes when they are rewritten in some combinations +/// of re-writing tree parsers. These issues will be corrected before release. +/// +/// \section debugger ANTLRWorks Debugger +/// +/// The ANTLRWorks debugger is now fully supported by this version of the runtime. It +/// supports remote debugging only (you cannot generate C, compile and debug it from the +/// ANTLRWorks IDE.) However both parser and tree parser debugging is supported providing +/// you are using a version of ANTLRWorks that supports tree parser debugging. Generate +/// the C code with the -debug option of the ANTLR tool, as per any other target. +/// +/// Note that when you invoke your debugging version of the parser, it will appear to hang +/// but is in fact waiting on a local TCP socket connection from the ANTLRWorks debugger. As the +/// target environment is unknown, it is not prudent to generate notification status messages +/// using something like printf, as the target environment may not have a console or implement +/// printf. +/// +/// \section macros Macro Changes +/// +/// Prior to the 3.1 release, accessing the token source of a lexer required knowledge of where +/// the token source pointer was located wihtin the lexer. In 3.1, the token source was burried +/// further in the innards of the C runtime and such knowledge is considerd irreleavant and confusing. +/// Hence, when creating a token stream from a token source, it is now mandatory to use the new +/// C macro TOKENSOURCE(lxr), which will expand to point at the token source interface. This MACRO +/// will be maintained across future versions. You can see how to use it in the downloadable +/// examples, avaiable from the downloads page of the ANTLR web site. Here is the relevant code +/// for creating a token stream, extracted from those examples: +/// +/// \code +/// tstream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lxr)); +/// \endcode +/// + diff --git a/antlr-3.1.3/runtime/C/doxygen/doxygengroups.dox b/antlr-3.1.3/runtime/C/doxygen/doxygengroups.dox new file mode 100644 index 0000000..de259f3 --- /dev/null +++ b/antlr-3.1.3/runtime/C/doxygen/doxygengroups.dox @@ -0,0 +1,243 @@ +// Definitions of documentation groups so we can organize the API and +// usage documentation nicely. + +/// \defgroup apiclasses API Classes +/// +/// The API classes are divided into the typdefs (and their underlying structs) +/// that are the containers for each 'object' within the ANTLR3C runtime, and +/// their implementations (the functions that are installed by default in to +/// these structures when you create them.) +/// +/// The typedefs contain data and function pointers, which together define +/// the object. The implementation functions are the default implementations +/// of the 'methds' encapsulated by the typdef structures.You may override +/// any of the methods once their objects are created by installing a pointer to +/// your own function. Some of these methods create other 'objects' (instances of +/// typedef structures), which allows you install your own method and create your +/// own implementation of these. +/// + + /// \defgroup apistructures API Typedefs and Structs + /// \ingroup apiclasses + /// + /// These structures (and the typedefs that you use to reference them + /// and their pointers) are the C equivalent of objects. They correspond + /// (roughly) to the Java runtime system classes and contain all the + /// data elements for a particular interface as well as all the pointers + /// to functions that implement these interfaces. + /// + /// There are constructor functions exported from the C runtime, which you + /// use to create a default implementation of one of these 'classes'. You can + /// then override any part of the implementation by installing your own + /// function pointers, before using the interface 'object' you have created. + /// + /// For instance, you can override the default error message reporting function + /// by replacing the standard (example) implementation of this function with + /// your own. In your grammar, you would place the following + /// + /// \code + /// @parser::apifuncs + /// { + /// // Install custom error message display + /// // + /// RECOGNIZER->displayRecognitionError = produceError; + /// } + /// \endcode + /// + /// The special section @parser::apiFuncs is guaranteed to be generated after + /// the RECONGIZER 'object' has already be created and initialized, so you may + /// install your own implementations of the #ANTLR3_BASE_RECOGNIZER interface + /// functions. The error display function is likely to be the only one you are + /// interested in replacing. + /// + /// Some typedef structures contain either pointers to 'inherited' objects (usual) + /// or embedded structures/typedefs (unusual). In some cases, the pointers passed + /// around by the paresr or tree parser are actually the pointers to these embedded + /// structures (such as #pANTLR3_BASE_TREE), and these embedded 'objects' contain + /// pointers to their encapsulating objects. This is the equivalent of passing + /// interface objects around in object oriented languages. + /// + + /// \defgroup ANTLR3_BASE_RECOGNIZER ANTLR3_BASE_RECOGNIZER - Base Recognizer Class Definition + /// \ingroup apistructures + /// + /// This is the definition of the base recognizer interface, instantiations + /// of which are referred to via #pANTLR3_BASE_RECOGNIZER. + /// + /// In general you will not refer to one of these structures directly as a + /// a #pANTLR3_BASE_RECOGNIZER will be embedded within a higher level + /// object such as #pANTLR3_PARSER + /// + /// \defgroup ANTLR3_RECOGNIZER_SHARED_STATE ANTLR3_RECOGNIZER_SHARED_STATE Recognizer Shared State Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_BITSET ANTLR3_BITSET - Bitset Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_TOKEN_FACTORY ANTLR3_TOKEN_FACTORY - Token Factory Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_COMMON_TOKEN ANTLR3_COMMON_TOKEN - Common Token Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_EXCEPTION ANTLR3_EXCEPTION - Exception Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_HASH_BUCKET ANTLR3_HASH_BUCKET - Hash Table Bucket Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_HASH_ENTRY ANTLR3_HASH_ENTRY - Hash Table Entry Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_HASH_ENUM ANTLR3_HASH_ENUM - Hash Table Enumerator Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_HASH_TABLE ANTLR3_HASH_TABLE - Hash Table Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_LIST ANTLR3_LIST - List Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_VECTOR_FACTORY ANTLR3_VECTOR_FACTORY - Vector Factory Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_VECTOR ANTLR3_VECTOR - Vector Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_STACK ANTLR3_STACK - Stack Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_INPUT_STREAM ANTLR3_INPUT_STREAM - Input Stream Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_LEX_STATE ANTLR3_LEX_STATE - Lexer State Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_STRING_FACTORY ANTLR3_STRING_FACTORY - String Factory Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_STRING ANTLR3_STRING - String Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_TOKEN_SOURCE ANTLR3_TOKEN_SOURCE - Token Source Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_TOKEN_STREAM ANTLR3_TOKEN_STREAM - Token Stream Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_COMMON_TOKEN_STREAM ANTLR3_COMMON_TOKEN_STREAM - Common Token Stream Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_CYCLIC_DFA ANTLR3_CYCLIC_DFA - Cyclic DFA Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_LEXER ANTLR3_LEXER - Lexer Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_PARSER ANTLR3_PARSER - Parser Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_BASE_TREE ANTLR3_BASE_TREE - Base Tree Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_COMMON_TREE ANTLR3_COMMON_TREE - Common Tree Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_ARBORETUM ANTLR3_ARBORETUM - Tree Factory Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_PARSE_TREE ANTLR3_PARSE_TREE - Parse Tree Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_TREE_NODE_STREAM ANTLR3_TREE_NODE_STREAM - Tree Node Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_COMMON_TREE_NODE_STREAM ANTLR3_COMMON_TREE_NODE_STREAM - Common Tree Node Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_TREE_WALK_STATE ANTLR3_TREE_WALK_STATE - Tree Walk State Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_BASE_TREE_ADAPTOR ANTLR3_BASE_TREE_ADAPTOR - Base Tree Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_COMMON_TREE_ADAPTOR ANTLR3_COMMON_TREE_ADAPTOR - Common Tree Adaptor Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_TREE_PARSER ANTLR3_TREE_PARSER - Tree Parser Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_INT_TRIE ANTLR3_INT_TRIE - Trie Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_REWRITE_RULE_ELEMENT_STREAM ANTLR3_REWRITE_RULE_ELEMENT_STREAM - Token Rewrite Stream Class Definition + /// \ingroup apistructures + /// \defgroup ANTLR3_DEBUG_EVENT_LISTENER ANTLR3_DEBUG_EVENT_LISTENER - Debugger Class Definition + /// \ingroup apistructures + + /// \defgroup apiimplementations API Implementation functions + /// \ingroup apiclasses + /// + /// API implementation functions are the default implementation of each of the + /// methods in a particular typedef structure. + /// + /// They are generally grouped together in the same source code file. + /// For instance the default implementations of the + /// methods contained by a #pANTLR3_BASE_RECOGNIZER will be found in the file + /// antlr3baserecognizer.c + /// + /// A source file that provides the default implementations of functions will usually + /// also supply the public (exported from the .DLL or code library) 'constructor' functions + /// that create and initialize the typedef structure that they implement. For instance, + /// in the antlr3baserecognizer.c file, you will find the function antlr3BaseRecognizerNew() + /// + + /// \defgroup pANTLR3_BASE_RECOGNIZER pANTLR3_BASE_RECOGNIZER Base Recognizer Implementation + /// \ingroup apiimplementations + /// + /// The base recognizer interface is implemented by all higher level recognizers + /// such as #pANTLR3_PARSER and provides methods common to all recognizers. + /// + /// \defgroup pANTLR3_RECOGNIZER_SHARED_STATE pANTLR3_RECOGNIZER_SHARED_STATE - Recognizer Shared State Implementation + /// \ingroup apiimplementations + /// + /// The recognizer shared state class does not have an implementation because it contains only + /// data fields, documented at #ANTLR3_RECOGNIZER_SHARED_STATE + /// + /// \defgroup pANTLR3_BITSET pANTLR3_BITSET - Bitset Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_TOKEN_FACTORY pANTLR3_TOKEN_FACTORY - Token Factory Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_COMMON_TOKEN pANTLR3_COMMON_TOKEN - Common Token Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_EXCEPTION pANTLR3_EXCEPTION - Exception Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_HASH_BUCKET pANTLR3_HASH_BUCKET - Hash Table Bucket Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_HASH_ENTRY pANTLR3_HASH_ENTRY - Hash Table Entry Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_HASH_ENUM pANTLR3_HASH_ENUM - Hash Table Enumerator Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_HASH_TABLE pANTLR3_HASH_TABLE - Hash Table Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_LIST pANTLR3_LIST - List Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_VECTOR_FACTORY pANTLR3_VECTOR_FACTORY - Vector Factory Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_VECTOR pANTLR3_VECTOR - Vector Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_STACK pANTLR3_STACK - Stack Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_INPUT_STREAM pANTLR3_INPUT_STREAM - Input Stream Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_LEX_STATE pANTLR3_LEX_STATE - Lexer State Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_STRING_FACTORY pANTLR3_STRING_FACTORY - String Factory Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_STRING pANTLR3_STRING - String Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_TOKEN_SOURCE pANTLR3_TOKEN_SOURCE - Token Source Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_TOKEN_STREAM pANTLR3_TOKEN_STREAM - Token Stream Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_COMMON_TOKEN_STREAM pANTLR3_COMMON_TOKEN_STREAM - Common Token Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_CYCLIC_DFA pANTLR3_CYCLIC_DFA - Cyclic DFA Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_LEXER pANTLR3_LEXER - Lexer Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_PARSER pANTLR3_PARSER - Parser Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_BASE_TREE pANTLR3_BASE_TREE - Base Tree Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_COMMON_TREE pANTLR3_COMMON_TREE - Common Tree Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_ARBORETUM pANTLR3_ARBORETUM - Tree Factory Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_PARSE_TREE pANTLR3_PARSE_TREE - Parse Tree Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_TREE_NODE_STREAM pANTLR3_TREE_NODE_STREAM - Tree Node Stream Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_COMMON_TREE_NODE_STREAM pANTLR3_COMMON_TREE_NODE_STREAM - Common Tree Node Stream Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_TREE_WALK_STATE pANTLR3_TREE_WALK_STATE - Tree Walk State Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_BASE_TREE_ADAPTOR pANTLR3_BASE_TREE_ADAPTOR - Base Tree Adaptor Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_COMMON_TREE_ADAPTOR pANTLR3_COMMON_TREE_ADAPTOR - Common Tree Adaptor Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_TREE_PARSER pANTLR3_TREE_PARSER - Tree ParserImplementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_INT_TRIE pANTLR3_INT_TRIE - Trie Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_REWRITE_RULE_ELEMENT_STREAM pANTLR3_REWRITE_RULE_ELEMENT_STREAM - Token Rewrite Stream Implementation + /// \ingroup apiimplementations + /// \defgroup pANTLR3_DEBUG_EVENT_LISTENER pANTLR3_DEBUG_EVENT_LISTENER - Debugger Implementation + /// \ingroup apiimplementations + \ No newline at end of file diff --git a/antlr-3.1.3/runtime/C/doxygen/generate.dox b/antlr-3.1.3/runtime/C/doxygen/generate.dox new file mode 100644 index 0000000..0173d78 --- /dev/null +++ b/antlr-3.1.3/runtime/C/doxygen/generate.dox @@ -0,0 +1,57 @@ +/// \page generate Generating Code for the C Target +/// +/// \section generate Generating C +/// +/// Before discussing how we compile or call the generated C code, we need to know how to invoke the C code generator. +/// This is achieved within the grammar file itself, using the language option: +/// +/// \verbatim +options { language = C;} +\endverbatim +/// +/// The code generator consists of a single .java file within the standard ANTLR tool jar, and a code generation template, +/// used by the StringTemplate engine, which drives code generation for all language targets. In fact you can make copies of the C.stg +/// and AST.stg templates and make changes to them (though you are encouraged not to, as it is better to provide bug fixes or +/// enhancements which we are happy to receive requests for and will do out best to incorporate. +/// +/// If you are working in the Windows environment, with Visual Studio 2005 or later, you may wish to utilize the custom rulefile +/// provided in the C source code distribution under the <code>./vs2005</code> directory for this purpose. If you are using a pre-built +/// library then you can also download this rule file directly from the FishEye source code browser for ANTLR3. +/// +/// In order to use the rulefile, you must adopt the following suffixes for your grammar files, though they are otherwise optional: +/// +/// <table> +/// +/// <tr> +/// <th> Suffix </th> +/// <th> Grammar should contain... </th> +/// </tr> +/// <tr> +/// <td> .g3l </td> +/// <td> A lexer grammar specification only. </td> +/// </tr> +/// <tr> +/// <td> .g3p </td> +/// <td> A parser grammar specification only. </td> +/// </tr> +/// <tr> +/// <td> .g3pl </td> +/// <td> A combined lexer and parser specification. </td> +/// </tr> +/// <tr> +/// <td> .g3t </td> +/// <td> A tree grammar specification. </td> +/// </tr> +/// +/// </table> +/// +/// You may also wish to use these suffixes if you are building your projects using Makefiles, as this makes the output deterministic. +/// However in this case a much better solution is probably to utilize the -depend option of the Antlr tool, which should tell your +/// Makefile what the grammar files generates, irrespective of its suffix. ANTLR does not care about the actual suffix you use for +/// your grammar file, so building for multiple platforms is relatively easy. +/// +/// <b>NOTE:</b> Your grammar source, regardless of suffix must be named the same as the grammar statement within it. Grammar xyz +/// must be contained within a file called xyz.<i>anything</i> +/// +/// + diff --git a/antlr-3.1.3/runtime/C/doxygen/interop.dox b/antlr-3.1.3/runtime/C/doxygen/interop.dox new file mode 100644 index 0000000..b07983a --- /dev/null +++ b/antlr-3.1.3/runtime/C/doxygen/interop.dox @@ -0,0 +1,327 @@ +/// \page interop Interacting with the Generated Code +/// +/// \section intro Introduction +/// +/// The main way to interact with the generated code is via action code placed within <code>{</code> and +/// <code>}</code> characters in your rules. In general, you are advised to keep the code you embed within +/// these actions, and the grammar itself to an absolute minimum. Rather than embed code directly in your +/// grammar, you should construct an API, that is called from the actions within your grammar. This way +/// you will keep the grammar clean and maintainable and separate the code generators or other code +/// from the definition of the grammar itself. +/// +/// However, when you wish to call your API functions, or insert small pieces of code that do not +/// warrant external functions, you will need to access elements of tokens, return elements from +/// parser rules and perhaps the internals of the recognizer itself. The C runtime provides a number +/// of MACROs that you can use within your action code. It also provides a number of performant +/// structures that you may find useful for building symbol tables, lists, tries, stacks, arrays and so on (all +/// of which are managed so that your memory allocation problems are minimized.) +/// +/// \section rules Parameters and Returns from Parser Rules +/// +/// The C target does not differ from the Java target in any major ways here, and you should consult +/// the standard documentation for the use of parameters on rules and the returns clause. You should +/// be aware though, that the rules generate C function calls and therefore the input and returns +/// clauses are subject to the constraints of C scoping. +/// +/// You should note that if your parser rule returns more than a single entity, then the return +/// type of the generated rule function is a struct, which is returned by value. This is also the case +/// if your rule is part of a tree building grammar (uses the <code>output=AST;</code> option. +/// +/// Other than the notes above, you can use any pre-declared type as an input or output parameter +/// for your rule. +/// +/// \section memory Memory Management +/// +/// You are responsible for allocating and freeing any memory used by your own +/// constructs, ANTLR will track and release any memory allocated internally for tokens, trees, stacks, scopes +/// and so on. This memory is returned to the malloc pool when you call the free method of any +/// ANTLR3 produced structure. +/// +/// For performance reasons, and to avoid thrashing the malloc allocation system, memory for amy elements +/// of your generated parser is allocated in chunks and parcelled out by factories. For instance memory +/// for tokens is created as an array of tokens, and a token factory hands out the next available slot +/// to the lexer. When you free the lexer, the allocated memory is returned to the pool. The same applies +/// to 'strings' that contain the token text and various other text elements accessed within the lexer. +/// +/// The only side effect of this is that after your parse and analysis is complete, if you wish to retain +/// anything generated automatically, you must copy it before freeing the recognizer structures. In practice +/// it is usually practical to retain the recognizer context objects until your processing is complete or +/// to use your own allocation scheme for generating output etc. +/// +/// The advantage of using object factories is of course that memory leaks and accessing de-allocated +/// memory are bugs that rarely occur within the ANTLR3 C runtime. Further, allocating memory for +/// tokens, trees and so on is very fast. +/// +/// \section ctx The CTX Macro +/// +/// The CTX macro is a fundamental parameter that is passed as the first parameter to any generated function +/// concerned with your lexer, parser, or tree parser. The is is the context pointer for your generated +/// recognizer and is how you invoke the generated functions, and access the data embedded within your generated +/// recognizer. While you can use it to directly access stacks, scopes and so on, this is not really recommended +/// as you should use the $xxx references that are available generically within ANTLR grammars. +/// +/// The context pointer is used because this removes the need for any global/static variables at all, either +/// within the generated code, or the C runtime. This is of course fundamental to creating free threading +/// recognizers. Wherever a function call or rule call required the ctx parameter, you either reference it +/// via the CTX macro, or the ctx parameter is in fact the return type from calling the 'constructor' +/// function for your parser/lexer/tree parser (see code example in "How to build Generated Code" .) +/// +/// \section macros Pre-Defined convenience MACROs +/// +/// While the author is not fond of using C MACROs to hide code or structure access, in the case of generated +/// code, they serve two useful purposes. The first is to simplify the references to internal constructs, +/// the second is to facilitate the change of any internal interface without requiring you to port grammars +/// from earlier versions (just regenerate and recompile). As of release 3.1, these macros are stable and +/// will only change their usage interface in the event of bugs being discovered. You are encouraged to +/// use these macros in your code, rather than access the raw interface. +/// +/// \bNB: Macros that act like statements must be terminated with a ';'. The macro body does not +/// supply this, nor should it. Macros that call functions are declared with () even if they +/// have no parameters, macros that reference fields do not have a () declaration. +/// +/// \section lexermacros Lexer Macros +/// +/// There are a number of macros that are useful exclusively within lexer rules. There are additional +/// macros, common to all recognizer, and these are documented in the section Common Macros. +/// +/// \subsection lexer LEXER +/// +/// The <code>LEXER</code> macro returns a pointer to the base lexer object, which is of type #pANTLR3_LEXER. This is +/// not the pointer to your generated lexer, which is supplied by the CTX macro, +/// but to the common implementation of a lexer interface, +/// which is supplied to all generated lexers. +/// +/// \subsection lexstate LEXSTATE +/// +/// Provides a pointer to the lexer shared state structure, which is where the tokens for a +/// rule are constructed and the status elements of the lexer are kept. This pointer is of type +/// #pANTLR3_RECOGNIZER_SHARED_STATE.In general you should only access elements of this structure +/// if there is not already another MACRO or standard $xxxx antlr reference that refers to it. +/// +/// \subsection la LA(n) +/// +/// The <code>LA</code> macro returns the character at index n from the current input stream index. The return +/// type is #ANTLR3_UINT32. Hence <code>LA(1)</code> returns the character at the current input position (the +/// character that will be consumed next), <code>LA(-1)</code> returns the character that has just been consumed +/// and so on. The <code>LA(n)</code> macro is useful for constructing semantic predicates in lexer rules. The +/// reference <code>LA(0)</code> is undefined and will cause an error in your lexer. +/// +/// \subsection getcharindex GETCHARINDEX() +/// +/// The <code>GETCHARINDEX</code> macro returns the index of the current character position as a 0 based +/// offset from the start of the input stream. It returns a value type of #ANTLR3_UINT32. +/// +/// \subsection getline GETLINE() +/// +/// The <code>GETLINE</code> macro returns the line number of current character (<code>LA(1)</code> in the input +/// stream. It returns a value type of #ANTLR3_UINT32. Note that the line number is incremented +/// automatically by an input stream when it sees the input character '\n'. The character that causes +/// the line number to increment can be changed by calling the SetNewLineChar() method on the input +/// stream before invoking the lexer and after creating the input stream. +/// +/// \subsection gettext GETTEXT() +/// +/// The <code>GETTEXT</code> macro returns the text currently matched by the lexer rule. In general you should use the +/// generic $text reference in ANTLR to retrieve this. The return type is a reference type of #pANTLR3_STRING +/// which allows you to manipulate the text you have retrieved (\b NB this does not change the input stream +/// only the text you copy from the input stream when you use this MACRO or $text). +/// +/// The reference $text->chars or GETTEXT()->chars will reference a pointer to the '\\0' terminated character +/// string that the ANTLR3 #pANTLR3_STRING represents. String space is allocated automatically as well as +/// the structure that holds the string. The #pANTLR3_STRING_FACTORY associated with the lexer handles this +/// and when you close the lexer, it will automatically free any space allocated for strings and their structures. +/// +/// \subsection getcharpositioninline GETCHARPOSITIONINLINE() +/// +/// The <code>GETCHARPOSITIONINLINE</code> returns the zero based offset of character <code>LA(1)</code> +/// from the start of the current input line. See the macro <code>GETLINE</code> for details on what the +/// line number means. +/// +/// \subsection emit EMIT() +/// +/// The macro <code>EMIT</code> causes the text range currently matched to the lexer rule to be emitted +/// immediately as the token for the rule. Subsequent text is matched but ignored. The type used for the +/// the token is the name of the lexer rule or, if you have change this by using $type = XXX;, the type +/// XXX is used. +/// +/// \subsection emitnew EMITNEW(t) +/// +/// The macro <code>EMITNEW</code> causes the supplied token reference <code>t</code> to be used as the +/// token emitted by the rule. The parameter <code>t </code> must be of type #pANTLR3_COMMON_TOKEN. +/// +/// \subsection index INDEX() +/// +/// The <code>INDEX</code> macro returns the current input position according to the input stream. It is not +/// guaranteed to be the character offset in the input stream but is instead used as a value +/// for marking and rewinding to specific points in the input stream. Use the macro <code>GETCHARINDEX()</code> +/// to find out the position of the <code>LA(1)</code> in the input stream. +/// +/// \subsection pushstream PUSHSTREAM(str) +/// +/// The <code>PUSHSTREAM</code> macro, in conjunction with the <code>POPSTREAM</code> macro (called internally in the runtime usually) +/// can be used to stack many input streams to the lexer, and implement constructs such as the C pre-processor +/// \#include directive. +/// +/// An input stream that is pushed on to the stack becomes the current input stream for the lexer and +/// the state of the previous stream is automatically saved. The input stream will be automatically +/// popped from the stack when it is exhausted by the lexer. You may use the macro <code>POPSTREAM</code> +/// to return to the previous input stream prior to exhausting the currently stacked input stream. +/// +/// Here is an example of using the macro in a lexer to implement the C \#include pre-processor directive: +/// +/// \code +/// fragment +/// STRING_GUTS : (~('\\'|'"') )* ; +/// +/// LINE_COMMAND +/// : '#' (' ' | '\t')* +/// ( +/// 'include' (' ' | '\t')+ '"' file = STRING_GUTS '"' (' ' | '\t')* '\r'? '\n' +/// { +/// pANTLR3_STRING fName; +/// pANTLR3_INPUT_STREAM in; +/// +/// // Create an initial string, then take a substring +/// // We can do this by messing with the start and end +/// // pointers of tokens and so on. This shows a reasonable way to +/// // manipulate strings. +/// // +/// fName = $file.text; +/// printf("Including file '\%s'\n", fName->chars); +/// +/// // Create a new input stream and take advantage of built in stream stacking +/// // in C target runtime. +/// // +/// in = antlr3AsciiFileStreamNew(fName->chars); +/// PUSHSTREAM(in); +/// +/// // Note that the input stream is not closed when it EOFs, I don't bother +/// // to do it here, but it is up to you to track streams created like this +/// // and destroy them when the whole parse session is complete. Remember that you +/// // don't want to do this until all tokens have been manipulated all the way through +/// // your tree parsers etc as the token does not store the text it just refers +/// // back to the input stream and trying to get the text for it will abort if you +/// // close the input stream too early. +/// // +/// +/// } +/// | (('0'..'9')=>('0'..'9'))+ ~('\n'|'\r')* '\r'? '\n' +/// ) +/// {$channel=HIDDEN;} +/// ; +/// \endcode +/// +/// \subsection popstream POPSTREAM() +/// +/// Assuming that you have stacked an input stream using the PUSHSTREAM macro, you can +/// remove it from the stream stack and revert to the previous input stream. You should be careful +/// to pop the stream at an appropriate point in your lexer action, so you do not match characters +/// from one stream with those from another in the same rule (unless this is what you want to do) +/// +/// \subsection settext SETTEXT(str) +/// +/// A token manufactured by the lexer does not actually physically store the text from the +/// input stream to which it matches. The token string is instead created only if you ask for +/// the text. However if you wish to change the text that the token represents you can use +/// this macro to set it explicitly. Note that this does not change the input stream text +/// but associates the supplied #pANTLR3_STRING with the token. This string is then returned +/// when parser and tree parser reference the tokens via the $xxx.text reference. +/// +/// \subsection user1 USER1 USER2 USER3 and CUSTOM +/// +/// While you can create your own custom token class and have the lexer deal with this, this +/// is a lot of work compared to the trivial inheritance that can be achieved in the Java target. +/// In many cases though, all that is needed is the addition of a few data items such as an +/// integer or a pointer. Rather than require C programmers to create complicated structures +/// just to add a few data items, the C target provides a few custom fields in the standard +/// token, which will fulfil the needs of most lexers and parsers. +/// +/// The token fields user1, user2, and user3 are all value types of #ANTLR_UINT32. In the +/// parser you can reference these fields directly from the token: <code>x=TOKNAME { $x->user1 ...</code> +/// but when you are building the token in the lexer, you must assign to the fields using the +/// macros <code>USER1</code>, <code>USER2</code>, or <code>USER3</code>. As in: +/// +/// \code +/// LEXTOK: 'AAAAA' { USER1 = 99; } ; +/// \endcode +/// +/// +/// \section parsermacros Parser and Tree Parser Macros +/// +/// \subsection parser PARSER +/// +/// The <code>PARSER</code> macro returns a pointer to the base parser or tree parser object, which is of type #pANTLR3_PARSER +/// or #pANTLR3_TREE_PARSER . This is not the pointer to your generated parser, which is supplied by the <code>CTX</code> macro, +/// but to the common implementation of a parser or tree parser interface, which is supplied to all generated parsers. +/// +/// \subsection index INDEX() +/// +/// When used in the parser, the <code>INDEX</code> macro returns the position of the current +/// token ( LT(1) ) in the input token stream. It can be used for <code>MARK</code> and <code>REWIND</code> +/// operations. +/// +/// \subsection lt LT(n) and LA(n) +/// +/// In the parser, the macro <code>LT(n)</code> returns the #pANTLR3_COMMON_TOKEN at offset <code>n</code> from +/// the current token stream input position. The macro <code>LA(n)</code> returns the token type of the token +/// at position <code>n</code>. The value <code>n</code> cannot be zero, and such a reference will return +/// <code>NULL</code> and possibly cause an error. <code>LA(1)</code> is the token that is about to be +/// recognized and <code>LA(-1)</code> is the token that has just been recognized. Values of n that exceed the +/// limits of the token stream boundaries will return <code>NULL</code>. +/// +/// \subsection psrstate PSRSTATE +/// +/// Returns the shared state pointer of type #pANTLR3_RECOGNIZER_SHARED_STATE. This is not generally +/// useful to the grammar programmer as the useful elements have generic $xxx references built in to +/// ANTLR. +/// +/// \subsection adaptor ADAPTOR +/// +/// When building an AST via a parser, the work of constructing and manipulating trees is done +/// by a supplied adaptor class. The default class is usually fine for most tree operations but +/// if you wish to build your own specialized linked/tree structure, then you may need to reference +/// the adaptor you supply directly. The <code>ADAPTOR</code> macro returns the reference to the tree adaptor +/// which is always of type #pANTLR3_BASE_TREE_ADAPTOR, even if it is your custom adapter. +/// +/// \section commonmacros Macros Common to All Recognizers +/// +/// \subsection recognizer RECOGNIZER +/// +/// Returns a reference type of #pANTRL3_BASE_RECOGNIZER, which is the base functionality supplied +/// to all recognizers, whether lexers, parsers or tree parsers. You can override methods in this +/// interface by installing your own function pointers (once you know what you are doing). +/// +/// \subsection input INPUT +/// +/// Returns a reference to the input stream of the appropriate type for the recognizer. In a lexer +/// this macro returns a reference type of #pANTLR3_INPUT_STREAM, in a parser this is type +/// #pANTLR3_TOKEN_STREAM and in a tree parser this is type #pANTLR3_COMMON_TREE_NODE_STREAM. +/// You can of course provide your own implementations of any of these interfaces. +/// +/// \subsection mark MARK() +/// +/// This macro will cause the input stream for the current recognizer to be marked with a +/// checkpoint. It will return a value type of #ANTLR3_MARKER which you can use as the +/// parameter to a <code>REWIND</code> macro to return to the marked point in the input. +/// +/// If you know you will only ever rewind to the last <code>MARK</code>, then you can ignore the return +/// value of this macro and just use the <code>REWINDLAST</code> macro to return to the last <code>MARK</code> that +/// was set in the input stream. +/// +/// \subsection rewind REWIND(m) +/// +/// Rewinds the appropriate input stream back to the marked checkpoint returned from a prior +/// MARK macro call and supplied as the parameter <code>m</code> to the <code>REWIND(m)</code> +/// macro. +/// +/// \subsection rewindlast REWINDLAST() +/// +/// Rewinds the current input stream (character, tokens, tree nodes) back to the last checkpoint +/// marker created by a <code>MARK</code> macro call. Fails silently if there was no prior +/// <code>MARK</code> call. +/// +/// \subsection seek SEEK(n) +/// +/// Causes the input stream to position itself directly at offset <code>n</code> in the stream. Works for all +/// input stream types, both lexer, parser and tree parser. +/// diff --git a/antlr-3.1.3/runtime/C/doxygen/knownissues.dox b/antlr-3.1.3/runtime/C/doxygen/knownissues.dox new file mode 100644 index 0000000..733c405 --- /dev/null +++ b/antlr-3.1.3/runtime/C/doxygen/knownissues.dox @@ -0,0 +1,3 @@ +/// \page knownissues Known Issues +/// +/// The following issues \ No newline at end of file diff --git a/antlr-3.1.3/runtime/C/doxygen/mainpage.dox b/antlr-3.1.3/runtime/C/doxygen/mainpage.dox new file mode 100644 index 0000000..3dd3596 --- /dev/null +++ b/antlr-3.1.3/runtime/C/doxygen/mainpage.dox @@ -0,0 +1,104 @@ +// Main page documentation for ANTLR3C runtime. Contains +// doxygen things only. +// + +/// \mainpage ANTLR3 C Runtime API and Usage Guide. +/// +/// \section version Version 3.1 +/// +/// This documentation is specifically for the C rutime version 3.1.x.x, which is +/// specifically for use with version 3.1.x.x of the ANTLR recognizer generation +/// tool. While some of the documentation may well apply to prior or future versions +/// you should consult the manuals for the correct version whenever possible. +/// +/// \section chchchchangeesss Changes from 3.0 to 3.1 +/// +/// Some changes in 3.1 may require small changes in your invoking programs or +/// in the grammar itself. Please read about them here before emailing the user group, +/// where you will be told to come and read about them here, unless they were missed +/// from this list. +/// +/// - \subpage changes31 Check here for API changes +/// +/// \section intro Introduction +/// +/// The ANTLR3 recognizer generation tool is written in Java, but allows the generation +/// of code targeted for a number of other languages. Each target language provides a code +/// generation template for the tool and a runtime library for use by generated recognizers. +/// The C runtime tracks the Java runtime releases and in general when a new version of the +/// tool is released, a new version of the C runtime will be released at the same time. +/// +/// The documentation here is in three parts: +/// +/// - \subpage build Building the runtime itself from source code; +/// - \subpage generate How to tell ANTLR to generate code for the C target; +/// - \subpage buildrec How to build the generated code +/// - \subpage using Using the runtime and the libraries and so on; +/// - \subpage runtime The documentation of the runtime code and functions; +/// +/// \section background Background Information +/// +/// The ANTLR 3 C runtime and code generation templates were written by <a href="http://www.linkedin.com/in/jimidle"> Jim Idle</a> +/// (jimi|at|temporal-wave|dott/com) of <a href="http://www.temporal-wave.com">Temporal Wave LLC</a>. +/// +/// The C runtime and therefore the code generated to utilize the runtime reflects the object model of the +/// Java version of the runtime as closely as a language without class structures and inheritance can. +/// Compromises have only been made where performance would be adversely affected such as minimizing the +/// number of pointer to pointer to pointer to function type structures that could ensue through trying to +/// model inheritance too exactly. Other differences include the use of token and string factories to minimize +/// the number of calls to system functions such as calloc().This model was adopted so that overriding any +/// default implementation of a function is relatively simple for the grammar programmer. +/// +/// The generated code is free threading (subject to the systems calls used on any particular platform +/// being likewise free threading.) +/// +/// \subsection model Runtime Model +/// +/// As there is no such thing as an object reference in C, the runtime defines a number of typedef structs that reflect +/// the calling interface chosen by Terence Parr for the Java version of the same. The initialization of a parser, +/// lexer, input stream or other internal structure therefore consists of allocating the memory required for +/// an instance of the typedef struct that represents the interface, initializing any counters, and buffers etc, +/// then populating a number of pointers to functions that implement the equivalent of the methods in the Java class. +/// +/// The use and initialization of the C versions of a parser is therefore similar to the examples given for Java, +/// but with a bent towards C of course. You may need to be aware of memory allocation and freeing operations +/// in certain environments such as Windows, where you cannot allocate memory in one DLL and free it in another. +/// +/// The runtime provides a number of structures and interfaces that the author has found useful when writing action and +/// processing code within java parsers, and furthermore were required by the C runtime code if it was not to +/// depart too far from the logical layout of the Java model. These include the C equivalents of String, List, +/// Hashtable, Vector and Trie, implemented by pointers to structures. These are freely available for your own programming needs. +/// +/// A goal of the generated code was to minimize the tracking, allocation and freeing of memory for reasons of both +/// performance and reliability. In essence any memory used by a lexer, parser or tree parser is automatically tracked and +/// freed when the instance of it is released. There are therefore factory functions for tokens and so on such that they +/// can be allocated in blocks and parceled out as they are required. They are all then freed in one go, minimizing the +/// risk of memory leaks and alloc/free thrashing. This has only one side effect, being that if you wish to preserve some structure generated by +/// the lexer, parser or tree parser, then you must make a copy of it before freeing those structures, and track it yourself +/// after that. In practice, it is easy enough just not to release the antlr generated components until you are +/// finished with their results. +/// +/// \section targets Target Platforms +/// +/// The C project is constructed such that it will compile on any reasonable ANSI C compiler in either 64 or 32 bit mode, +/// with all warnings turned on. This is true of both the runtime code and the generated code and has been summarily tested +/// with Visual Studio .Net (2003, 2005 and 2008) and later versions of gcc on Redhat Linux, as well as on AIX 5.2/5.3, Solaris 9/10, +/// HPUX 11.xx, OSX (PowerPC and Intel) and Cygwin. +/// +/// \b Notes +/// - The C runtime is constructed such that the library can be integrated as an archive library, or a shared library/DLL. +/// - The C language target code generation templates are distributed with the source code for the ANTLR tool itself. +/// +/// \section performance Performance +/// +/// It is C :-). Basic testing of performance against the Java runtime, +/// using the JDK1.6 java source code, and the Java parser provided in the examples (which is a tough test as it includes +/// backtracking and memoization) show that the C runtime uses about half the memory and is between 2 and 3 times the speed. +/// Tests of non-backtracking, non-memoizing parsers, indicate results significantly better than this. +/// +/// \section examples Downloading Examples +/// +/// The <a href="http://www.antlr.org/download.html">downloads page</a> of the ANTLR web site contains a downloadable +/// zip/tar of examples projects for use with the C runtime model. It contains .sln files and source code for a +/// number of example grammars and helps to see how to invoke and call the generated recognizers. +/// \ No newline at end of file diff --git a/antlr-3.1.3/runtime/C/doxygen/runtime.dox b/antlr-3.1.3/runtime/C/doxygen/runtime.dox new file mode 100644 index 0000000..0b0c7d9 --- /dev/null +++ b/antlr-3.1.3/runtime/C/doxygen/runtime.dox @@ -0,0 +1,35 @@ +/// \page runtime Navigating the C Runtime Documentation +/// +/// If you are familiar with Doxygen generated documentation, then the layout of the files, typedefs +/// and so on will be familiar to you. However there is also additional structure applied that helps +/// the programmer to see how the runtime is made up. +/// +/// \section modules Modules +/// +/// Under the Modules menu tree you will find the entry API Classes. This section is further +/// divided into typedefs and structs and the standard runtime supplied interface implementation +/// methods. +/// +/// The typedefs are the types that you declare in your code and which are returned by the +/// 'constructors' such as antlr3AsciiFileStreamNew(). The underlying structures document +/// the data elements of the type and what a function pointer installed in any particular +/// slot should do. +/// +/// The default implementations are the static methods within the default implementation file +/// for a 'class', which are installed by the runtime when a default instance of one the +/// typedefs (classes) is created. +/// +/// When navigating the source code, find the typedef you want to consult and inspect the documentation +/// for its function pointers, then look at the documentation for the default methods that implement +/// that 'method'. +/// +/// For example, under "API Typedefs and Structs" you will find "Base Recognizer Definition", which tells +/// you all the methods that belong to this interface. Under "API Implementation Functions", you will +/// find "Base Recognizer Implementation", which documents the actual functions that are installed +/// to implement the class methods. +/// +/// From here, the documentation should be obvious. If it is not, then you could try reading +/// the actual source code, but please don;t email the author directly, but use the ANTLR Interest +/// email group, which you should probably have signed up for if you have read this far into the +/// C runtime documentation. +/// \ No newline at end of file diff --git a/antlr-3.1.3/runtime/C/doxygen/using.dox b/antlr-3.1.3/runtime/C/doxygen/using.dox new file mode 100644 index 0000000..fb8424a --- /dev/null +++ b/antlr-3.1.3/runtime/C/doxygen/using.dox @@ -0,0 +1,62 @@ +/// \page using Using the ANTLR3 C Target +/// +/// \section intro Introduction +/// +/// Using the ANTLR target involves gaining knowledge of a number of elements: +/// +/// -# Writing ANTLR grammars (not covered in this manual); +/// -# How ANTLR works (not covered in this manual); +/// -# How to use the \@sections with the C target +/// -# Interoperation with the runtime within rule actions; +/// -# Implementing custom versions of the standard library methods; +/// +/// If you are as yet unfamiliar with how ANTLR works in general, then +/// it is suggested that you read the various <a href="http://www.antlr.org/wiki">wiki pages</a> concerned with +/// getting started. However there are a few things that you should note: +/// +/// - The lexer is independent of the parser. You \b cannot control the lexer from within the parser; +/// - The tree parser is independent of the parser. You \b cannot control the parser from within the tree parser(s); +/// - Each tree parser is independent of other tree parsers. +/// +/// This means that your lexer runs first and consumes all the input stream until +/// you stop it programmatically, or it reaches the end of the input stream. It produces +/// a complete stream of tokens, which the parser then consumes. +/// +/// \section Using \@sections in a C Targeted Grammar +/// +/// Within a grammar file there are a number of special sections you can add that cause the +/// code within them to be placed at strategic points in the generated code such as +/// before or after the #include statements in the .c file, within the generated header file +/// or within the constructor for the recognizer. +/// +/// Many of the \@sections used within a Java targeted grammar have some equivalent function within a +/// C targeted grammar, but their use may well be subtly different. There are also additional sections +/// that have meaning only within a grammar targeted for the C runtime. +/// +/// Detailed documentation of these sections is given here: \subpage atsections +/// +/// \section interop Interoperation Within Rule Actions +/// +/// Rule actions have a limited number of elements they can access by name, independently of the +/// target language generated. These are elements such as $line, $pos, $text and so on. Where the +/// $xxx returns a basic type such as \c int, then you can use these in C as you would in the Java +/// target, but where a reference returns a string, you will get a pointer to the C runtime +/// string implementation #pANTLR3_STRING. This will give you access to things like token text +/// but also provides some convenience methods such as #pANTLR3_STRING->substring() and #pANTLR3_STRING->toUTF8(). +/// +/// The generated code provides a number of C MACROs, which make it easier to access runtime +/// components. Always use these macros when available, to protect your action code from changes +/// to the underlying implementation. +/// +/// Detailed documentation of macros and rule action interoperation is given here: \subpage interop +/// +/// \section Custom Implementing Customized Methods +/// +/// Unless you wish to create your own tree structures using the built in ANTLR AST rewriting +/// notation, you will rarely need to override the default implementation of runtime methods. The +/// exception to this will be the syntax err reporting method, which is essentially a stub function +/// that you will usually want to provide your own implementation for. You should consider the built in function +/// displayRecognitionError() as an example of where to start as there can be no really useful +/// generic error message display. +/// +/// \ No newline at end of file diff --git a/antlr-3.1.3/runtime/C/include/antlr3.h b/antlr-3.1.3/runtime/C/include/antlr3.h new file mode 100644 index 0000000..2ead975 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3.h @@ -0,0 +1,56 @@ +#ifndef _ANTLR3_H +#define _ANTLR3_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> + +#include <antlr3errors.h> +#include <antlr3exception.h> +#include <antlr3encodings.h> +#include <antlr3string.h> +#include <antlr3input.h> +#include <antlr3cyclicdfa.h> +#include <antlr3intstream.h> +#include <antlr3filestream.h> +#include <antlr3collections.h> +#include <antlr3recognizersharedstate.h> +#include <antlr3baserecognizer.h> +#include <antlr3commontoken.h> +#include <antlr3tokenstream.h> +#include <antlr3bitset.h> +#include <antlr3lexer.h> +#include <antlr3parser.h> +#include <antlr3basetreeadaptor.h> +#include <antlr3commontreeadaptor.h> +#include <antlr3rewritestreams.h> +#include <antlr3debugeventlistener.h> + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3baserecognizer.h b/antlr-3.1.3/runtime/C/include/antlr3baserecognizer.h new file mode 100644 index 0000000..0a269d4 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3baserecognizer.h @@ -0,0 +1,371 @@ +/** \file + * Defines the basic structure to support recognizing by either a lexer, + * parser, or tree parser. + * \addtogroup ANTLR3_BASE_RECOGNIZER + * @{ + */ +#ifndef _ANTLR3_BASERECOGNIZER_H +#define _ANTLR3_BASERECOGNIZER_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3exception.h> +#include <antlr3input.h> +#include <antlr3tokenstream.h> +#include <antlr3commontoken.h> +#include <antlr3commontreenodestream.h> +#include <antlr3debugeventlistener.h> +#include <antlr3recognizersharedstate.h> + +/** Type indicator for a lexer recognizer + */ +#define ANTLR3_TYPE_LEXER 0x0001 + +/** Type indicator for a parser recognizer + */ +#define ANTLR3_TYPE_PARSER 0x0002 + +/** Type indicator for a tree parser recognizer + */ +#define ANTLR3_TYPE_TREE_PARSER 0x0004 + +#ifdef __cplusplus +extern "C" { +#endif + +/** \brief Base tracking context structure for all types of + * recognizers. + */ +typedef struct ANTLR3_BASE_RECOGNIZER_struct +{ + /// Whatever super structure is providing this interface needs a pointer to itself + /// so that this can be passed back to it whenever the api functions + /// are called back from here. + /// + void * super; + + /// Indicates the type of recognizer that we are an instance of. + /// The programmer may set this to anything of course, but the default + /// implementations of the interface only really understand the built in + /// types, so new error handlers etc would probably be required to as well. + /// + /// Valid types are: + /// + /// - #ANTLR3_TYPE_LEXER + /// - #ANTLR3_TYPE_PARSER + /// - #ANTLR3_TYPE_TREE_PARSER + /// + ANTLR3_UINT32 type; + + /// A pointer to the shared recognizer state, such that multiple + /// recognizers can use the same inputs streams and so on (in + /// the case of grammar inheritance for instance. + /// + pANTLR3_RECOGNIZER_SHARED_STATE state; + + /// If set to something other than NULL, then this structure is + /// points to an instance of the debugger interface. In general, the + /// debugger is only referenced internally in recovery/error operations + /// so that it does not cause overhead by having to check this pointer + /// in every function/method + /// + pANTLR3_DEBUG_EVENT_LISTENER debugger; + + + /// Pointer to a function that matches the current input symbol + /// against the supplied type. the function causes an error if a + /// match is not found and the default implementation will also + /// attempt to perform one token insertion or deletion if that is + /// possible with the input stream. You can override the default + /// implementation by installing a pointer to your own function + /// in this interface after the recognizer has initialized. This can + /// perform different recovery options or not recover at all and so on. + /// To ignore recovery altogether, see the comments in the default + /// implementation of this function in antlr3baserecognizer.c + /// + /// Note that errors are signalled by setting the error flag below + /// and creating a new exception structure and installing it in the + /// exception pointer below (you can chain these if you like and handle them + /// in some customized way). + /// + void * (*match) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, + ANTLR3_UINT32 ttype, pANTLR3_BITSET_LIST follow); + + /// Pointer to a function that matches the next token/char in the input stream + /// regardless of what it actually is. + /// + void (*matchAny) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer); + + /// Pointer to a function that decides if the token ahead of the current one is the + /// one we were loking for, in which case the curernt one is very likely extraneous + /// and can be reported that way. + /// + ANTLR3_BOOLEAN + (*mismatchIsUnwantedToken) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, pANTLR3_INT_STREAM input, ANTLR3_UINT32 ttype); + + /// Pointer to a function that decides if the current token is one that can logically + /// follow the one we were looking for, in which case the one we were looking for is + /// probably missing from the input. + /// + ANTLR3_BOOLEAN + (*mismatchIsMissingToken) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, pANTLR3_INT_STREAM input, pANTLR3_BITSET_LIST follow); + + /** Pointer to a function that works out what to do when a token mismatch + * occurs, so that Tree parsers can behave differently to other recognizers. + */ + void (*mismatch) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, + ANTLR3_UINT32 ttype, pANTLR3_BITSET_LIST follow); + + /** Pointer to a function to call to report a recognition problem. You may override + * this function with your own function, but refer to the standard implementation + * in antlr3baserecognizer.c for guidance. The function should recognize whether + * error recovery is in force, so that it does not print out more than one error messages + * for the same error. From the java comments in BaseRecognizer.java: + * + * This method sets errorRecovery to indicate the parser is recovering + * not parsing. Once in recovery mode, no errors are generated. + * To get out of recovery mode, the parser must successfully match + * a token (after a resync). So it will go: + * + * 1. error occurs + * 2. enter recovery mode, report error + * 3. consume until token found in resynch set + * 4. try to resume parsing + * 5. next match() will reset errorRecovery mode + */ + void (*reportError) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer); + + /** Pointer to a function that is called to display a recognition error message. You may + * override this function independently of (*reportError)() above as that function calls + * this one to do the actual exception printing. + */ + void (*displayRecognitionError) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, pANTLR3_UINT8 * tokenNames); + + /// Get number of recognition errors (lexer, parser, tree parser). Each + /// recognizer tracks its own number. So parser and lexer each have + /// separate count. Does not count the spurious errors found between + /// an error and next valid token match + /// + /// \see reportError() + /// + ANTLR3_UINT32 + (*getNumberOfSyntaxErrors) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer); + + /** Pointer to a function that recovers from an error found in the input stream. + * Generally, this will be a #ANTLR3_EXCEPTION_NOVIABLE_ALT but it could also + * be from a mismatched token that the (*match)() could not recover from. + */ + void (*recover) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer); + + /** Pointer to a function that is a hook to listen to token consumption during error recovery. + * This is mainly used by the debug parser to send events to the listener. + */ + void (*beginResync) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer); + + /** Pointer to a function that is a hook to listen to token consumption during error recovery. + * This is mainly used by the debug parser to send events to the listener. + */ + void (*endResync) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer); + + /** Pointer to a function that is a hook to listen to token consumption during error recovery. + * This is mainly used by the debug parser to send events to the listener. + */ + void (*beginBacktrack) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, ANTLR3_UINT32 level); + + /** Pointer to a function that is a hook to listen to token consumption during error recovery. + * This is mainly used by the debug parser to send events to the listener. + */ + void (*endBacktrack) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, ANTLR3_UINT32 level, ANTLR3_BOOLEAN successful); + + /** Pointer to a function to computer the error recovery set for the current rule. + * \see antlr3ComputeErrorRecoverySet() for details. + */ + pANTLR3_BITSET (*computeErrorRecoverySet) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer); + + /** Pointer to a function that computes the context-sensitive FOLLOW set for the + * current rule. + * \see antlr3ComputeCSRuleFollow() for details. + */ + pANTLR3_BITSET (*computeCSRuleFollow) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer); + + /** Pointer to a function to combine follow bitsets. + * \see antlr3CombineFollows() for details. + */ + pANTLR3_BITSET (*combineFollows) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, + ANTLR3_BOOLEAN exact); + + /** Pointer to a function that recovers from a mismatched token in the input stream. + * \see antlr3RecoverMismatch() for details. + */ + void * (*recoverFromMismatchedToken) + (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, + ANTLR3_UINT32 ttype, + pANTLR3_BITSET_LIST follow); + + /** Pointer to a function that recovers from a mismatched set in the token stream, in a similar manner + * to (*recoverFromMismatchedToken) + */ + void * (*recoverFromMismatchedSet) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, + pANTLR3_BITSET_LIST follow); + + /** Pointer to common routine to handle single token insertion for recovery functions. + */ + ANTLR3_BOOLEAN (*recoverFromMismatchedElement) + (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, + pANTLR3_BITSET_LIST follow); + + /** Pointer to function that consumes input until the next token matches + * the given token. + */ + void (*consumeUntil) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, + ANTLR3_UINT32 tokenType); + + /** Pointer to function that consumes input until the next token matches + * one in the given set. + */ + void (*consumeUntilSet) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, + pANTLR3_BITSET set); + + /** Pointer to function that returns an ANTLR3_LIST of the strings that identify + * the rules in the parser that got you to this point. Can be overridden by installing your + * own function set. + * + * \todo Document how to override invocation stack functions. + */ + pANTLR3_STACK (*getRuleInvocationStack) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer); + pANTLR3_STACK (*getRuleInvocationStackNamed) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, + pANTLR3_UINT8 name); + + /** Pointer to a function that converts an ANLR3_LIST of tokens to an ANTLR3_LIST of + * string token names. As this is mostly used in string template processing it may not be useful + * in the C runtime. + */ + pANTLR3_HASH_TABLE (*toStrings) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, + pANTLR3_HASH_TABLE); + + /** Pointer to a function to return whether the rule has parsed input starting at the supplied + * start index before. If the rule has not parsed input starting from the supplied start index, + * then it will return ANTLR3_MEMO_RULE_UNKNOWN. If it has parsed from the suppled start point + * then it will return the point where it last stopped parsing after that start point. + */ + ANTLR3_MARKER (*getRuleMemoization) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, + ANTLR3_INTKEY ruleIndex, + ANTLR3_MARKER ruleParseStart); + + /** Pointer to function that determines whether the rule has parsed input at the current index + * in the input stream + */ + ANTLR3_BOOLEAN (*alreadyParsedRule) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, + ANTLR3_MARKER ruleIndex); + + /** Pointer to function that records whether the rule has parsed the input at a + * current position successfully or not. + */ + void (*memoize) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, + ANTLR3_MARKER ruleIndex, + ANTLR3_MARKER ruleParseStart); + + /// Pointer to a function that returns the current input symbol. + /// The is placed into any label for the associated token ref; e.g., x=ID. Token + /// and tree parsers need to return different objects. Rather than test + /// for input stream type or change the IntStream interface, I use + /// a simple method to ask the recognizer to tell me what the current + /// input symbol is. + /// + /// This is ignored for lexers and the lexer implementation of this + /// function should return NULL. + /// + void * (*getCurrentInputSymbol) ( struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, + pANTLR3_INT_STREAM istream); + + /// Conjure up a missing token during error recovery. + /// + /// The recognizer attempts to recover from single missing + /// symbols. But, actions might refer to that missing symbol. + /// For example, x=ID {f($x);}. The action clearly assumes + /// that there has been an identifier matched previously and that + /// $x points at that token. If that token is missing, but + /// the next token in the stream is what we want we assume that + /// this token is missing and we keep going. Because we + /// have to return some token to replace the missing token, + /// we have to conjure one up. This method gives the user control + /// over the tokens returned for missing tokens. Mostly, + /// you will want to create something special for identifier + /// tokens. For literals such as '{' and ',', the default + /// action in the parser or tree parser works. It simply creates + /// a CommonToken of the appropriate type. The text will be the token. + /// If you change what tokens must be created by the lexer, + /// override this method to create the appropriate tokens. + /// + void * (*getMissingSymbol) ( struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, + pANTLR3_INT_STREAM istream, + pANTLR3_EXCEPTION e, + ANTLR3_UINT32 expectedTokenType, + pANTLR3_BITSET_LIST follow); + + /** Pointer to a function that returns whether the supplied grammar function + * will parse the current input stream or not. This is the way that syntactic + * predicates are evaluated. Unlike java, C is perfectly happy to invoke code + * via a pointer to a function (hence that's what all the ANTLR3 C interfaces + * do. + */ + ANTLR3_BOOLEAN (*synpred) ( struct ANTLR3_BASE_RECOGNIZER_struct * recognizer, void * ctx, + void (*predicate)(void * ctx)); + + /** Pointer to a function that can construct a generic exception structure + * with such information as the input stream can provide. + */ + void (*exConstruct) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer); + + /** Reset the recognizer + */ + void (*reset) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer); + + /** Pointer to a function that knows how to free the resources of a base recognizer. + */ + void (*free) (struct ANTLR3_BASE_RECOGNIZER_struct * recognizer); + +} + ANTLR3_BASE_RECOGNIZER; + +#ifdef __cplusplus +} +#endif + +#include <antlr3lexer.h> +#include <antlr3parser.h> +#include <antlr3treeparser.h> + +/// @} +/// + +#endif /* _ANTLR3_BASERECOGNIZER_H */ + diff --git a/antlr-3.1.3/runtime/C/include/antlr3basetree.h b/antlr-3.1.3/runtime/C/include/antlr3basetree.h new file mode 100644 index 0000000..023c4c2 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3basetree.h @@ -0,0 +1,143 @@ +/// \file +/// Definition of the ANTLR3 base tree. +/// + +#ifndef _ANTLR3_BASE_TREE_H +#define _ANTLR3_BASE_TREE_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3collections.h> +#include <antlr3string.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/// A generic tree implementation with no payload. You must subclass to +/// actually have any user data. ANTLR v3 uses a list of children approach +/// instead of the child-sibling approach in v2. A flat tree (a list) is +/// an empty node whose children represent the list. An empty (as in it does not +/// have payload itself), but non-null node is called "nil". +/// +typedef struct ANTLR3_BASE_TREE_struct +{ + + /// Implementers of this interface sometimes require a pointer to their selves. + /// + void * super; + + /// Generic void pointer allows the grammar programmer to attach any structure they + /// like to a tree node, in many cases saving the need to create their own tree + /// and tree adaptors. ANTLR does not use this pointer, but will copy it for you and so on. + /// + void * u; + + /// The list of all the children that belong to this node. They are not part of the node + /// as they belong to the common tree node that implements this. + /// + pANTLR3_VECTOR children; + + /// This is used to store the current child index position while descending + /// and ascending trees as the tree walk progresses. + /// + ANTLR3_MARKER savedIndex; + + /// A string factory to produce strings for toString etc + /// + pANTLR3_STRING_FACTORY strFactory; + + /// A pointer to a function that returns the common token pointer + /// for the payload in the supplied tree. + /// + pANTLR3_COMMON_TOKEN (*getToken) (struct ANTLR3_BASE_TREE_struct * tree); + + void (*addChild) (struct ANTLR3_BASE_TREE_struct * tree, void * child); + + void (*addChildren) (struct ANTLR3_BASE_TREE_struct * tree, pANTLR3_LIST kids); + + void (*createChildrenList) (struct ANTLR3_BASE_TREE_struct * tree); + + void * (*deleteChild) (struct ANTLR3_BASE_TREE_struct * tree, ANTLR3_UINT32 i); + + void (*replaceChildren) (struct ANTLR3_BASE_TREE_struct * parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, struct ANTLR3_BASE_TREE_struct * t); + + void * (*dupNode) (struct ANTLR3_BASE_TREE_struct * dupNode); + + void * (*dupTree) (struct ANTLR3_BASE_TREE_struct * tree); + + ANTLR3_UINT32 (*getCharPositionInLine) (struct ANTLR3_BASE_TREE_struct * tree); + + void * (*getChild) (struct ANTLR3_BASE_TREE_struct * tree, ANTLR3_UINT32 i); + + void (*setChildIndex) (struct ANTLR3_BASE_TREE_struct * tree, ANTLR3_INT32 ); + + ANTLR3_INT32 (*getChildIndex) (struct ANTLR3_BASE_TREE_struct * tree ); + + ANTLR3_UINT32 (*getChildCount) (struct ANTLR3_BASE_TREE_struct * tree); + + struct ANTLR3_BASE_TREE_struct * (*getParent) (struct ANTLR3_BASE_TREE_struct * tree); + + void (*setParent) (struct ANTLR3_BASE_TREE_struct * tree, struct ANTLR3_BASE_TREE_struct * parent); + + ANTLR3_UINT32 (*getType) (struct ANTLR3_BASE_TREE_struct * tree); + + void * (*getFirstChildWithType) (struct ANTLR3_BASE_TREE_struct * tree, ANTLR3_UINT32 type); + + ANTLR3_UINT32 (*getLine) (struct ANTLR3_BASE_TREE_struct * tree); + + pANTLR3_STRING (*getText) (struct ANTLR3_BASE_TREE_struct * tree); + + ANTLR3_BOOLEAN (*isNilNode) (struct ANTLR3_BASE_TREE_struct * tree); + + void (*setChild) (struct ANTLR3_BASE_TREE_struct * tree, ANTLR3_UINT32 i, void * child); + + pANTLR3_STRING (*toStringTree) (struct ANTLR3_BASE_TREE_struct * tree); + + pANTLR3_STRING (*toString) (struct ANTLR3_BASE_TREE_struct * tree); + + void (*freshenPACIndexesAll) (struct ANTLR3_BASE_TREE_struct * tree); + + void (*freshenPACIndexes) (struct ANTLR3_BASE_TREE_struct * tree, ANTLR3_UINT32 offset); + + void (*reuse) (struct ANTLR3_BASE_TREE_struct * tree); + + void (*free) (struct ANTLR3_BASE_TREE_struct * tree); + +} + ANTLR3_BASE_TREE; + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3basetreeadaptor.h b/antlr-3.1.3/runtime/C/include/antlr3basetreeadaptor.h new file mode 100644 index 0000000..cb303c2 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3basetreeadaptor.h @@ -0,0 +1,151 @@ +/** \file + * Definition of the ANTLR3 base tree adaptor. + */ + +#ifndef _ANTLR3_BASE_TREE_ADAPTOR_H +#define _ANTLR3_BASE_TREE_ADAPTOR_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3collections.h> +#include <antlr3string.h> +#include <antlr3basetree.h> +#include <antlr3commontoken.h> +#include <antlr3debugeventlistener.h> + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct ANTLR3_BASE_TREE_ADAPTOR_struct +{ + /** Pointer to any enclosing structure/interface that + * contains this structure. + */ + void * super; + + /** We need a string factory for creating imaginary tokens, we take this + * from the stream we are supplied to walk. + */ + pANTLR3_STRING_FACTORY strFactory; + + /* And we also need a token factory for creating imaginary tokens + * this is also taken from the input source. + */ + pANTLR3_TOKEN_FACTORY tokenFactory; + + /// If set to something other than NULL, then this structure is + /// points to an instance of the debugger interface. In general, the + /// debugger is only referenced internally in recovery/error operations + /// so that it does not cause overhead by having to check this pointer + /// in every function/method + /// + pANTLR3_DEBUG_EVENT_LISTENER debugger; + + void * (*nilNode) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor); + + + void * (*dupTree) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * tree); + void * (*dupTreeTT) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t, void * tree); + + void (*addChild) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t, void * child); + void (*addChildToken) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t, pANTLR3_COMMON_TOKEN child); + void (*setParent) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * child, void * parent); + + void * (*errorNode) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, pANTLR3_TOKEN_STREAM tnstream, pANTLR3_COMMON_TOKEN startToken, pANTLR3_COMMON_TOKEN stopToken, pANTLR3_EXCEPTION e); + ANTLR3_BOOLEAN (*isNilNode) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t); + + void * (*becomeRoot) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * newRoot, void * oldRoot); + + void * (*rulePostProcessing) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * root); + + void * (*becomeRootToken) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * newRoot, void * oldRoot); + + void * (*create) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, pANTLR3_COMMON_TOKEN payload); + void * (*createTypeToken) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken); + void * (*createTypeTokenText) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken, pANTLR3_UINT8 text); + void * (*createTypeText) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text); + + void * (*dupNode) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * treeNode); + + ANTLR3_UINT32 (*getType) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t); + + void (*setType) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t, ANTLR3_UINT32 type); + + pANTLR3_STRING (*getText) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t); + + void (*setText) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, pANTLR3_STRING t); + void (*setText8) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, pANTLR3_UINT8 t); + + void * (*getChild) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t, ANTLR3_UINT32 i); + void (*setChild) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t, ANTLR3_UINT32 i, void * child); + void (*deleteChild) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t, ANTLR3_UINT32 i); + void (*setChildIndex) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t, ANTLR3_UINT32 i); + ANTLR3_INT32 (*getChildIndex) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t); + + ANTLR3_UINT32 (*getChildCount) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void *); + + ANTLR3_UINT32 (*getUniqueID) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void *); + + pANTLR3_COMMON_TOKEN (*createToken) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text); + pANTLR3_COMMON_TOKEN (*createTokenFromToken) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, pANTLR3_COMMON_TOKEN fromToken); + pANTLR3_COMMON_TOKEN (*getToken) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t); + + void (*setTokenBoundaries) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t, pANTLR3_COMMON_TOKEN startToken, pANTLR3_COMMON_TOKEN stopToken); + + ANTLR3_MARKER (*getTokenStartIndex) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t); + + ANTLR3_MARKER (*getTokenStopIndex) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * t); + + void (*setDebugEventListener)(struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, pANTLR3_DEBUG_EVENT_LISTENER debugger); + + /// Produce a DOT (see graphviz freeware suite) from a base tree + /// + pANTLR3_STRING (*makeDot) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * theTree); + + /// Replace from start to stop child index of parent with t, which might + /// be a list. Number of children may be different + /// after this call. + /// + /// If parent is null, don't do anything; must be at root of overall tree. + /// Can't replace whatever points to the parent externally. Do nothing. + /// + void (*replaceChildren) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor, void * parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, void * t); + + void (*free) (struct ANTLR3_BASE_TREE_ADAPTOR_struct * adaptor); + +} + ANTLR3_TREE_ADAPTOR, *pANTLR3_TREE_ADAPTOR; +#ifdef __cplusplus +} +#endif + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3bitset.h b/antlr-3.1.3/runtime/C/include/antlr3bitset.h new file mode 100644 index 0000000..7d816ab --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3bitset.h @@ -0,0 +1,119 @@ +/** + * \file + * Defines the basic structures of an ANTLR3 bitset. this is a C version of the + * cut down Bitset class provided with the java version of antlr 3. + * + * + */ +#ifndef _ANTLR3_BITSET_H +#define _ANTLR3_BITSET_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3collections.h> + +/** How many bits in the elements + */ +#define ANTLR3_BITSET_BITS 64 + +/** How many bits in a nible of bits + */ +#define ANTLR3_BITSET_NIBBLE 4 + +/** log2 of ANTLR3_BITSET_BITS 2^ANTLR3_BITSET_LOG_BITS = ANTLR3_BITSET_BITS + */ +#define ANTLR3_BITSET_LOG_BITS 6 + +/** We will often need to do a mod operator (i mod nbits). + * For powers of two, this mod operation is the + * same as: + * - (i & (nbits-1)). + * + * Since mod is relatively slow, we use an easily + * precomputed mod mask to do the mod instead. + */ +#define ANTLR3_BITSET_MOD_MASK ANTLR3_BITSET_BITS - 1 + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct ANTLR3_BITSET_LIST_struct +{ + /// Pointer to the allocated array of bits for this bit set, which + /// is an array of 64 bit elements (of the architecture). If we find a + /// machine/C compiler that does not know anything about 64 bit values + /// then it should be easy enough to produce a 32 bit (or less) version + /// of the bitset code. Note that the pointer here may be static if laid down + /// by the code generation, and it must be copied if it is to be manipulated + /// to perform followset calculations. + /// + pANTLR3_BITWORD bits; + + /// Length of the current bit set in ANTLR3_UINT64 units. + /// + ANTLR3_UINT32 length; +} + ANTLR3_BITSET_LIST; + +typedef struct ANTLR3_BITSET_struct +{ + /// The actual bits themselves + /// + ANTLR3_BITSET_LIST blist; + + pANTLR3_BITSET (*clone) (struct ANTLR3_BITSET_struct * inSet); + pANTLR3_BITSET (*bor) (struct ANTLR3_BITSET_struct * bitset1, struct ANTLR3_BITSET_struct * bitset2); + void (*borInPlace) (struct ANTLR3_BITSET_struct * bitset, struct ANTLR3_BITSET_struct * bitset2); + ANTLR3_UINT32 (*size) (struct ANTLR3_BITSET_struct * bitset); + void (*add) (struct ANTLR3_BITSET_struct * bitset, ANTLR3_INT32 bit); + void (*grow) (struct ANTLR3_BITSET_struct * bitset, ANTLR3_INT32 newSize); + ANTLR3_BOOLEAN (*equals) (struct ANTLR3_BITSET_struct * bitset1, struct ANTLR3_BITSET_struct * bitset2); + ANTLR3_BOOLEAN (*isMember) (struct ANTLR3_BITSET_struct * bitset, ANTLR3_UINT32 bit); + ANTLR3_UINT32 (*numBits) (struct ANTLR3_BITSET_struct * bitset); + void (*remove) (struct ANTLR3_BITSET_struct * bitset, ANTLR3_UINT32 bit); + ANTLR3_BOOLEAN (*isNilNode) (struct ANTLR3_BITSET_struct * bitset); + pANTLR3_INT32 (*toIntList) (struct ANTLR3_BITSET_struct * bitset); + + void (*free) (struct ANTLR3_BITSET_struct * bitset); + + +} + ANTLR3_BITSET; + +#ifdef __cplusplus +} +#endif + + + +#endif + diff --git a/antlr-3.1.3/runtime/C/include/antlr3collections.h b/antlr-3.1.3/runtime/C/include/antlr3collections.h new file mode 100644 index 0000000..060b1d0 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3collections.h @@ -0,0 +1,384 @@ +#ifndef ANTLR3COLLECTIONS_H +#define ANTLR3COLLECTIONS_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> + + +#define ANTLR3_HASH_TYPE_INT 0 /**< Indicates the hashed file has integer keys */ +#define ANTLR3_HASH_TYPE_STR 1 /**< Indicates the hashed file has numeric keys */ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct ANTLR3_HASH_KEY_struct +{ + ANTLR3_UINT8 type; /**< One of ##ANTLR3_HASH_TYPE_INT or ##ANTLR3_HASH_TYPE_STR */ + + union + { + pANTLR3_UINT8 sKey; /**< Used if type is ANTLR3_HASH_TYPE_STR */ + ANTLR3_INTKEY iKey; /**< used if type is ANTLR3_HASH_TYPE_INT */ + } + key; + +} ANTLR3_HASH_KEY, *pANTLR3_HASH_KEY; + +/** Internal structure representing an element in a hash bucket. + * Stores the original key so that duplicate keys can be rejected + * if necessary, and contains function can be supported. If the hash key + * could be unique I would have invented the perfect compression algorithm ;-) + */ +typedef struct ANTLR3_HASH_ENTRY_struct +{ + /** Key that created this particular entry + */ + ANTLR3_HASH_KEY keybase; + + /** Pointer to the data for this particular entry + */ + void * data; + + /** Pointer to routine that knows how to release the memory + * structure pointed at by data. If this is NULL then we assume + * that the data pointer does not need to be freed when the entry + * is deleted from the table. + */ + void (ANTLR3_CDECL *free)(void * data); + + /** Pointer to the next entry in this bucket if there + * is one. Sometimes different keys will hash to the same bucket (especially + * if the number of buckets is small). We could implement dual hashing algorithms + * to minimize this, but that seems over the top for what this is needed for. + */ + struct ANTLR3_HASH_ENTRY_struct * nextEntry; +} + ANTLR3_HASH_ENTRY; + +/** Internal structure of a hash table bucket, which tracks + * all keys that hash to the same bucket. + */ +typedef struct ANTLR3_HASH_BUCKET_struct +{ + /** Pointer to the first entry in the bucket (if any, it + * may be NULL). Duplicate entries are chained from + * here. + */ + pANTLR3_HASH_ENTRY entries; + +} + ANTLR3_HASH_BUCKET; + +/** Structure that tracks a hash table + */ +typedef struct ANTLR3_HASH_TABLE_struct +{ + /** Indicates whether the table allows duplicate keys + */ + int allowDups; + + /** Number of buckets available in this table + */ + ANTLR3_UINT32 modulo; + + /** Points to the memory where the array of buckets + * starts. + */ + pANTLR3_HASH_BUCKET buckets; + + /** How many elements currently exist in the table. + */ + ANTLR3_UINT32 count; + + /** Whether the hash table should strdup the keys it is given or not. + */ + ANTLR3_BOOLEAN doStrdup; + + /** Pointer to function to completely delete this table + */ + void (*free) (struct ANTLR3_HASH_TABLE_struct * table); + + /* String keyed hashtable functions */ + void (*del) (struct ANTLR3_HASH_TABLE_struct * table, void * key); + pANTLR3_HASH_ENTRY (*remove) (struct ANTLR3_HASH_TABLE_struct * table, void * key); + void * (*get) (struct ANTLR3_HASH_TABLE_struct * table, void * key); + ANTLR3_INT32 (*put) (struct ANTLR3_HASH_TABLE_struct * table, void * key, void * element, void (ANTLR3_CDECL *freeptr)(void *)); + + /* Integer based hash functions */ + void (*delI) (struct ANTLR3_HASH_TABLE_struct * table, ANTLR3_INTKEY key); + pANTLR3_HASH_ENTRY (*removeI) (struct ANTLR3_HASH_TABLE_struct * table, ANTLR3_INTKEY key); + void * (*getI) (struct ANTLR3_HASH_TABLE_struct * table, ANTLR3_INTKEY key); + ANTLR3_INT32 (*putI) (struct ANTLR3_HASH_TABLE_struct * table, ANTLR3_INTKEY key, void * element, void (ANTLR3_CDECL *freeptr)(void *)); + + ANTLR3_UINT32 (*size) (struct ANTLR3_HASH_TABLE_struct * table); +} + ANTLR3_HASH_TABLE; + + +/** Internal structure representing an enumeration of a table. + * This is returned by antlr3Enumeration() + * Allows the programmer to traverse the table in hash order without + * knowing what is in the actual table. + * + * Note that it is up to the caller to ensure that the table + * structure does not change in the hash bucket that is currently being + * enumerated as this structure just tracks the next pointers in the + * bucket series. + */ +typedef struct ANTLR3_HASH_ENUM_struct +{ + /* Pointer to the table we are enumerating + */ + pANTLR3_HASH_TABLE table; + + /* Bucket we are currently enumerating (if NULL then we are done) + */ + ANTLR3_UINT32 bucket; + + /* Next entry to return, if NULL, then move to next bucket if any + */ + pANTLR3_HASH_ENTRY entry; + + /* Interface + */ + int (*next) (struct ANTLR3_HASH_ENUM_struct * en, pANTLR3_HASH_KEY *key, void ** data); + void (*free) (struct ANTLR3_HASH_ENUM_struct * table); +} + ANTLR3_HASH_ENUM; + +/** Structure that represents a LIST collection + */ +typedef struct ANTLR3_LIST_struct +{ + /** Hash table that is storing the list elements + */ + pANTLR3_HASH_TABLE table; + + void (*free) (struct ANTLR3_LIST_struct * list); + void (*del) (struct ANTLR3_LIST_struct * list, ANTLR3_INTKEY key); + void * (*get) (struct ANTLR3_LIST_struct * list, ANTLR3_INTKEY key); + void * (*remove) (struct ANTLR3_LIST_struct * list, ANTLR3_INTKEY key); + ANTLR3_INT32 (*add) (struct ANTLR3_LIST_struct * list, void * element, void (ANTLR3_CDECL *freeptr)(void *)); + ANTLR3_INT32 (*put) (struct ANTLR3_LIST_struct * list, ANTLR3_INTKEY key, void * element, void (ANTLR3_CDECL *freeptr)(void *)); + ANTLR3_UINT32 (*size) (struct ANTLR3_LIST_struct * list); + +} + ANTLR3_LIST; + +/** Structure that represents a Stack collection + */ +typedef struct ANTLR3_STACK_struct +{ + /** List that supports the stack structure + */ + pANTLR3_VECTOR vector; + + /** Used for quick access to the top of the stack + */ + void * top; + void (*free) (struct ANTLR3_STACK_struct * stack); + void * (*pop) (struct ANTLR3_STACK_struct * stack); + void * (*get) (struct ANTLR3_STACK_struct * stack, ANTLR3_INTKEY key); + ANTLR3_BOOLEAN (*push) (struct ANTLR3_STACK_struct * stack, void * element, void (ANTLR3_CDECL *freeptr)(void *)); + ANTLR3_UINT32 (*size) (struct ANTLR3_STACK_struct * stack); + void * (*peek) (struct ANTLR3_STACK_struct * stack); + +} + ANTLR3_STACK; + +/* Structure that represents a vector element + */ +typedef struct ANTLR3_VECTOR_ELEMENT_struct +{ + void * element; + void (ANTLR3_CDECL *freeptr)(void *); +} + ANTLR3_VECTOR_ELEMENT, *pANTLR3_VECTOR_ELEMENT; + +#define ANTLR3_VECTOR_INTERNAL_SIZE 16 +/* Structure that represents a vector collection. A vector is a simple list + * that contains a pointer to the element and a pointer to a function that + * that can free the element if it is removed. It auto resizes but does not + * use hash techniques as it is referenced by a simple numeric index. It is not a + * sparse list, so if any element is deleted, then the ones following are moved + * down in memory and the count is adjusted. + */ +typedef struct ANTLR3_VECTOR_struct +{ + /** Array of pointers to vector elements + */ + pANTLR3_VECTOR_ELEMENT elements; + + /** Number of entries currently in the list; + */ + ANTLR3_UINT32 count; + + /** Many times, a vector holds just a few nodes in an AST and it + * is too much overhead to malloc the space for elements so + * at the expense of a few bytes of memory, we hold the first + * few elements internally. It means we must copy them when + * we grow beyond this initial size, but that is less overhead than + * the malloc/free callas we would otherwise require. + */ + ANTLR3_VECTOR_ELEMENT internal[ANTLR3_VECTOR_INTERNAL_SIZE]; + + /** Indicates if the structure was made by a factory, in which + * case only the factory can free the memory for the actual vector, + * though the vector free function is called and will recurse through its + * entries calling any free pointers for each entry. + */ + ANTLR3_BOOLEAN factoryMade; + + /** Total number of entries in elements at any point in time + */ + ANTLR3_UINT32 elementsSize; + + void (ANTLR3_CDECL *free) (struct ANTLR3_VECTOR_struct * vector); + void (*del) (struct ANTLR3_VECTOR_struct * vector, ANTLR3_UINT32 entry); + void * (*get) (struct ANTLR3_VECTOR_struct * vector, ANTLR3_UINT32 entry); + void * (*remove) (struct ANTLR3_VECTOR_struct * vector, ANTLR3_UINT32 entry); + void (*clear) (struct ANTLR3_VECTOR_struct * vector); + ANTLR3_UINT32 (*add) (struct ANTLR3_VECTOR_struct * vector, void * element, void (ANTLR3_CDECL *freeptr)(void *)); + ANTLR3_UINT32 (*set) (struct ANTLR3_VECTOR_struct * vector, ANTLR3_UINT32 entry, void * element, void (ANTLR3_CDECL *freeptr)(void *), ANTLR3_BOOLEAN freeExisting); + ANTLR3_UINT32 (*size) (struct ANTLR3_VECTOR_struct * vector); +} + ANTLR3_VECTOR; + +/** Default vector pool size if otherwise unspecified + */ +#define ANTLR3_FACTORY_VPOOL_SIZE 256 + +/** Structure that tracks vectors in a vector and auto deletes the vectors + * in the vector factory when closed. + */ +typedef struct ANTLR3_VECTOR_FACTORY_struct +{ + + /** List of all vector pools allocated so far + */ + pANTLR3_VECTOR *pools; + + /** Count of the vector pools allocated so far (current active pool) + */ + ANTLR3_INT32 thisPool; + + /** The next vector available in the pool + */ + ANTLR3_UINT32 nextVector; + + /** Trick to quickly initialize a new vector via memcpy and not a function call + */ + ANTLR3_VECTOR unTruc; + + /** Function to close the vector factory + */ + void (*close) (struct ANTLR3_VECTOR_FACTORY_struct * factory); + + /** Function to supply a new vector + */ + pANTLR3_VECTOR (*newVector) (struct ANTLR3_VECTOR_FACTORY_struct * factory); + +} +ANTLR3_VECTOR_FACTORY; + + +/* -------------- TRIE Interfaces ---------------- */ + + +/** Structure that holds the payload entry in an ANTLR3_INT_TRIE or ANTLR3_STRING_TRIE + */ +typedef struct ANTLR3_TRIE_ENTRY_struct +{ + ANTLR3_UINT32 type; + void (ANTLR3_CDECL *freeptr)(void *); + union + { + ANTLR3_INTKEY intVal; + void * ptr; + } data; + + struct ANTLR3_TRIE_ENTRY_struct * next; /* Allows duplicate entries for same key in insertion order */ +} +ANTLR3_TRIE_ENTRY, * pANTLR3_TRIE_ENTRY; + + +/** Structure that defines an element/node in an ANTLR3_INT_TRIE + */ +typedef struct ANTLR3_INT_TRIE_NODE_struct +{ + ANTLR3_UINT32 bitNum; /**< This is the left/right bit index for traversal along the nodes */ + ANTLR3_INTKEY key; /**< This is the actual key that the entry represents if it is a terminal node */ + pANTLR3_TRIE_ENTRY buckets; /**< This is the data bucket(s) that the key indexes, which may be NULL */ + struct ANTLR3_INT_TRIE_NODE_struct * leftN; /**< Pointer to the left node from here when sKey & bitNum = 0 */ + struct ANTLR3_INT_TRIE_NODE_struct * rightN; /**< Pointer to the right node from here when sKey & bitNum, = 1 */ +} + ANTLR3_INT_TRIE_NODE, * pANTLR3_INT_TRIE_NODE; + +/** Structure that defines an ANTLR3_INT_TRIE. For this particular implementation, + * as you might expect, the key is turned into a "string" by looking at bit(key, depth) + * of the integer key. Using 64 bit keys gives us a depth limit of 64 (or bit 0..63) + * and potentially a huge trie. This is the algorithm for a Patricia Trie. + * Note also that this trie [can] accept multiple entries for the same key and is + * therefore a kind of elastic bucket patricia trie. + * + * If you find this code useful, please feel free to 'steal' it for any purpose + * as covered by the BSD license under which ANTLR is issued. You can cut the code + * but as the ANTLR library is only about 50K (Windows Vista), you might find it + * easier to just link the library. Please keep all comments and licenses and so on + * in any version of this you create of course. + * + * Jim Idle. + * + */ +typedef struct ANTLR3_INT_TRIE_struct +{ + pANTLR3_INT_TRIE_NODE root; /* Root node of this integer trie */ + pANTLR3_INT_TRIE_NODE current; /* Used to traverse the TRIE with the next() method */ + ANTLR3_UINT32 count; /* Current entry count */ + ANTLR3_BOOLEAN allowDups; /* Whether this trie accepts duplicate keys */ + + + pANTLR3_TRIE_ENTRY (*get) (struct ANTLR3_INT_TRIE_struct * trie, ANTLR3_INTKEY key); + ANTLR3_BOOLEAN (*del) (struct ANTLR3_INT_TRIE_struct * trie, ANTLR3_INTKEY key); + ANTLR3_BOOLEAN (*add) (struct ANTLR3_INT_TRIE_struct * trie, ANTLR3_INTKEY key, ANTLR3_UINT32 type, ANTLR3_INTKEY intVal, void * data, void (ANTLR3_CDECL *freeptr)(void *)); + void (*free) (struct ANTLR3_INT_TRIE_struct * trie); + +} + ANTLR3_INT_TRIE; + +#ifdef __cplusplus +} +#endif + +#endif + + diff --git a/antlr-3.1.3/runtime/C/include/antlr3commontoken.h b/antlr-3.1.3/runtime/C/include/antlr3commontoken.h new file mode 100644 index 0000000..ed1f7a0 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3commontoken.h @@ -0,0 +1,357 @@ +/** \file + * \brief Defines the interface for a common token. + * + * All token streams should provide their tokens using an instance + * of this common token. A custom pointer is provided, wher you may attach + * a further structure to enhance the common token if you feel the need + * to do so. The C runtime will assume that a token provides implementations + * of the interface functions, but all of them may be rplaced by your own + * implementation if you require it. + */ +#ifndef _ANTLR3_COMMON_TOKEN_H +#define _ANTLR3_COMMON_TOKEN_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> + +/** How many tokens to allocate at once in the token factory + */ +#define ANTLR3_FACTORY_POOL_SIZE 1024 + +/* Base token types, which all lexer/parser tokens come after in sequence. + */ + +/** Indicator of an invalid token + */ +#define ANTLR3_TOKEN_INVALID 0 + +#define ANTLR3_EOR_TOKEN_TYPE 1 + +/** Imaginary token type to cause a traversal of child nodes in a tree parser + */ +#define ANTLR3_TOKEN_DOWN 2 + +/** Imaginary token type to signal the end of a stream of child nodes. + */ +#define ANTLR3_TOKEN_UP 3 + +/** First token that can be used by users/generated code + */ +#define ANTLR3_MIN_TOKEN_TYPE ANTLR3_UP + 1 + +/** End of file token + */ +#define ANTLR3_TOKEN_EOF (ANTLR3_CHARSTREAM_EOF & 0xFFFFFFFF) + +/** Default channel for a token + */ +#define ANTLR3_TOKEN_DEFAULT_CHANNEL 0 + +/** Reserved channel number for a HIDDEN token - a token that + * is hidden from the parser. + */ +#define HIDDEN 99 + +#ifdef __cplusplus +extern "C" { +#endif + +// Indicates whether this token is carrying: +// +// State | Meaning +// ------+-------------------------------------- +// 0 | Nothing (neither rewrite text, nor setText) +// 1 | char * to user supplied rewrite text +// 2 | pANTLR3_STRING because of setText or similar action +// +#define ANTLR3_TEXT_NONE 0 +#define ANTLR3_TEXT_CHARP 1 +#define ANTLR3_TEXT_STRING 2 + +/** The definition of an ANTLR3 common token structure, which all implementations + * of a token stream should provide, installing any further structures in the + * custom pointer element of this structure. + * + * \remark + * Token streams are in essence provided by lexers or other programs that serve + * as lexers. + */ +typedef struct ANTLR3_COMMON_TOKEN_struct +{ + /** The actual type of this token + */ + ANTLR3_UINT32 type; + + /** Indicates that a token was produced from the token factory and therefore + * the the freeToken() method should not do anything itself because + * token factory is responsible for deleting it. + */ + ANTLR3_BOOLEAN factoryMade; + + /// A string factory that we can use if we ever need the text of a token + /// and need to manufacture a pANTLR3_STRING + /// + pANTLR3_STRING_FACTORY strFactory; + + /** The line number in the input stream where this token was derived from + */ + ANTLR3_UINT32 line; + + /** The offset into the input stream that the line in which this + * token resides starts. + */ + void * lineStart; + + /** The character position in the line that this token was derived from + */ + ANTLR3_INT32 charPosition; + + /** The virtual channel that this token exists in. + */ + ANTLR3_UINT32 channel; + + /** Pointer to the input stream that this token originated in. + */ + pANTLR3_INPUT_STREAM input; + + /** What the index of this token is, 0, 1, .., n-2, n-1 tokens + */ + ANTLR3_MARKER index; + + /** The character offset in the input stream where the text for this token + * starts. + */ + ANTLR3_MARKER start; + + /** The character offset in the input stream where the text for this token + * stops. + */ + ANTLR3_MARKER stop; + + /// Indicates whether this token is carrying: + /// + /// State | Meaning + /// ------+-------------------------------------- + /// 0 | Nothing (neither rewrite text, nor setText) + /// 1 | char * to user supplied rewrite text + /// 2 | pANTLR3_STRING because of setText or similar action + /// + /// Affects the union structure tokText below + /// (uses 32 bit so alignment is always good) + /// + ANTLR3_UINT32 textState; + + union + { + /// Pointer that is used when the token just has a pointer to + /// a char *, such as when a rewrite of an imaginary token supplies + /// a string in the grammar. No sense in constructing a pANTLR3_STRING just + /// for that, as mostly the text will not be accessed - if it is, then + /// we will build a pANTLR3_STRING for it a that point. + /// + pANTLR3_UCHAR chars; + + /// Some token types actually do carry around their associated text, hence + /// (*getText)() will return this pointer if it is not NULL + /// + pANTLR3_STRING text; + } + tokText; + + /** Because it is a bit more of a hassle to override an ANTLR3_COMMON_TOKEN + * as the standard structure for a token, a number of user programmable + * elements are allowed in a token. This is one of them. + */ + ANTLR3_UINT32 user1; + + /** Because it is a bit more of a hassle to override an ANTLR3_COMMON_TOKEN + * as the standard structure for a token, a number of user programmable + * elements are allowed in a token. This is one of them. + */ + ANTLR3_UINT32 user2; + + /** Because it is a bit more of a hassle to override an ANTLR3_COMMON_TOKEN + * as the standard structure for a token, a number of user programmable + * elements are allowed in a token. This is one of them. + */ + ANTLR3_UINT32 user3; + + /** Pointer to a custom element that the ANTLR3 programmer may define and install + */ + void * custom; + + /** Pointer to a function that knows how to free the custom structure when the + * token is destroyed. + */ + void (*freeCustom)(void * custom); + + /* ============================== + * API + */ + + /** Pointer to function that returns the text pointer of a token, use + * toString() if you want a pANTLR3_STRING version of the token. + */ + pANTLR3_STRING (*getText)(struct ANTLR3_COMMON_TOKEN_struct * token); + + /** Pointer to a function that 'might' be able to set the text associated + * with a token. Imaginary tokens such as an ANTLR3_CLASSIC_TOKEN may actually + * do this, however many tokens such as ANTLR3_COMMON_TOKEN do not actaully have + * strings associated with them but just point into the current input stream. These + * tokens will implement this function with a function that errors out (probably + * drastically. + */ + void (*setText)(struct ANTLR3_COMMON_TOKEN_struct * token, pANTLR3_STRING text); + + /** Pointer to a function that 'might' be able to set the text associated + * with a token. Imaginary tokens such as an ANTLR3_CLASSIC_TOKEN may actually + * do this, however many tokens such as ANTLR3_COMMON_TOKEN do not actully have + * strings associated with them but just point into the current input stream. These + * tokens will implement this function with a function that errors out (probably + * drastically. + */ + void (*setText8)(struct ANTLR3_COMMON_TOKEN_struct * token, pANTLR3_UINT8 text); + + /** Pointer to a function that returns the token type of this token + */ + ANTLR3_UINT32 (*getType)(struct ANTLR3_COMMON_TOKEN_struct * token); + + /** Pointer to a function that sets the type of this token + */ + void (*setType)(struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_UINT32 ttype); + + /** Pointer to a function that gets the 'line' number where this token resides + */ + ANTLR3_UINT32 (*getLine)(struct ANTLR3_COMMON_TOKEN_struct * token); + + /** Pointer to a function that sets the 'line' number where this token reside + */ + void (*setLine)(struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_UINT32 line); + + /** Pointer to a function that gets the offset in the line where this token exists + */ + ANTLR3_INT32 (*getCharPositionInLine) (struct ANTLR3_COMMON_TOKEN_struct * token); + + /** Pointer to a function that sets the offset in the line where this token exists + */ + void (*setCharPositionInLine) (struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_INT32 pos); + + /** Pointer to a function that gets the channel that this token was placed in (parsers + * can 'tune' to these channels. + */ + ANTLR3_UINT32 (*getChannel) (struct ANTLR3_COMMON_TOKEN_struct * token); + + /** Pointer to a function that sets the channel that this token should belong to + */ + void (*setChannel) (struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_UINT32 channel); + + /** Pointer to a function that returns an index 0...n-1 of the token in the token + * input stream. + */ + ANTLR3_MARKER (*getTokenIndex) (struct ANTLR3_COMMON_TOKEN_struct * token); + + /** Pointer to a function that can set the token index of this token in the token + * input stream. + */ + void (*setTokenIndex) (struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_MARKER); + + /** Pointer to a function that gets the start index in the input stream for this token. + */ + ANTLR3_MARKER (*getStartIndex) (struct ANTLR3_COMMON_TOKEN_struct * token); + + /** Pointer to a function that sets the start index in the input stream for this token. + */ + void (*setStartIndex) (struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_MARKER index); + + /** Pointer to a function that gets the stop index in the input stream for this token. + */ + ANTLR3_MARKER (*getStopIndex) (struct ANTLR3_COMMON_TOKEN_struct * token); + + /** Pointer to a function that sets the stop index in the input stream for this token. + */ + void (*setStopIndex) (struct ANTLR3_COMMON_TOKEN_struct * token, ANTLR3_MARKER index); + + /** Pointer to a function that returns this token as a text representation that can be + * printed with embedded control codes such as \n replaced with the printable sequence "\\n" + * This also yields a string structure that can be used more easily than the pointer to + * the input stream in certain situations. + */ + pANTLR3_STRING (*toString) (struct ANTLR3_COMMON_TOKEN_struct * token); +} + ANTLR3_COMMON_TOKEN; + +/** \brief ANTLR3 Token factory interface to create lots of tokens efficiently + * rather than creating and freeing lots of little bits of memory. + */ +typedef struct ANTLR3_TOKEN_FACTORY_struct +{ + /** Pointers to the array of tokens that this factory has produced so far + */ + pANTLR3_COMMON_TOKEN *pools; + + /** Current pool tokens we are allocating from + */ + ANTLR3_INT32 thisPool; + + /** The next token to throw out from the pool, will cause a new pool allocation + * if this exceeds the available tokenCount + */ + ANTLR3_UINT32 nextToken; + + /** Trick to initialize tokens and their API quickly, we set up this token when the + * factory is created, then just copy the memory it uses into the new token. + */ + ANTLR3_COMMON_TOKEN unTruc; + + /** Pointer to an input stream that is using this token factory (may be NULL) + * which will be assigned to the tokens automatically. + */ + pANTLR3_INPUT_STREAM input; + + /** Pointer to a function that returns a new token + */ + pANTLR3_COMMON_TOKEN (*newToken) (struct ANTLR3_TOKEN_FACTORY_struct * factory); + + /** Pointer to a function that changes teh curent inptu stream so that + * new tokens are created with reference to their originating text. + */ + void (*setInputStream) (struct ANTLR3_TOKEN_FACTORY_struct * factory, pANTLR3_INPUT_STREAM input); + /** Pointer to a function the destroys the factory + */ + void (*close) (struct ANTLR3_TOKEN_FACTORY_struct * factory); +} + ANTLR3_TOKEN_FACTORY; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3commontree.h b/antlr-3.1.3/runtime/C/include/antlr3commontree.h new file mode 100644 index 0000000..1516ecc --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3commontree.h @@ -0,0 +1,167 @@ +/** Interface for an ANTLR3 common tree which is what gets + * passed around by the AST producing parser. + */ + +#ifndef _ANTLR3_COMMON_TREE_H +#define _ANTLR3_COMMON_TREE_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3basetree.h> +#include <antlr3commontoken.h> + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct ANTLR3_COMMON_TREE_struct +{ + + /// Not used by ANTLR, but if a super structure is created above + /// this structure, it can be used to point to the start of the super + /// structure, where additional data and function pointers can be stored. + /// + void * super; + + /// Start token index that encases this tree + /// + ANTLR3_MARKER startIndex; + + /// End token that encases this tree + /// + ANTLR3_MARKER stopIndex; + + /// A single token, this is the payload for the tree + /// + pANTLR3_COMMON_TOKEN token; + + /// Points to the node that has this node as a child. + /// If this is NULL, then this is the root node. + /// + pANTLR3_COMMON_TREE parent; + + /// What index is this particular node in the child list it + /// belongs to? + /// + ANTLR3_INT32 childIndex; + + /// Pointer to the tree factory that manufactured this + /// token. This can be used by duplication methods and so on + /// to manufacture another auto-tracked common tree structure + /// + pANTLR3_ARBORETUM factory; + + /// An encapsulated BASE TREE structure (NOT a pointer) + /// that performs a lot of the dirty work of node management + /// To this we add just a few functions that are specific to the + /// payload. You can further abstract common tree so long + /// as you always have a baseTree pointer in the top structure + /// and copy it from the next one down. + /// So, lets say we have a structure JIMS_TREE. + /// It needs an ANTLR3_BASE_TREE that will support all the + /// general tree duplication stuff. + /// It needs a ANTLR3_COMMON_TREE structure embedded or completely + /// provides the equivalent interface. + /// It provides it's own methods and data. + /// To create a new one of these, the function provided to + /// the tree adaptor (see comments there) should allocate the + /// memory for a new JIMS_TREE structure, then call + /// antlr3InitCommonTree(<addressofembeddedCOMMON_TREE>) + /// antlr3BaseTreeNew(<addressofBASETREE>) + /// The interfaces for BASE_TREE and COMMON_TREE will then + /// be initialized. You then call and you can override them or just init + /// JIMS_TREE (note that the base tree in common tree will be ignored) + /// just the top level base tree is used). Codegen will take care of the rest. + /// + ANTLR3_BASE_TREE baseTree; + +} + ANTLR3_COMMON_TREE; + +/// \brief ANTLR3 Tree factory interface to create lots of trees efficiently +/// rather than creating and freeing lots of little bits of memory. +/// +typedef struct ANTLR3_ARBORETUM_struct +{ + /// Pointers to the array of tokens that this factory has produced so far + /// + pANTLR3_COMMON_TREE *pools; + + /// Current pool tokens we are allocating from + /// + ANTLR3_INT32 thisPool; + + /// The next token to throw out from the pool, will cause a new pool allocation + /// if this exceeds the available tokenCount + /// + ANTLR3_UINT32 nextTree; + + /// Trick to initialize tokens and their API quickly, we set up this token when the + /// factory is created, then just copy the memory it uses into the new token. + /// + ANTLR3_COMMON_TREE unTruc; + + /// Pointer to a vector factory that is used to create child list vectors + /// for any child nodes that need them. This means that we auto track the + /// vectors and auto free them when we close the factory. It also means + /// that all rewriting trees can use the same tree factory and the same + /// vector factory and we do not dup any nodes unless we must do so + /// explicitly because of context such as an empty rewrite stream and + /// ->IMAGINARY[ID] so on. This makes memory tracking much simpler and + /// tempts no errors. + /// + pANTLR3_VECTOR_FACTORY vFactory; + + /// A resuse stack for reclaiming Nil nodes that were used in rewrites + /// and are now dead. The nilNode() method will eat one of these before + /// creating a new node. + /// + pANTLR3_STACK nilStack; + + /// Pointer to a function that returns a new tree + /// + pANTLR3_BASE_TREE (*newTree) (struct ANTLR3_ARBORETUM_struct * factory); + pANTLR3_BASE_TREE (*newFromTree) (struct ANTLR3_ARBORETUM_struct * factory, pANTLR3_COMMON_TREE tree); + pANTLR3_BASE_TREE (*newFromToken) (struct ANTLR3_ARBORETUM_struct * factory, pANTLR3_COMMON_TOKEN token); + + /// Pointer to a function the destroys the factory + /// + void (*close) (struct ANTLR3_ARBORETUM_struct * factory); +} + ANTLR3_ARBORETUM; + +#ifdef __cplusplus +} +#endif + +#endif + + diff --git a/antlr-3.1.3/runtime/C/include/antlr3commontreeadaptor.h b/antlr-3.1.3/runtime/C/include/antlr3commontreeadaptor.h new file mode 100644 index 0000000..4827997 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3commontreeadaptor.h @@ -0,0 +1,70 @@ +/** \file + * Definition of the ANTLR3 common tree adaptor. + */ + +#ifndef _ANTLR3_COMMON_TREE_ADAPTOR_H +#define _ANTLR3_COMMON_TREE_ADAPTOR_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3collections.h> +#include <antlr3string.h> +#include <antlr3basetreeadaptor.h> +#include <antlr3commontree.h> +#include <antlr3debugeventlistener.h> + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct ANTLR3_COMMON_TREE_ADAPTOR_struct +{ + /** Any enclosing structure/class can use this pointer to point to its own interface. + */ + void * super; + + /** Base interface implementation, embedded structure + */ + ANTLR3_TREE_ADAPTOR baseAdaptor; + + /** Tree factory for producing new nodes as required without needing to track + * memory allocation per node. + */ + pANTLR3_ARBORETUM arboretum; + +} + ANTLR3_COMMON_TREE_ADAPTOR; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3commontreenodestream.h b/antlr-3.1.3/runtime/C/include/antlr3commontreenodestream.h new file mode 100644 index 0000000..beb0534 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3commontreenodestream.h @@ -0,0 +1,336 @@ +/// \file +/// Definition of the ANTLR3 common tree node stream. +/// + +#ifndef _ANTLR3_COMMON_TREE_NODE_STREAM__H +#define _ANTLR3_COMMON_TREE_NODE_STREAM__H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3commontreeadaptor.h> +#include <antlr3commontree.h> +#include <antlr3collections.h> +#include <antlr3intstream.h> +#include <antlr3string.h> + +/// Token buffer initial size settings ( will auto increase) +/// +#define DEFAULT_INITIAL_BUFFER_SIZE 100 +#define INITIAL_CALL_STACK_SIZE 10 + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct ANTLR3_TREE_NODE_STREAM_struct +{ + /// Any interface that implements this interface (is a + /// super structure containing this structure), may store the pointer + /// to itself here in the super pointer, which is not used by + /// the tree node stream. This will point to an implementation + /// of ANTLR3_COMMON_TREE_NODE_STREAM in this case. + /// + pANTLR3_COMMON_TREE_NODE_STREAM ctns; + + /// All input streams implement the ANTLR3_INT_STREAM interface... + /// + pANTLR3_INT_STREAM istream; + + /// Get tree node at current input pointer + i ahead where i=1 is next node. + /// i<0 indicates nodes in the past. So LT(-1) is previous node, but + /// implementations are not required to provide results for k < -1. + /// LT(0) is undefined. For i>=n, return null. + /// Return NULL for LT(0) and any index that results in an absolute address + /// that is negative (beyond the start of the list). + /// + /// This is analogous to the LT() method of the TokenStream, but this + /// returns a tree node instead of a token. Makes code gen identical + /// for both parser and tree grammars. :) + /// + pANTLR3_BASE_TREE (*_LT) (struct ANTLR3_TREE_NODE_STREAM_struct * tns, ANTLR3_INT32 k); + + /// Where is this stream pulling nodes from? This is not the name, but + /// the object that provides node objects. + /// + pANTLR3_BASE_TREE (*getTreeSource) (struct ANTLR3_TREE_NODE_STREAM_struct * tns); + + /// What adaptor can tell me how to interpret/navigate nodes and + /// trees. E.g., get text of a node. + /// + pANTLR3_BASE_TREE_ADAPTOR (*getTreeAdaptor) (struct ANTLR3_TREE_NODE_STREAM_struct * tns); + + /// As we flatten the tree, we use UP, DOWN nodes to represent + /// the tree structure. When debugging we need unique nodes + /// so we have to instantiate new ones. When doing normal tree + /// parsing, it's slow and a waste of memory to create unique + /// navigation nodes. Default should be false; + /// + void (*setUniqueNavigationNodes) (struct ANTLR3_TREE_NODE_STREAM_struct * tns, ANTLR3_BOOLEAN uniqueNavigationNodes); + + pANTLR3_STRING (*toString) (struct ANTLR3_TREE_NODE_STREAM_struct * tns); + + /// Return the text of all nodes from start to stop, inclusive. + /// If the stream does not buffer all the nodes then it can still + /// walk recursively from start until stop. You can always return + /// null or "" too, but users should not access $ruleLabel.text in + /// an action of course in that case. + /// + pANTLR3_STRING (*toStringSS) (struct ANTLR3_TREE_NODE_STREAM_struct * tns, pANTLR3_BASE_TREE start, pANTLR3_BASE_TREE stop); + + /// Return the text of all nodes from start to stop, inclusive, into the + /// supplied buffer. + /// If the stream does not buffer all the nodes then it can still + /// walk recursively from start until stop. You can always return + /// null or "" too, but users should not access $ruleLabel.text in + /// an action of course in that case. + /// + void (*toStringWork) (struct ANTLR3_TREE_NODE_STREAM_struct * tns, pANTLR3_BASE_TREE start, pANTLR3_BASE_TREE stop, pANTLR3_STRING buf); + + /// Release up any and all space the the interface allocate, including for this structure. + /// + void (*free) (struct ANTLR3_TREE_NODE_STREAM_struct * tns); + + /// Get a tree node at an absolute index i; 0..n-1. + /// If you don't want to buffer up nodes, then this method makes no + /// sense for you. + /// + pANTLR3_BASE_TREE (*get) (struct ANTLR3_TREE_NODE_STREAM_struct * tns, ANTLR3_INT32 i); + + // REWRITING TREES (used by tree parser) + + /// Replace from start to stop child index of parent with t, which might + /// be a list. Number of children may be different + /// after this call. The stream is notified because it is walking the + /// tree and might need to know you are monkeying with the underlying + /// tree. Also, it might be able to modify the node stream to avoid + /// restreaming for future phases. + /// + /// If parent is null, don't do anything; must be at root of overall tree. + /// Can't replace whatever points to the parent externally. Do nothing. + /// + void (*replaceChildren) (struct ANTLR3_TREE_NODE_STREAM_struct * tns, pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE t); + +} + ANTLR3_TREE_NODE_STREAM; + +typedef struct ANTLR3_COMMON_TREE_NODE_STREAM_struct +{ + /// Any interface that implements this interface (is a + /// super structure containing this structure), may store the pointer + /// to itself here in the super pointer, which is not used by + /// the common tree node stream. + /// + void * super; + + /// Pointer to the tree node stream interface + /// + pANTLR3_TREE_NODE_STREAM tnstream; + + /// String factory for use by anything that wishes to create strings + /// such as a tree representation or some copy of the text etc. + /// + pANTLR3_STRING_FACTORY stringFactory; + + /// Dummy tree node that indicates a descent into a child + /// tree. Initialized by a call to create a new interface. + /// + ANTLR3_COMMON_TREE DOWN; + + /// Dummy tree node that indicates a descent up to a parent + /// tree. Initialized by a call to create a new interface. + /// + ANTLR3_COMMON_TREE UP; + + /// Dummy tree node that indicates the termination point of the + /// tree. Initialized by a call to create a new interface. + /// + ANTLR3_COMMON_TREE EOF_NODE; + + /// Dummy node that is returned if we need to indicate an invalid node + /// for any reason. + /// + ANTLR3_COMMON_TREE INVALID_NODE; + + /// The complete mapping from stream index to tree node. + /// This buffer includes pointers to DOWN, UP, and EOF nodes. + /// It is built upon ctor invocation. The elements are type + /// Object as we don't what the trees look like. + /// + /// Load upon first need of the buffer so we can set token types + /// of interest for reverseIndexing. Slows us down a wee bit to + /// do all of the if p==-1 testing everywhere though, though in C + /// you won't really be able to measure this. + /// + /// Must be freed when the tree node stream is torn down. + /// + pANTLR3_VECTOR nodes; + + /// If set to ANTLR3_TRUE then the navigation nodes UP, DOWN are + /// duplicated rather than reused within the tree. + /// + ANTLR3_BOOLEAN uniqueNavigationNodes; + + /// Which tree are we navigating ? + /// + pANTLR3_BASE_TREE root; + + /// Pointer to tree adaptor interface that manipulates/builds + /// the tree. + /// + pANTLR3_BASE_TREE_ADAPTOR adaptor; + + /// As we walk down the nodes, we must track parent nodes so we know + /// where to go after walking the last child of a node. When visiting + /// a child, push current node and current index (current index + /// is first stored in the tree node structure to avoid two stacks. + /// + pANTLR3_STACK nodeStack; + + /// The current index into the nodes vector of the current tree + /// we are parsing and possibly rewriting. + /// + ANTLR3_INT32 p; + + /// Which node are we currently visiting? + /// + pANTLR3_BASE_TREE currentNode; + + /// Which node did we last visit? Used for LT(-1) + /// + pANTLR3_BASE_TREE previousNode; + + /// Which child are we currently visiting? If -1 we have not visited + /// this node yet; next consume() request will set currentIndex to 0. + /// + ANTLR3_INT32 currentChildIndex; + + /// What node index did we just consume? i=0..n-1 for n node trees. + /// IntStream.next is hence 1 + this value. Size will be same. + /// + ANTLR3_MARKER absoluteNodeIndex; + + /// Buffer tree node stream for use with LT(i). This list grows + /// to fit new lookahead depths, but consume() wraps like a circular + /// buffer. + /// + pANTLR3_BASE_TREE * lookAhead; + + /// Number of elements available in the lookahead buffer at any point in + /// time. This is the current size of the array. + /// + ANTLR3_UINT32 lookAheadLength; + + /// lookAhead[head] is the first symbol of lookahead, LT(1). + /// + ANTLR3_UINT32 head; + + /// Add new lookahead at lookahead[tail]. tail wraps around at the + /// end of the lookahead buffer so tail could be less than head. + /// + ANTLR3_UINT32 tail; + + /// Calls to mark() may be nested so we have to track a stack of + /// them. The marker is an index into this stack. Index 0 is + /// the first marker. This is a List<TreeWalkState> + /// + pANTLR3_VECTOR markers; + + // INTERFACE + // + void (*fill) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns, ANTLR3_INT32 k); + + void (*addLookahead) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns, pANTLR3_BASE_TREE node); + + ANTLR3_BOOLEAN (*hasNext) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns); + + pANTLR3_BASE_TREE (*next) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns); + + pANTLR3_BASE_TREE (*handleRootnode) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns); + + pANTLR3_BASE_TREE (*visitChild) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns, ANTLR3_UINT32 child); + + void (*addNavigationNode) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns, ANTLR3_UINT32 ttype); + + pANTLR3_BASE_TREE (*newDownNode) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns); + + pANTLR3_BASE_TREE (*newUpNode) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns); + + void (*walkBackToMostRecentNodeWithUnvisitedChildren) + (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns); + + ANTLR3_BOOLEAN (*hasUniqueNavigationNodes) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns); + + ANTLR3_UINT32 (*getLookaheadSize) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns); + + void (*push) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns, ANTLR3_INT32 index); + + ANTLR3_INT32 (*pop) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns); + + void (*reset) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns); + + void (*free) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns); + + /// Indicates whether this node stream was derived from a prior + /// node stream to be used by a rewriting tree parser for instance. + /// If this flag is set to ANTLR3_TRUE, then when this stream is + /// closed it will not free the root tree as this tree always + /// belongs to the origniating node stream. + /// + ANTLR3_BOOLEAN isRewriter; + +} + ANTLR3_COMMON_TREE_NODE_STREAM; + +/** This structure is used to save the state information in the treenodestream + * when walking ahead with cyclic DFA or for syntactic predicates, + * we need to record the state of the tree node stream. This + * class wraps up the current state of the CommonTreeNodeStream. + * Calling mark() will push another of these on the markers stack. + */ +typedef struct ANTLR3_TREE_WALK_STATE_struct +{ + ANTLR3_UINT32 currentChildIndex; + ANTLR3_MARKER absoluteNodeIndex; + pANTLR3_BASE_TREE currentNode; + pANTLR3_BASE_TREE previousNode; + ANTLR3_UINT32 nodeStackSize; + pANTLR3_BASE_TREE * lookAhead; + ANTLR3_UINT32 lookAheadLength; + ANTLR3_UINT32 tail; + ANTLR3_UINT32 head; +} + ANTLR3_TREE_WALK_STATE; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3convertutf.h b/antlr-3.1.3/runtime/C/include/antlr3convertutf.h new file mode 100644 index 0000000..ffe44b0 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3convertutf.h @@ -0,0 +1,166 @@ +/* + * Copyright 2001-2004 Unicode, Inc. + * + * Disclaimer + * + * This source code is provided as is by Unicode, Inc. No claims are + * made as to fitness for any particular purpose. No warranties of any + * kind are expressed or implied. The recipient agrees to determine + * applicability of information provided. If this file has been + * purchased on magnetic or optical media from Unicode, Inc., the + * sole remedy for any claim will be exchange of defective media + * within 90 days of receipt. + * + * Limitations on Rights to Redistribute This Code + * + * Unicode, Inc. hereby grants the right to freely use the information + * supplied in this file in the creation of products supporting the + * Unicode Standard, and to make copies of this file in any form + * for internal or external distribution as long as this notice + * remains attached. + */ + +/* --------------------------------------------------------------------- + + Conversions between UTF32, UTF-16, and UTF-8. Header file. + + Several functions are included here, forming a complete set of + conversions between the three formats. UTF-7 is not included + here, but is handled in a separate source file. + + Each of these routines takes pointers to input buffers and output + buffers. The input buffers are const. + + Each routine converts the text between *sourceStart and sourceEnd, + putting the result into the buffer between *targetStart and + targetEnd. Note: the end pointers are *after* the last item: e.g. + *(sourceEnd - 1) is the last item. + + The return result indicates whether the conversion was successful, + and if not, whether the problem was in the source or target buffers. + (Only the first encountered problem is indicated.) + + After the conversion, *sourceStart and *targetStart are both + updated to point to the end of last text successfully converted in + the respective buffers. + + Input parameters: + sourceStart - pointer to a pointer to the source buffer. + The contents of this are modified on return so that + it points at the next thing to be converted. + targetStart - similarly, pointer to pointer to the target buffer. + sourceEnd, targetEnd - respectively pointers to the ends of the + two buffers, for overflow checking only. + + These conversion functions take a ConversionFlags argument. When this + flag is set to strict, both irregular sequences and isolated surrogates + will cause an error. When the flag is set to lenient, both irregular + sequences and isolated surrogates are converted. + + Whether the flag is strict or lenient, all illegal sequences will cause + an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>, + or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code + must check for illegal sequences. + + When the flag is set to lenient, characters over 0x10FFFF are converted + to the replacement character; otherwise (when the flag is set to strict) + they constitute an error. + + Output parameters: + The value "sourceIllegal" is returned from some routines if the input + sequence is malformed. When "sourceIllegal" is returned, the source + value will point to the illegal value that caused the problem. E.g., + in UTF-8 when a sequence is malformed, it points to the start of the + malformed sequence. + + Author: Mark E. Davis, 1994. + Rev History: Rick McGowan, fixes & updates May 2001. + Fixes & updates, Sept 2001. + +------------------------------------------------------------------------ */ + +/* --------------------------------------------------------------------- + The following 4 definitions are compiler-specific. + The C standard does not guarantee that wchar_t has at least + 16 bits, so wchar_t is no less portable than unsigned short! + All should be unsigned values to avoid sign extension during + bit mask & shift operations. +------------------------------------------------------------------------ */ + + +// Changes for ANTLR3 - Jim Idle, January 2008. +// builtin types defined for Unicode types changed to +// aliases for the types that are system determined by +// ANTLR at compile time. +// +// typedef unsigned long UTF32; /* at least 32 bits */ +// typedef unsigned short UTF16; /* at least 16 bits */ +// typedef unsigned char UTF8; /* typically 8 bits */ +// typedef unsigned char Boolean; /* 0 or 1 */ + +#ifndef _ANTLR3_CONVERTUTF_H +#define _ANTLR3_CONVERTUTF_H + +#include <antlr3defs.h> + +typedef ANTLR3_UINT32 UTF32; /* at least 32 bits */ +typedef ANTLR3_UINT16 UTF16; /* at least 16 bits */ +typedef ANTLR3_UINT8 UTF8; /* typically 8 bits */ + +/* Some fundamental constants */ +#define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD +#define UNI_MAX_BMP (UTF32)0x0000FFFF +#define UNI_MAX_UTF16 (UTF32)0x0010FFFF +#define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF +#define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF + +typedef enum { + conversionOK, /* conversion successful */ + sourceExhausted, /* partial character in source, but hit end */ + targetExhausted, /* insuff. room in target for conversion */ + sourceIllegal /* source sequence is illegal/malformed */ +} ConversionResult; + +typedef enum { + strictConversion = 0, + lenientConversion +} ConversionFlags; + +/* This is for C++ and does no harm in C */ +#ifdef __cplusplus +extern "C" { +#endif + +ConversionResult ConvertUTF8toUTF16 ( + const UTF8** sourceStart, const UTF8* sourceEnd, + UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags); + +ConversionResult ConvertUTF16toUTF8 ( + const UTF16** sourceStart, const UTF16* sourceEnd, + UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags); + +ConversionResult ConvertUTF8toUTF32 ( + const UTF8** sourceStart, const UTF8* sourceEnd, + UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags); + +ConversionResult ConvertUTF32toUTF8 ( + const UTF32** sourceStart, const UTF32* sourceEnd, + UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags); + +ConversionResult ConvertUTF16toUTF32 ( + const UTF16** sourceStart, const UTF16* sourceEnd, + UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags); + +ConversionResult ConvertUTF32toUTF16 ( + const UTF32** sourceStart, const UTF32* sourceEnd, + UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags); + +ANTLR3_BOOLEAN isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd); + +#ifdef __cplusplus +} +#endif + +#endif + +/* --------------------------------------------------------------------- */ diff --git a/antlr-3.1.3/runtime/C/include/antlr3cyclicdfa.h b/antlr-3.1.3/runtime/C/include/antlr3cyclicdfa.h new file mode 100644 index 0000000..78708a0 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3cyclicdfa.h @@ -0,0 +1,97 @@ +/// Definition of a cyclic dfa structure such that it can be +/// initialized at compile time and have only a single +/// runtime function that can deal with all cyclic dfa +/// structures and show Java how it is done ;-) +/// +#ifndef ANTLR3_CYCLICDFA_H +#define ANTLR3_CYCLICDFA_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3baserecognizer.h> +#include <antlr3intstream.h> + +#ifdef __cplusplus +extern "C" { + +// If this header file is included as part of a generated recognizer that +// is being compiled as if it were C++, and this is Windows, then the const elements +// of the structure cause the C++ compiler to (rightly) point out that +// there can be no instantiation of the structure because it needs a constructor +// that can initialize the data, however these structures are not +// useful for C++ as they are pre-generated and static in the recognizer. +// So, we turn off those warnings, which are only at /W4 anyway. +// +#ifdef ANTLR3_WINDOWS +#pragma warning (push) +#pragma warning (disable : 4510) +#pragma warning (disable : 4512) +#pragma warning (disable : 4610) +#endif +#endif + +typedef struct ANTLR3_CYCLIC_DFA_struct +{ + /// Decision number that a particular static structure + /// represents. + /// + const ANTLR3_INT32 decisionNumber; + + /// What this decision represents + /// + const pANTLR3_UCHAR description; + + ANTLR3_INT32 (*specialStateTransition) (void * ctx, pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM is, struct ANTLR3_CYCLIC_DFA_struct * dfa, ANTLR3_INT32 s); + + ANTLR3_INT32 (*specialTransition) (void * ctx, pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM is, struct ANTLR3_CYCLIC_DFA_struct * dfa, ANTLR3_INT32 s); + + ANTLR3_INT32 (*predict) (void * ctx, pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM is, struct ANTLR3_CYCLIC_DFA_struct * dfa); + + const ANTLR3_INT32 * const eot; + const ANTLR3_INT32 * const eof; + const ANTLR3_INT32 * const min; + const ANTLR3_INT32 * const max; + const ANTLR3_INT32 * const accept; + const ANTLR3_INT32 * const special; + const ANTLR3_INT32 * const * const transition; + +} + ANTLR3_CYCLIC_DFA; + +typedef ANTLR3_INT32 (*CDFA_SPECIAL_FUNC) (void * , pANTLR3_BASE_RECOGNIZER , pANTLR3_INT_STREAM , struct ANTLR3_CYCLIC_DFA_struct * , ANTLR3_INT32); + +#ifdef __cplusplus +} +#ifdef ANTLR3_WINDOWS +#pragma warning (pop) +#endif +#endif + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3debugeventlistener.h b/antlr-3.1.3/runtime/C/include/antlr3debugeventlistener.h new file mode 100644 index 0000000..c9cd6ce --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3debugeventlistener.h @@ -0,0 +1,398 @@ +/** + * \file + * The definition of all debugging events that a recognizer can trigger. + * + * \remark + * From the java implementation by Terence Parr... + * I did not create a separate AST debugging interface as it would create + * lots of extra classes and DebugParser has a dbg var defined, which makes + * it hard to change to ASTDebugEventListener. I looked hard at this issue + * and it is easier to understand as one monolithic event interface for all + * possible events. Hopefully, adding ST debugging stuff won't be bad. Leave + * for future. 4/26/2006. + */ + +#ifndef ANTLR3_DEBUG_EVENT_LISTENER_H +#define ANTLR3_DEBUG_EVENT_LISTENER_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3basetree.h> +#include <antlr3commontoken.h> + + +/// Default debugging port +/// +#define DEFAULT_DEBUGGER_PORT 0xBFCC; + +#ifdef __cplusplus +extern "C" { +#endif + +/** The ANTLR3 debugging interface for communicating with ANLTR Works. Function comments + * mostly taken from the Java version. + */ +typedef struct ANTLR3_DEBUG_EVENT_LISTENER_struct +{ + /// The port number which the debug listener should listen on for a connection + /// + ANTLR3_UINT32 port; + + /// The socket structure we receive after a successful accept on the serverSocket + /// + SOCKET socket; + + /** The version of the debugging protocol supported by the providing + * instance of the debug event listener. + */ + int PROTOCOL_VERSION; + + /// The name of the grammar file that we are debugging + /// + pANTLR3_STRING grammarFileName; + + /// Indicates whether we have already connected or not + /// + ANTLR3_BOOLEAN initialized; + + /// Used to serialize the values of any particular token we need to + /// send back to the debugger. + /// + pANTLR3_STRING tokenString; + + + /// Allows the debug event system to access the adapter in use + /// by the recognizer, if this is a tree parser of some sort. + /// + pANTLR3_BASE_TREE_ADAPTOR adaptor; + + /// Wait for a connection from the debugger and initiate the + /// debugging session. + /// + ANTLR3_BOOLEAN (*handshake) (pANTLR3_DEBUG_EVENT_LISTENER delboy); + + /** The parser has just entered a rule. No decision has been made about + * which alt is predicted. This is fired AFTER init actions have been + * executed. Attributes are defined and available etc... + */ + void (*enterRule) (pANTLR3_DEBUG_EVENT_LISTENER delboy, const char * grammarFileName, const char * ruleName); + + /** Because rules can have lots of alternatives, it is very useful to + * know which alt you are entering. This is 1..n for n alts. + */ + void (*enterAlt) (pANTLR3_DEBUG_EVENT_LISTENER delboy, int alt); + + /** This is the last thing executed before leaving a rule. It is + * executed even if an exception is thrown. This is triggered after + * error reporting and recovery have occurred (unless the exception is + * not caught in this rule). This implies an "exitAlt" event. + */ + void (*exitRule) (pANTLR3_DEBUG_EVENT_LISTENER delboy, const char * grammarFileName, const char * ruleName); + + /** Track entry into any (...) subrule other EBNF construct + */ + void (*enterSubRule) (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber); + + void (*exitSubRule) (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber); + + /** Every decision, fixed k or arbitrary, has an enter/exit event + * so that a GUI can easily track what LT/consume events are + * associated with prediction. You will see a single enter/exit + * subrule but multiple enter/exit decision events, one for each + * loop iteration. + */ + void (*enterDecision) (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber); + + void (*exitDecision) (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber); + + /** An input token was consumed; matched by any kind of element. + * Trigger after the token was matched by things like match(), matchAny(). + */ + void (*consumeToken) (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_COMMON_TOKEN t); + + /** An off-channel input token was consumed. + * Trigger after the token was matched by things like match(), matchAny(). + * (unless of course the hidden token is first stuff in the input stream). + */ + void (*consumeHiddenToken) (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_COMMON_TOKEN t); + + /** Somebody (anybody) looked ahead. Note that this actually gets + * triggered by both LA and LT calls. The debugger will want to know + * which Token object was examined. Like consumeToken, this indicates + * what token was seen at that depth. A remote debugger cannot look + * ahead into a file it doesn't have so LT events must pass the token + * even if the info is redundant. + */ + void (*LT) (pANTLR3_DEBUG_EVENT_LISTENER delboy, int i, pANTLR3_COMMON_TOKEN t); + + /** The parser is going to look arbitrarily ahead; mark this location, + * the token stream's marker is sent in case you need it. + */ + void (*mark) (pANTLR3_DEBUG_EVENT_LISTENER delboy, ANTLR3_MARKER marker); + + /** After an arbitrarily long lookahead as with a cyclic DFA (or with + * any backtrack), this informs the debugger that stream should be + * rewound to the position associated with marker. + */ + void (*rewind) (pANTLR3_DEBUG_EVENT_LISTENER delboy, ANTLR3_MARKER marker); + + /** Rewind to the input position of the last marker. + * Used currently only after a cyclic DFA and just + * before starting a sem/syn predicate to get the + * input position back to the start of the decision. + * Do not "pop" the marker off the state. mark(i) + * and rewind(i) should balance still. + */ + void (*rewindLast) (pANTLR3_DEBUG_EVENT_LISTENER delboy); + + void (*beginBacktrack) (pANTLR3_DEBUG_EVENT_LISTENER delboy, int level); + + void (*endBacktrack) (pANTLR3_DEBUG_EVENT_LISTENER delboy, int level, ANTLR3_BOOLEAN successful); + + /** To watch a parser move through the grammar, the parser needs to + * inform the debugger what line/charPos it is passing in the grammar. + * For now, this does not know how to switch from one grammar to the + * other and back for island grammars etc... + * + * This should also allow breakpoints because the debugger can stop + * the parser whenever it hits this line/pos. + */ + void (*location) (pANTLR3_DEBUG_EVENT_LISTENER delboy, int line, int pos); + + /** A recognition exception occurred such as NoViableAltException. I made + * this a generic event so that I can alter the exception hierarchy later + * without having to alter all the debug objects. + * + * Upon error, the stack of enter rule/subrule must be properly unwound. + * If no viable alt occurs it is within an enter/exit decision, which + * also must be rewound. Even the rewind for each mark must be unwound. + * In the Java target this is pretty easy using try/finally, if a bit + * ugly in the generated code. The rewind is generated in DFA.predict() + * actually so no code needs to be generated for that. For languages + * w/o this "finally" feature (C++?), the target implementor will have + * to build an event stack or something. + * + * Across a socket for remote debugging, only the RecognitionException + * data fields are transmitted. The token object or whatever that + * caused the problem was the last object referenced by LT. The + * immediately preceding LT event should hold the unexpected Token or + * char. + * + * Here is a sample event trace for grammar: + * + * b : C ({;}A|B) // {;} is there to prevent A|B becoming a set + * | D + * ; + * + * The sequence for this rule (with no viable alt in the subrule) for + * input 'c c' (there are 3 tokens) is: + * + * commence + * LT(1) + * enterRule b + * location 7 1 + * enter decision 3 + * LT(1) + * exit decision 3 + * enterAlt1 + * location 7 5 + * LT(1) + * consumeToken [c/<4>,1:0] + * location 7 7 + * enterSubRule 2 + * enter decision 2 + * LT(1) + * LT(1) + * recognitionException NoViableAltException 2 1 2 + * exit decision 2 + * exitSubRule 2 + * beginResync + * LT(1) + * consumeToken [c/<4>,1:1] + * LT(1) + * endResync + * LT(-1) + * exitRule b + * terminate + */ + void (*recognitionException) (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_EXCEPTION e); + + /** Indicates the recognizer is about to consume tokens to resynchronize + * the parser. Any consume events from here until the recovered event + * are not part of the parse--they are dead tokens. + */ + void (*beginResync) (pANTLR3_DEBUG_EVENT_LISTENER delboy); + + /** Indicates that the recognizer has finished consuming tokens in order + * to resynchronize. There may be multiple beginResync/endResync pairs + * before the recognizer comes out of errorRecovery mode (in which + * multiple errors are suppressed). This will be useful + * in a gui where you want to probably grey out tokens that are consumed + * but not matched to anything in grammar. Anything between + * a beginResync/endResync pair was tossed out by the parser. + */ + void (*endResync) (pANTLR3_DEBUG_EVENT_LISTENER delboy); + + /** A semantic predicate was evaluate with this result and action text + */ + void (*semanticPredicate) (pANTLR3_DEBUG_EVENT_LISTENER delboy, ANTLR3_BOOLEAN result, const char * predicate); + + /** Announce that parsing has begun. Not technically useful except for + * sending events over a socket. A GUI for example will launch a thread + * to connect and communicate with a remote parser. The thread will want + * to notify the GUI when a connection is made. ANTLR parsers + * trigger this upon entry to the first rule (the ruleLevel is used to + * figure this out). + */ + void (*commence) (pANTLR3_DEBUG_EVENT_LISTENER delboy); + + /** Parsing is over; successfully or not. Mostly useful for telling + * remote debugging listeners that it's time to quit. When the rule + * invocation level goes to zero at the end of a rule, we are done + * parsing. + */ + void (*terminate) (pANTLR3_DEBUG_EVENT_LISTENER delboy); + + /// Retrieve acknowledge response from the debugger. in fact this + /// response is never used at the moment. So we just read whatever + /// is in the socket buffer and throw it away. + /// + void (*ack) (pANTLR3_DEBUG_EVENT_LISTENER delboy); + + // T r e e P a r s i n g + + /** Input for a tree parser is an AST, but we know nothing for sure + * about a node except its type and text (obtained from the adaptor). + * This is the analog of the consumeToken method. The ID is usually + * the memory address of the node. + * If the type is UP or DOWN, then + * the ID is not really meaningful as it's fixed--there is + * just one UP node and one DOWN navigation node. + * + * Note that unlike the Java version, the node type of the C parsers + * is always fixed as pANTLR3_BASE_TREE because all such structures + * contain a super pointer to their parent, which is generally COMMON_TREE and within + * that there is a super pointer that can point to a user type that encapsulates it. + * Almost akin to saying that it is an interface pointer except we don't need to + * know what the interface is in full, just those bits that are the base. + * @param t + */ + void (*consumeNode) (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t); + + /** The tree parser looked ahead. If the type is UP or DOWN, + * then the ID is not really meaningful as it's fixed--there is + * just one UP node and one DOWN navigation node. + */ + void (*LTT) (pANTLR3_DEBUG_EVENT_LISTENER delboy, int i, pANTLR3_BASE_TREE t); + + + // A S T E v e n t s + + /** A nil was created (even nil nodes have a unique ID... + * they are not "null" per se). As of 4/28/2006, this + * seems to be uniquely triggered when starting a new subtree + * such as when entering a subrule in automatic mode and when + * building a tree in rewrite mode. + * + * If you are receiving this event over a socket via + * RemoteDebugEventSocketListener then only t.ID is set. + */ + void (*nilNode) (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t); + + /** If a syntax error occurs, recognizers bracket the error + * with an error node if they are building ASTs. This event + * notifies the listener that this is the case + */ + void (*errorNode) (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t); + + /** Announce a new node built from token elements such as type etc... + * + * If you are receiving this event over a socket via + * RemoteDebugEventSocketListener then only t.ID, type, text are + * set. + */ + void (*createNode) (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t); + + /** Announce a new node built from an existing token. + * + * If you are receiving this event over a socket via + * RemoteDebugEventSocketListener then only node.ID and token.tokenIndex + * are set. + */ + void (*createNodeTok) (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE node, pANTLR3_COMMON_TOKEN token); + + /** Make a node the new root of an existing root. See + * + * Note: the newRootID parameter is possibly different + * than the TreeAdaptor.becomeRoot() newRoot parameter. + * In our case, it will always be the result of calling + * TreeAdaptor.becomeRoot() and not root_n or whatever. + * + * The listener should assume that this event occurs + * only when the current subrule (or rule) subtree is + * being reset to newRootID. + * + * If you are receiving this event over a socket via + * RemoteDebugEventSocketListener then only IDs are set. + * + * @see org.antlr.runtime.tree.TreeAdaptor.becomeRoot() + */ + void (*becomeRoot) (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE newRoot, pANTLR3_BASE_TREE oldRoot); + + /** Make childID a child of rootID. + * + * If you are receiving this event over a socket via + * RemoteDebugEventSocketListener then only IDs are set. + * + * @see org.antlr.runtime.tree.TreeAdaptor.addChild() + */ + void (*addChild) (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE root, pANTLR3_BASE_TREE child); + + /** Set the token start/stop token index for a subtree root or node. + * + * If you are receiving this event over a socket via + * RemoteDebugEventSocketListener then only t.ID is set. + */ + void (*setTokenBoundaries) (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t, ANTLR3_MARKER tokenStartIndex, ANTLR3_MARKER tokenStopIndex); + + /// Free up the resources allocated to this structure + /// + void (*free) (pANTLR3_DEBUG_EVENT_LISTENER delboy); + +} + ANTLR3_DEBUG_EVENT_LISTENER; + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/antlr-3.1.3/runtime/C/include/antlr3defs.h b/antlr-3.1.3/runtime/C/include/antlr3defs.h new file mode 100644 index 0000000..c970348 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3defs.h @@ -0,0 +1,606 @@ +/** \file + * Basic type and constant definitions for ANTLR3 Runtime. + */ +#ifndef _ANTLR3DEFS_H +#define _ANTLR3DEFS_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +/* Following are for generated code, they are not referenced internally!!! + */ +#if !defined(ANTLR3_HUGE) && !defined(ANTLR3_AVERAGE) && !defined(ANTLR3_SMALL) +#define ANTLR3_AVERAGE +#endif + +#ifdef ANTLR3_HUGE +#ifndef ANTLR3_SIZE_HINT +#define ANTLR3_SIZE_HINT 2049 +#endif +#ifndef ANTLR3_LIST_SIZE_HINT +#define ANTLR3_LIST_SIZE_HINT 127 +#endif +#endif + +#ifdef ANTLR3_AVERAGE +#ifndef ANTLR3_SIZE_HINT +#define ANTLR3_SIZE_HINT 1025 +#define ANTLR3_LIST_SIZE_HINT 63 +#endif +#endif + +#ifdef ANTLR3_SMALL +#ifndef ANTLR3_SIZE_HINT +#define ANTLR3_SIZE_HINT 211 +#define ANTLR3_LIST_SIZE_HINT 31 +#endif +#endif + +/* Common definitions come first + */ +#include <antlr3errors.h> + +#define ANTLR3_ENCODING_LATIN1 0 +#define ANTLR3_ENCODING_UCS2 1 +#define ANTLR3_ENCODING_UTF8 2 +#define ANTLR3_ENCODING_UTF32 3 + +/* Work out what operating system/compiler this is. We just do this once + * here and use an internal symbol after this. + */ +#ifdef _WIN64 + +# ifndef ANTLR3_WINDOWS +# define ANTLR3_WINDOWS +# endif +# define ANTLR3_WIN64 +# define ANTLR3_USE_64BIT + +#else + +#ifdef _WIN32 +# ifndef ANTLR3_WINDOWS +# define ANTLR3_WINDOWS +# endif + +#define ANTLR3_WIN32 +#endif + +#endif + +#ifdef ANTLR3_WINDOWS + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif + +/* Allow VC 8 (vs2005) and above to use 'secure' versions of various functions such as sprintf + */ +#ifndef _CRT_SECURE_NO_DEPRECATE +#define _CRT_SECURE_NO_DEPRECATE +#endif + +#include <windows.h> +#include <stdlib.h> +#include <winsock.h> +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <stdarg.h> + +#define ANTLR3_API __declspec(dllexport) +#define ANTLR3_CDECL __cdecl +#define ANTLR3_FASTCALL __fastcall + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef __MINGW32__ +// Standard Windows types +// +typedef INT32 ANTLR3_CHAR, *pANTLR3_CHAR; +typedef UINT32 ANTLR3_UCHAR, *pANTLR3_UCHAR; + +typedef INT8 ANTLR3_INT8, *pANTLR3_INT8; +typedef INT16 ANTLR3_INT16, *pANTLR3_INT16; +typedef INT32 ANTLR3_INT32, *pANTLR3_INT32; +typedef INT64 ANTLR3_INT64, *pANTLR3_INT64; +typedef UINT8 ANTLR3_UINT8, *pANTLR3_UINT8; +typedef UINT16 ANTLR3_UINT16, *pANTLR3_UINT16; +typedef UINT32 ANTLR3_UINT32, *pANTLR3_UINT32; +typedef UINT64 ANTLR3_UINT64, *pANTLR3_UINT64; +typedef UINT64 ANTLR3_BITWORD, *pANTLR3_BITWORD; +typedef UINT8 ANTLR3_BOOLEAN, *pANTLR3_BOOLEAN; + +#else +// Mingw uses stdint.h and fails to define standard Microsoft typedefs +// such as UINT16, hence we must use stdint.h for Mingw. +// +#include <stdint.h> +typedef int32_t ANTLR3_CHAR, *pANTLR3_CHAR; +typedef uint32_t ANTLR3_UCHAR, *pANTLR3_UCHAR; + +typedef int8_t ANTLR3_INT8, *pANTLR3_INT8; +typedef int16_t ANTLR3_INT16, *pANTLR3_INT16; +typedef int32_t ANTLR3_INT32, *pANTLR3_INT32; +typedef int64_t ANTLR3_INT64, *pANTLR3_INT64; + +typedef uint8_t ANTLR3_UINT8, *pANTLR3_UINT8; +typedef uint16_t ANTLR3_UINT16, *pANTLR3_UINT16; +typedef uint32_t ANTLR3_UINT32, *pANTLR3_UINT32; +typedef uint64_t ANTLR3_UINT64, *pANTLR3_UINT64; +typedef uint64_t ANTLR3_BITWORD, *pANTLR3_BITWORD; + +typedef uint8_t ANTLR3_BOOLEAN, *pANTLR3_BOOLEAN; + +#endif + + + +#define ANTLR3_UINT64_LIT(lit) lit##ULL + +#define ANTLR3_INLINE __inline + +typedef FILE * ANTLR3_FDSC; +typedef struct stat ANTLR3_FSTAT_STRUCT; + +#ifdef ANTLR3_USE_64BIT +#define ANTLR3_FUNC_PTR(ptr) (void *)((ANTLR3_UINT64)(ptr)) +#define ANTLR3_UINT64_CAST(ptr) (ANTLR3_UINT64)(ptr)) +#define ANTLR3_UINT32_CAST(ptr) (ANTLR3_UINT32)((ANTLR3_UINT64)(ptr)) +typedef ANTLR3_INT64 ANTLR3_MARKER; +typedef ANTLR3_UINT64 ANTLR3_INTKEY; +#else +#define ANTLR3_FUNC_PTR(ptr) (void *)((ANTLR3_UINT32)(ptr)) +#define ANTLR3_UINT64_CAST(ptr) (ANTLR3_UINT64)((ANTLR3_UINT32)(ptr)) +#define ANTLR3_UINT32_CAST(ptr) (ANTLR3_UINT32)(ptr) +typedef ANTLR3_INT32 ANTLR3_MARKER; +typedef ANTLR3_UINT32 ANTLR3_INTKEY; +#endif + +#ifdef ANTLR3_WIN32 +#endif + +#ifdef ANTLR3_WIN64 +#endif + + +typedef int ANTLR3_SALENT; // Type used for size of accept structure +typedef struct sockaddr_in ANTLR3_SOCKADDRT, * pANTLR3_SOCKADDRT; // Type used for socket address declaration +typedef struct sockaddr ANTLR3_SOCKADDRC, * pANTLR3_SOCKADDRC; // Type used for cast on accept() + +#define ANTLR3_CLOSESOCKET closesocket + +#ifdef __cplusplus +} +#endif + +/* Warnings that are over-zealous such as complaining about strdup, we + * can turn off. + */ + +/* Don't complain about "deprecated" functions such as strdup + */ +#pragma warning( disable : 4996 ) + +#else + +/* Include configure generated header file + */ +#include <antlr3config.h> + +#include <stdio.h> + +#if HAVE_STDINT_H +# include <stdint.h> +#endif + +#if HAVE_SYS_TYPES_H +# include <sys/types.h> +#endif + +#if HAVE_SYS_STAT_H +# include <sys/stat.h> +#endif + +#if STDC_HEADERS +# include <stdlib.h> +# include <stddef.h> +# include <stdarg.h> +#else +# if HAVE_STDLIB_H +# include <stdlib.h> +# endif +# if HAVE_STDARG_H +# include <stdarg.h> +# endif +#endif + +#if HAVE_STRING_H +# if !STDC_HEADERS && HAVE_MEMORY_H +# include <memory.h> +# endif +# include <string.h> +#endif + +#if HAVE_STRINGS_H +# include <strings.h> +#endif + +#if HAVE_INTTYPES_H +# include <inttypes.h> +#endif + +#if HAVE_UNISTD_H +# include <unistd.h> +#endif + +#ifdef HAVE_NETINET_IN_H +#include <netinet/in.h> +#endif + +#ifdef HAVE_SOCKET_H +# include <socket.h> +#else +# if HAVE_SYS_SOCKET_H +# include <sys/socket.h> +# endif +#endif + +#ifdef HAVE_NETINET_TCP_H +#include <netinet/tcp.h> +#endif + +#ifdef HAVE_ARPA_NAMESER_H +#include <arpa/nameser.h> /* DNS HEADER struct */ +#endif + +#ifdef HAVE_NETDB_H +#include <netdb.h> +#endif + + +#ifdef HAVE_SYS_RESOLVE_H +#include <sys/resolv.h> +#endif + +#ifdef HAVE_RESOLVE_H +#include <resolv.h> +#endif + + +#ifdef HAVE_MALLOC_H +# include <malloc.h> +#else +# ifdef HAVE_SYS_MALLOC_H +# include <sys/malloc.h> +# endif +#endif + +#ifdef HAVE_CTYPE_H +# include <ctype.h> +#endif + +/* Some platforms define a macro, index() in string.h. AIX is + * one of these for instance. We must get rid of that definition + * as we use ->index all over the place. defining macros like this in system header + * files is a really bad idea, but I doubt that IBM will listen to me ;-) + */ +#ifdef index +#undef index +#endif + +#define _stat stat + +// SOCKET not defined on Unix +// +typedef int SOCKET; + +#define ANTLR3_API +#define ANTLR3_CDECL +#define ANTLR3_FASTCALL + +#ifdef __hpux + + // HPUX is always different usually for no good reason. Tru64 should have kicked it + // into touch and everyone knows it ;-) + // + typedef struct sockaddr_in ANTLR3_SOCKADDRT, * pANTLR3_SOCKADDRT; // Type used for socket address declaration + typedef void * pANTLR3_SOCKADDRC; // Type used for cast on accept() + typedef int ANTLR3_SALENT; + +#else + +# if defined(_AIX) || __GNUC__ > 3 + + typedef socklen_t ANTLR3_SALENT; + +# else + + typedef size_t ANTLR3_SALENT; + +# endif + + typedef struct sockaddr_in ANTLR3_SOCKADDRT, * pANTLR3_SOCKADDRT; // Type used for socket address declaration + typedef struct sockaddr * pANTLR3_SOCKADDRC; // Type used for cast on accept() + +#endif + +#define INVALID_SOCKET ((SOCKET)-1) +#define ANTLR3_CLOSESOCKET close + +#ifdef __cplusplus +extern "C" { +#endif + +/* Inherit type definitions for autoconf + */ +typedef int32_t ANTLR3_CHAR, *pANTLR3_CHAR; +typedef uint32_t ANTLR3_UCHAR, *pANTLR3_UCHAR; + +typedef int8_t ANTLR3_INT8, *pANTLR3_INT8; +typedef int16_t ANTLR3_INT16, *pANTLR3_INT16; +typedef int32_t ANTLR3_INT32, *pANTLR3_INT32; +typedef int64_t ANTLR3_INT64, *pANTLR3_INT64; + +typedef uint8_t ANTLR3_UINT8, *pANTLR3_UINT8; +typedef uint16_t ANTLR3_UINT16, *pANTLR3_UINT16; +typedef uint32_t ANTLR3_UINT32, *pANTLR3_UINT32; +typedef uint64_t ANTLR3_UINT64, *pANTLR3_UINT64; +typedef uint64_t ANTLR3_BITWORD, *pANTLR3_BITWORD; + +typedef uint32_t ANTLR3_BOOLEAN, *pANTLR3_BOOLEAN; + +#define ANTLR3_INLINE inline +#define ANTLR3_API + +typedef FILE * ANTLR3_FDSC; +typedef struct stat ANTLR3_FSTAT_STRUCT; + +#ifdef ANTLR3_USE_64BIT +#define ANTLR3_FUNC_PTR(ptr) (void *)((ANTLR3_UINT64)(ptr)) +#define ANTLR3_UINT64_CAST(ptr) (ANTLR3_UINT64)(ptr)) +#define ANTLR3_UINT32_CAST(ptr) (ANTLR3_UINT32)((ANTLR3_UINT64)(ptr)) +typedef ANTLR3_INT64 ANTLR3_MARKER; +typedef ANTLR3_UINT64 ANTLR3_INTKEY; +#else +#define ANTLR3_FUNC_PTR(ptr) (void *)((ANTLR3_UINT32)(ptr)) +#define ANTLR3_UINT64_CAST(ptr) (ANTLR3_UINT64)((ANTLR3_UINT32)(ptr)) +#define ANTLR3_UINT32_CAST(ptr) (ANTLR3_UINT32)(ptr) +typedef ANTLR3_INT32 ANTLR3_MARKER; +typedef ANTLR3_UINT32 ANTLR3_INTKEY; +#endif +#define ANTLR3_UINT64_LIT(lit) lit##ULL + +#ifdef __cplusplus +} +#endif + +#endif + +#ifdef ANTLR3_USE_64BIT +#define ANTLR3_TRIE_DEPTH 63 +#else +#define ANTLR3_TRIE_DEPTH 31 +#endif +/* Pre declare the typedefs for all the interfaces, then + * they can be inter-dependant and we will let the linker + * sort it out for us. + */ +#include <antlr3interfaces.h> + +// Include the unicode.org conversion library header. +// +#include <antlr3convertutf.h> + +/* Prototypes + */ +#ifndef ANTLR3_MALLOC +/// Default definition of ANTLR3_MALLOC. You can override this before including +/// antlr3.h if you wish to use your own implementation. +/// +#define ANTLR3_MALLOC(request) malloc ((size_t)(request)) +#endif + +#ifndef ANTLR3_CALLOC +/// Default definition of ANTLR3_CALLOC. You can override this before including +/// antlr3.h if you wish to use your own implementation. +/// +#define ANTLR3_CALLOC(numEl, elSize) calloc (numEl, (size_t)(elSize)) +#endif + +#ifndef ANTLR3_REALLOC +/// Default definition of ANTLR3_REALLOC. You can override this before including +/// antlr3.h if you wish to use your own implementation. +/// +#define ANTLR3_REALLOC(current, request) realloc ((void *)(current), (size_t)(request)) +#endif +#ifndef ANTLR3_FREE +/// Default definition of ANTLR3_FREE. You can override this before including +/// antlr3.h if you wish to use your own implementation. +/// +#define ANTLR3_FREE(ptr) free ((void *)(ptr)) +#endif +#ifndef ANTLR3_FREE_FUNC +/// Default definition of ANTLR3_FREE_FUNC . You can override this before including +/// antlr3.h if you wish to use your own implementation. +/// +#define ANTLR3_FREE_FUNC free +#endif +#ifndef ANTLR3_STRDUP +/// Default definition of ANTLR3_STRDUP. You can override this before including +/// antlr3.h if you wish to use your own implementation. +/// +#define ANTLR3_STRDUP(instr) (pANTLR3_UINT8)(strdup ((const char *)(instr))) +#endif +#ifndef ANTLR3_MEMCPY +/// Default definition of ANTLR3_MEMCPY. You can override this before including +/// antlr3.h if you wish to use your own implementation. +/// +#define ANTLR3_MEMCPY(target, source, size) memcpy((void *)(target), (const void *)(source), (size_t)(size)) +#endif +#ifndef ANTLR3_MEMMOVE +/// Default definition of ANTLR3_MEMMOVE. You can override this before including +/// antlr3.h if you wish to use your own implementation. +/// +#define ANTLR3_MEMMOVE(target, source, size) memmove((void *)(target), (const void *)(source), (size_t)(size)) +#endif +#ifndef ANTLR3_MEMSET +/// Default definition of ANTLR3_MEMSET. You can override this before including +/// antlr3.h if you wish to use your own implementation. +/// +#define ANTLR3_MEMSET(target, byte, size) memset((void *)(target), (int)(byte), (size_t)(size)) +#endif + +#ifndef ANTLR3_PRINTF +/// Default definition of printf, set this to something other than printf before including antlr3.h +/// if your system does not have a printf. Note that you can define this to be <code>//</code> +/// without harming the runtime. +/// +#define ANTLR3_PRINTF printf +#endif + +#ifndef ANTLR3_FPRINTF +/// Default definition of fprintf, set this to something other than fprintf before including antlr3.h +/// if your system does not have a fprintf. Note that you can define this to be <code>//</code> +/// without harming the runtime. +/// +#define ANTLR3_FPRINTF fprintf +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +ANTLR3_API pANTLR3_INT_TRIE antlr3IntTrieNew (ANTLR3_UINT32 depth); + +ANTLR3_API pANTLR3_BITSET antlr3BitsetNew (ANTLR3_UINT32 numBits); +ANTLR3_API pANTLR3_BITSET antlr3BitsetOf (ANTLR3_INT32 bit, ...); +ANTLR3_API pANTLR3_BITSET antlr3BitsetList (pANTLR3_HASH_TABLE list); +ANTLR3_API pANTLR3_BITSET antlr3BitsetCopy (pANTLR3_BITSET_LIST blist); +ANTLR3_API pANTLR3_BITSET antlr3BitsetLoad (pANTLR3_BITSET_LIST blist); +ANTLR3_API void antlr3BitsetSetAPI (pANTLR3_BITSET bitset); + + +ANTLR3_API pANTLR3_BASE_RECOGNIZER antlr3BaseRecognizerNew (ANTLR3_UINT32 type, ANTLR3_UINT32 sizeHint, pANTLR3_RECOGNIZER_SHARED_STATE state); +ANTLR3_API void antlr3RecognitionExceptionNew (pANTLR3_BASE_RECOGNIZER recognizer); +ANTLR3_API void antlr3MTExceptionNew (pANTLR3_BASE_RECOGNIZER recognizer); +ANTLR3_API void antlr3MTNExceptionNew (pANTLR3_BASE_RECOGNIZER recognizer); +ANTLR3_API pANTLR3_HASH_TABLE antlr3HashTableNew (ANTLR3_UINT32 sizeHint); +ANTLR3_API ANTLR3_UINT32 antlr3Hash (void * key, ANTLR3_UINT32 keylen); +ANTLR3_API pANTLR3_HASH_ENUM antlr3EnumNew (pANTLR3_HASH_TABLE table); +ANTLR3_API pANTLR3_LIST antlr3ListNew (ANTLR3_UINT32 sizeHint); +ANTLR3_API pANTLR3_VECTOR_FACTORY antlr3VectorFactoryNew (ANTLR3_UINT32 sizeHint); +ANTLR3_API pANTLR3_VECTOR antlr3VectorNew (ANTLR3_UINT32 sizeHint); +ANTLR3_API pANTLR3_STACK antlr3StackNew (ANTLR3_UINT32 sizeHint); +ANTLR3_API void antlr3SetVectorApi (pANTLR3_VECTOR vector, ANTLR3_UINT32 sizeHint); +ANTLR3_API ANTLR3_UCHAR antlr3c8toAntlrc (ANTLR3_INT8 inc); + +ANTLR3_API pANTLR3_EXCEPTION antlr3ExceptionNew (ANTLR3_UINT32 exception, void * name, void * message, ANTLR3_BOOLEAN freeMessage); + +ANTLR3_API pANTLR3_INPUT_STREAM antlr3AsciiFileStreamNew (pANTLR3_UINT8 fileName); + +ANTLR3_API pANTLR3_INPUT_STREAM antlr3NewAsciiStringInPlaceStream (pANTLR3_UINT8 inString, ANTLR3_UINT32 size, pANTLR3_UINT8 name); +ANTLR3_API pANTLR3_INPUT_STREAM antlr3NewUCS2StringInPlaceStream (pANTLR3_UINT16 inString, ANTLR3_UINT32 size, pANTLR3_UINT16 name); +ANTLR3_API pANTLR3_INPUT_STREAM antlr3NewAsciiStringCopyStream (pANTLR3_UINT8 inString, ANTLR3_UINT32 size, pANTLR3_UINT8 name); + +ANTLR3_API pANTLR3_INT_STREAM antlr3IntStreamNew (void); + +ANTLR3_API pANTLR3_STRING_FACTORY antlr3StringFactoryNew (void); +ANTLR3_API pANTLR3_STRING_FACTORY antlr3UCS2StringFactoryNew (void); + +ANTLR3_API pANTLR3_COMMON_TOKEN antlr3CommonTokenNew (ANTLR3_UINT32 ttype); +ANTLR3_API pANTLR3_TOKEN_FACTORY antlr3TokenFactoryNew (pANTLR3_INPUT_STREAM input); +ANTLR3_API void antlr3SetTokenAPI (pANTLR3_COMMON_TOKEN token); + +ANTLR3_API pANTLR3_LEXER antlr3LexerNewStream (ANTLR3_UINT32 sizeHint, pANTLR3_INPUT_STREAM input, pANTLR3_RECOGNIZER_SHARED_STATE state); +ANTLR3_API pANTLR3_LEXER antlr3LexerNew (ANTLR3_UINT32 sizeHint, pANTLR3_RECOGNIZER_SHARED_STATE state); +ANTLR3_API pANTLR3_PARSER antlr3ParserNewStreamDbg (ANTLR3_UINT32 sizeHint, pANTLR3_TOKEN_STREAM tstream, pANTLR3_DEBUG_EVENT_LISTENER dbg, pANTLR3_RECOGNIZER_SHARED_STATE state); +ANTLR3_API pANTLR3_PARSER antlr3ParserNewStream (ANTLR3_UINT32 sizeHint, pANTLR3_TOKEN_STREAM tstream, pANTLR3_RECOGNIZER_SHARED_STATE state); +ANTLR3_API pANTLR3_PARSER antlr3ParserNew (ANTLR3_UINT32 sizeHint, pANTLR3_RECOGNIZER_SHARED_STATE state); + +ANTLR3_API pANTLR3_COMMON_TOKEN_STREAM antlr3CommonTokenStreamSourceNew (ANTLR3_UINT32 hint, pANTLR3_TOKEN_SOURCE source); +ANTLR3_API pANTLR3_COMMON_TOKEN_STREAM antlr3CommonTokenStreamNew (ANTLR3_UINT32 hint); +ANTLR3_API pANTLR3_COMMON_TOKEN_STREAM antlr3CommonTokenDebugStreamSourceNew + (ANTLR3_UINT32 hint, pANTLR3_TOKEN_SOURCE source, pANTLR3_DEBUG_EVENT_LISTENER debugger); + +ANTLR3_API pANTLR3_BASE_TREE_ADAPTOR ANTLR3_TREE_ADAPTORNew (pANTLR3_STRING_FACTORY strFactory); +ANTLR3_API pANTLR3_BASE_TREE_ADAPTOR ANTLR3_TREE_ADAPTORDebugNew (pANTLR3_STRING_FACTORY strFactory, pANTLR3_DEBUG_EVENT_LISTENER debugger); +ANTLR3_API pANTLR3_COMMON_TREE antlr3CommonTreeNew (void); +ANTLR3_API pANTLR3_COMMON_TREE antlr3CommonTreeNewFromTree (pANTLR3_COMMON_TREE tree); +ANTLR3_API pANTLR3_COMMON_TREE antlr3CommonTreeNewFromToken (pANTLR3_COMMON_TOKEN tree); +ANTLR3_API pANTLR3_ARBORETUM antlr3ArboretumNew (pANTLR3_STRING_FACTORY factory); +ANTLR3_API void antlr3SetCTAPI (pANTLR3_COMMON_TREE tree); +ANTLR3_API pANTLR3_BASE_TREE antlr3BaseTreeNew (pANTLR3_BASE_TREE tree); + +ANTLR3_API void antlr3BaseTreeAdaptorInit (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_DEBUG_EVENT_LISTENER debugger); + +ANTLR3_API pANTLR3_TREE_PARSER antlr3TreeParserNewStream (ANTLR3_UINT32 sizeHint, pANTLR3_COMMON_TREE_NODE_STREAM ctnstream, pANTLR3_RECOGNIZER_SHARED_STATE state); + +ANTLR3_API ANTLR3_INT32 antlr3dfaspecialTransition (void * ctx, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_INT_STREAM is, pANTLR3_CYCLIC_DFA dfa, ANTLR3_INT32 s); +ANTLR3_API ANTLR3_INT32 antlr3dfaspecialStateTransition (void * ctx, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_INT_STREAM is, pANTLR3_CYCLIC_DFA dfa, ANTLR3_INT32 s); +ANTLR3_API ANTLR3_INT32 antlr3dfapredict (void * ctx, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_INT_STREAM is, pANTLR3_CYCLIC_DFA cdfa); + +ANTLR3_API pANTLR3_COMMON_TREE_NODE_STREAM antlr3CommonTreeNodeStreamNewTree (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 hint); +ANTLR3_API pANTLR3_COMMON_TREE_NODE_STREAM antlr3CommonTreeNodeStreamNew (pANTLR3_STRING_FACTORY strFactory, ANTLR3_UINT32 hint); +ANTLR3_API pANTLR3_COMMON_TREE_NODE_STREAM antlr3UnbufTreeNodeStreamNewTree (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 hint); +ANTLR3_API pANTLR3_COMMON_TREE_NODE_STREAM antlr3UnbufTreeNodeStreamNew (pANTLR3_STRING_FACTORY strFactory, ANTLR3_UINT32 hint); +ANTLR3_API pANTLR3_COMMON_TREE_NODE_STREAM antlr3CommonTreeNodeStreamNewStream (pANTLR3_COMMON_TREE_NODE_STREAM inStream); +ANTLR3_API pANTLR3_TREE_NODE_STREAM antlr3TreeNodeStreamNew (); +ANTLR3_API void fillBufferExt (pANTLR3_COMMON_TOKEN_STREAM tokenStream); + +ANTLR3_API pANTLR3_REWRITE_RULE_TOKEN_STREAM + antlr3RewriteRuleTOKENStreamNewAE (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description); +ANTLR3_API pANTLR3_REWRITE_RULE_TOKEN_STREAM + antlr3RewriteRuleTOKENStreamNewAEE (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description, void * oneElement); +ANTLR3_API pANTLR3_REWRITE_RULE_TOKEN_STREAM + antlr3RewriteRuleTOKENStreamNewAEV (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description, pANTLR3_VECTOR vector); + +ANTLR3_API pANTLR3_REWRITE_RULE_NODE_STREAM + antlr3RewriteRuleNODEStreamNewAE (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description); +ANTLR3_API pANTLR3_REWRITE_RULE_NODE_STREAM + antlr3RewriteRuleNODEStreamNewAEE (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description, void * oneElement); +ANTLR3_API pANTLR3_REWRITE_RULE_NODE_STREAM + antlr3RewriteRuleNODEStreamNewAEV (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description, pANTLR3_VECTOR vector); + +ANTLR3_API pANTLR3_REWRITE_RULE_SUBTREE_STREAM + antlr3RewriteRuleSubtreeStreamNewAE (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description); +ANTLR3_API pANTLR3_REWRITE_RULE_SUBTREE_STREAM + antlr3RewriteRuleSubtreeStreamNewAEE(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description, void * oneElement); +ANTLR3_API pANTLR3_REWRITE_RULE_SUBTREE_STREAM + antlr3RewriteRuleSubtreeStreamNewAEV(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description, pANTLR3_VECTOR vector); + +ANTLR3_API pANTLR3_DEBUG_EVENT_LISTENER antlr3DebugListenerNew (); + +#ifdef __cplusplus +} +#endif + +#endif /* _ANTLR3DEFS_H */ diff --git a/antlr-3.1.3/runtime/C/include/antlr3encodings.h b/antlr-3.1.3/runtime/C/include/antlr3encodings.h new file mode 100644 index 0000000..f706735 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3encodings.h @@ -0,0 +1,38 @@ +#ifndef _ANTLR3_ENCODINGS_H +#define _ANTLR3_ENCODINGS_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> + + + +#endif + diff --git a/antlr-3.1.3/runtime/C/include/antlr3errors.h b/antlr-3.1.3/runtime/C/include/antlr3errors.h new file mode 100644 index 0000000..4419ee3 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3errors.h @@ -0,0 +1,53 @@ +#ifndef _ANTLR3ERRORS_H +#define _ANTLR3ERRORS_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#define ANTLR3_SUCCESS 0 +#define ANTLR3_FAIL 1 + +#define ANTLR3_TRUE 1 +#define ANTLR3_FALSE 0 + +/** Indicates end of character stream and is an invalid Unicode code point. */ +#define ANTLR3_CHARSTREAM_EOF 0xFFFFFFFF + +/** Indicates memoizing on a rule failed. + */ +#define MEMO_RULE_FAILED 0xFFFFFFFE +#define MEMO_RULE_UNKNOWN 0xFFFFFFFF + + +#define ANTLR3_ERR_BASE 0 +#define ANTLR3_ERR_NOMEM (ANTLR3_ERR_BASE + 1) +#define ANTLR3_ERR_NOFILE (ANTLR3_ERR_BASE + 2) +#define ANTLR3_ERR_HASHDUP (ANTLR3_ERR_BASE + 3) + +#endif /* _ANTLR3ERRORS_H */ diff --git a/antlr-3.1.3/runtime/C/include/antlr3exception.h b/antlr-3.1.3/runtime/C/include/antlr3exception.h new file mode 100644 index 0000000..a135126 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3exception.h @@ -0,0 +1,218 @@ +/** \file + * Contains the definition of a basic ANTLR3 exception structure created + * by a recognizer when errors are found/predicted. + */ +#ifndef _ANTLR3_EXCEPTION_H +#define _ANTLR3_EXCEPTION_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> + +/** Indicates that the recognizer received a token + * in the input that was not predicted. + */ +#define ANTLR3_RECOGNITION_EXCEPTION 1 + +/** Name of exception #ANTLR3_RECOGNITION_EXCEPTION + */ +#define ANTLR3_RECOGNITION_EX_NAME "Recognition Exception" + +/** Indicates that the recognizer was expecting one token and found a + * a different one. + */ +#define ANTLR3_MISMATCHED_TOKEN_EXCEPTION 2 + +/** Name of #ANTLR3_MISMATCHED_TOKEN_EXCEPTION + */ +#define ANTLR3_MISMATCHED_EX_NAME "Mismatched Token Exception" + +/** Recognizer could not find a valid alternative from the input + */ +#define ANTLR3_NO_VIABLE_ALT_EXCEPTION 3 + +/** Name of #ANTLR3_NO_VIABLE_ALT_EXCEPTION + */ +#define ANTLR3_NO_VIABLE_ALT_NAME "No Viable Alt" + +/* Character in a set was not found + */ +#define ANTLR3_MISMATCHED_SET_EXCEPTION 4 + +/* Name of #ANTLR3_MISMATCHED_SET_EXCEPTION + */ +#define ANTLR3_MISMATCHED_SET_NAME "Mismatched set" + +/* A rule predicting at least n elements found less than that, + * such as: WS: " "+; + */ +#define ANTLR3_EARLY_EXIT_EXCEPTION 5 + +/* Name of #ANTLR3_EARLY_EXIT_EXCEPTION + */ +#define ANTLR3_EARLY_EXIT_NAME "Early exit" + +#define ANTLR3_FAILED_PREDICATE_EXCEPTION 6 +#define ANTLR3_FAILED_PREDICATE_NAME "Predicate failed!" + +#define ANTLR3_MISMATCHED_TREE_NODE_EXCEPTION 7 +#define ANTLR3_MISMATCHED_TREE_NODE_NAME "Mismatched tree node!" + +#define ANTLR3_REWRITE_EARLY_EXCEPTION 8 +#define ANTLR3_REWRITE_EARLY_EXCEPTION_NAME "Mismatched tree node!" + +#define ANTLR3_UNWANTED_TOKEN_EXCEPTION 9 +#define ANTLR3_UNWANTED_TOKEN_EXCEPTION_NAME "Extraneous token" + +#define ANTLR3_MISSING_TOKEN_EXCEPTION 10 +#define ANTLR3_MISSING_TOKEN_EXCEPTION_NAME "Missing token" + +#ifdef __cplusplus +extern "C" { +#endif + +/** Base structure for an ANTLR3 exception tracker + */ +typedef struct ANTLR3_EXCEPTION_struct +{ + /// Set to one of the exception type defines: + /// + /// - #ANTLR3_RECOGNITION_EXCEPTION + /// - #ANTLR3_MISMATCHED_TOKEN_EXCEPTION + /// - #ANTLR3_NO_VIABLE_ALT_EXCEPTION + /// - #ANTLR3_MISMATCHED_SET_EXCEPTION + /// - #ANTLR3_EARLY_EXIT_EXCEPTION + /// - #ANTLR3_FAILED_PREDICATE_EXCEPTION + /// - #ANTLR3_EARLY_EXIT_EXCEPTION + /// + ANTLR3_UINT32 type; + + /** The string name of the exception + */ + void * name; + + /** The printable message that goes with this exception, in your preferred + * encoding format. ANTLR just uses ASCII by default but you can ignore these + * messages or convert them to another format or whatever of course. They are + * really internal messages that you then decide how to print out in a form that + * the users of your product will understand, as they are unlikely to know what + * to do with "Recognition exception at: [[TOK_GERUND..... " ;-) + */ + void * message; + + /** Name of the file/input source for reporting. Note that this may be NULL!! + */ + pANTLR3_STRING streamName; + + /** If set to ANTLR3_TRUE, this indicates that the message element of this structure + * should be freed by calling ANTLR3_FREE() when the exception is destroyed. + */ + ANTLR3_BOOLEAN freeMessage; + + /** Indicates the index of the 'token' we were looking at when the + * exception occurred. + */ + ANTLR3_MARKER index; + + /** Indicates what the current token/tree was when the error occurred. Since not + * all input streams will be able to retrieve the nth token, we track it here + * instead. This is for parsers, and even tree parsers may set this. + */ + void * token; + + /** Indicates the token we were expecting to see next when the error occurred + */ + ANTLR3_UINT32 expecting; + + /** Indicates a set of tokens that we were expecting to see one of when the + * error occurred. It is a following bitset list, so you can use load it and use ->toIntList() on it + * to generate an array of integer tokens that it represents. + */ + pANTLR3_BITSET_LIST expectingSet; + + /** If this is a tree parser exception then the node is set to point to the node + * that caused the issue. + */ + void * node; + + /** The current character when an error occurred - for lexers. + */ + ANTLR3_UCHAR c; + + /** Track the line at which the error occurred in case this is + * generated from a lexer. We need to track this since the + * unexpected char doesn't carry the line info. + */ + ANTLR3_UINT32 line; + + /** Character position in the line where the error occurred. + */ + ANTLR3_INT32 charPositionInLine; + + /** decision number for NVE + */ + ANTLR3_UINT32 decisionNum; + + /** State for NVE + */ + ANTLR3_UINT32 state; + + /** Rule name for failed predicate exception + */ + void * ruleName; + + /** Pointer to the next exception in the chain (if any) + */ + struct ANTLR3_EXCEPTION_struct * nextException; + + /** Pointer to the input stream that this exception occurred in. + */ + pANTLR3_INT_STREAM input; + + /** Pointer for you, the programmer to add anything you like to an exception. + */ + void * custom; + + /** Pointer to a routine that is called to free the custom exception structure + * when the exception is destroyed. Set to NULL if nothing should be done. + */ + void (*freeCustom) (void * custom); + void (*print) (struct ANTLR3_EXCEPTION_struct * ex); + void (*freeEx) (struct ANTLR3_EXCEPTION_struct * ex); + +} + ANTLR3_EXCEPTION; + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3filestream.h b/antlr-3.1.3/runtime/C/include/antlr3filestream.h new file mode 100644 index 0000000..5576563 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3filestream.h @@ -0,0 +1,50 @@ +#ifndef _ANTLR3_FILESTREAM_H +#define _ANTLR3_FILESTREAM_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> + +#ifdef __cplusplus +extern "C" { +#endif + +ANTLR3_API ANTLR3_FDSC antlr3Fopen (pANTLR3_UINT8 filename, const char * mode); +ANTLR3_API void antlr3Fclose (ANTLR3_FDSC fd); + +ANTLR3_API ANTLR3_UINT32 antlr3Fsize (pANTLR3_UINT8 filename); +ANTLR3_API ANTLR3_UINT32 antlr3readAscii (pANTLR3_INPUT_STREAM input, pANTLR3_UINT8 fileName); +ANTLR3_API ANTLR3_UINT32 antlr3Fread (ANTLR3_FDSC fdsc, ANTLR3_UINT32 count, void * data); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3input.h b/antlr-3.1.3/runtime/C/include/antlr3input.h new file mode 100644 index 0000000..1ed37f7 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3input.h @@ -0,0 +1,270 @@ +/** \file + * Defines the basic structures used to manipulate character + * streams from any input source. The first implementation of + * this stream was ASCII 8 bit, but any character size and encoding + * can in theory be used, so long as they can return a 32 bit Integer + * representation of their characters amd efficiently mark and revert + * to specific offsets into their input streams. + */ +#ifndef _ANTLR3_INPUT_H +#define _ANTLR3_INPUT_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3string.h> +#include <antlr3commontoken.h> +#include <antlr3intstream.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/// Master context structure for an ANTLR3 C runtime based input stream. +/// \ingroup apistructures +/// +typedef struct ANTLR3_INPUT_STREAM_struct +{ + /** Interfaces that provide streams must all provide + * a generic ANTLR3_INT_STREAM interface and an ANTLR3_INPUT_STREAM + * is no different. + */ + pANTLR3_INT_STREAM istream; + + /** Whatever super structure is providing the INPUT stream needs a pointer to itself + * so that this can be passed back to it whenever the api functions + * are called back from this interface. + */ + void * super; + + /// Indicates the size, in 8 bit units, of a single character. Note that + /// the C runtime does not deal with surrogates and UTF8 directly as this would be + /// slow and complicated. Variable character width inputs are expected to be converted + /// into fixed width formats, so that would be a UTF32 format for anything that cannot + /// work with a UCS2 encoding, such as UTF-8. Generally you are best + /// working internally with 32 bit characters. + /// + ANTLR3_UINT8 charByteSize; + + /** Pointer the start of the input string, characters may be + * taken as offsets from here and in original input format encoding. + */ + void * data; + + /** Indicates if the data pointer was allocated by us, and so should be freed + * when the stream dies. + */ + int isAllocated; + + /** String factory for this input stream + */ + pANTLR3_STRING_FACTORY strFactory; + + + /** Pointer to the next character to be consumed from the input data + * This is cast to point at the encoding of the original file that + * was read by the functions installed as pointer in this input stream + * context instance at file/string/whatever load time. + */ + void * nextChar; + + /** Number of characters that can be consumed at this point in time. + * Mostly this is just what is left in the pre-read buffer, but if the + * input source is a stream such as a socket or something then we may + * call special read code to wait for more input. + */ + ANTLR3_UINT32 sizeBuf; + + /** The line number we are traversing in the input file. This gets incremented + * by a newline() call in the lexer grammar actions. + */ + ANTLR3_UINT32 line; + + /** Pointer into the input buffer where the current line + * started. + */ + void * currentLine; + + /** The offset within the current line of the current character + */ + ANTLR3_INT32 charPositionInLine; + + /** Tracks how deep mark() calls are nested + */ + ANTLR3_UINT32 markDepth; + + /** List of mark() points in the input stream + */ + pANTLR3_VECTOR markers; + + /** File name string, set to pointer to memory if + * you set it manually as it will be free()d + */ + pANTLR3_STRING fileName; + + /** File number, needs to be set manually to some file index of your devising. + */ + ANTLR3_UINT32 fileNo; + + /** Character that automatically causes an internal line count + * increment. + */ + ANTLR3_UCHAR newlineChar; + + /* API */ + + + /** Pointer to function that closes the input stream + */ + void (*close) (struct ANTLR3_INPUT_STREAM_struct * input); + void (*free) (struct ANTLR3_INPUT_STREAM_struct * input); + + /** Pointer to function that resets the input stream + */ + void (*reset) (struct ANTLR3_INPUT_STREAM_struct * input); + + /** + * Pinter to function that installs a version of LA that always + * returns upper case. Only valid for character streams and creates a case + * insensitive lexer if the lexer tokens are described in upper case. The + * tokens will preserve case in the token text. + */ + void (*setUcaseLA) (pANTLR3_INPUT_STREAM input, ANTLR3_BOOLEAN flag); + + /** Pointer to function to return input stream element at 1 based + * offset from nextChar. Same as _LA for char stream, but token + * streams etc. have one of these that does other stuff of course. + */ + void * (*_LT) (struct ANTLR3_INPUT_STREAM_struct * input, ANTLR3_INT32 lt); + + /** Pointer to function to return the total size of the input buffer. For streams + * this may be just the total we have available so far. This means of course that + * the input stream must be careful to accumulate enough input so that any backtracking + * can be satisfied. + */ + ANTLR3_UINT32 (*size) (struct ANTLR3_INPUT_STREAM_struct * input); + + /** Pointer to function to return a substring of the input stream. String is returned in allocated + * memory and is in same encoding as the input stream itself, NOT internal ANTLR3_UCHAR form. + */ + pANTLR3_STRING (*substr) (struct ANTLR3_INPUT_STREAM_struct * input, ANTLR3_MARKER start, ANTLR3_MARKER stop); + + /** Pointer to function to return the current line number in the input stream + */ + ANTLR3_UINT32 (*getLine) (struct ANTLR3_INPUT_STREAM_struct * input); + + /** Pointer to function to return the current line buffer in the input stream + * The pointer returned is directly into the input stream so you must copy + * it if you wish to manipulate it without damaging the input stream. Encoding + * is obviously in the same form as the input stream. + * \remark + * - Note taht this function wil lbe inaccurate if setLine is called as there + * is no way at the moment to position the input stream at a particular line + * number offset. + */ + void * (*getLineBuf) (struct ANTLR3_INPUT_STREAM_struct * input); + + /** Pointer to function to return the current offset in the current input stream line + */ + ANTLR3_UINT32 (*getCharPositionInLine) (struct ANTLR3_INPUT_STREAM_struct * input); + + /** Pointer to function to set the current line number in the input stream + */ + void (*setLine) (struct ANTLR3_INPUT_STREAM_struct * input, ANTLR3_UINT32 line); + + /** Pointer to function to set the current position in the current line. + */ + void (*setCharPositionInLine) (struct ANTLR3_INPUT_STREAM_struct * input, ANTLR3_UINT32 position); + + /** Pointer to function to override the default newline character that the input stream + * looks for to trigger the line and offset and line buffer recording information. + * \remark + * - By default the chracter '\n' will be instaleldas tehe newline trigger character. When this + * character is seen by the consume() function then the current line number is incremented and the + * current line offset is reset to 0. The Pointer for the line of input we are consuming + * is updated to point to the next character after this one in the input stream (which means it + * may become invlaid if the last newline character in the file is seen (so watch out). + * - If for some reason you do not want teh counters and pointesr to be restee, yu can set the + * chracter to some impossible charater such as '\0' or whatever. + * - This is a single character only, so choose the last chracter in a sequence of two or more. + * - This is only a simple aid to error reporting - if you have a complicated binary inptu structure + * it may not be adequate, but you can always override every function in the input stream with your + * own of course, and can even write your own complete input stream set if you like. + * - It is your responsiblity to set a valid cahracter for the input stream type. Ther is no point + * setting this to 0xFFFFFFFF if the input stream is 8 bit ASCII as this will just be truncated and never + * trigger as the comparison will be (INT32)0xFF == (INT32)0xFFFFFFFF + */ + void (*SetNewLineChar) (struct ANTLR3_INPUT_STREAM_struct * input, ANTLR3_UINT32 newlineChar); + +} + + ANTLR3_INPUT_STREAM; + + +/** \brief Structure for track lex input states as part of mark() + * and rewind() of lexer. + */ +typedef struct ANTLR3_LEX_STATE_struct +{ + /** Pointer to the next character to be consumed from the input data + * This is cast to point at the encoding of the original file that + * was read by the functions installed as pointer in this input stream + * context instance at file/string/whatever load time. + */ + void * nextChar; + + /** The line number we are traversing in the input file. This gets incremented + * by a newline() call in the lexer grammer actions. + */ + ANTLR3_UINT32 line; + + /** Pointer into the input buffer where the current line + * started. + */ + void * currentLine; + + /** The offset within the current line of the current character + */ + ANTLR3_INT32 charPositionInLine; + +} + ANTLR3_LEX_STATE; + + /* Prototypes + */ + void antlr3AsciiSetupStream (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 type); + void antlr3UCS2SetupStream (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 type); + void antlr3GenericSetupStream (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 type); + +#ifdef __cplusplus +} +#endif + +#endif /* _ANTLR3_INPUT_H */ diff --git a/antlr-3.1.3/runtime/C/include/antlr3interfaces.h b/antlr-3.1.3/runtime/C/include/antlr3interfaces.h new file mode 100644 index 0000000..efae932 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3interfaces.h @@ -0,0 +1,234 @@ +/** \file + * Declarations for all the antlr3 C runtime interfaces/classes. This + * allows the structures that define the interfaces to contain pointers to + * each other without trying to sort out the cyclic interdependencies that + * would otherwise result. + */ +#ifndef _ANTLR3_INTERFACES_H +#define _ANTLR3_INTERFACES_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct ANTLR3_INT_STREAM_struct *pANTLR3_INT_STREAM; + +/// Pointer to an instantiation of the 'class' #ANTLR3_BASE_RECOGNIZER +/// \ingroup ANTLR3_BASE_RECOGNIZER +/// +typedef struct ANTLR3_BASE_RECOGNIZER_struct *pANTLR3_BASE_RECOGNIZER; +/// Pointer to an instantiation of 'class' #ANTLR3_RECOGNIZER_SHARED_STATE +/// \ingroup ANTLR3_RECOGNIZER_SHARED_STATE +/// +typedef struct ANTLR3_RECOGNIZER_SHARED_STATE_struct *pANTLR3_RECOGNIZER_SHARED_STATE; + +/// Pointer to an instantiation of 'class' #ANTLR3_BITSET_LIST +/// \ingroup ANTLR3_BITSET_LIST +/// +typedef struct ANTLR3_BITSET_LIST_struct *pANTLR3_BITSET_LIST; + +/// Pointer to an instantiation of 'class' #ANTLR3_BITSET +/// \ingroup ANTLR3_BITSET +/// +typedef struct ANTLR3_BITSET_struct *pANTLR3_BITSET; + +/// Pointer to an instantiation of 'class' #ANTLR3_TOKEN_FACTORY +/// \ingroup ANTLR3_TOKEN_FACTORY +/// +typedef struct ANTLR3_TOKEN_FACTORY_struct *pANTLR3_TOKEN_FACTORY; +/// Pointer to an instantiation of 'class' #ANTLR3_COMMON_TOKEN +/// \ingroup ANTLR3_COMMON_TOKEN +/// +typedef struct ANTLR3_COMMON_TOKEN_struct *pANTLR3_COMMON_TOKEN; + +/// Pointer to an instantiation of 'class' #ANTLR3_EXCEPTION +/// \ingroup ANTLR3_EXCEPTION +/// +typedef struct ANTLR3_EXCEPTION_struct *pANTLR3_EXCEPTION; + +/// Pointer to an instantiation of 'class' #ANTLR3_HASH_BUCKET +/// \ingroup ANTLR3_HASH_BUCKET +/// +typedef struct ANTLR3_HASH_BUCKET_struct *pANTLR3_HASH_BUCKET; +/// Pointer to an instantiation of 'class' #ANTLR3_HASH_ENTRY +/// \ingroup ANTLR3_HASH_ENTRY +/// +typedef struct ANTLR3_HASH_ENTRY_struct *pANTLR3_HASH_ENTRY; +/// Pointer to an instantiation of 'class' #ANTLR3_HASH_ENUM +/// \ingroup ANTLR3_HASH_ENUM +/// +typedef struct ANTLR3_HASH_ENUM_struct *pANTLR3_HASH_ENUM; +/// Pointer to an instantiation of 'class' #ANTLR3_HASH_TABLE +/// \ingroup ANTLR3_HASH_TABLE +/// +typedef struct ANTLR3_HASH_TABLE_struct *pANTLR3_HASH_TABLE; + +/// Pointer to an instantiation of 'class' #ANTLR3_LIST +/// \ingroup ANTLR3_LIST +/// +typedef struct ANTLR3_LIST_struct *pANTLR3_LIST; +/// Pointer to an instantiation of 'class' #ANTLR3_VECTOR_FACTORY +/// \ingroup ANTLR3_VECTOR_FACTORY +/// +typedef struct ANTLR3_VECTOR_FACTORY_struct *pANTLR3_VECTOR_FACTORY; +/// Pointer to an instantiation of 'class' #ANTLR3_VECTOR +/// \ingroup ANTLR3_VECTOR +/// +typedef struct ANTLR3_VECTOR_struct *pANTLR3_VECTOR; +/// Pointer to an instantiation of 'class' #ANTLR3_STACK +/// \ingroup ANTLR3_STACK +/// +typedef struct ANTLR3_STACK_struct *pANTLR3_STACK; + +/// Pointer to an instantiation of 'class' #ANTLR3_INPUT_STREAM +/// \ingroup ANTLR3_INPUT_STREAM +/// +typedef struct ANTLR3_INPUT_STREAM_struct *pANTLR3_INPUT_STREAM; +/// Pointer to an instantiation of 'class' #ANTLR3_LEX_STATE +/// \ingroup ANTLR3_LEX_STATE +/// +typedef struct ANTLR3_LEX_STATE_struct *pANTLR3_LEX_STATE; + +/// Pointer to an instantiation of 'class' #ANTLR3_STRING_FACTORY +/// \ingroup ANTLR3_STRING_FACTORY +/// +typedef struct ANTLR3_STRING_FACTORY_struct *pANTLR3_STRING_FACTORY; +/// Pointer to an instantiation of 'class' #ANTLR3_STRING +/// \ingroup ANTLR3_STRING +/// +typedef struct ANTLR3_STRING_struct *pANTLR3_STRING; + +/// Pointer to an instantiation of 'class' #ANTLR3_TOKEN_SOURCE +/// \ingroup ANTLR3_TOKEN_SOURCE +/// +typedef struct ANTLR3_TOKEN_SOURCE_struct *pANTLR3_TOKEN_SOURCE; +/// Pointer to an instantiation of 'class' #ANTLR3_TOKEN_STREAM +/// \ingroup ANTLR3_TOKEN_STREAM +/// +typedef struct ANTLR3_TOKEN_STREAM_struct *pANTLR3_TOKEN_STREAM; +/// Pointer to an instantiation of 'class' #ANTLR3_COMMON_TOKEN_STREAM +/// \ingroup ANTLR3_COMMON_TOKEN_STREAM +/// +typedef struct ANTLR3_COMMON_TOKEN_STREAM_struct *pANTLR3_COMMON_TOKEN_STREAM; + +/// Pointer to an instantiation of 'class' #ANTLR3_CYCLIC_DFA +/// \ingroup ANTLR3_CYCLIC_DFA +/// +typedef struct ANTLR3_CYCLIC_DFA_struct *pANTLR3_CYCLIC_DFA; + +/// Pointer to an instantiation of 'class' #ANTLR3_LEXER +/// \ingroup ANTLR3_LEXER +/// +typedef struct ANTLR3_LEXER_struct *pANTLR3_LEXER; +/// Pointer to an instantiation of 'class' #ANTLR3_PARSER +/// \ingroup ANTLR3_PARSER +/// +typedef struct ANTLR3_PARSER_struct *pANTLR3_PARSER; + +/// Pointer to an instantiation of 'class' #ANTLR3_BASE_TREE +/// \ingroup ANTLR3_BASE_TREE +/// +typedef struct ANTLR3_BASE_TREE_struct *pANTLR3_BASE_TREE; +/// Pointer to an instantiation of 'class' #ANTLR3_COMMON_TREE +/// \ingroup ANTLR3_COMMON_TREE +/// +typedef struct ANTLR3_COMMON_TREE_struct *pANTLR3_COMMON_TREE; +/// Pointer to an instantiation of 'class' #ANTLR3_ARBORETUM +/// \ingroup ANTLR3_ARBORETUM +/// +typedef struct ANTLR3_ARBORETUM_struct *pANTLR3_ARBORETUM; +/// Pointer to an instantiation of 'class' #ANTLR3_PARSE_TREE +/// \ingroup ANTLR3_PARSE_TREE +/// +typedef struct ANTLR3_PARSE_TREE_struct *pANTLR3_PARSE_TREE; + +/// Pointer to an instantiation of 'class' #ANTLR3_TREE_NODE_STREAM +/// \ingroup ANTLR3_TREE_NODE_STREAM +/// +typedef struct ANTLR3_TREE_NODE_STREAM_struct *pANTLR3_TREE_NODE_STREAM; +/// Pointer to an instantiation of 'class' #ANTLR3_COMMON_TREE_NODE_STREAM +/// \ingroup ANTLR3_COMMON_TREE_NODE_STREAM +/// +typedef struct ANTLR3_COMMON_TREE_NODE_STREAM_struct *pANTLR3_COMMON_TREE_NODE_STREAM; +/// Pointer to an instantiation of 'class' #ANTLR3_TREE_WALK_STATE +/// \ingroup ANTLR3_TREE_WALK_STATE +/// +typedef struct ANTLR3_TREE_WALK_STATE_struct *pANTLR3_TREE_WALK_STATE; + +/// Pointer to an instantiation of 'class' #ANTLR3_BASE_TREE_ADAPTOR +/// \ingroup ANTLR3_BASE_TREE_ADAPTOR +/// +typedef struct ANTLR3_BASE_TREE_ADAPTOR_struct *pANTLR3_BASE_TREE_ADAPTOR; +/// Pointer to an instantiation of 'class' #ANTLR3_COMMON_TREE_ADAPTOR +/// \ingroup ANTLR3_COMMON_TREE_ADAPTOR +/// +typedef struct ANTLR3_COMMON_TREE_ADAPTOR_struct *pANTLR3_COMMON_TREE_ADAPTOR; + +/// Pointer to an instantiation of 'class' #ANTLR3_TREE_PARSER +/// \ingroup ANTLR3_TREE_PARSER +/// +typedef struct ANTLR3_TREE_PARSER_struct *pANTLR3_TREE_PARSER; + +/// Pointer to an instantiation of 'class' #ANTLR3_INT_TRIE +/// \ingroup ANTLR3_INT_TRIE +/// +typedef struct ANTLR3_INT_TRIE_struct *pANTLR3_INT_TRIE; + +/// Pointer to an instantiation of 'class' #ANTLR3_REWRITE_RULE_ELEMENT_STREAM +/// \ingroup ANTLR3_REWRITE_RULE_ELEMENT_STREAM +/// +typedef struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct *pANTLR3_REWRITE_RULE_ELEMENT_STREAM; +/// Pointer to an instantiation of 'class' #ANTLR3_REWRITE_RULE_ELEMENT_STREAM +/// \ingroup ANTLR3_REWRITE_RULE_ELEMENT_STREAM +/// +typedef struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct *pANTLR3_REWRITE_RULE_TOKEN_STREAM; + +/// Pointer to an instantiation of 'class' #ANTLR3_REWRITE_RULE_SUBSTREE_STREAM +/// \ingroup ANTLR3_REWRITE_RULE_SUBTREE_STREAM +/// +typedef struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct *pANTLR3_REWRITE_RULE_SUBTREE_STREAM; + +/// Pointer to an instantiation of 'class' #ANTLR3_REWRITE_RULE_NODE_STREAM +/// \ingroup ANTLR3_REWRITE_RULE_NODE_STREAM +/// +typedef struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct *pANTLR3_REWRITE_RULE_NODE_STREAM; + +/// Pointer to an instantiation of 'class' #ANTLR3_DEBUG_EVENT_LISTENER +/// \ingroup ANTLR3_DEBUG_EVENT_LISTENER +/// +typedef struct ANTLR3_DEBUG_EVENT_LISTENER_struct *pANTLR3_DEBUG_EVENT_LISTENER; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3intstream.h b/antlr-3.1.3/runtime/C/include/antlr3intstream.h new file mode 100644 index 0000000..21bac5b --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3intstream.h @@ -0,0 +1,205 @@ +/** \file + * Defines the the class interface for an antlr3 INTSTREAM. + * + * Certain functionality (such as DFAs for instance) abstract the stream of tokens + * or characters in to a steam of integers. Hence this structure should be included + * in any stream that is able to provide the output as a stream of integers (which is anything + * basically. + * + * There are no specific implementations of the methods in this interface in general. Though + * for purposes of casting and so on, it may be necesssary to implement a function with + * the signature in this interface which abstracts the base immplementation. In essence though + * the base stream provides a pointer to this interface, within which it installs its + * normal match() functions and so on. Interaces such as DFA are then passed the pANTLR3_INT_STREAM + * and can treat any input as an int stream. + * + * For instance, a lexer implements a pANTLR3_BASE_RECOGNIZER, within which there is a pANTLR3_INT_STREAM. + * However, a pANTLR3_INPUT_STREAM also provides a pANTLR3_INT_STREAM, which it has constructed from + * it's normal interface when it was created. This is then pointed at by the pANTLR_BASE_RECOGNIZER + * when it is intialized with a pANTLR3_INPUT_STREAM. + * + * Similarly if a pANTLR3_BASE_RECOGNIZER is initialized with a pANTLR3_TOKEN_STREAM, then the + * pANTLR3_INT_STREAM is taken from the pANTLR3_TOKEN_STREAM. + * + * If a pANTLR3_BASE_RECOGNIZER is initialized with a pANTLR3_TREENODE_STREAM, then guess where + * the pANTLR3_INT_STREAM comes from? + * + * Note that because the context pointer points to the actual interface structure that is providing + * the ANTLR3_INT_STREAM it is defined as a (void *) in this interface. There is no direct implementation + * of an ANTLR3_INT_STREAM (unless someone did not understand what I was doing here =;?P + */ +#ifndef _ANTLR3_INTSTREAM_H +#define _ANTLR3_INTSTREAM_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3commontoken.h> + +/** Type indicator for a character stream + * \remark if a custom stream is created but it can be treated as + * a char stream, then you may OR in this value to your type indicator + */ +#define ANTLR3_CHARSTREAM 0x0001 + +/** Type indicator for a Token stream + * \remark if a custom stream is created but it can be treated as + * a token stream, then you may OR in this value to your type indicator + */ +#define ANTLR3_TOKENSTREAM 0x0002 + +/** Type indicator for a common tree node stream + * \remark if a custom stream is created but it can be treated as + * a common tree node stream, then you may OR in this value to your type indicator + */ +#define ANTLR3_COMMONTREENODE 0x0004 + +/** Type mask for input stream so we can switch in the above types + * \remark DO NOT USE 0x0000 as a stream type! + */ +#define ANTLR3_INPUT_MASK 0x0007 + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct ANTLR3_INT_STREAM_struct +{ + /** Input stream type indicator. Sometimes useful for error reporting etc. + */ + ANTLR3_UINT32 type; + + /** Potentially useful in error reporting and so on, this string is + * an identification of the input source. It may be NULL, so anything + * attempting to access it needs to check this and substitute a sensible + * default. + */ + pANTLR3_STRING streamName; + + /** Pointer to the super structure that contains this interface. This + * will usually be a token stream or a tree stream. + */ + void * super; + + /** Last marker position allocated + */ + ANTLR3_MARKER lastMarker; + + // Return a string that identifies the input source + // + pANTLR3_STRING (*getSourceName) (struct ANTLR3_INT_STREAM_struct * intStream); + + /** Consume the next 'ANTR3_UINT32' in the stream + */ + void (*consume) (struct ANTLR3_INT_STREAM_struct * intStream); + + /** Get ANTLR3_UINT32 at current input pointer + i ahead where i=1 is next ANTLR3_UINT32 + */ + ANTLR3_UINT32 (*_LA) (struct ANTLR3_INT_STREAM_struct * intStream, ANTLR3_INT32 i); + + /** Tell the stream to start buffering if it hasn't already. Return + * current input position, index(), or some other marker so that + * when passed to rewind() you get back to the same spot. + * rewind(mark()) should not affect the input cursor. + */ + ANTLR3_MARKER (*mark) (struct ANTLR3_INT_STREAM_struct * intStream); + + /** Return the current input symbol index 0..n where n indicates the + * last symbol has been read. + */ + ANTLR3_MARKER (*index) (struct ANTLR3_INT_STREAM_struct * intStream); + + /** Reset the stream so that next call to index would return marker. + * The marker will usually be index() but it doesn't have to be. It's + * just a marker to indicate what state the stream was in. This is + * essentially calling release() and seek(). If there are markers + * created after this marker argument, this routine must unroll them + * like a stack. Assume the state the stream was in when this marker + * was created. + */ + void (*rewind) (struct ANTLR3_INT_STREAM_struct * intStream, ANTLR3_MARKER marker); + + /** Reset the stream to the last marker position, witouh destryoing the + * last marker position. + */ + void (*rewindLast) (struct ANTLR3_INT_STREAM_struct * intStream); + + /** You may want to commit to a backtrack but don't want to force the + * stream to keep bookkeeping objects around for a marker that is + * no longer necessary. This will have the same behavior as + * rewind() except it releases resources without the backward seek. + */ + void (*release) (struct ANTLR3_INT_STREAM_struct * intStream, ANTLR3_MARKER mark); + + /** Set the input cursor to the position indicated by index. This is + * normally used to seek ahead in the input stream. No buffering is + * required to do this unless you know your stream will use seek to + * move backwards such as when backtracking. + * + * This is different from rewind in its multi-directional + * requirement and in that its argument is strictly an input cursor (index). + * + * For char streams, seeking forward must update the stream state such + * as line number. For seeking backwards, you will be presumably + * backtracking using the mark/rewind mechanism that restores state and + * so this method does not need to update state when seeking backwards. + * + * Currently, this method is only used for efficient backtracking, but + * in the future it may be used for incremental parsing. + */ + void (*seek) (struct ANTLR3_INT_STREAM_struct * intStream, ANTLR3_MARKER index); + + /** Only makes sense for streams that buffer everything up probably, but + * might be useful to display the entire stream or for testing. + */ + ANTLR3_UINT32 (*size) (struct ANTLR3_INT_STREAM_struct * intStream); + + /** Because the indirect call, though small in individual cases can + * mount up if there are thousands of tokens (very large input streams), callers + * of size can optionally use this cached size field. + */ + ANTLR3_UINT32 cachedSize; + + /** Frees any resources that were allocated for the implementation of this + * interface. Usually this is just releasing the memory allocated + * for the structure itself, but it may of course do anything it need to + * so long as it does not stamp on anything else. + */ + void (*free) (struct ANTLR3_INT_STREAM_struct * stream); + +} + ANTLR3_INT_STREAM; + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/antlr-3.1.3/runtime/C/include/antlr3lexer.h b/antlr-3.1.3/runtime/C/include/antlr3lexer.h new file mode 100644 index 0000000..3955748 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3lexer.h @@ -0,0 +1,195 @@ +/** \file + * Base interface for any ANTLR3 lexer. + * + * An ANLTR3 lexer builds from two sets of components: + * + * - The runtime components that provide common functionality such as + * traversing character streams, building tokens for output and so on. + * - The generated rules and struutre of the actual lexer, which call upon the + * runtime components. + * + * A lexer class contains a character input stream, a base recognizer interface + * (which it will normally implement) and a token source interface (which it also + * implements. The Tokensource interface is called by a token consumer (such as + * a parser, but in theory it can be anything that wants a set of abstract + * tokens in place of a raw character stream. + * + * So then, we set up a lexer in a sequence akin to: + * + * - Create a character stream (something which implements ANTLR3_INPUT_STREAM) + * and initialize it. + * - Create a lexer interface and tell it where it its input stream is. + * This will cause the creation of a base recognizer class, which it will + * override with its own implementations of some methods. The lexer creator + * can also then in turn override anything it likes. + * - The lexer token source interface is then passed to some interface that + * knows how to use it, byte calling for a next token. + * - When a next token is called, let ze lexing begin. + * + */ +#ifndef _ANTLR3_LEXER +#define _ANTLR3_LEXER + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +/* Definitions + */ +#define ANTLR3_STRING_TERMINATOR 0xFFFFFFFF + +#include <antlr3defs.h> +#include <antlr3input.h> +#include <antlr3commontoken.h> +#include <antlr3tokenstream.h> +#include <antlr3baserecognizer.h> + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct ANTLR3_LEXER_struct +{ + /** If there is a super structure that is implementing the + * lexer, then a pointer to it can be stored here in case + * implementing functions are overridden by this super structure. + */ + void * super; + + /** A generated lexer has an mTokens() function, which needs + * the context pointer of the generated lexer, not the base lexer interface + * this is stored here and initialized by the generated code (or manually + * if this is a manually built lexer. + */ + void * ctx; + + /** A pointer to the character stream whence this lexer is receiving + * characters. + * TODO: I may come back to this and implement charstream outside + * the input stream as per the java implementation. + */ + pANTLR3_INPUT_STREAM input; + + /** Pointer to the implementation of a base recognizer, which the lexer + * creates and then overrides with its own lexer oriented functions (the + * default implementation is parser oriented). This also contains a + * token source interface, which the lexer instance will provide to anything + * that needs it, which is anything else that implements a base recognizer, + * such as a parser. + */ + pANTLR3_BASE_RECOGNIZER rec; + + /** Pointer to a function that sets the charstream source for the lexer and + * causes it to be reset. + */ + void (*setCharStream) (struct ANTLR3_LEXER_struct * lexer, pANTLR3_INPUT_STREAM input); + + /** Pointer to a function that switches the current character input stream to + * a new one, saving the old one, which we will revert to at the end of this + * new one. + */ + void (*pushCharStream) (struct ANTLR3_LEXER_struct * lexer, pANTLR3_INPUT_STREAM input); + + /** Pointer to a function that abandons the current input stream, whether it + * is empty or not and reverts to the previous stacked input stream. + */ + void (*popCharStream) (struct ANTLR3_LEXER_struct * lexer); + + /** Pointer to a function that emits the supplied token as the next token in + * the stream. + */ + void (*emitNew) (struct ANTLR3_LEXER_struct * lexer, pANTLR3_COMMON_TOKEN token); + + /** Pointer to a function that constructs a new token from the lexer stored information + */ + pANTLR3_COMMON_TOKEN (*emit) (struct ANTLR3_LEXER_struct * lexer); + + /** Pointer to the user provided (either manually or through code generation + * function that causes the lexer rules to run the lexing rules and produce + * the next token if there iss one. This is called from nextToken() in the + * pANTLR3_TOKEN_SOURCE. Note that the input parameter for this funciton is + * the generated lexer context (stored in ctx in this interface) it is a generated + * function and expects the context to be the generated lexer. + */ + void (*mTokens) (void * ctx); + + /** Pointer to a function that attempts to match and consume the specified string from the input + * stream. Note that strings muse be passed as terminated arrays of ANTLR3_UCHAR. Strings are terminated + * with 0xFFFFFFFF, which is an invalid UTF32 character + */ + ANTLR3_BOOLEAN (*matchs) (struct ANTLR3_LEXER_struct * lexer, ANTLR3_UCHAR * string); + + /** Pointer to a function that matches and consumes the specified character from the input stream. + * As the input stream is required to provide characters via LA() as UTF32 characters it does not + * need to provide an implementation if it is not sourced from 8 bit ASCII. The default lexer + * implementation is source encoding agnostic, unless for some reason it takes two 32 bit characters + * to specify a single character, in which case the input stream and the lexer rules would have to match + * in encoding and then it would work 'by accident' anyway. + */ + ANTLR3_BOOLEAN (*matchc) (struct ANTLR3_LEXER_struct * lexer, ANTLR3_UCHAR c); + + /** Pointer to a function that matches any character in the supplied range (I suppose it could be a token range too + * but this would only be useful if the tokens were in tsome guaranteed order which is + * only going to happen with a hand crafted token set). + */ + ANTLR3_BOOLEAN (*matchRange) (struct ANTLR3_LEXER_struct * lexer, ANTLR3_UCHAR low, ANTLR3_UCHAR high); + + /** Pointer to a function that matches the next token/char in the input stream + * regardless of what it actaully is. + */ + void (*matchAny) (struct ANTLR3_LEXER_struct * lexer); + + /** Pointer to a function that recovers from an error found in the input stream. + * Generally, this will be a #ANTLR3_EXCEPTION_NOVIABLE_ALT but it could also + * be from a mismatched token that the (*match)() could not recover from. + */ + void (*recover) (struct ANTLR3_LEXER_struct * lexer); + + /** Pointer to function to return the current line number in the input stream + */ + ANTLR3_UINT32 (*getLine) (struct ANTLR3_LEXER_struct * lexer); + ANTLR3_MARKER (*getCharIndex) (struct ANTLR3_LEXER_struct * lexer); + ANTLR3_UINT32 (*getCharPositionInLine)(struct ANTLR3_LEXER_struct * lexer); + + /** Pointer to function to return the text so far for the current token being generated + */ + pANTLR3_STRING (*getText) (struct ANTLR3_LEXER_struct * lexer); + + + /** Pointer to a function that knows how to free the resources of a lexer + */ + void (*free) (struct ANTLR3_LEXER_struct * lexer); + +} + ANTLR3_LEXER; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3memory.h b/antlr-3.1.3/runtime/C/include/antlr3memory.h new file mode 100644 index 0000000..5e2d4c9 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3memory.h @@ -0,0 +1,36 @@ +#ifndef _ANTLR3MEMORY_H +#define _ANTLR3MEMORY_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> + + +#endif /* _ANTLR3MEMORY_H */ diff --git a/antlr-3.1.3/runtime/C/include/antlr3parser.h b/antlr-3.1.3/runtime/C/include/antlr3parser.h new file mode 100644 index 0000000..95b7d18 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3parser.h @@ -0,0 +1,93 @@ +/** \file + * Base implementation of an ANTLR3 parser. + * + * + */ +#ifndef _ANTLR3_PARSER_H +#define _ANTLR3_PARSER_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3baserecognizer.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** This is the main interface for an ANTLR3 parser. + */ +typedef struct ANTLR3_PARSER_struct +{ + /** All superstructure implementers of this interface require a pointer to their selves, + * which they can reference using the super pointer here. + */ + void * super; + + /** A pointer to the base recognizer, where most of the parser functions actually + * live because they are shared between parser and tree parser and this is the + * easier way than copying the interface all over the place. Macros hide this + * for the generated code so it is easier on the eye (though not the debugger ;-). + */ + pANTLR3_BASE_RECOGNIZER rec; + + /** A provider of a tokenstream interface, for the parser to consume + * tokens from. + */ + pANTLR3_TOKEN_STREAM tstream; + + /** A pointer to a function that installs a debugger object (it also + * installs the debugging versions of the parser methods. This means that + * a non debug parser incurs no overhead because of the debugging stuff. + */ + void (*setDebugListener) (struct ANTLR3_PARSER_struct * parser, pANTLR3_DEBUG_EVENT_LISTENER dbg); + + /** A pointer to a function that installs a token stream + * for the parser. + */ + void (*setTokenStream) (struct ANTLR3_PARSER_struct * parser, pANTLR3_TOKEN_STREAM); + + /** A pointer to a function that returns the token stream for this + * parser. + */ + pANTLR3_TOKEN_STREAM (*getTokenStream) (struct ANTLR3_PARSER_struct * parser); + + /** Pointer to a function that knows how to free resources of an ANTLR3 parser. + */ + void (*free) (struct ANTLR3_PARSER_struct * parser); + +} + ANTLR3_PARSER; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3parsetree.h b/antlr-3.1.3/runtime/C/include/antlr3parsetree.h new file mode 100644 index 0000000..0e8c157 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3parsetree.h @@ -0,0 +1,85 @@ +/** \file + * Abstraction of Common tree to provide payload and string representation of node. + * + * \todo May not need this in the end + */ + +#ifndef ANTLR3_PARSETREE_H +#define ANTLR3_PARSETREE_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3basetree.h> + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct ANTLR3_PARSE_TREE_struct +{ + /** Any interface that implements methods in this interface + * may need to point back to itself using this pointer to its + * super structure. + */ + void * super; + + /** The payload that the parse tree node passes around + */ + void * payload; + + /** An encapsulated BASE TREE strcuture (NOT a pointer) + * that perfoms a lot of the dirty work of node management + */ + ANTLR3_BASE_TREE baseTree; + + /** How to dup this node + */ + pANTLR3_BASE_TREE (*dupNode) (struct ANTLR3_PARSE_TREE_struct * tree); + + /** Return the type of this node + */ + ANTLR3_UINT32 (*getType) (struct ANTLR3_PARSE_TREE_struct * tree); + + /** Return the string representation of the payload (must be installed + * when the payload is added and point to a function that knwos how to + * manifest a pANTLR3_STRING from a node. + */ + pANTLR3_STRING (*toString) (struct ANTLR3_PARSE_TREE_struct * payload); + + void (*free) (struct ANTLR3_PARSE_TREE_struct * tree); + +} + ANTLR3_PARSE_TREE; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3recognizersharedstate.h b/antlr-3.1.3/runtime/C/include/antlr3recognizersharedstate.h new file mode 100644 index 0000000..9e024d8 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3recognizersharedstate.h @@ -0,0 +1,218 @@ +/** \file + * While the C runtime does not need to model the state of + * multiple lexers and parsers in the same way as the Java runtime does + * it is no overhead to reflect that model. In fact the + * C runtime has always been able to share recognizer state. + * + * This 'class' therefore defines all the elements of a recognizer + * (either lexer, parser or tree parser) that are need to + * track the current recognition state. Multiple recognizers + * may then share this state, for instance when one grammar + * imports another. + */ + +#ifndef _ANTLR3_RECOGNIZER_SHARED_STATE_H +#define _ANTLR3_RECOGNIZER_SHARED_STATE_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** All the data elements required to track the current state + * of any recognizer (lexer, parser, tree parser). + * May be share between multiple recognizers such that + * grammar inheritance is easily supported. + */ +typedef struct ANTLR3_RECOGNIZER_SHARED_STATE_struct +{ + /** If set to ANTLR3_TRUE then the recognizer has an exception + * condition (this is tested by the generated code for the rules of + * the grammar). + */ + ANTLR3_BOOLEAN error; + + /** Points to the first in a possible chain of exceptions that the + * recognizer has discovered. + */ + pANTLR3_EXCEPTION exception; + + /** Track around a hint from the creator of the recognizer as to how big this + * thing is going to get, as the actress said to the bishop. This allows us + * to tune hash tables accordingly. This might not be the best place for this + * in the end but we will see. + */ + ANTLR3_UINT32 sizeHint; + + /** Track the set of token types that can follow any rule invocation. + * Stack structure, to support: List<BitSet>. + */ + pANTLR3_STACK following; + + + /** This is true when we see an error and before having successfully + * matched a token. Prevents generation of more than one error message + * per error. + */ + ANTLR3_BOOLEAN errorRecovery; + + /** The index into the input stream where the last error occurred. + * This is used to prevent infinite loops where an error is found + * but no token is consumed during recovery...another error is found, + * ad nauseam. This is a failsafe mechanism to guarantee that at least + * one token/tree node is consumed for two errors. + */ + ANTLR3_MARKER lastErrorIndex; + + /** In lieu of a return value, this indicates that a rule or token + * has failed to match. Reset to false upon valid token match. + */ + ANTLR3_BOOLEAN failed; + + /** When the recognizer terminates, the error handling functions + * will have incremented this value if any error occurred (that was displayed). It can then be + * used by the grammar programmer without having to use static globals. + */ + ANTLR3_UINT32 errorCount; + + /** If 0, no backtracking is going on. Safe to exec actions etc... + * If >0 then it's the level of backtracking. + */ + ANTLR3_INT32 backtracking; + + /** ANTLR3_VECTOR of ANTLR3_LIST for rule memoizing. + * Tracks the stop token index for each rule. ruleMemo[ruleIndex] is + * the memoization table for ruleIndex. For key ruleStartIndex, you + * get back the stop token for associated rule or MEMO_RULE_FAILED. + * + * This is only used if rule memoization is on. + */ + pANTLR3_INT_TRIE ruleMemo; + + /** Pointer to an array of token names + * that are generally useful in error reporting. The generated parsers install + * this pointer. The table it points to is statically allocated as 8 bit ascii + * at parser compile time - grammar token names are thus restricted in character + * sets, which does not seem to terrible. + */ + pANTLR3_UINT8 * tokenNames; + + /** User programmable pointer that can be used for instance as a place to + * store some tracking structure specific to the grammar that would not normally + * be available to the error handling functions. + */ + void * userp; + + /** The goal of all lexer rules/methods is to create a token object. + * This is an instance variable as multiple rules may collaborate to + * create a single token. For example, NUM : INT | FLOAT ; + * In this case, you want the INT or FLOAT rule to set token and not + * have it reset to a NUM token in rule NUM. + */ + pANTLR3_COMMON_TOKEN token; + + /** The goal of all lexer rules being to create a token, then a lexer + * needs to build a token factory to create them. + */ + pANTLR3_TOKEN_FACTORY tokFactory; + + /** A lexer is a source of tokens, produced by all the generated (or + * hand crafted if you like) matching rules. As such it needs to provide + * a token source interface implementation. + */ + pANTLR3_TOKEN_SOURCE tokSource; + + /** The channel number for the current token + */ + ANTLR3_UINT32 channel; + + /** The token type for the current token + */ + ANTLR3_UINT32 type; + + /** The input line (where it makes sense) on which the first character of the current + * token resides. + */ + ANTLR3_INT32 tokenStartLine; + + /** The character position of the first character of the current token + * within the line specified by tokenStartLine + */ + ANTLR3_INT32 tokenStartCharPositionInLine; + + /** What character index in the stream did the current token start at? + * Needed, for example, to get the text for current token. Set at + * the start of nextToken. + */ + ANTLR3_MARKER tokenStartCharIndex; + + /** Text for the current token. This can be overridden by setting this + * variable directly or by using the SETTEXT() macro (preferred) in your + * lexer rules. + */ + pANTLR3_STRING text; + + /** User controlled variables that will be installed in a newly created + * token. + */ + ANTLR3_UINT32 user1, user2, user3; + void * custom; + + /** Input stream stack, which allows the C programmer to switch input streams + * easily and allow the standard nextToken() implementation to deal with it + * as this is a common requirement. + */ + pANTLR3_STACK streams; + + /// A stack of token/tree rewrite streams that are available for use + /// by a parser or tree parser that is using rewrites to generate + /// an AST. This saves each rule in the recongizer from having to + /// allocate and deallocate rewtire streams on entry and exit. As + /// the parser recurses throgh the rules it will reach a steady state + /// of the maximum number of allocated streams, which instead of + /// deallocating them at rule exit, it will place on this stack for + /// reuse. The streams are then all finally freed when this stack + /// is freed. + /// + pANTLR3_VECTOR rStreams; + +} + ANTLR3_RECOGNIZER_SHARED_STATE; + +#ifdef __cplusplus +} +#endif + +#endif + + diff --git a/antlr-3.1.3/runtime/C/include/antlr3rewritestreams.h b/antlr-3.1.3/runtime/C/include/antlr3rewritestreams.h new file mode 100644 index 0000000..bf83fe9 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3rewritestreams.h @@ -0,0 +1,180 @@ +#ifndef ANTLR3REWRITESTREAM_H +#define ANTLR3REWRITESTREAM_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3collections.h> +#include <antlr3commontreeadaptor.h> +#include <antlr3baserecognizer.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/// A generic list of elements tracked in an alternative to be used in +/// a -> rewrite rule. +/// +/// In the C implementation, all tree oriented streams return a pointer to +/// the same type: pANTLR3_BASE_TREE. Anything that has subclassed from this +/// still passes this type, within which there is a super pointer, which points +/// to it's own data and methods. Hence we do not need to implement this as +/// the equivalent of an abstract class, but just fill in the appropriate interface +/// as usual with this model. +/// +/// Once you start next()ing, do not try to add more elements. It will +/// break the cursor tracking I believe. +/// +/// +/// \see #pANTLR3_REWRITE_RULE_NODE_STREAM +/// \see #pANTLR3_REWRITE_RULE_ELEMENT_STREAM +/// \see #pANTLR3_REWRITE_RULE_SUBTREE_STREAM +/// +/// TODO: add mechanism to detect/puke on modification after reading from stream +/// +typedef struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct +{ + + /// Cursor 0..n-1. If singleElement!=NULL, cursor is 0 until you next(), + /// which bumps it to 1 meaning no more elements. + /// + ANTLR3_UINT32 cursor; + + /// Track single elements w/o creating a list. Upon 2nd add, alloc list + /// + void * singleElement; + + /// The list of tokens or subtrees we are tracking + /// + pANTLR3_VECTOR elements; + + /// Indicates whether we should free the vector or it was supplied to us + /// + ANTLR3_BOOLEAN freeElements; + + /// The element or stream description; usually has name of the token or + /// rule reference that this list tracks. Can include rulename too, but + /// the exception would track that info. + /// + void * elementDescription; + + /// Pointer to the tree adaptor in use for this stream + /// + pANTLR3_BASE_TREE_ADAPTOR adaptor; + + /// Once a node / subtree has been used in a stream, it must be dup'ed + /// from then on. Streams are reset after sub rules so that the streams + /// can be reused in future sub rules. So, reset must set a dirty bit. + /// If dirty, then next() always returns a dup. + /// + ANTLR3_BOOLEAN dirty; + + // Pointer to the recognizer shared state to which this stream belongs + // + pANTLR3_BASE_RECOGNIZER rec; + + // Methods + + /// Reset the condition of this stream so that it appears we have + /// not consumed any of its elements. Elements themselves are untouched. + /// + void (*reset) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); + + /// Add a new pANTLR3_BASE_TREE to this stream + /// + void (*add) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream, void *el, void (ANTLR3_CDECL *freePtr)(void *)); + + /// Return the next element in the stream. If out of elements, throw + /// an exception unless size()==1. If size is 1, then return elements[0]. + /// + void * (*next) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); + pANTLR3_BASE_TREE (*nextTree) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); + void * (*nextToken) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); + void * (*_next) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); + + /// When constructing trees, sometimes we need to dup a token or AST + /// subtree. Dup'ing a token means just creating another AST node + /// around it. For trees, you must call the adaptor.dupTree(). + /// + void * (*dup) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream, void * el); + + /// Ensure stream emits trees; tokens must be converted to AST nodes. + /// AST nodes can be passed through unmolested. + /// + pANTLR3_BASE_TREE (*toTree) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream, void * el); + + /// Returns ANTLR3_TRUE if there is a next element available + /// + ANTLR3_BOOLEAN (*hasNext) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); + + /// Treat next element as a single node even if it's a subtree. + /// This is used instead of next() when the result has to be a + /// tree root node. Also prevents us from duplicating recently-added + /// children; e.g., ^(type ID)+ adds ID to type and then 2nd iteration + /// must dup the type node, but ID has been added. + /// + /// Referencing to a rule result twice is ok; dup entire tree as + /// we can't be adding trees; e.g., expr expr. + /// + pANTLR3_BASE_TREE (*nextNode) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); + + /// Number of elements available in the stream + /// + ANTLR3_UINT32 (*size) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); + + /// Returns the description string if there is one available (check for NULL). + /// + void * (*getDescription) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); + + void (*free) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); + +} + ANTLR3_REWRITE_RULE_ELEMENT_STREAM; + +/// This is an implementation of a token stream, which is basically an element +/// stream that deals with tokens only. +/// +typedef struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct ANTLR3_REWRITE_RULE_TOKEN_STREAM; + +/// This is an implementation of a subtree stream which is a set of trees +/// modelled as an element stream. +/// +typedef struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct ANTLR3_REWRITE_RULE_SUBTREE_STREAM; + +/// This is an implementation of a node stream, which is basically an element +/// stream that deals with tree nodes only. +/// +typedef struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct ANTLR3_REWRITE_RULE_NODE_STREAM; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3string.h b/antlr-3.1.3/runtime/C/include/antlr3string.h new file mode 100644 index 0000000..6e83afb --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3string.h @@ -0,0 +1,273 @@ +/** \file + * Simple string interface allows indiscriminate allocation of strings + * such that they can be allocated all over the place and released in + * one chunk via a string factory - saves lots of hassle in remembering what + * strings were allocated where. + */ +#ifndef _ANTLR3_STRING_H +#define _ANTLR3_STRING_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3collections.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** Base string class tracks the allocations and provides simple string + * tracking functions. Mostly you can work directly on the string for things + * that don't reallocate it, like strchr() etc. Perhaps someone will want to provide implementations for UTF8 + * and so on. + */ +typedef struct ANTLR3_STRING_struct +{ + + /** The factory that created this string + */ + pANTLR3_STRING_FACTORY factory; + + /** Pointer to the current string value (starts at NULL unless + * the string allocator is told to create it with a pre known size. + */ + pANTLR3_UINT8 chars; + + /** Current length of the string up to and not including, the trailing '\0' + * Note that the actual allocation (->size) + * is always at least one byte more than this to accommodate trailing '\0' + */ + ANTLR3_UINT32 len; + + /** Current size of the string in bytes including the trailing '\0' + */ + ANTLR3_UINT32 size; + + /** Index of string (allocation number) in case someone wants + * to explicitly release it. + */ + ANTLR3_UINT32 index; + + /** Occasionally it is useful to know what the encoding of the string + * actually is, hence it is stored here as one the ANTLR3_ENCODING_ values + */ + ANTLR3_UINT8 encoding; + + /** Pointer to function that sets the string value to a specific string in the default encoding + * for this string. For instance, if this is ASCII 8 bit, then this function is the same as set8 + * but if the encoding is 16 bit, then the pointer is assumed to point to 16 bit characters not + * 8 bit. + */ + pANTLR3_UINT8 (*set) (struct ANTLR3_STRING_struct * string, const char * chars); + + /** Pointer to function that sets the string value to a specific 8 bit string in the default encoding + * for this string. For instance, if this is a 16 bit string, then this function is the same as set8 + * but if the encoding is 16 bit, then the pointer is assumed to point to 8 bit characters that must + * be converted to 16 bit characters on the fly. + */ + pANTLR3_UINT8 (*set8) (struct ANTLR3_STRING_struct * string, const char * chars); + + /** Pointer to function adds a raw char * type pointer in the default encoding + * for this string. For instance, if this is ASCII 8 bit, then this function is the same as append8 + * but if the encoding is 16 bit, then the pointer is assumed to point to 16 bit characters not + * 8 bit. + */ + pANTLR3_UINT8 (*append) (struct ANTLR3_STRING_struct * string, const char * newbit); + + /** Pointer to function adds a raw char * type pointer in the default encoding + * for this string. For instance, if this is a 16 bit string, then this function assumes the pointer + * points to 8 bit characters that must be converted on the fly. + */ + pANTLR3_UINT8 (*append8) (struct ANTLR3_STRING_struct * string, const char * newbit); + + /** Pointer to function that inserts the supplied string at the specified + * offset in the current string in the default encoding for this string. For instance, if this is an 8 + * bit string, then this is the same as insert8, but if this is a 16 bit string, then the poitner + * must point to 16 bit characters. + * + */ + pANTLR3_UINT8 (*insert) (struct ANTLR3_STRING_struct * string, ANTLR3_UINT32 point, const char * newbit); + + /** Pointer to function that inserts the supplied string at the specified + * offset in the current string in the default encoding for this string. For instance, if this is a 16 bit string + * then the pointer is assumed to point at 8 bit characteres that must be converted on the fly. + */ + pANTLR3_UINT8 (*insert8) (struct ANTLR3_STRING_struct * string, ANTLR3_UINT32 point, const char * newbit); + + /** Pointer to function that sets the string value to a copy of the supplied string (strings must be in the + * same encoding. + */ + pANTLR3_UINT8 (*setS) (struct ANTLR3_STRING_struct * string, struct ANTLR3_STRING_struct * chars); + + /** Pointer to function appends a copy of the characters contained in another string. Strings must be in the + * same encoding. + */ + pANTLR3_UINT8 (*appendS) (struct ANTLR3_STRING_struct * string, struct ANTLR3_STRING_struct * newbit); + + /** Pointer to function that inserts a copy of the characters in the supplied string at the specified + * offset in the current string. strings must be in the same encoding. + */ + pANTLR3_UINT8 (*insertS) (struct ANTLR3_STRING_struct * string, ANTLR3_UINT32 point, struct ANTLR3_STRING_struct * newbit); + + /** Pointer to function that inserts the supplied integer in string form at the specified + * offset in the current string. + */ + pANTLR3_UINT8 (*inserti) (struct ANTLR3_STRING_struct * string, ANTLR3_UINT32 point, ANTLR3_INT32 i); + + /** Pointer to function that adds a single character to the end of the string, in the encoding of the + * string - 8 bit, 16 bit, utf-8 etc. Input is a single UTF32 (32 bits wide integer) character. + */ + pANTLR3_UINT8 (*addc) (struct ANTLR3_STRING_struct * string, ANTLR3_UINT32 c); + + /** Pointer to function that adds the stringified representation of an integer + * to the string. + */ + pANTLR3_UINT8 (*addi) (struct ANTLR3_STRING_struct * string, ANTLR3_INT32 i); + + /** Pointer to function that compares the text of a string to the supplied + * 8 bit character string and returns a result a la strcmp() + */ + ANTLR3_UINT32 (*compare8) (struct ANTLR3_STRING_struct * string, const char * compStr); + + /** Pointer to a function that compares the text of a string with the supplied character string + * (which is assumed to be in the same encoding as the string itself) and returns a result + * a la strcmp() + */ + ANTLR3_UINT32 (*compare) (struct ANTLR3_STRING_struct * string, const char * compStr); + + /** Pointer to a function that compares the text of a string with the supplied string + * (which is assumed to be in the same encoding as the string itself) and returns a result + * a la strcmp() + */ + ANTLR3_UINT32 (*compareS) (struct ANTLR3_STRING_struct * string, struct ANTLR3_STRING_struct * compStr); + + /** Pointer to a function that returns the character indexed at the supplied + * offset as a 32 bit character. + */ + ANTLR3_UCHAR (*charAt) (struct ANTLR3_STRING_struct * string, ANTLR3_UINT32 offset); + + /** Pointer to a function that returns a substring of the supplied string a la .subString(s,e) + * in the Java language. + */ + struct ANTLR3_STRING_struct * + (*subString) (struct ANTLR3_STRING_struct * string, ANTLR3_UINT32 startIndex, ANTLR3_UINT32 endIndex); + + /** Pointer to a function that returns the integer representation of any numeric characters + * at the beginning of the string + */ + ANTLR3_INT32 (*toInt32) (struct ANTLR3_STRING_struct * string); + + /** Pointer to a function that yields an 8 bit string regardless of the encoding of the supplied + * string. This is useful when you want to use the text of a token in some way that requires an 8 bit + * value, such as the key for a hashtable. The function is required to produce a usable string even + * if the text given as input has characters that do not fit in 8 bit space, it will replace them + * with some arbitrary character such as '?' + */ + struct ANTLR3_STRING_struct * + (*to8) (struct ANTLR3_STRING_struct * string); + + /// Pointer to a function that yields a UT8 encoded string of the current string, + /// regardless of the current encoding of the string. Because there is currently no UTF8 + /// handling in the string class, it creates therefore, a string that is useful only for read only + /// applications as it will not contain methods that deal with UTF8 at the moment. + /// + struct ANTLR3_STRING_struct * + (*toUTF8) (struct ANTLR3_STRING_struct * string); + +} + ANTLR3_STRING; + +/** Definition of the string factory interface, which creates and tracks + * strings for you of various shapes and sizes. + */ +typedef struct ANTLR3_STRING_FACTORY_struct +{ + /** List of all the strings that have been allocated by the factory + */ + pANTLR3_VECTOR strings; + + /* Index of next string that we allocate + */ + ANTLR3_UINT32 index; + + /** Pointer to function that manufactures an empty string + */ + pANTLR3_STRING (*newRaw) (struct ANTLR3_STRING_FACTORY_struct * factory); + + /** Pointer to function that manufactures a raw string with no text in it but space for size + * characters. + */ + pANTLR3_STRING (*newSize) (struct ANTLR3_STRING_FACTORY_struct * factory, ANTLR3_UINT32 size); + + /** Pointer to function that manufactures a string from a given pointer and length. The pointer is assumed + * to point to characters in the same encoding as the string type, hence if this is a 16 bit string the + * pointer should point to 16 bit characters. + */ + pANTLR3_STRING (*newPtr) (struct ANTLR3_STRING_FACTORY_struct * factory, pANTLR3_UINT8 string, ANTLR3_UINT32 size); + + /** Pointer to function that manufactures a string from a given pointer and length. The pointer is assumed to + * point at 8 bit characters which must be converted on the fly to the encoding of the actual string. + */ + pANTLR3_STRING (*newPtr8) (struct ANTLR3_STRING_FACTORY_struct * factory, pANTLR3_UINT8 string, ANTLR3_UINT32 size); + + /** Pointer to function that manufactures a string from a given pointer and works out the length. The pointer is + * assumed to point to characters in the same encoding as the string itself, i.e. 16 bit if a 16 bit + * string and so on. + */ + pANTLR3_STRING (*newStr) (struct ANTLR3_STRING_FACTORY_struct * factory, pANTLR3_UINT8 string); + + /** Pointer to function that manufactures a string from a given pointer and length. The pointer should + * point to 8 bit characters regardless of the actual encoding of the string. The 8 bit characters + * will be converted to the actual string encoding on the fly. + */ + pANTLR3_STRING (*newStr8) (struct ANTLR3_STRING_FACTORY_struct * factory, pANTLR3_UINT8 string); + + /** Pointer to function that deletes the string altogether + */ + void (*destroy) (struct ANTLR3_STRING_FACTORY_struct * factory, pANTLR3_STRING string); + + /** Pointer to function that returns a copy of the string in printable form without any control + * characters in it. + */ + pANTLR3_STRING (*printable)(struct ANTLR3_STRING_FACTORY_struct * factory, pANTLR3_STRING string); + + /** Pointer to function that closes the factory + */ + void (*close) (struct ANTLR3_STRING_FACTORY_struct * factory); + +} + ANTLR3_STRING_FACTORY; + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/antlr-3.1.3/runtime/C/include/antlr3stringstream.h b/antlr-3.1.3/runtime/C/include/antlr3stringstream.h new file mode 100644 index 0000000..6d5a0a4 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3stringstream.h @@ -0,0 +1,36 @@ +#ifndef _ANTLR3_STRINGSTREAM_H +#define _ANTLR3_STRINGSTREAM_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> + + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3tokenstream.h b/antlr-3.1.3/runtime/C/include/antlr3tokenstream.h new file mode 100644 index 0000000..3a826ae --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3tokenstream.h @@ -0,0 +1,293 @@ +/** \file + * Defines the interface for an ANTLR3 common token stream. Custom token streams should create + * one of these and then override any functions by installing their own pointers + * to implement the various functions. + */ +#ifndef _ANTLR3_TOKENSTREAM_H +#define _ANTLR3_TOKENSTREAM_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3string.h> +#include <antlr3collections.h> +#include <antlr3input.h> +#include <antlr3commontoken.h> +#include <antlr3bitset.h> +#include <antlr3debugeventlistener.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** Definition of a token source, which has a pointer to a function that + * returns the next token (using a token factory if it is going to be + * efficient) and a pointer to an ANTLR3_INPUT_STREAM. This is slightly + * different to the Java interface because we have no way to implement + * multiple interfaces without defining them in the interface structure + * or casting (void *), which is too convoluted. + */ +typedef struct ANTLR3_TOKEN_SOURCE_struct +{ + /** Pointer to a function that returns the next token in the stream. + */ + pANTLR3_COMMON_TOKEN (*nextToken)(struct ANTLR3_TOKEN_SOURCE_struct * tokenSource); + + /** Whoever is providing tokens, needs to provide a string factory too + */ + pANTLR3_STRING_FACTORY strFactory; + + /** A special pre-allocated token, which signifies End Of Tokens. Because this must + * be set up with the current input index and so on, we embed the structure and + * return the address of it. It is marked as factoryMade, so that it is never + * attempted to be freed. + */ + ANTLR3_COMMON_TOKEN eofToken; + + /// A special pre-allocated token, which is returned by mTokens() if the + /// lexer rule said to just skip the generated token altogether. + /// Having this single token stops us wasting memory by have the token factory + /// actually create something that we are going to SKIP(); anyway. + /// + ANTLR3_COMMON_TOKEN skipToken; + + /** Whatever is supplying the token source interface, needs a pointer to + * itself so that this pointer can be passed to it when the nextToken + * function is called. + */ + void * super; + + /** When the token source is constructed, it is populated with the file + * name from whence the tokens were produced by the lexer. This pointer is a + * copy of the one supplied by the CharStream (and may be NULL) so should + * not be manipulated other than to copy or print it. + */ + pANTLR3_STRING fileName; +} + ANTLR3_TOKEN_SOURCE; + +/** Definition of the ANTLR3 common token stream interface. + * \remark + * Much of the documentation for this interface is stolen from Ter's Java implementation. + */ +typedef struct ANTLR3_TOKEN_STREAM_struct +{ + /** Pointer to the token source for this stream + */ + pANTLR3_TOKEN_SOURCE tokenSource; + + /** Whatever is providing this interface needs a pointer to itself + * so that this can be passed back to it whenever the api functions + * are called. + */ + void * super; + + /** All input streams implement the ANTLR3_INT_STREAM interface... + */ + pANTLR3_INT_STREAM istream; + + /// Debugger interface, is this is a debugging token stream + /// + pANTLR3_DEBUG_EVENT_LISTENER debugger; + + /// Indicates the initial stream state for dbgConsume() + /// + ANTLR3_BOOLEAN initialStreamState; + + /** Get Token at current input pointer + i ahead where i=1 is next Token. + * i<0 indicates tokens in the past. So -1 is previous token and -2 is + * two tokens ago. LT(0) is undefined. For i>=n, return Token.EOFToken. + * Return null for LT(0) and any index that results in an absolute address + * that is negative. + */ + pANTLR3_COMMON_TOKEN (*_LT) (struct ANTLR3_TOKEN_STREAM_struct * tokenStream, ANTLR3_INT32 k); + + /** Get a token at an absolute index i; 0..n-1. This is really only + * needed for profiling and debugging and token stream rewriting. + * If you don't want to buffer up tokens, then this method makes no + * sense for you. Naturally you can't use the rewrite stream feature. + * I believe DebugTokenStream can easily be altered to not use + * this method, removing the dependency. + */ + pANTLR3_COMMON_TOKEN (*get) (struct ANTLR3_TOKEN_STREAM_struct * tokenStream, ANTLR3_UINT32 i); + + /** Where is this stream pulling tokens from? This is not the name, but + * a pointer into an interface that contains a ANTLR3_TOKEN_SOURCE interface. + * The Token Source interface contains a pointer to the input stream and a pointer + * to a function that returns the next token. + */ + pANTLR3_TOKEN_SOURCE (*getTokenSource) (struct ANTLR3_TOKEN_STREAM_struct * tokenStream); + + /** Function that installs a token source for teh stream + */ + void (*setTokenSource) (struct ANTLR3_TOKEN_STREAM_struct * tokenStream, + pANTLR3_TOKEN_SOURCE tokenSource); + + /** Return the text of all the tokens in the stream, as the old tramp in + * Leeds market used to say; "Get the lot!" + */ + pANTLR3_STRING (*toString) (struct ANTLR3_TOKEN_STREAM_struct * tokenStream); + + /** Return the text of all tokens from start to stop, inclusive. + * If the stream does not buffer all the tokens then it can just + * return an empty ANTLR3_STRING or NULL; Grammars should not access $ruleLabel.text in + * an action in that case. + */ + pANTLR3_STRING (*toStringSS) (struct ANTLR3_TOKEN_STREAM_struct * tokenStream, ANTLR3_UINT32 start, ANTLR3_UINT32 stop); + + /** Because the user is not required to use a token with an index stored + * in it, we must provide a means for two token objects themselves to + * indicate the start/end location. Most often this will just delegate + * to the other toString(int,int). This is also parallel with + * the pTREENODE_STREAM->toString(Object,Object). + */ + pANTLR3_STRING (*toStringTT) (struct ANTLR3_TOKEN_STREAM_struct * tokenStream, pANTLR3_COMMON_TOKEN start, pANTLR3_COMMON_TOKEN stop); + + + /** Function that sets the token stream into debugging mode + */ + void (*setDebugListener) (struct ANTLR3_TOKEN_STREAM_struct * tokenStream, pANTLR3_DEBUG_EVENT_LISTENER debugger); + + /** Function that knows how to free the memory for an ANTLR3_TOKEN_STREAM + */ + void (*free) (struct ANTLR3_TOKEN_STREAM_struct * tokenStream); +} + ANTLR3_TOKEN_STREAM; + +/** Common token stream is an implementation of ANTLR_TOKEN_STREAM for the default + * parsers and recognizers. You may of course build your own implementation if + * you are so inclined. + */ +typedef struct ANTLR3_COMMON_TOKEN_STREAM_struct +{ + /** The ANTLR3_TOKEN_STREAM interface implementation, which also includes + * the intstream implementation. We could duplicate the pANTLR_INT_STREAM + * in this interface and initialize it to a copy, but this could be confusing + * it just results in one more level of indirection and I think that with + * judicial use of 'const' later, the optimizer will do decent job. + */ + pANTLR3_TOKEN_STREAM tstream; + + /** Whatever is supplying the COMMON_TOKEN_STREAM needs a pointer to itself + * so that this can be accessed by any of the API functions which it implements. + */ + void * super; + + /** Records every single token pulled from the source indexed by the token index. + * There might be more efficient ways to do this, such as referencing directly in to + * the token factory pools, but for now this is convenient and the ANTLR3_LIST is not + * a huge overhead as it only stores pointers anyway, but allows for iterations and + * so on. + */ + pANTLR3_VECTOR tokens; + + /** Override map of tokens. If a token type has an entry in here, then + * the pointer in the table points to an int, being the override channel number + * that should always be used for this token type. + */ + pANTLR3_LIST channelOverrides; + + /** Discared set. If a token has an entry in this table, then it is thrown + * away (data pointer is always NULL). + */ + pANTLR3_LIST discardSet; + + /* The channel number that this token stream is tuned to. For instance, whitespace + * is usually tuned to channel 99, which no token stream would normally tune to and + * so it is thrown away. + */ + ANTLR3_UINT32 channel; + + /** If this flag is set to ANTLR3_TRUE, then tokens that the stream sees that are not + * in the channel that this stream is tuned to, are not tracked in the + * tokens table. When set to false, ALL tokens are added to the tracking. + */ + ANTLR3_BOOLEAN discardOffChannel; + + /** The index into the tokens list of the current token (the next one that will be + * consumed. p = -1 indicates that the token list is empty. + */ + ANTLR3_INT32 p; + + /** A simple filter mechanism whereby you can tell this token stream + * to force all tokens of type ttype to be on channel. For example, + * when interpreting, we cannot exec actions so we need to tell + * the stream to force all WS and NEWLINE to be a different, ignored + * channel. + */ + void (*setTokenTypeChannel) (struct ANTLR3_COMMON_TOKEN_STREAM_struct * tokenStream, + ANTLR3_UINT32 ttype, ANTLR3_UINT32 channel); + + /** Add a particular token type to the discard set. If a token is found to belong + * to this set, then it is skipped/thrown away + */ + void (*discardTokenType) (struct ANTLR3_COMMON_TOKEN_STREAM_struct * tokenStream, ANTLR3_INT32 ttype); + + /** Signal to discard off channel tokens from here on in. + */ + void (*discardOffChannelToks)(struct ANTLR3_COMMON_TOKEN_STREAM_struct * tokenStream, ANTLR3_BOOLEAN discard); + + /** Function that returns a pointer to the ANTLR3_LIST of all tokens + * in the stream (this causes the buffer to fill if we have not get any yet) + */ + pANTLR3_VECTOR (*getTokens) (struct ANTLR3_COMMON_TOKEN_STREAM_struct * tokenStream); + + /** Function that returns all the tokens between a start and a stop index. + * TODO: This is a new list (Ack! Maybe this is a reason to have factories for LISTS and HASHTABLES etc :-( come back to this) + */ + pANTLR3_LIST (*getTokenRange) (struct ANTLR3_COMMON_TOKEN_STREAM_struct * tokenStream, ANTLR3_UINT32 start, ANTLR3_UINT32 stop); + + /** Function that returns all the tokens indicated by the specified bitset, within a range of tokens + */ + pANTLR3_LIST (*getTokensSet) (struct ANTLR3_COMMON_TOKEN_STREAM_struct * tokenStream, + ANTLR3_UINT32 start, ANTLR3_UINT32 stop, pANTLR3_BITSET types); + + /** Function that returns all the tokens indicated by being a member of the supplied List + */ + pANTLR3_LIST (*getTokensList) (struct ANTLR3_COMMON_TOKEN_STREAM_struct * tokenStream, + ANTLR3_UINT32 start, ANTLR3_UINT32 stop, pANTLR3_LIST list); + + /** Function that returns all tokens of a certain type within a range. + */ + pANTLR3_LIST (*getTokensType) (struct ANTLR3_COMMON_TOKEN_STREAM_struct * tokenStream, + ANTLR3_UINT32 start, ANTLR3_UINT32 stop, ANTLR3_UINT32 type); + + + /** Function that knows how to free an ANTLR3_COMMON_TOKEN_STREAM + */ + void (*free) (struct ANTLR3_COMMON_TOKEN_STREAM_struct * tokenStream); +} + ANTLR3_COMMON_TOKEN_STREAM; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/antlr-3.1.3/runtime/C/include/antlr3treeparser.h b/antlr-3.1.3/runtime/C/include/antlr3treeparser.h new file mode 100644 index 0000000..8ee0e58 --- /dev/null +++ b/antlr-3.1.3/runtime/C/include/antlr3treeparser.h @@ -0,0 +1,81 @@ +#ifndef ANTLR3TREEPARSER_H +#define ANTLR3TREEPARSER_H + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3baserecognizer.h> +#include <antlr3commontreenodestream.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** Internal structure representing an element in a hash bucket. + * Stores the original key so that duplicate keys can be rejected + * if necessary, and contains function can be supported If the hash key + * could be unique I would have invented the perfect compression algorithm ;-) + */ +typedef struct ANTLR3_TREE_PARSER_struct +{ + /** Pointer to any super class + */ + void * super; + + /** A pointer to the base recognizer, where most of the parser functions actually + * live because they are shared between parser and tree parser and this is the + * easier way than copying the interface all over the place. Macros hide this + * for the generated code so it is easier on the eye (though not the debugger ;-). + */ + pANTLR3_BASE_RECOGNIZER rec; + + /** Pointer to the common tree node stream for the parser + */ + pANTLR3_COMMON_TREE_NODE_STREAM ctnstream; + + /** Set the input stream and reset the parser + */ + void (*setTreeNodeStream) (struct ANTLR3_TREE_PARSER_struct * parser, pANTLR3_COMMON_TREE_NODE_STREAM input); + + /** Return a pointer to the input stream + */ + pANTLR3_COMMON_TREE_NODE_STREAM (*getTreeNodeStream) (struct ANTLR3_TREE_PARSER_struct * parser); + + /** Pointer to a function that knows how to free resources of an ANTLR3 tree parser. + */ + void (*free) (struct ANTLR3_TREE_PARSER_struct * parser); +} + ANTLR3_TREE_PARSER; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/antlr-3.1.3/runtime/C/src/antlr3baserecognizer.c b/antlr-3.1.3/runtime/C/src/antlr3baserecognizer.c new file mode 100644 index 0000000..330f0f1 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3baserecognizer.c @@ -0,0 +1,2234 @@ +/** \file + * Contains the base functions that all recognizers require. + * Any function can be overridden by a lexer/parser/tree parser or by the + * ANTLR3 programmer. + * + * \addtogroup pANTLR3_BASE_RECOGNIZER + * @{ + */ +#include <antlr3baserecognizer.h> + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#ifdef ANTLR3_WINDOWS +#pragma warning( disable : 4100 ) +#endif + +/* Interface functions -standard implementations cover parser and treeparser + * almost completely but are overridden by the parser or tree parser as needed. Lexer overrides + * most of these functions. + */ +static void beginResync (pANTLR3_BASE_RECOGNIZER recognizer); +static pANTLR3_BITSET computeErrorRecoverySet (pANTLR3_BASE_RECOGNIZER recognizer); +static void endResync (pANTLR3_BASE_RECOGNIZER recognizer); +static void beginBacktrack (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 level); +static void endBacktrack (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 level, ANTLR3_BOOLEAN successful); + +static void * match (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 ttype, pANTLR3_BITSET_LIST follow); +static void matchAny (pANTLR3_BASE_RECOGNIZER recognizer); +static void mismatch (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 ttype, pANTLR3_BITSET_LIST follow); +static ANTLR3_BOOLEAN mismatchIsUnwantedToken (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM is, ANTLR3_UINT32 ttype); +static ANTLR3_BOOLEAN mismatchIsMissingToken (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM is, pANTLR3_BITSET_LIST follow); +static void reportError (pANTLR3_BASE_RECOGNIZER recognizer); +static pANTLR3_BITSET computeCSRuleFollow (pANTLR3_BASE_RECOGNIZER recognizer); +static pANTLR3_BITSET combineFollows (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_BOOLEAN exact); +static void displayRecognitionError (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_UINT8 * tokenNames); +static void recover (pANTLR3_BASE_RECOGNIZER recognizer); +static void * recoverFromMismatchedToken (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 ttype, pANTLR3_BITSET_LIST follow); +static void * recoverFromMismatchedSet (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_BITSET_LIST follow); +static ANTLR3_BOOLEAN recoverFromMismatchedElement(pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_BITSET_LIST follow); +static void consumeUntil (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 tokenType); +static void consumeUntilSet (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_BITSET set); +static pANTLR3_STACK getRuleInvocationStack (pANTLR3_BASE_RECOGNIZER recognizer); +static pANTLR3_STACK getRuleInvocationStackNamed (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_UINT8 name); +static pANTLR3_HASH_TABLE toStrings (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_HASH_TABLE); +static ANTLR3_MARKER getRuleMemoization (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_INTKEY ruleIndex, ANTLR3_MARKER ruleParseStart); +static ANTLR3_BOOLEAN alreadyParsedRule (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_MARKER ruleIndex); +static void memoize (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_MARKER ruleIndex, ANTLR3_MARKER ruleParseStart); +static ANTLR3_BOOLEAN synpred (pANTLR3_BASE_RECOGNIZER recognizer, void * ctx, void (*predicate)(void * ctx)); +static void reset (pANTLR3_BASE_RECOGNIZER recognizer); +static void freeBR (pANTLR3_BASE_RECOGNIZER recognizer); +static void * getCurrentInputSymbol (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM istream); +static void * getMissingSymbol (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM istream, pANTLR3_EXCEPTION e, + ANTLR3_UINT32 expectedTokenType, pANTLR3_BITSET_LIST follow); +static ANTLR3_UINT32 getNumberOfSyntaxErrors (pANTLR3_BASE_RECOGNIZER recognizer); + +ANTLR3_API pANTLR3_BASE_RECOGNIZER +antlr3BaseRecognizerNew(ANTLR3_UINT32 type, ANTLR3_UINT32 sizeHint, pANTLR3_RECOGNIZER_SHARED_STATE state) +{ + pANTLR3_BASE_RECOGNIZER recognizer; + + // Allocate memory for the structure + // + recognizer = (pANTLR3_BASE_RECOGNIZER) ANTLR3_MALLOC((size_t)sizeof(ANTLR3_BASE_RECOGNIZER)); + + if (recognizer == NULL) + { + // Allocation failed + // + return NULL; + } + + + // If we have been supplied with a pre-existing recognizer state + // then we just install it, otherwise we must create one from scratch + // + if (state == NULL) + { + recognizer->state = (pANTLR3_RECOGNIZER_SHARED_STATE) ANTLR3_CALLOC(1, (size_t)sizeof(ANTLR3_RECOGNIZER_SHARED_STATE)); + + if (recognizer->state == NULL) + { + ANTLR3_FREE(recognizer); + return NULL; + } + + // Initialize any new recognizer state + // + recognizer->state->errorRecovery = ANTLR3_FALSE; + recognizer->state->lastErrorIndex = -1; + recognizer->state->failed = ANTLR3_FALSE; + recognizer->state->errorCount = 0; + recognizer->state->backtracking = 0; + recognizer->state->following = NULL; + recognizer->state->ruleMemo = NULL; + recognizer->state->tokenNames = NULL; + recognizer->state->sizeHint = sizeHint; + recognizer->state->tokSource = NULL; + recognizer->state->tokFactory = NULL; + + // Rather than check to see if we must initialize + // the stack every time we are asked for an new rewrite stream + // we just always create an empty stack and then just + // free it when the base recognizer is freed. + // + recognizer->state->rStreams = antlr3VectorNew(0); // We don't know the size. + + if (recognizer->state->rStreams == NULL) + { + // Out of memory + // + ANTLR3_FREE(recognizer->state); + ANTLR3_FREE(recognizer); + return NULL; + } + } + else + { + // Install the one we were given, and do not reset it here + // as it will either already have been initialized or will + // be in a state that needs to be preserved. + // + recognizer->state = state; + } + + // Install the BR API + // + recognizer->alreadyParsedRule = alreadyParsedRule; + recognizer->beginResync = beginResync; + recognizer->combineFollows = combineFollows; + recognizer->beginBacktrack = beginBacktrack; + recognizer->endBacktrack = endBacktrack; + recognizer->computeCSRuleFollow = computeCSRuleFollow; + recognizer->computeErrorRecoverySet = computeErrorRecoverySet; + recognizer->consumeUntil = consumeUntil; + recognizer->consumeUntilSet = consumeUntilSet; + recognizer->displayRecognitionError = displayRecognitionError; + recognizer->endResync = endResync; + recognizer->exConstruct = antlr3MTExceptionNew; + recognizer->getRuleInvocationStack = getRuleInvocationStack; + recognizer->getRuleInvocationStackNamed = getRuleInvocationStackNamed; + recognizer->getRuleMemoization = getRuleMemoization; + recognizer->match = match; + recognizer->matchAny = matchAny; + recognizer->memoize = memoize; + recognizer->mismatch = mismatch; + recognizer->mismatchIsUnwantedToken = mismatchIsUnwantedToken; + recognizer->mismatchIsMissingToken = mismatchIsMissingToken; + recognizer->recover = recover; + recognizer->recoverFromMismatchedElement= recoverFromMismatchedElement; + recognizer->recoverFromMismatchedSet = recoverFromMismatchedSet; + recognizer->recoverFromMismatchedToken = recoverFromMismatchedToken; + recognizer->getNumberOfSyntaxErrors = getNumberOfSyntaxErrors; + recognizer->reportError = reportError; + recognizer->reset = reset; + recognizer->synpred = synpred; + recognizer->toStrings = toStrings; + recognizer->getCurrentInputSymbol = getCurrentInputSymbol; + recognizer->getMissingSymbol = getMissingSymbol; + recognizer->debugger = NULL; + + recognizer->free = freeBR; + + /* Initialize variables + */ + recognizer->type = type; + + + return recognizer; +} +static void +freeBR (pANTLR3_BASE_RECOGNIZER recognizer) +{ + pANTLR3_EXCEPTION thisE; + + // Did we have a state allocated? + // + if (recognizer->state != NULL) + { + // Free any rule memoization we set up + // + if (recognizer->state->ruleMemo != NULL) + { + recognizer->state->ruleMemo->free(recognizer->state->ruleMemo); + recognizer->state->ruleMemo = NULL; + } + + // Free any exception space we have left around + // + thisE = recognizer->state->exception; + if (thisE != NULL) + { + thisE->freeEx(thisE); + } + + // Free any rewrite streams we have allocated + // + if (recognizer->state->rStreams != NULL) + { + recognizer->state->rStreams->free(recognizer->state->rStreams); + } + + // Free up any token factory we created (error recovery for instance) + // + if (recognizer->state->tokFactory != NULL) + { + recognizer->state->tokFactory->close(recognizer->state->tokFactory); + } + // Free the shared state memory + // + ANTLR3_FREE(recognizer->state); + } + + // Free the actual recognizer space + // + ANTLR3_FREE(recognizer); +} + +/** + * Creates a new Mismatched Token Exception and inserts in the recognizer + * exception stack. + * + * \param recognizer + * Context pointer for this recognizer + * + */ +ANTLR3_API void +antlr3MTExceptionNew(pANTLR3_BASE_RECOGNIZER recognizer) +{ + /* Create a basic recognition exception structure + */ + antlr3RecognitionExceptionNew(recognizer); + + /* Now update it to indicate this is a Mismatched token exception + */ + recognizer->state->exception->name = ANTLR3_MISMATCHED_EX_NAME; + recognizer->state->exception->type = ANTLR3_MISMATCHED_TOKEN_EXCEPTION; + + return; +} + +ANTLR3_API void +antlr3RecognitionExceptionNew(pANTLR3_BASE_RECOGNIZER recognizer) +{ + pANTLR3_EXCEPTION ex; + pANTLR3_LEXER lexer; + pANTLR3_PARSER parser; + pANTLR3_TREE_PARSER tparser; + + pANTLR3_INPUT_STREAM ins; + pANTLR3_INT_STREAM is; + pANTLR3_COMMON_TOKEN_STREAM cts; + pANTLR3_TREE_NODE_STREAM tns; + + ins = NULL; + cts = NULL; + tns = NULL; + is = NULL; + lexer = NULL; + parser = NULL; + tparser = NULL; + + switch (recognizer->type) + { + case ANTLR3_TYPE_LEXER: + + lexer = (pANTLR3_LEXER) (recognizer->super); + ins = lexer->input; + is = ins->istream; + + break; + + case ANTLR3_TYPE_PARSER: + + parser = (pANTLR3_PARSER) (recognizer->super); + cts = (pANTLR3_COMMON_TOKEN_STREAM)(parser->tstream->super); + is = parser->tstream->istream; + + break; + + case ANTLR3_TYPE_TREE_PARSER: + + tparser = (pANTLR3_TREE_PARSER) (recognizer->super); + tns = tparser->ctnstream->tnstream; + is = tns->istream; + + break; + + default: + + ANTLR3_FPRINTF(stderr, "Base recognizer function antlr3RecognitionExceptionNew called by unknown parser type - provide override for this function\n"); + return; + + break; + } + + /* Create a basic exception structure + */ + ex = antlr3ExceptionNew(ANTLR3_RECOGNITION_EXCEPTION, + (void *)ANTLR3_RECOGNITION_EX_NAME, + NULL, + ANTLR3_FALSE); + + /* Rest of information depends on the base type of the + * input stream. + */ + switch (is->type & ANTLR3_INPUT_MASK) + { + case ANTLR3_CHARSTREAM: + + ex->c = is->_LA (is, 1); /* Current input character */ + ex->line = ins->getLine (ins); /* Line number comes from stream */ + ex->charPositionInLine = ins->getCharPositionInLine (ins); /* Line offset also comes from the stream */ + ex->index = is->index (is); + ex->streamName = ins->fileName; + ex->message = "Unexpected character"; + break; + + case ANTLR3_TOKENSTREAM: + + ex->token = cts->tstream->_LT (cts->tstream, 1); /* Current input token */ + ex->line = ((pANTLR3_COMMON_TOKEN)(ex->token))->getLine (ex->token); + ex->charPositionInLine = ((pANTLR3_COMMON_TOKEN)(ex->token))->getCharPositionInLine (ex->token); + ex->index = cts->tstream->istream->index (cts->tstream->istream); + if (((pANTLR3_COMMON_TOKEN)(ex->token))->type == ANTLR3_TOKEN_EOF) + { + ex->streamName = NULL; + } + else + { + ex->streamName = ((pANTLR3_COMMON_TOKEN)(ex->token))->input->fileName; + } + ex->message = "Unexpected token"; + break; + + case ANTLR3_COMMONTREENODE: + + ex->token = tns->_LT (tns, 1); /* Current input tree node */ + ex->line = ((pANTLR3_BASE_TREE)(ex->token))->getLine (ex->token); + ex->charPositionInLine = ((pANTLR3_BASE_TREE)(ex->token))->getCharPositionInLine (ex->token); + ex->index = tns->istream->index (tns->istream); + + // Are you ready for this? Deep breath now... + // + { + pANTLR3_COMMON_TREE tnode; + + tnode = ((pANTLR3_COMMON_TREE)(((pANTLR3_BASE_TREE)(ex->token))->super)); + + if (tnode->token == NULL) + { + ex->streamName = ((pANTLR3_BASE_TREE)(ex->token))->strFactory->newStr(((pANTLR3_BASE_TREE)(ex->token))->strFactory, (pANTLR3_UINT8)"-unknown source-"); + } + else + { + if (tnode->token->input == NULL) + { + ex->streamName = NULL; + } + else + { + ex->streamName = tnode->token->input->fileName; + } + } + ex->message = "Unexpected node"; + } + break; + } + + ex->input = is; + ex->nextException = recognizer->state->exception; /* So we don't leak the memory */ + recognizer->state->exception = ex; + recognizer->state->error = ANTLR3_TRUE; /* Exception is outstanding */ + + return; +} + + +/// Match current input symbol against ttype. Upon error, do one token +/// insertion or deletion if possible. +/// To turn off single token insertion or deletion error +/// recovery, override mismatchRecover() and have it call +/// plain mismatch(), which does not recover. Then any error +/// in a rule will cause an exception and immediate exit from +/// rule. Rule would recover by resynchronizing to the set of +/// symbols that can follow rule ref. +/// +static void * +match( pANTLR3_BASE_RECOGNIZER recognizer, + ANTLR3_UINT32 ttype, pANTLR3_BITSET_LIST follow) +{ + pANTLR3_PARSER parser; + pANTLR3_TREE_PARSER tparser; + pANTLR3_INT_STREAM is; + void * matchedSymbol; + + switch (recognizer->type) + { + case ANTLR3_TYPE_PARSER: + + parser = (pANTLR3_PARSER) (recognizer->super); + tparser = NULL; + is = parser->tstream->istream; + + break; + + case ANTLR3_TYPE_TREE_PARSER: + + tparser = (pANTLR3_TREE_PARSER) (recognizer->super); + parser = NULL; + is = tparser->ctnstream->tnstream->istream; + + break; + + default: + + ANTLR3_FPRINTF(stderr, "Base recognizer function 'match' called by unknown parser type - provide override for this function\n"); + return ANTLR3_FALSE; + + break; + } + + // Pick up the current input token/node for assignment to labels + // + matchedSymbol = recognizer->getCurrentInputSymbol(recognizer, is); + + if (is->_LA(is, 1) == ttype) + { + // The token was the one we were told to expect + // + is->consume(is); // Consume that token from the stream + recognizer->state->errorRecovery = ANTLR3_FALSE; // Not in error recovery now (if we were) + recognizer->state->failed = ANTLR3_FALSE; // The match was a success + return matchedSymbol; // We are done + } + + // We did not find the expected token type, if we are backtracking then + // we just set the failed flag and return. + // + if (recognizer->state->backtracking > 0) + { + // Backtracking is going on + // + recognizer->state->failed = ANTLR3_TRUE; + return matchedSymbol; + } + + // We did not find the expected token and there is no backtracking + // going on, so we mismatch, which creates an exception in the recognizer exception + // stack. + // + matchedSymbol = recognizer->recoverFromMismatchedToken(recognizer, ttype, follow); + return matchedSymbol; +} + +/// Consumes the next token, whatever it is, and resets the recognizer state +/// so that it is not in error. +/// +/// \param recognizer +/// Recognizer context pointer +/// +static void +matchAny(pANTLR3_BASE_RECOGNIZER recognizer) +{ + pANTLR3_PARSER parser; + pANTLR3_TREE_PARSER tparser; + pANTLR3_INT_STREAM is; + + switch (recognizer->type) + { + case ANTLR3_TYPE_PARSER: + + parser = (pANTLR3_PARSER) (recognizer->super); + tparser = NULL; + is = parser->tstream->istream; + + break; + + case ANTLR3_TYPE_TREE_PARSER: + + tparser = (pANTLR3_TREE_PARSER) (recognizer->super); + parser = NULL; + is = tparser->ctnstream->tnstream->istream; + + break; + + default: + + ANTLR3_FPRINTF(stderr, "Base recognizer function 'matchAny' called by unknown parser type - provide override for this function\n"); + return; + + break; + } + recognizer->state->errorRecovery = ANTLR3_FALSE; + recognizer->state->failed = ANTLR3_FALSE; + is->consume(is); + + return; +} +/// +/// +static ANTLR3_BOOLEAN +mismatchIsUnwantedToken(pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM is, ANTLR3_UINT32 ttype) +{ + ANTLR3_UINT32 nextt; + + nextt = is->_LA(is, 2); + + if (nextt == ttype) + { + if (recognizer->state->exception != NULL) + { + recognizer->state->exception->expecting = nextt; + } + return ANTLR3_TRUE; // This token is unknown, but the next one is the one we wanted + } + else + { + return ANTLR3_FALSE; // Neither this token, nor the one following is the one we wanted + } +} + +/// +/// +static ANTLR3_BOOLEAN +mismatchIsMissingToken(pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM is, pANTLR3_BITSET_LIST follow) +{ + ANTLR3_BOOLEAN retcode; + pANTLR3_BITSET followClone; + pANTLR3_BITSET viableTokensFollowingThisRule; + + if (follow == NULL) + { + // There is no information about the tokens that can follow the last one + // hence we must say that the current one we found is not a member of the + // follow set and does not indicate a missing token. We will just consume this + // single token and see if the parser works it out from there. + // + return ANTLR3_FALSE; + } + + followClone = NULL; + viableTokensFollowingThisRule = NULL; + + // The C bitset maps are laid down at compile time by the + // C code generation. Hence we cannot remove things from them + // and so on. So, in order to remove EOR (if we need to) then + // we clone the static bitset. + // + followClone = antlr3BitsetLoad(follow); + if (followClone == NULL) + { + return ANTLR3_FALSE; + } + + // Compute what can follow this grammar reference + // + if (followClone->isMember(followClone, ANTLR3_EOR_TOKEN_TYPE)) + { + // EOR can follow, but if we are not the start symbol, we + // need to remove it. + // + if (recognizer->state->following->vector->count >= 0) + { + followClone->remove(followClone, ANTLR3_EOR_TOKEN_TYPE); + } + + // Now compute the visiable tokens that can follow this rule, according to context + // and make them part of the follow set. + // + viableTokensFollowingThisRule = recognizer->computeCSRuleFollow(recognizer); + followClone->borInPlace(followClone, viableTokensFollowingThisRule); + } + + /// if current token is consistent with what could come after set + /// then we know we're missing a token; error recovery is free to + /// "insert" the missing token + /// + /// BitSet cannot handle negative numbers like -1 (EOF) so I leave EOR + /// in follow set to indicate that the fall of the start symbol is + /// in the set (EOF can follow). + /// + if ( followClone->isMember(followClone, is->_LA(is, 1)) + || followClone->isMember(followClone, ANTLR3_EOR_TOKEN_TYPE) + ) + { + retcode = ANTLR3_TRUE; + } + else + { + retcode = ANTLR3_FALSE; + } + + if (viableTokensFollowingThisRule != NULL) + { + viableTokensFollowingThisRule->free(viableTokensFollowingThisRule); + } + if (followClone != NULL) + { + followClone->free(followClone); + } + + return retcode; + +} + +/// Factor out what to do upon token mismatch so tree parsers can behave +/// differently. Override and call mismatchRecover(input, ttype, follow) +/// to get single token insertion and deletion. Use this to turn off +/// single token insertion and deletion. Override mismatchRecover +/// to call this instead. +/// +/// \remark mismatch only works for parsers and must be overridden for anything else. +/// +static void +mismatch(pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 ttype, pANTLR3_BITSET_LIST follow) +{ + pANTLR3_PARSER parser; + pANTLR3_TREE_PARSER tparser; + pANTLR3_INT_STREAM is; + + // Install a mismatched token exception in the exception stack + // + antlr3MTExceptionNew(recognizer); + recognizer->state->exception->expecting = ttype; + + switch (recognizer->type) + { + case ANTLR3_TYPE_PARSER: + + parser = (pANTLR3_PARSER) (recognizer->super); + tparser = NULL; + is = parser->tstream->istream; + + break; + + default: + + ANTLR3_FPRINTF(stderr, "Base recognizer function 'mismatch' called by unknown parser type - provide override for this function\n"); + return; + + break; + } + + if (mismatchIsUnwantedToken(recognizer, is, ttype)) + { + // Create a basic recognition exception structure + // + antlr3RecognitionExceptionNew(recognizer); + + // Now update it to indicate this is an unwanted token exception + // + recognizer->state->exception->name = ANTLR3_UNWANTED_TOKEN_EXCEPTION_NAME; + recognizer->state->exception->type = ANTLR3_UNWANTED_TOKEN_EXCEPTION; + + return; + } + + if (mismatchIsMissingToken(recognizer, is, follow)) + { + // Create a basic recognition exception structure + // + antlr3RecognitionExceptionNew(recognizer); + + // Now update it to indicate this is an unwanted token exception + // + recognizer->state->exception->name = ANTLR3_MISSING_TOKEN_EXCEPTION_NAME; + recognizer->state->exception->type = ANTLR3_MISSING_TOKEN_EXCEPTION; + + return; + } + + // Just a mismatched token is all we can dtermine + // + antlr3MTExceptionNew(recognizer); + + return; +} +/// Report a recognition problem. +/// +/// This method sets errorRecovery to indicate the parser is recovering +/// not parsing. Once in recovery mode, no errors are generated. +/// To get out of recovery mode, the parser must successfully match +/// a token (after a resync). So it will go: +/// +/// 1. error occurs +/// 2. enter recovery mode, report error +/// 3. consume until token found in resynch set +/// 4. try to resume parsing +/// 5. next match() will reset errorRecovery mode +/// +/// If you override, make sure to update errorCount if you care about that. +/// +static void +reportError (pANTLR3_BASE_RECOGNIZER recognizer) +{ + if (recognizer->state->errorRecovery == ANTLR3_TRUE) + { + // Already in error recovery so don't display another error while doing so + // + return; + } + + // Signal we are in error recovery now + // + recognizer->state->errorRecovery = ANTLR3_TRUE; + + // Indicate this recognizer had an error while processing. + // + recognizer->state->errorCount++; + + // Call the error display routine + // + recognizer->displayRecognitionError(recognizer, recognizer->state->tokenNames); +} + +static void +beginBacktrack (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 level) +{ + if (recognizer->debugger != NULL) + { + recognizer->debugger->beginBacktrack(recognizer->debugger, level); + } +} + +static void +endBacktrack (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 level, ANTLR3_BOOLEAN successful) +{ + if (recognizer->debugger != NULL) + { + recognizer->debugger->endBacktrack(recognizer->debugger, level, successful); + } +} +static void +beginResync (pANTLR3_BASE_RECOGNIZER recognizer) +{ + if (recognizer->debugger != NULL) + { + recognizer->debugger->beginResync(recognizer->debugger); + } +} + +static void +endResync (pANTLR3_BASE_RECOGNIZER recognizer) +{ + if (recognizer->debugger != NULL) + { + recognizer->debugger->endResync(recognizer->debugger); + } +} + +/// Compute the error recovery set for the current rule. +/// Documentation below is from the Java implementation. +/// +/// During rule invocation, the parser pushes the set of tokens that can +/// follow that rule reference on the stack; this amounts to +/// computing FIRST of what follows the rule reference in the +/// enclosing rule. This local follow set only includes tokens +/// from within the rule; i.e., the FIRST computation done by +/// ANTLR stops at the end of a rule. +// +/// EXAMPLE +// +/// When you find a "no viable alt exception", the input is not +/// consistent with any of the alternatives for rule r. The best +/// thing to do is to consume tokens until you see something that +/// can legally follow a call to r *or* any rule that called r. +/// You don't want the exact set of viable next tokens because the +/// input might just be missing a token--you might consume the +/// rest of the input looking for one of the missing tokens. +/// +/// Consider grammar: +/// +/// a : '[' b ']' +/// | '(' b ')' +/// ; +/// b : c '^' INT ; +/// c : ID +/// | INT +/// ; +/// +/// At each rule invocation, the set of tokens that could follow +/// that rule is pushed on a stack. Here are the various "local" +/// follow sets: +/// +/// FOLLOW(b1_in_a) = FIRST(']') = ']' +/// FOLLOW(b2_in_a) = FIRST(')') = ')' +/// FOLLOW(c_in_b) = FIRST('^') = '^' +/// +/// Upon erroneous input "[]", the call chain is +/// +/// a -> b -> c +/// +/// and, hence, the follow context stack is: +/// +/// depth local follow set after call to rule +/// 0 <EOF> a (from main()) +/// 1 ']' b +/// 3 '^' c +/// +/// Notice that ')' is not included, because b would have to have +/// been called from a different context in rule a for ')' to be +/// included. +/// +/// For error recovery, we cannot consider FOLLOW(c) +/// (context-sensitive or otherwise). We need the combined set of +/// all context-sensitive FOLLOW sets--the set of all tokens that +/// could follow any reference in the call chain. We need to +/// resync to one of those tokens. Note that FOLLOW(c)='^' and if +/// we resync'd to that token, we'd consume until EOF. We need to +/// sync to context-sensitive FOLLOWs for a, b, and c: {']','^'}. +/// In this case, for input "[]", LA(1) is in this set so we would +/// not consume anything and after printing an error rule c would +/// return normally. It would not find the required '^' though. +/// At this point, it gets a mismatched token error and throws an +/// exception (since LA(1) is not in the viable following token +/// set). The rule exception handler tries to recover, but finds +/// the same recovery set and doesn't consume anything. Rule b +/// exits normally returning to rule a. Now it finds the ']' (and +/// with the successful match exits errorRecovery mode). +/// +/// So, you can see that the parser walks up call chain looking +/// for the token that was a member of the recovery set. +/// +/// Errors are not generated in errorRecovery mode. +/// +/// ANTLR's error recovery mechanism is based upon original ideas: +/// +/// "Algorithms + Data Structures = Programs" by Niklaus Wirth +/// +/// and +/// +/// "A note on error recovery in recursive descent parsers": +/// http://portal.acm.org/citation.cfm?id=947902.947905 +/// +/// Later, Josef Grosch had some good ideas: +/// +/// "Efficient and Comfortable Error Recovery in Recursive Descent +/// Parsers": +/// ftp://www.cocolab.com/products/cocktail/doca4.ps/ell.ps.zip +/// +/// Like Grosch I implemented local FOLLOW sets that are combined +/// at run-time upon error to avoid overhead during parsing. +/// +static pANTLR3_BITSET +computeErrorRecoverySet (pANTLR3_BASE_RECOGNIZER recognizer) +{ + return recognizer->combineFollows(recognizer, ANTLR3_FALSE); +} + +/// Compute the context-sensitive FOLLOW set for current rule. +/// Documentation below is from the Java runtime. +/// +/// This is the set of token types that can follow a specific rule +/// reference given a specific call chain. You get the set of +/// viable tokens that can possibly come next (look ahead depth 1) +/// given the current call chain. Contrast this with the +/// definition of plain FOLLOW for rule r: +/// +/// FOLLOW(r)={x | S=>*alpha r beta in G and x in FIRST(beta)} +/// +/// where x in T* and alpha, beta in V*; T is set of terminals and +/// V is the set of terminals and non terminals. In other words, +/// FOLLOW(r) is the set of all tokens that can possibly follow +/// references to r in///any* sentential form (context). At +/// runtime, however, we know precisely which context applies as +/// we have the call chain. We may compute the exact (rather +/// than covering superset) set of following tokens. +/// +/// For example, consider grammar: +/// +/// stat : ID '=' expr ';' // FOLLOW(stat)=={EOF} +/// | "return" expr '.' +/// ; +/// expr : atom ('+' atom)* ; // FOLLOW(expr)=={';','.',')'} +/// atom : INT // FOLLOW(atom)=={'+',')',';','.'} +/// | '(' expr ')' +/// ; +/// +/// The FOLLOW sets are all inclusive whereas context-sensitive +/// FOLLOW sets are precisely what could follow a rule reference. +/// For input input "i=(3);", here is the derivation: +/// +/// stat => ID '=' expr ';' +/// => ID '=' atom ('+' atom)* ';' +/// => ID '=' '(' expr ')' ('+' atom)* ';' +/// => ID '=' '(' atom ')' ('+' atom)* ';' +/// => ID '=' '(' INT ')' ('+' atom)* ';' +/// => ID '=' '(' INT ')' ';' +/// +/// At the "3" token, you'd have a call chain of +/// +/// stat -> expr -> atom -> expr -> atom +/// +/// What can follow that specific nested ref to atom? Exactly ')' +/// as you can see by looking at the derivation of this specific +/// input. Contrast this with the FOLLOW(atom)={'+',')',';','.'}. +/// +/// You want the exact viable token set when recovering from a +/// token mismatch. Upon token mismatch, if LA(1) is member of +/// the viable next token set, then you know there is most likely +/// a missing token in the input stream. "Insert" one by just not +/// throwing an exception. +/// +static pANTLR3_BITSET +computeCSRuleFollow (pANTLR3_BASE_RECOGNIZER recognizer) +{ + return recognizer->combineFollows(recognizer, ANTLR3_FALSE); +} + +/// Compute the current followset for the input stream. +/// +static pANTLR3_BITSET +combineFollows (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_BOOLEAN exact) +{ + pANTLR3_BITSET followSet; + pANTLR3_BITSET localFollowSet; + ANTLR3_UINT32 top; + ANTLR3_UINT32 i; + + top = recognizer->state->following->size(recognizer->state->following); + + followSet = antlr3BitsetNew(0); + localFollowSet = NULL; + + for (i = top; i>0; i--) + { + localFollowSet = antlr3BitsetLoad((pANTLR3_BITSET_LIST) recognizer->state->following->get(recognizer->state->following, i-1)); + + if (localFollowSet != NULL) + { + followSet->borInPlace(followSet, localFollowSet); + + if (exact == ANTLR3_TRUE) + { + if (localFollowSet->isMember(localFollowSet, ANTLR3_EOR_TOKEN_TYPE) == ANTLR3_FALSE) + { + // Only leave EOR in the set if at top (start rule); this lets us know + // if we have to include the follow(start rule); I.E., EOF + // + if (i>1) + { + followSet->remove(followSet, ANTLR3_EOR_TOKEN_TYPE); + } + } + else + { + break; // Cannot see End Of Rule from here, just drop out + } + } + localFollowSet->free(localFollowSet); + localFollowSet = NULL; + } + } + + if (localFollowSet != NULL) + { + localFollowSet->free(localFollowSet); + } + return followSet; +} + +/// Standard/Example error display method. +/// No generic error message display funciton coudl possibly do everything correctly +/// for all possible parsers. Hence you are provided with this example routine, which +/// you should override in your parser/tree parser to do as you will. +/// +/// Here we depart somewhat from the Java runtime as that has now split up a lot +/// of the error display routines into spearate units. However, ther is little advantage +/// to this in the C version as you will probably implement all such routines as a +/// separate translation unit, rather than install them all as pointers to functions +/// in the base recognizer. +/// +static void +displayRecognitionError (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_UINT8 * tokenNames) +{ + pANTLR3_PARSER parser; + pANTLR3_TREE_PARSER tparser; + pANTLR3_INT_STREAM is; + pANTLR3_STRING ttext; + pANTLR3_STRING ftext; + pANTLR3_EXCEPTION ex; + pANTLR3_COMMON_TOKEN theToken; + pANTLR3_BASE_TREE theBaseTree; + pANTLR3_COMMON_TREE theCommonTree; + + // Retrieve some info for easy reading. + // + ex = recognizer->state->exception; + ttext = NULL; + + // See if there is a 'filename' we can use + // + if (ex->streamName == NULL) + { + if (((pANTLR3_COMMON_TOKEN)(ex->token))->type == ANTLR3_TOKEN_EOF) + { + ANTLR3_FPRINTF(stderr, "-end of input-("); + } + else + { + ANTLR3_FPRINTF(stderr, "-unknown source-("); + } + } + else + { + ftext = ex->streamName->to8(ex->streamName); + ANTLR3_FPRINTF(stderr, "%s(", ftext->chars); + } + + // Next comes the line number + // + + ANTLR3_FPRINTF(stderr, "%d) ", recognizer->state->exception->line); + ANTLR3_FPRINTF(stderr, " : error %d : %s", + recognizer->state->exception->type, + (pANTLR3_UINT8) (recognizer->state->exception->message)); + + + // How we determine the next piece is dependent on which thing raised the + // error. + // + switch (recognizer->type) + { + case ANTLR3_TYPE_PARSER: + + // Prepare the knowledge we know we have + // + parser = (pANTLR3_PARSER) (recognizer->super); + tparser = NULL; + is = parser->tstream->istream; + theToken = (pANTLR3_COMMON_TOKEN)(recognizer->state->exception->token); + ttext = theToken->toString(theToken); + + ANTLR3_FPRINTF(stderr, ", at offset %d", recognizer->state->exception->charPositionInLine); + if (theToken != NULL) + { + if (theToken->type == ANTLR3_TOKEN_EOF) + { + ANTLR3_FPRINTF(stderr, ", at <EOF>"); + } + else + { + // Guard against null text in a token + // + ANTLR3_FPRINTF(stderr, "\n near %s\n ", ttext == NULL ? (pANTLR3_UINT8)"<no text for the token>" : ttext->chars); + } + } + break; + + case ANTLR3_TYPE_TREE_PARSER: + + tparser = (pANTLR3_TREE_PARSER) (recognizer->super); + parser = NULL; + is = tparser->ctnstream->tnstream->istream; + theBaseTree = (pANTLR3_BASE_TREE)(recognizer->state->exception->token); + ttext = theBaseTree->toStringTree(theBaseTree); + + if (theBaseTree != NULL) + { + theCommonTree = (pANTLR3_COMMON_TREE) theBaseTree->super; + + if (theCommonTree != NULL) + { + theToken = (pANTLR3_COMMON_TOKEN) theBaseTree->getToken(theBaseTree); + } + ANTLR3_FPRINTF(stderr, ", at offset %d", theBaseTree->getCharPositionInLine(theBaseTree)); + ANTLR3_FPRINTF(stderr, ", near %s", ttext->chars); + } + break; + + default: + + ANTLR3_FPRINTF(stderr, "Base recognizer function displayRecognitionError called by unknown parser type - provide override for this function\n"); + return; + break; + } + + // Although this function should generally be provided by the implementation, this one + // should be as helpful as possible for grammar developers and serve as an example + // of what you can do with each exception type. In general, when you make up your + // 'real' handler, you should debug the routine with all possible errors you expect + // which will then let you be as specific as possible about all circumstances. + // + // Note that in the general case, errors thrown by tree parsers indicate a problem + // with the output of the parser or with the tree grammar itself. The job of the parser + // is to produce a perfect (in traversal terms) syntactically correct tree, so errors + // at that stage should really be semantic errors that your own code determines and handles + // in whatever way is appropriate. + // + switch (ex->type) + { + case ANTLR3_UNWANTED_TOKEN_EXCEPTION: + + // Indicates that the recognizer was fed a token which seesm to be + // spurious input. We can detect this when the token that follows + // this unwanted token would normally be part of the syntactically + // correct stream. Then we can see that the token we are looking at + // is just something that should not be there and throw this exception. + // + if (tokenNames == NULL) + { + ANTLR3_FPRINTF(stderr, " : Extraneous input..."); + } + else + { + if (ex->expecting == ANTLR3_TOKEN_EOF) + { + ANTLR3_FPRINTF(stderr, " : Extraneous input - expected <EOF>\n"); + } + else + { + ANTLR3_FPRINTF(stderr, " : Extraneous input - expected %s ...\n", tokenNames[ex->expecting]); + } + } + break; + + case ANTLR3_MISSING_TOKEN_EXCEPTION: + + // Indicates that the recognizer detected that the token we just + // hit would be valid syntactically if preceeded by a particular + // token. Perhaps a missing ';' at line end or a missing ',' in an + // expression list, and such like. + // + if (tokenNames == NULL) + { + ANTLR3_FPRINTF(stderr, " : Missing token (%d)...\n", ex->expecting); + } + else + { + if (ex->expecting == ANTLR3_TOKEN_EOF) + { + ANTLR3_FPRINTF(stderr, " : Missing <EOF>\n"); + } + else + { + ANTLR3_FPRINTF(stderr, " : Missing %s \n", tokenNames[ex->expecting]); + } + } + break; + + case ANTLR3_RECOGNITION_EXCEPTION: + + // Indicates that the recognizer received a token + // in the input that was not predicted. This is the basic exception type + // from which all others are derived. So we assume it was a syntax error. + // You may get this if there are not more tokens and more are needed + // to complete a parse for instance. + // + ANTLR3_FPRINTF(stderr, " : syntax error...\n"); + break; + + case ANTLR3_MISMATCHED_TOKEN_EXCEPTION: + + // We were expecting to see one thing and got another. This is the + // most common error if we coudl not detect a missing or unwanted token. + // Here you can spend your efforts to + // derive more useful error messages based on the expected + // token set and the last token and so on. The error following + // bitmaps do a good job of reducing the set that we were looking + // for down to something small. Knowing what you are parsing may be + // able to allow you to be even more specific about an error. + // + if (tokenNames == NULL) + { + ANTLR3_FPRINTF(stderr, " : syntax error...\n"); + } + else + { + if (ex->expecting == ANTLR3_TOKEN_EOF) + { + ANTLR3_FPRINTF(stderr, " : expected <EOF>\n"); + } + else + { + ANTLR3_FPRINTF(stderr, " : expected %s ...\n", tokenNames[ex->expecting]); + } + } + break; + + case ANTLR3_NO_VIABLE_ALT_EXCEPTION: + + // We could not pick any alt decision from the input given + // so god knows what happened - however when you examine your grammar, + // you should. It means that at the point where the current token occurred + // that the DFA indicates nowhere to go from here. + // + ANTLR3_FPRINTF(stderr, " : cannot match to any predicted input...\n"); + + break; + + case ANTLR3_MISMATCHED_SET_EXCEPTION: + + { + ANTLR3_UINT32 count; + ANTLR3_UINT32 bit; + ANTLR3_UINT32 size; + ANTLR3_UINT32 numbits; + pANTLR3_BITSET errBits; + + // This means we were able to deal with one of a set of + // possible tokens at this point, but we did not see any + // member of that set. + // + ANTLR3_FPRINTF(stderr, " : unexpected input...\n expected one of : "); + + // What tokens could we have accepted at this point in the + // parse? + // + count = 0; + errBits = antlr3BitsetLoad (ex->expectingSet); + numbits = errBits->numBits (errBits); + size = errBits->size (errBits); + + if (size > 0) + { + // However many tokens we could have dealt with here, it is usually + // not useful to print ALL of the set here. I arbitrarily chose 8 + // here, but you should do whatever makes sense for you of course. + // No token number 0, so look for bit 1 and on. + // + for (bit = 1; bit < numbits && count < 8 && count < size; bit++) + { + // TODO: This doesn;t look right - should be asking if the bit is set!! + // + if (tokenNames[bit]) + { + ANTLR3_FPRINTF(stderr, "%s%s", count > 0 ? ", " : "", tokenNames[bit]); + count++; + } + } + ANTLR3_FPRINTF(stderr, "\n"); + } + else + { + ANTLR3_FPRINTF(stderr, "Actually dude, we didn't seem to be expecting anything here, or at least\n"); + ANTLR3_FPRINTF(stderr, "I could not work out what I was expecting, like so many of us these days!\n"); + } + } + break; + + case ANTLR3_EARLY_EXIT_EXCEPTION: + + // We entered a loop requiring a number of token sequences + // but found a token that ended that sequence earlier than + // we should have done. + // + ANTLR3_FPRINTF(stderr, " : missing elements...\n"); + break; + + default: + + // We don't handle any other exceptions here, but you can + // if you wish. If we get an exception that hits this point + // then we are just going to report what we know about the + // token. + // + ANTLR3_FPRINTF(stderr, " : syntax not recognized...\n"); + break; + } + + // Here you have the token that was in error which if this is + // the standard implementation will tell you the line and offset + // and also record the address of the start of the line in the + // input stream. You could therefore print the source line and so on. + // Generally though, I would expect that your lexer/parser will keep + // its own map of lines and source pointers or whatever as there + // are a lot of specific things you need to know about the input + // to do something like that. + // Here is where you do it though :-). + // +} + +/// Return how many syntax errors were detected by this recognizer +/// +static ANTLR3_UINT32 +getNumberOfSyntaxErrors(pANTLR3_BASE_RECOGNIZER recognizer) +{ + return recognizer->state->errorCount; +} + +/// Recover from an error found on the input stream. Mostly this is +/// NoViableAlt exceptions, but could be a mismatched token that +/// the match() routine could not recover from. +/// +static void +recover (pANTLR3_BASE_RECOGNIZER recognizer) +{ + // Used to compute the follow set of tokens + // + pANTLR3_BITSET followSet; + pANTLR3_PARSER parser; + pANTLR3_TREE_PARSER tparser; + pANTLR3_INT_STREAM is; + + switch (recognizer->type) + { + case ANTLR3_TYPE_PARSER: + + parser = (pANTLR3_PARSER) (recognizer->super); + tparser = NULL; + is = parser->tstream->istream; + + break; + + case ANTLR3_TYPE_TREE_PARSER: + + tparser = (pANTLR3_TREE_PARSER) (recognizer->super); + parser = NULL; + is = tparser->ctnstream->tnstream->istream; + + break; + + default: + + ANTLR3_FPRINTF(stderr, "Base recognizer function recover called by unknown parser type - provide override for this function\n"); + return; + + break; + } + + // Are we about to repeat the same error? + // + if (recognizer->state->lastErrorIndex == is->index(is)) + { + // The last error was at the same token index point. This must be a case + // where LT(1) is in the recovery token set so nothing is + // consumed. Consume a single token so at least to prevent + // an infinite loop; this is a failsafe. + // + is->consume(is); + } + + // Record error index position + // + recognizer->state->lastErrorIndex = is->index(is); + + // Work out the follows set for error recovery + // + followSet = recognizer->computeErrorRecoverySet(recognizer); + + // Call resync hook (for debuggers and so on) + // + recognizer->beginResync(recognizer); + + // Consume tokens until we have resynced to something in the follows set + // + recognizer->consumeUntilSet(recognizer, followSet); + + // End resync hook + // + recognizer->endResync(recognizer); + + // Destroy the temporary bitset we produced. + // + followSet->free(followSet); + + // Reset the inError flag so we don't re-report the exception + // + recognizer->state->error = ANTLR3_FALSE; + recognizer->state->failed = ANTLR3_FALSE; +} + + +/// Attempt to recover from a single missing or extra token. +/// +/// EXTRA TOKEN +/// +/// LA(1) is not what we are looking for. If LA(2) has the right token, +/// however, then assume LA(1) is some extra spurious token. Delete it +/// and LA(2) as if we were doing a normal match(), which advances the +/// input. +/// +/// MISSING TOKEN +/// +/// If current token is consistent with what could come after +/// ttype then it is ok to "insert" the missing token, else throw +/// exception For example, Input "i=(3;" is clearly missing the +/// ')'. When the parser returns from the nested call to expr, it +/// will have call chain: +/// +/// stat -> expr -> atom +/// +/// and it will be trying to match the ')' at this point in the +/// derivation: +/// +/// => ID '=' '(' INT ')' ('+' atom)* ';' +/// ^ +/// match() will see that ';' doesn't match ')' and report a +/// mismatched token error. To recover, it sees that LA(1)==';' +/// is in the set of tokens that can follow the ')' token +/// reference in rule atom. It can assume that you forgot the ')'. +/// +/// The exception that was passed in, in the java implementation is +/// sorted in the recognizer exception stack in the C version. To 'throw' it we set the +/// error flag and rules cascade back when this is set. +/// +static void * +recoverFromMismatchedToken (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 ttype, pANTLR3_BITSET_LIST follow) +{ + pANTLR3_PARSER parser; + pANTLR3_TREE_PARSER tparser; + pANTLR3_INT_STREAM is; + void * matchedSymbol; + + // Invoke the debugger event if there is a debugger listening to us + // + if (recognizer->debugger != NULL) + { + recognizer->debugger->recognitionException(recognizer->debugger, recognizer->state->exception); + } + + switch (recognizer->type) + { + case ANTLR3_TYPE_PARSER: + + parser = (pANTLR3_PARSER) (recognizer->super); + tparser = NULL; + is = parser->tstream->istream; + + break; + + case ANTLR3_TYPE_TREE_PARSER: + + tparser = (pANTLR3_TREE_PARSER) (recognizer->super); + parser = NULL; + is = tparser->ctnstream->tnstream->istream; + + break; + + default: + + ANTLR3_FPRINTF(stderr, "Base recognizer function recoverFromMismatchedToken called by unknown parser type - provide override for this function\n"); + return NULL; + + break; + } + + // Create an exception if we need one + // + if (recognizer->state->exception == NULL) + { + antlr3RecognitionExceptionNew(recognizer); + } + + // If the next token after the one we are looking at in the input stream + // is what we are looking for then we remove the one we have discovered + // from the stream by consuming it, then consume this next one along too as + // if nothing had happened. + // + if ( recognizer->mismatchIsUnwantedToken(recognizer, is, ttype) == ANTLR3_TRUE) + { + recognizer->state->exception->type = ANTLR3_UNWANTED_TOKEN_EXCEPTION; + recognizer->state->exception->message = ANTLR3_UNWANTED_TOKEN_EXCEPTION_NAME; + + // Call resync hook (for debuggers and so on) + // + if (recognizer->debugger != NULL) + { + recognizer->debugger->beginResync(recognizer->debugger); + } + + recognizer->beginResync(recognizer); + + // "delete" the extra token + // + recognizer->beginResync(recognizer); + is->consume(is); + recognizer->endResync(recognizer); + // End resync hook + // + if (recognizer->debugger != NULL) + { + recognizer->debugger->endResync(recognizer->debugger); + } + + // Print out the error after we consume so that ANTLRWorks sees the + // token in the exception. + // + recognizer->reportError(recognizer); + + // Return the token we are actually matching + // + matchedSymbol = recognizer->getCurrentInputSymbol(recognizer, is); + + // Consume the token that the rule actually expected to get as if everything + // was hunky dory. + // + is->consume(is); + + recognizer->state->error = ANTLR3_FALSE; // Exception is not outstanding any more + + return matchedSymbol; + } + + // Single token deletion (Unwanted above) did not work + // so we see if we can insert a token instead by calculating which + // token would be missing + // + if (mismatchIsMissingToken(recognizer, is, follow)) + { + // We can fake the missing token and proceed + // + matchedSymbol = recognizer->getMissingSymbol(recognizer, is, recognizer->state->exception, ttype, follow); + recognizer->state->exception->type = ANTLR3_MISSING_TOKEN_EXCEPTION; + recognizer->state->exception->message = ANTLR3_MISSING_TOKEN_EXCEPTION_NAME; + recognizer->state->exception->token = matchedSymbol; + recognizer->state->exception->expecting = ttype; + + // Print out the error after we insert so that ANTLRWorks sees the + // token in the exception. + // + recognizer->reportError(recognizer); + + recognizer->state->error = ANTLR3_FALSE; // Exception is not outstanding any more + + return matchedSymbol; + } + + + // Neither deleting nor inserting tokens allows recovery + // must just report the exception. + // + recognizer->state->error = ANTLR3_TRUE; + return NULL; +} + +static void * +recoverFromMismatchedSet (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_BITSET_LIST follow) +{ + pANTLR3_PARSER parser; + pANTLR3_TREE_PARSER tparser; + pANTLR3_INT_STREAM is; + pANTLR3_COMMON_TOKEN matchedSymbol; + + switch (recognizer->type) + { + case ANTLR3_TYPE_PARSER: + + parser = (pANTLR3_PARSER) (recognizer->super); + tparser = NULL; + is = parser->tstream->istream; + + break; + + case ANTLR3_TYPE_TREE_PARSER: + + tparser = (pANTLR3_TREE_PARSER) (recognizer->super); + parser = NULL; + is = tparser->ctnstream->tnstream->istream; + + break; + + default: + + ANTLR3_FPRINTF(stderr, "Base recognizer function recoverFromMismatchedSet called by unknown parser type - provide override for this function\n"); + return NULL; + + break; + } + + if (recognizer->mismatchIsMissingToken(recognizer, is, follow) == ANTLR3_TRUE) + { + // We can fake the missing token and proceed + // + matchedSymbol = recognizer->getMissingSymbol(recognizer, is, recognizer->state->exception, ANTLR3_TOKEN_INVALID, follow); + recognizer->state->exception->type = ANTLR3_MISSING_TOKEN_EXCEPTION; + recognizer->state->exception->token = matchedSymbol; + + // Print out the error after we insert so that ANTLRWorks sees the + // token in the exception. + // + recognizer->reportError(recognizer); + + recognizer->state->error = ANTLR3_FALSE; // Exception is not outstanding any more + + return matchedSymbol; + } + + // TODO - Single token deletion like in recoverFromMismatchedToken() + // + recognizer->state->error = ANTLR3_TRUE; + recognizer->state->failed = ANTLR3_TRUE; + return NULL; +} + +/// This code is factored out from mismatched token and mismatched set +/// recovery. It handles "single token insertion" error recovery for +/// both. No tokens are consumed to recover from insertions. Return +/// true if recovery was possible else return false. +/// +static ANTLR3_BOOLEAN +recoverFromMismatchedElement (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_BITSET_LIST followBits) +{ + pANTLR3_BITSET viableToksFollowingRule; + pANTLR3_BITSET follow; + pANTLR3_PARSER parser; + pANTLR3_TREE_PARSER tparser; + pANTLR3_INT_STREAM is; + + switch (recognizer->type) + { + case ANTLR3_TYPE_PARSER: + + parser = (pANTLR3_PARSER) (recognizer->super); + tparser = NULL; + is = parser->tstream->istream; + + break; + + case ANTLR3_TYPE_TREE_PARSER: + + tparser = (pANTLR3_TREE_PARSER) (recognizer->super); + parser = NULL; + is = tparser->ctnstream->tnstream->istream; + + break; + + default: + + ANTLR3_FPRINTF(stderr, "Base recognizer function recover called by unknown parser type - provide override for this function\n"); + return ANTLR3_FALSE; + + break; + } + + follow = antlr3BitsetLoad(followBits); + + if (follow == NULL) + { + /* The follow set is NULL, which means we don't know what can come + * next, so we "hit and hope" by just signifying that we cannot + * recover, which will just cause the next token to be consumed, + * which might dig us out. + */ + return ANTLR3_FALSE; + } + + /* We have a bitmap for the follow set, hence we can compute + * what can follow this grammar element reference. + */ + if (follow->isMember(follow, ANTLR3_EOR_TOKEN_TYPE) == ANTLR3_TRUE) + { + /* First we need to know which of the available tokens are viable + * to follow this reference. + */ + viableToksFollowingRule = recognizer->computeCSRuleFollow(recognizer); + + /* Remove the EOR token, which we do not wish to compute with + */ + follow->remove(follow, ANTLR3_EOR_TOKEN_TYPE); + viableToksFollowingRule->free(viableToksFollowingRule); + /* We now have the computed set of what can follow the current token + */ + } + + /* We can now see if the current token works with the set of tokens + * that could follow the current grammar reference. If it looks like it + * is consistent, then we can "insert" that token by not throwing + * an exception and assuming that we saw it. + */ + if ( follow->isMember(follow, is->_LA(is, 1)) == ANTLR3_TRUE) + { + /* report the error, but don't cause any rules to abort and stuff + */ + recognizer->reportError(recognizer); + if (follow != NULL) + { + follow->free(follow); + } + recognizer->state->error = ANTLR3_FALSE; + recognizer->state->failed = ANTLR3_FALSE; + return ANTLR3_TRUE; /* Success in recovery */ + } + + if (follow != NULL) + { + follow->free(follow); + } + + /* We could not find anything viable to do, so this is going to + * cause an exception. + */ + return ANTLR3_FALSE; +} + +/// Eat tokens from the input stream until we get one of JUST the right type +/// +static void +consumeUntil (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 tokenType) +{ + ANTLR3_UINT32 ttype; + pANTLR3_PARSER parser; + pANTLR3_TREE_PARSER tparser; + pANTLR3_INT_STREAM is; + + switch (recognizer->type) + { + case ANTLR3_TYPE_PARSER: + + parser = (pANTLR3_PARSER) (recognizer->super); + tparser = NULL; + is = parser->tstream->istream; + + break; + + case ANTLR3_TYPE_TREE_PARSER: + + tparser = (pANTLR3_TREE_PARSER) (recognizer->super); + parser = NULL; + is = tparser->ctnstream->tnstream->istream; + + break; + + default: + + ANTLR3_FPRINTF(stderr, "Base recognizer function 'consumeUntil' called by unknown parser type - provide override for this function\n"); + return; + + break; + } + + // What do have at the moment? + // + ttype = is->_LA(is, 1); + + // Start eating tokens until we get to the one we want. + // + while (ttype != ANTLR3_TOKEN_EOF && ttype != tokenType) + { + is->consume(is); + ttype = is->_LA(is, 1); + } +} + +/// Eat tokens from the input stream until we find one that +/// belongs to the supplied set. +/// +static void +consumeUntilSet (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_BITSET set) +{ + ANTLR3_UINT32 ttype; + pANTLR3_PARSER parser; + pANTLR3_TREE_PARSER tparser; + pANTLR3_INT_STREAM is; + + switch (recognizer->type) + { + case ANTLR3_TYPE_PARSER: + + parser = (pANTLR3_PARSER) (recognizer->super); + tparser = NULL; + is = parser->tstream->istream; + + break; + + case ANTLR3_TYPE_TREE_PARSER: + + tparser = (pANTLR3_TREE_PARSER) (recognizer->super); + parser = NULL; + is = tparser->ctnstream->tnstream->istream; + + break; + + default: + + ANTLR3_FPRINTF(stderr, "Base recognizer function 'consumeUntilSet' called by unknown parser type - provide override for this function\n"); + return; + + break; + } + + // What do have at the moment? + // + ttype = is->_LA(is, 1); + + // Start eating tokens until we get to one we want. + // + while (ttype != ANTLR3_TOKEN_EOF && set->isMember(set, ttype) == ANTLR3_FALSE) + { + is->consume(is); + ttype = is->_LA(is, 1); + } +} + +/** Return the rule invocation stack (how we got here in the parse. + * In the java version Ter just asks the JVM for all the information + * but in C we don't get this information, so I am going to do nothing + * right now. + */ +static pANTLR3_STACK +getRuleInvocationStack (pANTLR3_BASE_RECOGNIZER recognizer) +{ + return NULL; +} + +static pANTLR3_STACK +getRuleInvocationStackNamed (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_UINT8 name) +{ + return NULL; +} + +/** Convenience method for template rewrites - NYI. + */ +static pANTLR3_HASH_TABLE +toStrings (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_HASH_TABLE tokens) +{ + return NULL; +} + +static void ANTLR3_CDECL +freeIntTrie (void * trie) +{ + ((pANTLR3_INT_TRIE)trie)->free((pANTLR3_INT_TRIE)trie); +} + + +/** Pointer to a function to return whether the rule has parsed input starting at the supplied + * start index before. If the rule has not parsed input starting from the supplied start index, + * then it will return ANTLR3_MEMO_RULE_UNKNOWN. If it has parsed from the suppled start point + * then it will return the point where it last stopped parsing after that start point. + * + * \remark + * The rule memos are an ANTLR3_LIST of ANTLR3_LISTS, however if this becomes any kind of performance + * issue (it probably won't, the hash tables are pretty quick) then we could make a special int only + * version of the table. + */ +static ANTLR3_MARKER +getRuleMemoization (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_INTKEY ruleIndex, ANTLR3_MARKER ruleParseStart) +{ + /* The rule memos are an ANTLR3_LIST of ANTLR3_LIST. + */ + pANTLR3_INT_TRIE ruleList; + ANTLR3_MARKER stopIndex; + pANTLR3_TRIE_ENTRY entry; + + /* See if we have a list in the ruleMemos for this rule, and if not, then create one + * as we will need it eventually if we are being asked for the memo here. + */ + entry = recognizer->state->ruleMemo->get(recognizer->state->ruleMemo, (ANTLR3_INTKEY)ruleIndex); + + if (entry == NULL) + { + /* Did not find it, so create a new one for it, with a bit depth based on the + * size of the input stream. We need the bit depth to incorporate the number if + * bits required to represent the largest possible stop index in the input, which is the + * last character. An int stream is free to return the largest 64 bit offset if it has + * no idea of the size, but you should remember that this will cause the leftmost + * bit match algorithm to run to 63 bits, which will be the whole time spent in the trie ;-) + */ + ruleList = antlr3IntTrieNew(63); /* Depth is theoretically 64 bits, but probably not ;-) */ + + if (ruleList != NULL) + { + recognizer->state->ruleMemo->add(recognizer->state->ruleMemo, (ANTLR3_INTKEY)ruleIndex, ANTLR3_HASH_TYPE_STR, 0, ANTLR3_FUNC_PTR(ruleList), freeIntTrie); + } + + /* We cannot have a stopIndex in a trie we have just created of course + */ + return MEMO_RULE_UNKNOWN; + } + + ruleList = (pANTLR3_INT_TRIE) (entry->data.ptr); + + /* See if there is a stop index associated with the supplied start index. + */ + stopIndex = 0; + + entry = ruleList->get(ruleList, ruleParseStart); + if (entry != NULL) + { + stopIndex = (ANTLR3_MARKER)(entry->data.intVal); + } + + if (stopIndex == 0) + { + return MEMO_RULE_UNKNOWN; + } + + return stopIndex; +} + +/** Has this rule already parsed input at the current index in the + * input stream? Return ANTLR3_TRUE if we have and ANTLR3_FALSE + * if we have not. + * + * This method has a side-effect: if we have seen this input for + * this rule and successfully parsed before, then seek ahead to + * 1 past the stop token matched for this rule last time. + */ +static ANTLR3_BOOLEAN +alreadyParsedRule (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_MARKER ruleIndex) +{ + ANTLR3_MARKER stopIndex; + pANTLR3_LEXER lexer; + pANTLR3_PARSER parser; + pANTLR3_TREE_PARSER tparser; + pANTLR3_INT_STREAM is; + + switch (recognizer->type) + { + case ANTLR3_TYPE_PARSER: + + parser = (pANTLR3_PARSER) (recognizer->super); + tparser = NULL; + lexer = NULL; + is = parser->tstream->istream; + + break; + + case ANTLR3_TYPE_TREE_PARSER: + + tparser = (pANTLR3_TREE_PARSER) (recognizer->super); + parser = NULL; + lexer = NULL; + is = tparser->ctnstream->tnstream->istream; + + break; + + case ANTLR3_TYPE_LEXER: + + lexer = (pANTLR3_LEXER) (recognizer->super); + parser = NULL; + tparser = NULL; + is = lexer->input->istream; + break; + + default: + + ANTLR3_FPRINTF(stderr, "Base recognizer function 'alreadyParsedRule' called by unknown parser type - provide override for this function\n"); + return ANTLR3_FALSE; + + break; + } + + /* See if we have a memo marker for this. + */ + stopIndex = recognizer->getRuleMemoization(recognizer, ruleIndex, is->index(is)); + + if (stopIndex == MEMO_RULE_UNKNOWN) + { + return ANTLR3_FALSE; + } + + if (stopIndex == MEMO_RULE_FAILED) + { + recognizer->state->failed = ANTLR3_TRUE; + } + else + { + is->seek(is, stopIndex+1); + } + + /* If here then the rule was executed for this input already + */ + return ANTLR3_TRUE; +} + +/** Record whether or not this rule parsed the input at this position + * successfully. + */ +static void +memoize (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_MARKER ruleIndex, ANTLR3_MARKER ruleParseStart) +{ + /* The rule memos are an ANTLR3_LIST of ANTLR3_LIST. + */ + pANTLR3_INT_TRIE ruleList; + pANTLR3_TRIE_ENTRY entry; + ANTLR3_MARKER stopIndex; + pANTLR3_LEXER lexer; + pANTLR3_PARSER parser; + pANTLR3_TREE_PARSER tparser; + pANTLR3_INT_STREAM is; + + switch (recognizer->type) + { + case ANTLR3_TYPE_PARSER: + + parser = (pANTLR3_PARSER) (recognizer->super); + tparser = NULL; + is = parser->tstream->istream; + + break; + + case ANTLR3_TYPE_TREE_PARSER: + + tparser = (pANTLR3_TREE_PARSER) (recognizer->super); + parser = NULL; + is = tparser->ctnstream->tnstream->istream; + + break; + + case ANTLR3_TYPE_LEXER: + + lexer = (pANTLR3_LEXER) (recognizer->super); + parser = NULL; + tparser = NULL; + is = lexer->input->istream; + break; + + default: + + ANTLR3_FPRINTF(stderr, "Base recognizer function consumeUntilSet called by unknown parser type - provide override for this function\n"); + return; + + break; + } + + stopIndex = recognizer->state->failed == ANTLR3_TRUE ? MEMO_RULE_FAILED : is->index(is) - 1; + + entry = recognizer->state->ruleMemo->get(recognizer->state->ruleMemo, (ANTLR3_INTKEY)ruleIndex); + + if (entry != NULL) + { + ruleList = (pANTLR3_INT_TRIE)(entry->data.ptr); + + /* If we don't already have this entry, append it. The memoize trie does not + * accept duplicates so it won't add it if already there and we just ignore the + * return code as we don't care if it is there already. + */ + ruleList->add(ruleList, ruleParseStart, ANTLR3_HASH_TYPE_INT, stopIndex, NULL, NULL); + } +} +/** A syntactic predicate. Returns true/false depending on whether + * the specified grammar fragment matches the current input stream. + * This resets the failed instance var afterwards. + */ +static ANTLR3_BOOLEAN +synpred (pANTLR3_BASE_RECOGNIZER recognizer, void * ctx, void (*predicate)(void * ctx)) +{ + ANTLR3_MARKER start; + pANTLR3_PARSER parser; + pANTLR3_TREE_PARSER tparser; + pANTLR3_INT_STREAM is; + + switch (recognizer->type) + { + case ANTLR3_TYPE_PARSER: + + parser = (pANTLR3_PARSER) (recognizer->super); + tparser = NULL; + is = parser->tstream->istream; + + break; + + case ANTLR3_TYPE_TREE_PARSER: + + tparser = (pANTLR3_TREE_PARSER) (recognizer->super); + parser = NULL; + is = tparser->ctnstream->tnstream->istream; + + break; + + default: + + ANTLR3_FPRINTF(stderr, "Base recognizer function 'synPred' called by unknown parser type - provide override for this function\n"); + return ANTLR3_FALSE; + + break; + } + + /* Begin backtracking so we can get back to where we started after trying out + * the syntactic predicate. + */ + start = is->mark(is); + recognizer->state->backtracking++; + + /* Try the syntactical predicate + */ + predicate(ctx); + + /* Reset + */ + is->rewind(is, start); + recognizer->state->backtracking--; + + if (recognizer->state->failed == ANTLR3_TRUE) + { + /* Predicate failed + */ + recognizer->state->failed = ANTLR3_FALSE; + return ANTLR3_FALSE; + } + else + { + /* Predicate was successful + */ + recognizer->state->failed = ANTLR3_FALSE; + return ANTLR3_TRUE; + } +} + +static void +reset(pANTLR3_BASE_RECOGNIZER recognizer) +{ + if (recognizer->state->following != NULL) + { + recognizer->state->following->free(recognizer->state->following); + } + + // Reset the state flags + // + recognizer->state->errorRecovery = ANTLR3_FALSE; + recognizer->state->lastErrorIndex = -1; + recognizer->state->failed = ANTLR3_FALSE; + recognizer->state->errorCount = 0; + recognizer->state->backtracking = 0; + recognizer->state->following = NULL; + + if (recognizer->state != NULL) + { + if (recognizer->state->ruleMemo != NULL) + { + recognizer->state->ruleMemo->free(recognizer->state->ruleMemo); + recognizer->state->ruleMemo = antlr3IntTrieNew(15); /* 16 bit depth is enough for 32768 rules! */ + } + } + + + // Install a new following set + // + recognizer->state->following = antlr3StackNew(8); + +} + +// Default implementation is for parser and assumes a token stream as supplied by the runtime. +// You MAY need override this function if the standard TOKEN_STREAM is not what you are using. +// +static void * +getCurrentInputSymbol (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM istream) +{ + return ((pANTLR3_TOKEN_STREAM)istream->super)->_LT((pANTLR3_TOKEN_STREAM)istream->super, 1); +} + +// Default implementation is for parser and assumes a token stream as supplied by the runtime. +// You MAY need override this function if the standard COMMON_TOKEN_STREAM is not what you are using. +// +static void * +getMissingSymbol (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM istream, pANTLR3_EXCEPTION e, + ANTLR3_UINT32 expectedTokenType, pANTLR3_BITSET_LIST follow) +{ + pANTLR3_TOKEN_STREAM ts; + pANTLR3_COMMON_TOKEN_STREAM cts; + pANTLR3_COMMON_TOKEN token; + pANTLR3_COMMON_TOKEN current; + pANTLR3_STRING text; + + // Dereference the standard pointers + // + ts = (pANTLR3_TOKEN_STREAM)istream->super; + cts = (pANTLR3_COMMON_TOKEN_STREAM)ts->super; + + // Work out what to use as the current symbol to make a line and offset etc + // If we are at EOF, we use the token before EOF + // + current = ts->_LT(ts, 1); + if (current->getType(current) == ANTLR3_TOKEN_EOF) + { + current = ts->_LT(ts, -1); + } + + // Create a new empty token + // + if (recognizer->state->tokFactory == NULL) + { + // We don't yet have a token factory for making tokens + // we just need a fake one using the input stream of the current + // token. + // + recognizer->state->tokFactory = antlr3TokenFactoryNew(current->input); + } + token = recognizer->state->tokFactory->newToken(recognizer->state->tokFactory); + + // Set some of the token properties based on the current token + // + token->setLine (token, current->getLine(current)); + token->setCharPositionInLine (token, current->getCharPositionInLine(current)); + token->setChannel (token, ANTLR3_TOKEN_DEFAULT_CHANNEL); + token->setType (token, expectedTokenType); + token->user1 = current->user1; + token->user2 = current->user2; + token->user3 = current->user3; + token->custom = current->custom; + + // Create the token text that shows it has been inserted + // + token->setText8(token, (pANTLR3_UINT8)"<missing "); + text = token->getText(token); + + if (text != NULL) + { + text->append8(text, (const char *)recognizer->state->tokenNames[expectedTokenType]); + text->append8(text, (const char *)">"); + } + + // Finally return the pointer to our new token + // + return token; +} + + +#ifdef ANTLR3_WINDOWS +#pragma warning( default : 4100 ) +#endif + +/// @} +/// + diff --git a/antlr-3.1.3/runtime/C/src/antlr3basetree.c b/antlr-3.1.3/runtime/C/src/antlr3basetree.c new file mode 100644 index 0000000..bbc81e7 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3basetree.c @@ -0,0 +1,489 @@ +#include <antlr3basetree.h> + +#ifdef ANTLR3_WINDOWS +#pragma warning( disable : 4100 ) +#endif + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +static void * getChild (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i); +static ANTLR3_UINT32 getChildCount (pANTLR3_BASE_TREE tree); +static ANTLR3_UINT32 getCharPositionInLine +(pANTLR3_BASE_TREE tree); +static ANTLR3_UINT32 getLine (pANTLR3_BASE_TREE tree); +static pANTLR3_BASE_TREE +getFirstChildWithType +(pANTLR3_BASE_TREE tree, ANTLR3_UINT32 type); +static void addChild (pANTLR3_BASE_TREE tree, pANTLR3_BASE_TREE child); +static void addChildren (pANTLR3_BASE_TREE tree, pANTLR3_LIST kids); +static void replaceChildren (pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE t); + +static void freshenPACIndexesAll(pANTLR3_BASE_TREE tree); +static void freshenPACIndexes (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 offset); + +static void setChild (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i, void * child); +static void * deleteChild (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i); +static void * dupTree (pANTLR3_BASE_TREE tree); +static pANTLR3_STRING toStringTree (pANTLR3_BASE_TREE tree); + + +ANTLR3_API pANTLR3_BASE_TREE +antlr3BaseTreeNew(pANTLR3_BASE_TREE tree) +{ + /* api */ + tree->getChild = getChild; + tree->getChildCount = getChildCount; + tree->addChild = (void (*)(pANTLR3_BASE_TREE, void *))(addChild); + tree->addChildren = addChildren; + tree->setChild = setChild; + tree->deleteChild = deleteChild; + tree->dupTree = dupTree; + tree->toStringTree = toStringTree; + tree->getCharPositionInLine = getCharPositionInLine; + tree->getLine = getLine; + tree->replaceChildren = replaceChildren; + tree->freshenPACIndexesAll = freshenPACIndexesAll; + tree->freshenPACIndexes = freshenPACIndexes; + tree->getFirstChildWithType = (void *(*)(pANTLR3_BASE_TREE, ANTLR3_UINT32))(getFirstChildWithType); + tree->children = NULL; + tree->strFactory = NULL; + + /* Rest must be filled in by caller. + */ + return tree; +} + +static ANTLR3_UINT32 +getCharPositionInLine (pANTLR3_BASE_TREE tree) +{ + return 0; +} + +static ANTLR3_UINT32 +getLine (pANTLR3_BASE_TREE tree) +{ + return 0; +} +static pANTLR3_BASE_TREE +getFirstChildWithType (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 type) +{ + ANTLR3_UINT32 i; + ANTLR3_UINT32 cs; + + pANTLR3_BASE_TREE t; + if (tree->children != NULL) + { + cs = tree->children->size(tree->children); + for (i = 0; i < cs; i++) + { + t = (pANTLR3_BASE_TREE) (tree->children->get(tree->children, i)); + if (tree->getType(t) == type) + { + return (pANTLR3_BASE_TREE)t; + } + } + } + return NULL; +} + + + +static void * +getChild (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i) +{ + if ( tree->children == NULL + || i >= tree->children->size(tree->children)) + { + return NULL; + } + return tree->children->get(tree->children, i); +} + + +static ANTLR3_UINT32 +getChildCount (pANTLR3_BASE_TREE tree) +{ + if (tree->children == NULL) + { + return 0; + } + else + { + return tree->children->size(tree->children); + } +} + +void +addChild (pANTLR3_BASE_TREE tree, pANTLR3_BASE_TREE child) +{ + ANTLR3_UINT32 n; + ANTLR3_UINT32 i; + + if (child == NULL) + { + return; + } + + if (child->isNilNode(child) == ANTLR3_TRUE) + { + if (child->children != NULL && child->children == tree->children) + { + // TODO: Change to exception rather than ANTLR3_FPRINTF? + // + ANTLR3_FPRINTF(stderr, "ANTLR3: An attempt was made to add a child list to itself!\n"); + return; + } + + // Add all of the children's children to this list + // + if (child->children != NULL) + { + if (tree->children == NULL) + { + // We are build ing the tree structure here, so we need not + // worry about duplication of pointers as the tree node + // factory will only clean up each node once. So we just + // copy in the child's children pointer as the child is + // a nil node (has not root itself). + // + tree->children = child->children; + child->children = NULL; + freshenPACIndexesAll(tree); + + } + else + { + // Need to copy the children + // + n = child->children->size(child->children); + + for (i = 0; i < n; i++) + { + pANTLR3_BASE_TREE entry; + entry = child->children->get(child->children, i); + + // ANTLR3 lists can be sparse, unlike Array Lists + // + if (entry != NULL) + { + tree->children->add(tree->children, entry, (void (ANTLR3_CDECL *) (void *))child->free); + } + } + } + } + } + else + { + // Tree we are adding is not a Nil and might have children to copy + // + if (tree->children == NULL) + { + // No children in the tree we are adding to, so create a new list on + // the fly to hold them. + // + tree->createChildrenList(tree); + } + + tree->children->add(tree->children, child, (void (ANTLR3_CDECL *)(void *))child->free); + + } +} + +/// Add all elements of the supplied list as children of this node +/// +static void +addChildren (pANTLR3_BASE_TREE tree, pANTLR3_LIST kids) +{ + ANTLR3_UINT32 i; + ANTLR3_UINT32 s; + + s = kids->size(kids); + for (i = 0; i<s; i++) + { + tree->addChild(tree, (pANTLR3_BASE_TREE)(kids->get(kids, i+1))); + } +} + + +static void +setChild (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i, void * child) +{ + if (tree->children == NULL) + { + tree->createChildrenList(tree); + } + tree->children->set(tree->children, i, child, NULL, ANTLR3_FALSE); +} + +static void * +deleteChild (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i) +{ + if ( tree->children == NULL) + { + return NULL; + } + + return tree->children->remove(tree->children, i); +} + +static void * +dupTree (pANTLR3_BASE_TREE tree) +{ + pANTLR3_BASE_TREE newTree; + ANTLR3_UINT32 i; + ANTLR3_UINT32 s; + + newTree = tree->dupNode (tree); + + if (tree->children != NULL) + { + s = tree->children->size (tree->children); + + for (i = 0; i < s; i++) + { + pANTLR3_BASE_TREE t; + pANTLR3_BASE_TREE newNode; + + t = (pANTLR3_BASE_TREE) tree->children->get(tree->children, i); + + if (t!= NULL) + { + newNode = t->dupTree(t); + newTree->addChild(newTree, newNode); + } + } + } + + return newTree; +} + +static pANTLR3_STRING +toStringTree (pANTLR3_BASE_TREE tree) +{ + pANTLR3_STRING string; + ANTLR3_UINT32 i; + ANTLR3_UINT32 n; + pANTLR3_BASE_TREE t; + + if (tree->children == NULL || tree->children->size(tree->children) == 0) + { + return tree->toString(tree); + } + + /* Need a new string with nothing at all in it. + */ + string = tree->strFactory->newRaw(tree->strFactory); + + if (tree->isNilNode(tree) == ANTLR3_FALSE) + { + string->append8 (string, "("); + string->appendS (string, tree->toString(tree)); + string->append8 (string, " "); + } + if (tree->children != NULL) + { + n = tree->children->size(tree->children); + + for (i = 0; i < n; i++) + { + t = (pANTLR3_BASE_TREE) tree->children->get(tree->children, i); + + if (i > 0) + { + string->append8(string, " "); + } + string->appendS(string, t->toStringTree(t)); + } + } + if (tree->isNilNode(tree) == ANTLR3_FALSE) + { + string->append8(string,")"); + } + + return string; +} + +/// Delete children from start to stop and replace with t even if t is +/// a list (nil-root tree). Num of children can increase or decrease. +/// For huge child lists, inserting children can force walking rest of +/// children to set their child index; could be slow. +/// +static void +replaceChildren (pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE newTree) +{ + ANTLR3_INT32 replacingHowMany; // How many nodes will go away + ANTLR3_INT32 replacingWithHowMany; // How many nodes will replace them + ANTLR3_INT32 numNewChildren; // Tracking variable + ANTLR3_INT32 delta; // Difference in new vs existing count + + ANTLR3_INT32 i; + ANTLR3_INT32 j; + + pANTLR3_VECTOR newChildren; // Iterator for whatever we are going to add in + ANTLR3_BOOLEAN freeNewChildren; // Whether we created the iterator locally or reused it + + if (parent->children == NULL) + { + ANTLR3_FPRINTF(stderr, "replaceChildren call: Indexes are invalid; no children in list for %s", parent->getText(parent)->chars); + return; + } + + // Either use the existing list of children in the supplied nil node, or build a vector of the + // tree we were given if it is not a nil node, then we treat both situations exactly the same + // + if (newTree->isNilNode(newTree)) + { + newChildren = newTree->children; + freeNewChildren = ANTLR3_FALSE; // We must NO free this memory + } + else + { + newChildren = antlr3VectorNew(1); + if (newChildren == NULL) + { + ANTLR3_FPRINTF(stderr, "replaceChildren: out of memory!!"); + exit(1); + } + newChildren->add(newChildren, (void *)newTree, NULL); + + freeNewChildren = ANTLR3_TRUE; // We must free this memory + } + + // Initialize + // + replacingHowMany = stopChildIndex - startChildIndex + 1; + replacingWithHowMany = newChildren->size(newChildren); + delta = replacingHowMany - replacingWithHowMany; + numNewChildren = newChildren->size(newChildren); + + // If it is the same number of nodes, then do a direct replacement + // + if (delta == 0) + { + pANTLR3_BASE_TREE child; + + // Same number of nodes + // + j = 0; + for (i = startChildIndex; i <= stopChildIndex; i++) + { + child = (pANTLR3_BASE_TREE) newChildren->get(newChildren, j); + parent->children->set(parent->children, i, child, NULL, ANTLR3_FALSE); + child->setParent(child, parent); + child->setChildIndex(child, i); + } + } + else if (delta > 0) + { + ANTLR3_UINT32 indexToDelete; + + // Less nodes than there were before + // reuse what we have then delete the rest + // + for (j = 0; j < numNewChildren; j++) + { + parent->children->set(parent->children, startChildIndex + j, newChildren->get(newChildren, j), NULL, ANTLR3_FALSE); + } + + // We just delete the same index position until done + // + indexToDelete = startChildIndex + numNewChildren; + + for (j = indexToDelete; j <= (ANTLR3_INT32)stopChildIndex; j++) + { + parent->children->remove(parent->children, indexToDelete); + } + + parent->freshenPACIndexes(parent, startChildIndex); + } + else + { + ANTLR3_UINT32 numToInsert; + + // More nodes than there were before + // Use what we can, then start adding + // + for (j = 0; j < replacingHowMany; j++) + { + parent->children->set(parent->children, startChildIndex + j, newChildren->get(newChildren, j), NULL, ANTLR3_FALSE); + } + + numToInsert = replacingWithHowMany - replacingHowMany; + + for (j = replacingHowMany; j < replacingWithHowMany; j++) + { + parent->children->add(parent->children, newChildren->get(newChildren, j), NULL); + } + + parent->freshenPACIndexes(parent, startChildIndex); + } + + if (freeNewChildren == ANTLR3_TRUE) + { + ANTLR3_FREE(newChildren->elements); + newChildren->elements = NULL; + newChildren->size = 0; + ANTLR3_FREE(newChildren); // Will not free the nodes + } +} + +/// Set the parent and child indexes for all children of the +/// supplied tree. +/// +static void +freshenPACIndexesAll(pANTLR3_BASE_TREE tree) +{ + tree->freshenPACIndexes(tree, 0); +} + +/// Set the parent and child indexes for some of the children of the +/// supplied tree, starting with the child at the supplied index. +/// +static void +freshenPACIndexes (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 offset) +{ + ANTLR3_UINT32 count; + ANTLR3_UINT32 c; + + count = tree->getChildCount(tree); // How many children do we have + + // Loop from the supplied index and set the indexes and parent + // + for (c = offset; c < count; c++) + { + pANTLR3_BASE_TREE child; + + child = tree->getChild(tree, c); + + child->setChildIndex(child, c); + child->setParent(child, tree); + } +} + diff --git a/antlr-3.1.3/runtime/C/src/antlr3basetreeadaptor.c b/antlr-3.1.3/runtime/C/src/antlr3basetreeadaptor.c new file mode 100644 index 0000000..8a165d3 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3basetreeadaptor.c @@ -0,0 +1,802 @@ +/** \file + * Contains the base functions that all tree adaptors start with. + * this implementation can then be overridden by any higher implementation. + * + */ + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3basetreeadaptor.h> + +#ifdef ANTLR3_WINDOWS +#pragma warning( disable : 4100 ) +#endif + +/* Interface functions + */ +static pANTLR3_BASE_TREE nilNode (pANTLR3_BASE_TREE_ADAPTOR adaptor); +static pANTLR3_BASE_TREE dbgNil (pANTLR3_BASE_TREE_ADAPTOR adaptor); +static pANTLR3_BASE_TREE dupTree (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t); +static pANTLR3_BASE_TREE dbgDupTree (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t); +static pANTLR3_BASE_TREE dupTreeTT (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE parent); +static void addChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE child); +static void dbgAddChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE child); +static pANTLR3_BASE_TREE becomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE newRoot, pANTLR3_BASE_TREE oldRoot); +static pANTLR3_BASE_TREE dbgBecomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE newRoot, pANTLR3_BASE_TREE oldRoot); +static pANTLR3_BASE_TREE rulePostProcessing (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE root); +static void addChildToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN child); +static void dbgAddChildToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN child); +static pANTLR3_BASE_TREE becomeRootToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * newRoot, pANTLR3_BASE_TREE oldRoot); +static pANTLR3_BASE_TREE dbgBecomeRootToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * newRoot, pANTLR3_BASE_TREE oldRoot); +static pANTLR3_BASE_TREE createTypeToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken); +static pANTLR3_BASE_TREE dbgCreateTypeToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken); +static pANTLR3_BASE_TREE createTypeTokenText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken, pANTLR3_UINT8 text); +static pANTLR3_BASE_TREE dbgCreateTypeTokenText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken, pANTLR3_UINT8 text); +static pANTLR3_BASE_TREE createTypeText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text); +static pANTLR3_BASE_TREE dbgCreateTypeText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text); +static ANTLR3_UINT32 getType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t); +static void setType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 type); +static pANTLR3_STRING getText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t); +static void setText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_STRING t); +static void setText8 (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_UINT8 t); +static pANTLR3_BASE_TREE getChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 i); +static ANTLR3_UINT32 getChildCount (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t); +static ANTLR3_UINT32 getUniqueID (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t); +static ANTLR3_BOOLEAN isNilNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t); +static pANTLR3_STRING makeDot (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * theTree); + +/** Given a pointer to a base tree adaptor structure (which is usually embedded in the + * super class the implements the tree adaptor used in the parse), initialize its + * function pointers and so on. + */ +ANTLR3_API void +antlr3BaseTreeAdaptorInit(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_DEBUG_EVENT_LISTENER debugger) +{ + // Initialize the interface + // + if (debugger == NULL) + { + adaptor->nilNode = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR)) + nilNode; + adaptor->addChild = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *)) + addChild; + adaptor->becomeRoot = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *)) + becomeRoot; + adaptor->addChildToken = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, pANTLR3_COMMON_TOKEN)) + addChildToken; + adaptor->becomeRootToken = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *)) + becomeRootToken; + adaptor->createTypeToken = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_COMMON_TOKEN)) + createTypeToken; + adaptor->createTypeTokenText = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_COMMON_TOKEN, pANTLR3_UINT8)) + createTypeTokenText; + adaptor->createTypeText = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_UINT8)) + createTypeText; + adaptor->dupTree = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *)) + dupTree; + } + else + { + adaptor->nilNode = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR)) + dbgNil; + adaptor->addChild = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *)) + dbgAddChild; + adaptor->becomeRoot = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *)) + dbgBecomeRoot; + adaptor->addChildToken = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, pANTLR3_COMMON_TOKEN)) + dbgAddChildToken; + adaptor->becomeRootToken = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *)) + dbgBecomeRootToken; + adaptor->createTypeToken = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_COMMON_TOKEN)) + dbgCreateTypeToken; + adaptor->createTypeTokenText = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_COMMON_TOKEN, pANTLR3_UINT8)) + dbgCreateTypeTokenText; + adaptor->createTypeText = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_UINT8)) + dbgCreateTypeText; + adaptor->dupTree = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *)) + dbgDupTree; + debugger->adaptor = adaptor; + } + + adaptor->dupTreeTT = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *)) + dupTreeTT; + adaptor->rulePostProcessing = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *)) + rulePostProcessing; + adaptor->getType = (ANTLR3_UINT32 (*)(pANTLR3_BASE_TREE_ADAPTOR, void *)) + getType; + adaptor->setType = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_UINT32)) + setType; + adaptor->getText = (pANTLR3_STRING (*) (pANTLR3_BASE_TREE_ADAPTOR, void *)) + getText; + adaptor->setText8 = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, pANTLR3_UINT8)) + setText8; + adaptor->setText = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, pANTLR3_STRING)) + setText; + adaptor->getChild = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_UINT32)) + getChild; + adaptor->getChildCount = (ANTLR3_UINT32 (*)(pANTLR3_BASE_TREE_ADAPTOR, void *)) + getChildCount; + adaptor->getUniqueID = (ANTLR3_UINT32 (*)(pANTLR3_BASE_TREE_ADAPTOR, void *)) + getUniqueID; + adaptor->isNilNode = (ANTLR3_BOOLEAN (*)(pANTLR3_BASE_TREE_ADAPTOR, void *)) + isNilNode; + + adaptor->makeDot = (pANTLR3_STRING (*)(pANTLR3_BASE_TREE_ADAPTOR, void *)) + makeDot; + + /* Remaining functions filled in by the caller. + */ + return; +} + +static void +defineDotNodes(pANTLR3_BASE_TREE_ADAPTOR adaptor, void * t, pANTLR3_STRING dotSpec ) +{ + // How many nodes are we talking about? + // + int nCount; + int i; + + if (t == NULL) + { + // No tree, so create a blank spec + // + dotSpec->append8(dotSpec, "n0[label=\"EMPTY TREE\"]\n"); + return; + } + + // Count the nodes + // + nCount = adaptor->getChildCount(adaptor, t); + + if (nCount == 0) + { + // This will already have been included as a child of another node + // so there is nothing to add. + // + return; + } + + // For each child of the current tree, define a node using the + // memory address of the node to name it + // + for (i = 0; i<nCount; i++) + { + pANTLR3_BASE_TREE child; + char buff[64]; + pANTLR3_STRING text; + int j; + + // Pick up a pointer for the child + // + child = adaptor->getChild(adaptor, t, i); + + // Name the node + // + sprintf(buff, "\tn%p[label=\"", child); + dotSpec->append8(dotSpec, buff); + text = adaptor->getText(adaptor, child); + for (j = 0; j < (ANTLR3_INT32)(text->len); j++) + { + if (text->charAt(text, j) == '"') + { + dotSpec->append8(dotSpec, "\\\""); + } + else + { + dotSpec->addc(dotSpec, text->charAt(text, j)); + } + } + dotSpec->append8(dotSpec, "\"]\n"); + + // And now define the children of this child (if any) + // + defineDotNodes(adaptor, child, dotSpec); + } + + // Done + // + return; +} + +static void +defineDotEdges(pANTLR3_BASE_TREE_ADAPTOR adaptor, void * t, pANTLR3_STRING dotSpec) +{ + // How many nodes are we talking about? + // + int nCount; + int i; + + if (t == NULL) + { + // No tree, so do nothing + // + return; + } + + // Count the nodes + // + nCount = adaptor->getChildCount(adaptor, t); + + if (nCount == 0) + { + // This will already have been included as a child of another node + // so there is nothing to add. + // + return; + } + + // For each child, define an edge from this parent, then process + // and children of this child in the same way + // + for (i=0; i<nCount; i++) + { + pANTLR3_BASE_TREE child; + char buff[128]; + + // Next child + // + child = adaptor->getChild(adaptor, t, i); + + // Create the edge relation + // + sprintf(buff, "\t\tn%p -> n%p\t\t// ", t, child); + dotSpec->append8(dotSpec, buff); + + // Document the relationship + // + dotSpec->appendS(dotSpec, adaptor->getText(adaptor, t)); + dotSpec->append8(dotSpec, " -> "); + dotSpec->appendS(dotSpec, adaptor->getText(adaptor, child)); + dotSpec->append8(dotSpec, "\n"); + + // Define edges for this child + // + defineDotEdges(adaptor, child, dotSpec); + } + + // Done + // + return; +} + +/// Produce a DOT specification for graphviz +// +static pANTLR3_STRING +makeDot (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * theTree) +{ + // The string we are building up + // + pANTLR3_STRING dotSpec; + + dotSpec = adaptor->strFactory->newStr + + ( + adaptor->strFactory, + + // Default look and feel + // + (pANTLR3_UINT8) + "digraph {\n\n" + "\tordering=out;\n" + "\tranksep=.4;\n" + "\tbgcolor=\"lightgrey\"; node [shape=box, fixedsize=false, fontsize=12, fontname=\"Helvetica-bold\", fontcolor=\"blue\"\n" + "\twidth=.25, height=.25, color=\"black\", fillcolor=\"white\", style=\"filled, solid, bold\"];\n\n" + "\tedge [arrowsize=.5, color=\"black\", style=\"bold\"]\n\n" + ); + + // First produce the node defintions + // + defineDotNodes(adaptor, theTree, dotSpec); + dotSpec->append8(dotSpec, "\n"); + defineDotEdges(adaptor, theTree, dotSpec); + + // Terminate the spec + // + dotSpec->append8(dotSpec, "\n}"); + + // Result + // + return dotSpec; +} + + +/** Create and return a nil tree node (no token payload) + */ +static pANTLR3_BASE_TREE +nilNode (pANTLR3_BASE_TREE_ADAPTOR adaptor) +{ + return adaptor->create(adaptor, NULL); +} + +static pANTLR3_BASE_TREE +dbgNil (pANTLR3_BASE_TREE_ADAPTOR adaptor) +{ + pANTLR3_BASE_TREE t; + + t = adaptor->create (adaptor, NULL); + adaptor->debugger->createNode (adaptor->debugger, t); + + return t; +} + +/** Return a duplicate of the entire tree (implementation provided by the + * BASE_TREE interface.) + */ +static pANTLR3_BASE_TREE +dupTree (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t) +{ + return adaptor->dupTreeTT(adaptor, t, NULL); +} + +pANTLR3_BASE_TREE +dupTreeTT (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE parent) +{ + pANTLR3_BASE_TREE newTree; + pANTLR3_BASE_TREE child; + pANTLR3_BASE_TREE newSubTree; + ANTLR3_UINT32 n; + ANTLR3_UINT32 i; + + if (t == NULL) + { + return NULL; + } + newTree = t->dupNode(t); + + // Ensure new subtree root has parent/child index set + // + adaptor->setChildIndex (adaptor, newTree, t->getChildIndex(t)); + adaptor->setParent (adaptor, newTree, parent); + n = adaptor->getChildCount (adaptor, t); + + for (i=0; i < n; i++) + { + child = adaptor->getChild (adaptor, t, i); + newSubTree = adaptor->dupTreeTT (adaptor, child, t); + adaptor->addChild (adaptor, newTree, newSubTree); + } + return newTree; +} + +/// Sends the required debugging events for duplicating a tree +/// to the debugger. +/// +static void +simulateTreeConstruction(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE tree) +{ + ANTLR3_UINT32 n; + ANTLR3_UINT32 i; + pANTLR3_BASE_TREE child; + + // Send the create node event + // + adaptor->debugger->createNode(adaptor->debugger, tree); + + n = adaptor->getChildCount(adaptor, tree); + for (i = 0; i < n; i++) + { + child = adaptor->getChild(adaptor, tree, i); + simulateTreeConstruction(adaptor, child); + adaptor->debugger->addChild(adaptor->debugger, tree, child); + } +} + +pANTLR3_BASE_TREE +dbgDupTree (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE tree) +{ + pANTLR3_BASE_TREE t; + + // Call the normal dup tree mechanism first + // + t = adaptor->dupTreeTT(adaptor, tree, NULL); + + // In order to tell the debugger what we have just done, we now + // simulate the tree building mechanism. THis will fire + // lots of debugging events to the client and look like we + // duped the tree.. + // + simulateTreeConstruction(adaptor, t); + + return t; +} + +/** Add a child to the tree t. If child is a flat tree (a list), make all + * in list children of t. Warning: if t has no children, but child does + * and child isNilNode then it is ok to move children to t via + * t.children = child.children; i.e., without copying the array. This + * is for construction and I'm not sure it's completely general for + * a tree's addChild method to work this way. Make sure you differentiate + * between your tree's addChild and this parser tree construction addChild + * if it's not ok to move children to t with a simple assignment. + */ +static void +addChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE child) +{ + if (t != NULL && child != NULL) + { + t->addChild(t, child); + } +} +static void +dbgAddChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE child) +{ + if (t != NULL && child != NULL) + { + t->addChild(t, child); + adaptor->debugger->addChild(adaptor->debugger, t, child); + } +} +/** Use the adaptor implementation to add a child node with the supplied token + */ +static void +addChildToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN child) +{ + if (t != NULL && child != NULL) + { + adaptor->addChild(adaptor, t, adaptor->create(adaptor, child)); + } +} +static void +dbgAddChildToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN child) +{ + pANTLR3_BASE_TREE tc; + + if (t != NULL && child != NULL) + { + tc = adaptor->create(adaptor, child); + adaptor->addChild(adaptor, t, tc); + adaptor->debugger->addChild(adaptor->debugger, t, tc); + } +} + +/** If oldRoot is a nil root, just copy or move the children to newRoot. + * If not a nil root, make oldRoot a child of newRoot. + * + * \code + * old=^(nil a b c), new=r yields ^(r a b c) + * old=^(a b c), new=r yields ^(r ^(a b c)) + * \endcode + * + * If newRoot is a nil-rooted single child tree, use the single + * child as the new root node. + * + * \code + * old=^(nil a b c), new=^(nil r) yields ^(r a b c) + * old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) + * \endcode + * + * If oldRoot was null, it's ok, just return newRoot (even if isNilNode). + * + * \code + * old=null, new=r yields r + * old=null, new=^(nil r) yields ^(nil r) + * \endcode + * + * Return newRoot. Throw an exception if newRoot is not a + * simple node or nil root with a single child node--it must be a root + * node. If newRoot is <code>^(nil x)</endcode> return x as newRoot. + * + * Be advised that it's ok for newRoot to point at oldRoot's + * children; i.e., you don't have to copy the list. We are + * constructing these nodes so we should have this control for + * efficiency. + */ +static pANTLR3_BASE_TREE +becomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE newRootTree, pANTLR3_BASE_TREE oldRootTree) +{ + pANTLR3_BASE_TREE saveRoot; + + /* Protect against tree rewrites if we are in some sort of error + * state, but have tried to recover. In C we can end up with a null pointer + * for a tree that was not produced. + */ + if (newRootTree == NULL) + { + return oldRootTree; + } + + /* root is just the new tree as is if there is no + * current root tree. + */ + if (oldRootTree == NULL) + { + return newRootTree; + } + + /* Produce ^(nil real-node) + */ + if (newRootTree->isNilNode(newRootTree)) + { + if (newRootTree->getChildCount(newRootTree) > 1) + { + /* TODO: Handle tree exceptions + */ + ANTLR3_FPRINTF(stderr, "More than one node as root! TODO: Create tree exception handling\n"); + return newRootTree; + } + + /* The new root is the first child, keep track of the original newRoot + * because if it was a Nil Node, then we can reuse it now. + */ + saveRoot = newRootTree; + newRootTree = newRootTree->getChild(newRootTree, 0); + + // Reclaim the old nilNode() + // + saveRoot->reuse(saveRoot); + } + + /* Add old root into new root. addChild takes care of the case where oldRoot + * is a flat list (nill rooted tree). All children of oldroot are added to + * new root. + */ + newRootTree->addChild(newRootTree, oldRootTree); + + // If the oldroot tree was a nil node, then we know at this point + // it has become orphaned by the rewrite logic, so we tell it to do + // whatever it needs to do to be reused. + // + if (oldRootTree->isNilNode(oldRootTree)) + { + // We have taken an old Root Tree and appended all its children to the new + // root. In addition though it was a nil node, which means the generated code + // will not reuse it again, so we will reclaim it here. First we want to zero out + // any pointers it was carrying around. We are just the baseTree handler so we + // don't know necessarilly know how to do this for the real node, we just ask the tree itself + // to do it. + // + oldRootTree->reuse(oldRootTree); + } + /* Always returns new root structure + */ + return newRootTree; + +} +static pANTLR3_BASE_TREE +dbgBecomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE newRootTree, pANTLR3_BASE_TREE oldRootTree) +{ + pANTLR3_BASE_TREE t; + + t = becomeRoot(adaptor, newRootTree, oldRootTree); + + adaptor->debugger->becomeRoot(adaptor->debugger, newRootTree, oldRootTree); + + return t; +} +/** Transform ^(nil x) to x + */ +static pANTLR3_BASE_TREE + rulePostProcessing (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE root) +{ + pANTLR3_BASE_TREE saveRoot; + + // Keep track of the root we are given. If it is a nilNode, then we + // can reuse it rather than orphaning it! + // + saveRoot = root; + + if (root != NULL && root->isNilNode(root)) + { + if (root->getChildCount(root) == 0) + { + root = NULL; + } + else if (root->getChildCount(root) == 1) + { + root = root->getChild(root, 0); + root->setParent(root, NULL); + root->setChildIndex(root, -1); + + // The root we were given was a nil node, wiht one child, which means it has + // been abandoned and would be lost in the node factory. However + // nodes can be flagged as resuable to prevent this terrible waste + // + saveRoot->reuse(saveRoot); + } + } + + return root; +} + +/** Use the adaptor interface to set a new tree node with the supplied token + * to the root of the tree. + */ +static pANTLR3_BASE_TREE + becomeRootToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * newRoot, pANTLR3_BASE_TREE oldRoot) +{ + return adaptor->becomeRoot(adaptor, adaptor->create(adaptor, newRoot), oldRoot); +} +static pANTLR3_BASE_TREE +dbgBecomeRootToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * newRoot, pANTLR3_BASE_TREE oldRoot) +{ + pANTLR3_BASE_TREE t; + + t = adaptor->becomeRoot(adaptor, adaptor->create(adaptor, newRoot), oldRoot); + + adaptor->debugger->becomeRoot(adaptor->debugger,t, oldRoot); + + return t; +} + +/** Use the super class supplied create() method to create a new node + * from the supplied token. + */ +static pANTLR3_BASE_TREE +createTypeToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken) +{ + /* Create the new token + */ + fromToken = adaptor->createTokenFromToken(adaptor, fromToken); + + /* Set the type of the new token to that supplied + */ + fromToken->setType(fromToken, tokenType); + + /* Return a new node based upon this token + */ + return adaptor->create(adaptor, fromToken); +} +static pANTLR3_BASE_TREE +dbgCreateTypeToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken) +{ + pANTLR3_BASE_TREE t; + + t = createTypeToken(adaptor, tokenType, fromToken); + + adaptor->debugger->createNode(adaptor->debugger, t); + + return t; +} + +static pANTLR3_BASE_TREE +createTypeTokenText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken, pANTLR3_UINT8 text) +{ + /* Create the new token + */ + fromToken = adaptor->createTokenFromToken(adaptor, fromToken); + + /* Set the type of the new token to that supplied + */ + fromToken->setType(fromToken, tokenType); + + /* Set the text of the token accordingly + */ + fromToken->setText8(fromToken, text); + + /* Return a new node based upon this token + */ + return adaptor->create(adaptor, fromToken); +} +static pANTLR3_BASE_TREE +dbgCreateTypeTokenText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken, pANTLR3_UINT8 text) +{ + pANTLR3_BASE_TREE t; + + t = createTypeTokenText(adaptor, tokenType, fromToken, text); + + adaptor->debugger->createNode(adaptor->debugger, t); + + return t; +} + +static pANTLR3_BASE_TREE + createTypeText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text) +{ + pANTLR3_COMMON_TOKEN fromToken; + + /* Create the new token + */ + fromToken = adaptor->createToken(adaptor, tokenType, text); + + /* Return a new node based upon this token + */ + return adaptor->create(adaptor, fromToken); +} +static pANTLR3_BASE_TREE + dbgCreateTypeText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text) +{ + pANTLR3_BASE_TREE t; + + t = createTypeText(adaptor, tokenType, text); + + adaptor->debugger->createNode(adaptor->debugger, t); + + return t; + +} +/** Dummy implementation - will be supplied by super class + */ +static ANTLR3_UINT32 + getType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t) +{ + return 0; +} + +/** Dummy implementation - will be supplied by super class + */ +static void + setType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 type) +{ + ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement setType()\n"); +} + +/** Dummy implementation - will be supplied by super class + */ +static pANTLR3_STRING + getText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t) +{ + ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement getText()\n"); + return NULL; +} + +/** Dummy implementation - will be supplied by super class + */ +static void + setText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_STRING t) +{ + ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement setText()\n"); +} +/** Dummy implementation - will be supplied by super class + */ +static void +setText8 (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_UINT8 t) +{ + ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement setText()\n"); +} + +static pANTLR3_BASE_TREE + getChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i) +{ + ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement getChild()\n"); + return NULL; +} + +static ANTLR3_UINT32 + getChildCount (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE tree) +{ + ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement getChildCount()\n"); + return 0; +} + +/** Returns a uniqueID for the node. Because this is the C implementation + * we can just use its address suitably converted/cast to an integer. + */ +static ANTLR3_UINT32 + getUniqueID (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE node) +{ + return ANTLR3_UINT32_CAST(node); +} + +static ANTLR3_BOOLEAN +isNilNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t) +{ + return t->isNilNode(t); +} diff --git a/antlr-3.1.3/runtime/C/src/antlr3bitset.c b/antlr-3.1.3/runtime/C/src/antlr3bitset.c new file mode 100644 index 0000000..26fcccc --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3bitset.c @@ -0,0 +1,680 @@ +/// +/// \file +/// Contains the C implementation of ANTLR3 bitsets as adapted from Terence Parr's +/// Java implementation. +/// + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3bitset.h> + +// External interface +// + +static pANTLR3_BITSET antlr3BitsetClone (pANTLR3_BITSET inSet); +static pANTLR3_BITSET antlr3BitsetOR (pANTLR3_BITSET bitset1, pANTLR3_BITSET bitset2); +static void antlr3BitsetORInPlace (pANTLR3_BITSET bitset, pANTLR3_BITSET bitset2); +static ANTLR3_UINT32 antlr3BitsetSize (pANTLR3_BITSET bitset); +static void antlr3BitsetAdd (pANTLR3_BITSET bitset, ANTLR3_INT32 bit); +static ANTLR3_BOOLEAN antlr3BitsetEquals (pANTLR3_BITSET bitset1, pANTLR3_BITSET bitset2); +static ANTLR3_BOOLEAN antlr3BitsetMember (pANTLR3_BITSET bitset, ANTLR3_UINT32 bit); +static ANTLR3_UINT32 antlr3BitsetNumBits (pANTLR3_BITSET bitset); +static void antlr3BitsetRemove (pANTLR3_BITSET bitset, ANTLR3_UINT32 bit); +static ANTLR3_BOOLEAN antlr3BitsetIsNil (pANTLR3_BITSET bitset); +static pANTLR3_INT32 antlr3BitsetToIntList (pANTLR3_BITSET bitset); + +// Local functions +// +static void growToInclude (pANTLR3_BITSET bitset, ANTLR3_INT32 bit); +static void grow (pANTLR3_BITSET bitset, ANTLR3_INT32 newSize); +static ANTLR3_UINT64 bitMask (ANTLR3_UINT32 bitNumber); +static ANTLR3_UINT32 numWordsToHold (ANTLR3_UINT32 bit); +static ANTLR3_UINT32 wordNumber (ANTLR3_UINT32 bit); +static void antlr3BitsetFree (pANTLR3_BITSET bitset); + +static void +antlr3BitsetFree(pANTLR3_BITSET bitset) +{ + if (bitset->blist.bits != NULL) + { + ANTLR3_FREE(bitset->blist.bits); + bitset->blist.bits = NULL; + } + ANTLR3_FREE(bitset); + + return; +} + +ANTLR3_API pANTLR3_BITSET +antlr3BitsetNew(ANTLR3_UINT32 numBits) +{ + pANTLR3_BITSET bitset; + + ANTLR3_UINT32 numelements; + + // Allocate memory for the bitset structure itself + // + bitset = (pANTLR3_BITSET) ANTLR3_MALLOC((size_t)sizeof(ANTLR3_BITSET)); + + if (bitset == NULL) + { + return NULL; + } + + // Avoid memory thrashing at the up front expense of a few bytes + // + if (numBits < (8 * ANTLR3_BITSET_BITS)) + { + numBits = 8 * ANTLR3_BITSET_BITS; + } + + // No we need to allocate the memory for the number of bits asked for + // in multiples of ANTLR3_UINT64. + // + numelements = ((numBits -1) >> ANTLR3_BITSET_LOG_BITS) + 1; + + bitset->blist.bits = (pANTLR3_BITWORD) ANTLR3_MALLOC((size_t)(numelements * sizeof(ANTLR3_BITWORD))); + memset(bitset->blist.bits, 0, (size_t)(numelements * sizeof(ANTLR3_BITWORD))); + bitset->blist.length = numelements; + + if (bitset->blist.bits == NULL) + { + ANTLR3_FREE(bitset); + return NULL; + } + + antlr3BitsetSetAPI(bitset); + + + // All seems good + // + return bitset; +} + +ANTLR3_API void +antlr3BitsetSetAPI(pANTLR3_BITSET bitset) +{ + bitset->clone = antlr3BitsetClone; + bitset->bor = antlr3BitsetOR; + bitset->borInPlace = antlr3BitsetORInPlace; + bitset->size = antlr3BitsetSize; + bitset->add = antlr3BitsetAdd; + bitset->grow = grow; + bitset->equals = antlr3BitsetEquals; + bitset->isMember = antlr3BitsetMember; + bitset->numBits = antlr3BitsetNumBits; + bitset->remove = antlr3BitsetRemove; + bitset->isNilNode = antlr3BitsetIsNil; + bitset->toIntList = antlr3BitsetToIntList; + + bitset->free = antlr3BitsetFree; +} + +ANTLR3_API pANTLR3_BITSET +antlr3BitsetCopy(pANTLR3_BITSET_LIST blist) +{ + pANTLR3_BITSET bitset; + int numElements; + + // Allocate memory for the bitset structure itself + // + bitset = (pANTLR3_BITSET) ANTLR3_MALLOC((size_t)sizeof(ANTLR3_BITSET)); + + if (bitset == NULL) + { + return NULL; + } + + numElements = blist->length; + + // Avoid memory thrashing at the expense of a few more bytes + // + if (numElements < 8) + { + numElements = 8; + } + + // Install the length in ANTLR3_UINT64 units + // + bitset->blist.length = numElements; + + bitset->blist.bits = (pANTLR3_BITWORD)ANTLR3_MALLOC((size_t)(numElements * sizeof(ANTLR3_BITWORD))); + + if (bitset->blist.bits == NULL) + { + ANTLR3_FREE(bitset); + return NULL; + } + + ANTLR3_MEMCPY(bitset->blist.bits, blist->bits, (ANTLR3_UINT64)(numElements * sizeof(ANTLR3_BITWORD))); + + // All seems good + // + return bitset; +} + +static pANTLR3_BITSET +antlr3BitsetClone(pANTLR3_BITSET inSet) +{ + pANTLR3_BITSET bitset; + + // Allocate memory for the bitset structure itself + // + bitset = antlr3BitsetNew(ANTLR3_BITSET_BITS * inSet->blist.length); + + if (bitset == NULL) + { + return NULL; + } + + // Install the actual bits in the source set + // + ANTLR3_MEMCPY(bitset->blist.bits, inSet->blist.bits, (ANTLR3_UINT64)(inSet->blist.length * sizeof(ANTLR3_BITWORD))); + + // All seems good + // + return bitset; +} + + +ANTLR3_API pANTLR3_BITSET +antlr3BitsetList(pANTLR3_HASH_TABLE list) +{ + pANTLR3_BITSET bitSet; + pANTLR3_HASH_ENUM en; + pANTLR3_HASH_KEY key; + ANTLR3_UINT64 bit; + + // We have no idea what exactly is in the list + // so create a default bitset and then just add stuff + // as we enumerate. + // + bitSet = antlr3BitsetNew(0); + + en = antlr3EnumNew(list); + + while (en->next(en, &key, (void **)(&bit)) == ANTLR3_SUCCESS) + { + bitSet->add(bitSet, (ANTLR3_UINT32)bit); + } + en->free(en); + + return NULL; +} + +/// +/// \brief +/// Creates a new bitset with at least one 64 bit bset of bits, but as +/// many 64 bit sets as are required. +/// +/// \param[in] bset +/// A variable number of bits to add to the set, ending in -1 (impossible bit). +/// +/// \returns +/// A new bit set with all of the specified bitmaps in it and the API +/// initialized. +/// +/// Call as: +/// - pANTLR3_BITSET = antlrBitsetLoad(bset, bset11, ..., -1); +/// - pANTLR3_BITSET = antlrBitsetOf(-1); Create empty bitset +/// +/// \remarks +/// Stdargs function - must supply -1 as last paremeter, which is NOT +/// added to the set. +/// +/// +ANTLR3_API pANTLR3_BITSET +antlr3BitsetLoad(pANTLR3_BITSET_LIST inBits) +{ + pANTLR3_BITSET bitset; + ANTLR3_UINT32 count; + + // Allocate memory for the bitset structure itself + // the input parameter is the bit number (0 based) + // to include in the bitset, so we need at at least + // bit + 1 bits. If any arguments indicate a + // a bit higher than the default number of bits (0 means default size) + // then Add() will take care + // of it. + // + bitset = antlr3BitsetNew(0); + + if (bitset == NULL) + { + return NULL; + } + + if (inBits != NULL) + { + // Now we can add the element bits into the set + // + count=0; + while (count < inBits->length) + { + if (bitset->blist.length <= count) + { + bitset->grow(bitset, count+1); + } + + bitset->blist.bits[count] = *((inBits->bits)+count); + count++; + } + } + + // return the new bitset + // + return bitset; +} + +/// +/// \brief +/// Creates a new bitset with at least one element, but as +/// many elements are required. +/// +/// \param[in] bit +/// A variable number of bits to add to the set, ending in -1 (impossible bit). +/// +/// \returns +/// A new bit set with all of the specified elements added into it. +/// +/// Call as: +/// - pANTLR3_BITSET = antlrBitsetOf(n, n1, n2, -1); +/// - pANTLR3_BITSET = antlrBitsetOf(-1); Create empty bitset +/// +/// \remarks +/// Stdargs function - must supply -1 as last paremeter, which is NOT +/// added to the set. +/// +/// +ANTLR3_API pANTLR3_BITSET +antlr3BitsetOf(ANTLR3_INT32 bit, ...) +{ + pANTLR3_BITSET bitset; + + va_list ap; + + // Allocate memory for the bitset structure itself + // the input parameter is the bit number (0 based) + // to include in the bitset, so we need at at least + // bit + 1 bits. If any arguments indicate a + // a bit higher than the default number of bits (0 menas default size) + // then Add() will take care + // of it. + // + bitset = antlr3BitsetNew(0); + + if (bitset == NULL) + { + return NULL; + } + + // Now we can add the element bits into the set + // + va_start(ap, bit); + while (bit != -1) + { + antlr3BitsetAdd(bitset, bit); + bit = va_arg(ap, ANTLR3_UINT32); + } + va_end(ap); + + // return the new bitset + // + return bitset; +} + +static pANTLR3_BITSET +antlr3BitsetOR(pANTLR3_BITSET bitset1, pANTLR3_BITSET bitset2) +{ + pANTLR3_BITSET bitset; + + if (bitset1 == NULL) + { + return antlr3BitsetClone(bitset2); + } + + if (bitset2 == NULL) + { + return antlr3BitsetClone(bitset1); + } + + // Allocate memory for the newly ordered bitset structure itself. + // + bitset = antlr3BitsetClone(bitset1); + + antlr3BitsetORInPlace(bitset, bitset2); + + return bitset; + +} + +static void +antlr3BitsetAdd(pANTLR3_BITSET bitset, ANTLR3_INT32 bit) +{ + ANTLR3_UINT32 word; + + word = wordNumber(bit); + + if (word > bitset->blist.length) + { + growToInclude(bitset, bit); + } + + bitset->blist.bits[word] |= bitMask(bit); + +} + +static void +grow(pANTLR3_BITSET bitset, ANTLR3_INT32 newSize) +{ + pANTLR3_BITWORD newBits; + + // Space for newly sized bitset - TODO: come back to this and use realloc?, it may + // be more efficient... + // + newBits = (pANTLR3_BITWORD) ANTLR3_MALLOC((size_t)(newSize * sizeof(ANTLR3_BITWORD))); + + if (bitset->blist.bits != NULL) + { + // Copy existing bits + // + ANTLR3_MEMCPY((void *)newBits, (const void *)bitset->blist.bits, (size_t)(bitset->blist.length * sizeof(ANTLR3_BITWORD))); + + // Out with the old bits... de de de derrr + // + ANTLR3_FREE(bitset->blist.bits); + } + + // In with the new bits... keerrrang. + // + bitset->blist.bits = newBits; +} + +static void +growToInclude(pANTLR3_BITSET bitset, ANTLR3_INT32 bit) +{ + ANTLR3_UINT32 bl; + ANTLR3_UINT32 nw; + + bl = (bitset->blist.length << 1); + nw = numWordsToHold(bit); + if (bl > nw) + { + bitset->grow(bitset, bl); + } + else + { + bitset->grow(bitset, nw); + } +} + +static void +antlr3BitsetORInPlace(pANTLR3_BITSET bitset, pANTLR3_BITSET bitset2) +{ + ANTLR3_UINT32 minimum; + ANTLR3_UINT32 i; + + if (bitset2 == NULL) + { + return; + } + + + // First make sure that the target bitset is big enough + // for the new bits to be ored in. + // + if (bitset->blist.length < bitset2->blist.length) + { + growToInclude(bitset, (bitset2->blist.length * sizeof(ANTLR3_BITWORD))); + } + + // Or the miniimum number of bits after any resizing went on + // + if (bitset->blist.length < bitset2->blist.length) + { + minimum = bitset->blist.length; + } + else + { + minimum = bitset2->blist.length; + } + + for (i = minimum; i > 0; i--) + { + bitset->blist.bits[i-1] |= bitset2->blist.bits[i-1]; + } +} + +static ANTLR3_UINT64 +bitMask(ANTLR3_UINT32 bitNumber) +{ + return ((ANTLR3_UINT64)1) << (bitNumber & (ANTLR3_BITSET_MOD_MASK)); +} + +static ANTLR3_UINT32 +antlr3BitsetSize(pANTLR3_BITSET bitset) +{ + ANTLR3_UINT32 degree; + ANTLR3_INT32 i; + ANTLR3_INT8 bit; + + // TODO: Come back to this, it may be faster to & with 0x01 + // then shift right a copy of the 4 bits, than shift left a constant of 1. + // But then again, the optimizer might just work this out + // anyway. + // + degree = 0; + for (i = bitset->blist.length - 1; i>= 0; i--) + { + if (bitset->blist.bits[i] != 0) + { + for (bit = ANTLR3_BITSET_BITS - 1; bit >= 0; bit--) + { + if ((bitset->blist.bits[i] & (((ANTLR3_BITWORD)1) << bit)) != 0) + { + degree++; + } + } + } + } + return degree; +} + +static ANTLR3_BOOLEAN +antlr3BitsetEquals(pANTLR3_BITSET bitset1, pANTLR3_BITSET bitset2) +{ + ANTLR3_INT32 minimum; + ANTLR3_INT32 i; + + if (bitset1 == NULL || bitset2 == NULL) + { + return ANTLR3_FALSE; + } + + // Work out the minimum comparison set + // + if (bitset1->blist.length < bitset2->blist.length) + { + minimum = bitset1->blist.length; + } + else + { + minimum = bitset2->blist.length; + } + + // Make sure explict in common bits are equal + // + for (i = minimum - 1; i >=0 ; i--) + { + if (bitset1->blist.bits[i] != bitset2->blist.bits[i]) + { + return ANTLR3_FALSE; + } + } + + // Now make sure the bits of the larger set are all turned + // off. + // + if (bitset1->blist.length > (ANTLR3_UINT32)minimum) + { + for (i = minimum ; (ANTLR3_UINT32)i < bitset1->blist.length; i++) + { + if (bitset1->blist.bits[i] != 0) + { + return ANTLR3_FALSE; + } + } + } + else if (bitset2->blist.length > (ANTLR3_UINT32)minimum) + { + for (i = minimum; (ANTLR3_UINT32)i < bitset2->blist.length; i++) + { + if (bitset2->blist.bits[i] != 0) + { + return ANTLR3_FALSE; + } + } + } + + return ANTLR3_TRUE; +} + +static ANTLR3_BOOLEAN +antlr3BitsetMember(pANTLR3_BITSET bitset, ANTLR3_UINT32 bit) +{ + ANTLR3_UINT32 wordNo; + + wordNo = wordNumber(bit); + + if (wordNo >= bitset->blist.length) + { + return ANTLR3_FALSE; + } + + if ((bitset->blist.bits[wordNo] & bitMask(bit)) == 0) + { + return ANTLR3_FALSE; + } + else + { + return ANTLR3_TRUE; + } +} + +static void +antlr3BitsetRemove(pANTLR3_BITSET bitset, ANTLR3_UINT32 bit) +{ + ANTLR3_UINT32 wordNo; + + wordNo = wordNumber(bit); + + if (wordNo < bitset->blist.length) + { + bitset->blist.bits[wordNo] &= ~(bitMask(bit)); + } +} +static ANTLR3_BOOLEAN +antlr3BitsetIsNil(pANTLR3_BITSET bitset) +{ + ANTLR3_INT32 i; + + for (i = bitset->blist.length -1; i>= 0; i--) + { + if (bitset->blist.bits[i] != 0) + { + return ANTLR3_FALSE; + } + } + + return ANTLR3_TRUE; +} + +static ANTLR3_UINT32 +numWordsToHold(ANTLR3_UINT32 bit) +{ + return (bit >> ANTLR3_BITSET_LOG_BITS) + 1; +} + +static ANTLR3_UINT32 +wordNumber(ANTLR3_UINT32 bit) +{ + return bit >> ANTLR3_BITSET_LOG_BITS; +} + +static ANTLR3_UINT32 +antlr3BitsetNumBits(pANTLR3_BITSET bitset) +{ + return bitset->blist.length << ANTLR3_BITSET_LOG_BITS; +} + +/** Produce an integer list of all the bits that are turned on + * in this bitset. Used for error processing in the main as the bitset + * reresents a number of integer tokens which we use for follow sets + * and so on. + * + * The first entry is the number of elements following in the list. + */ +static pANTLR3_INT32 +antlr3BitsetToIntList (pANTLR3_BITSET bitset) +{ + ANTLR3_UINT32 numInts; // How many integers we will need + ANTLR3_UINT32 numBits; // How many bits are in the set + ANTLR3_UINT32 i; + ANTLR3_UINT32 index; + + pANTLR3_INT32 intList; + + numInts = bitset->size(bitset) + 1; + numBits = bitset->numBits(bitset); + + intList = (pANTLR3_INT32)ANTLR3_MALLOC(numInts * sizeof(ANTLR3_INT32)); + + if (intList == NULL) + { + return NULL; // Out of memory + } + + intList[0] = numInts; + + // Enumerate the bits that are turned on + // + for (i = 0, index = 1; i<numBits; i++) + { + if (bitset->isMember(bitset, i) == ANTLR3_TRUE) + { + intList[index++] = i; + } + } + + // Result set + // + return intList; +} + diff --git a/antlr-3.1.3/runtime/C/src/antlr3collections.c b/antlr-3.1.3/runtime/C/src/antlr3collections.c new file mode 100644 index 0000000..c495c9f --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3collections.c @@ -0,0 +1,2159 @@ +/// \file +/// Provides a number of useful functions that are roughly equivalent +/// to java HashTable and List for the purposes of Antlr 3 C runtime. +/// Also useable by the C programmer for things like symbol tables pointers +/// and so on. +/// +/// + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3.h> + +// Interface functions for hash table +// + +// String based keys +// +static void antlr3HashDelete (pANTLR3_HASH_TABLE table, void * key); +static void * antlr3HashGet (pANTLR3_HASH_TABLE table, void * key); +static pANTLR3_HASH_ENTRY antlr3HashRemove (pANTLR3_HASH_TABLE table, void * key); +static ANTLR3_INT32 antlr3HashPut (pANTLR3_HASH_TABLE table, void * key, void * element, void (ANTLR3_CDECL *freeptr)(void *)); + +// Integer based keys (Lists and so on) +// +static void antlr3HashDeleteI (pANTLR3_HASH_TABLE table, ANTLR3_INTKEY key); +static void * antlr3HashGetI (pANTLR3_HASH_TABLE table, ANTLR3_INTKEY key); +static pANTLR3_HASH_ENTRY antlr3HashRemoveI (pANTLR3_HASH_TABLE table, ANTLR3_INTKEY key); +static ANTLR3_INT32 antlr3HashPutI (pANTLR3_HASH_TABLE table, ANTLR3_INTKEY key, void * element, void (ANTLR3_CDECL *freeptr)(void *)); + +static void antlr3HashFree (pANTLR3_HASH_TABLE table); +static ANTLR3_UINT32 antlr3HashSize (pANTLR3_HASH_TABLE table); + +// ----------- + +// Interface functions for enumeration +// +static int antlr3EnumNext (pANTLR3_HASH_ENUM en, pANTLR3_HASH_KEY * key, void ** data); +static void antlr3EnumFree (pANTLR3_HASH_ENUM en); + +// Interface functions for List +// +static void antlr3ListFree (pANTLR3_LIST list); +static void antlr3ListDelete(pANTLR3_LIST list, ANTLR3_INTKEY key); +static void * antlr3ListGet (pANTLR3_LIST list, ANTLR3_INTKEY key); +static ANTLR3_INT32 antlr3ListPut (pANTLR3_LIST list, ANTLR3_INTKEY key, void * element, void (ANTLR3_CDECL *freeptr)(void *)); +static ANTLR3_INT32 antlr3ListAdd (pANTLR3_LIST list, void * element, void (ANTLR3_CDECL *freeptr)(void *)); +static void * antlr3ListRemove(pANTLR3_LIST list, ANTLR3_INTKEY key); +static ANTLR3_UINT32 antlr3ListSize (pANTLR3_LIST list); + +// Interface functions for Stack +// +static void antlr3StackFree (pANTLR3_STACK stack); +static void * antlr3StackPop (pANTLR3_STACK stack); +static void * antlr3StackGet (pANTLR3_STACK stack, ANTLR3_INTKEY key); +static ANTLR3_BOOLEAN antlr3StackPush (pANTLR3_STACK stack, void * element, void (ANTLR3_CDECL *freeptr)(void *)); +static ANTLR3_UINT32 antlr3StackSize (pANTLR3_STACK stack); +static void * antlr3StackPeek (pANTLR3_STACK stack); + +// Interface functions for vectors +// +static void ANTLR3_CDECL antlr3VectorFree (pANTLR3_VECTOR vector); +static void antlr3VectorDel (pANTLR3_VECTOR vector, ANTLR3_UINT32 entry); +static void * antlr3VectorGet (pANTLR3_VECTOR vector, ANTLR3_UINT32 entry); +static void * antrl3VectorRemove (pANTLR3_VECTOR vector, ANTLR3_UINT32 entry); +static void antlr3VectorClear (pANTLR3_VECTOR vector); +static ANTLR3_UINT32 antlr3VectorAdd (pANTLR3_VECTOR vector, void * element, void (ANTLR3_CDECL *freeptr)(void *)); +static ANTLR3_UINT32 antlr3VectorSet (pANTLR3_VECTOR vector, ANTLR3_UINT32 entry, void * element, void (ANTLR3_CDECL *freeptr)(void *), ANTLR3_BOOLEAN freeExisting); +static ANTLR3_UINT32 antlr3VectorSize (pANTLR3_VECTOR vector); + +static void newPool (pANTLR3_VECTOR_FACTORY factory); +static void closeVectorFactory (pANTLR3_VECTOR_FACTORY factory); +static pANTLR3_VECTOR newVector (pANTLR3_VECTOR_FACTORY factory); + + +// Interface functions for int TRIE +// +static pANTLR3_TRIE_ENTRY intTrieGet (pANTLR3_INT_TRIE trie, ANTLR3_INTKEY key); +static ANTLR3_BOOLEAN intTrieDel (pANTLR3_INT_TRIE trie, ANTLR3_INTKEY key); +static ANTLR3_BOOLEAN intTrieAdd (pANTLR3_INT_TRIE trie, ANTLR3_INTKEY key, ANTLR3_UINT32 type, ANTLR3_INTKEY intType, void * data, void (ANTLR3_CDECL *freeptr)(void *)); +static void intTrieFree (pANTLR3_INT_TRIE trie); + +// Local function to advance enumeration structure pointers +// +static void antlr3EnumNextEntry(pANTLR3_HASH_ENUM en); + +pANTLR3_HASH_TABLE +antlr3HashTableNew(ANTLR3_UINT32 sizeHint) +{ + // All we have to do is create the hashtable tracking structure + // and allocate memory for the requested number of buckets. + // + pANTLR3_HASH_TABLE table; + + ANTLR3_UINT32 bucket; // Used to traverse the buckets + + table = ANTLR3_MALLOC(sizeof(ANTLR3_HASH_TABLE)); + + // Error out if no memory left + if (table == NULL) + { + return NULL; + } + + // Allocate memory for the buckets + // + table->buckets = (pANTLR3_HASH_BUCKET) ANTLR3_MALLOC((size_t) (sizeof(ANTLR3_HASH_BUCKET) * sizeHint)); + + if (table->buckets == NULL) + { + ANTLR3_FREE((void *)table); + return NULL; + } + + // Modulo of the table, (bucket count). + // + table->modulo = sizeHint; + + table->count = 0; /* Nothing in there yet ( I hope) */ + + /* Initialize the buckets to empty + */ + for (bucket = 0; bucket < sizeHint; bucket++) + { + table->buckets[bucket].entries = NULL; + } + + /* Exclude duplicate entries by default + */ + table->allowDups = ANTLR3_FALSE; + + /* Assume that keys should by strduped before they are + * entered in the table. + */ + table->doStrdup = ANTLR3_TRUE; + + /* Install the interface + */ + + table->get = antlr3HashGet; + table->put = antlr3HashPut; + table->del = antlr3HashDelete; + table->remove = antlr3HashRemove; + + table->getI = antlr3HashGetI; + table->putI = antlr3HashPutI; + table->delI = antlr3HashDeleteI; + table->removeI = antlr3HashRemoveI; + + table->size = antlr3HashSize; + table->free = antlr3HashFree; + + return table; +} + +static void +antlr3HashFree(pANTLR3_HASH_TABLE table) +{ + ANTLR3_UINT32 bucket; /* Used to traverse the buckets */ + + pANTLR3_HASH_BUCKET thisBucket; + pANTLR3_HASH_ENTRY entry; + pANTLR3_HASH_ENTRY nextEntry; + + /* Free the table, all buckets and all entries, and all the + * keys and data (if the table exists) + */ + if (table != NULL) + { + for (bucket = 0; bucket < table->modulo; bucket++) + { + thisBucket = &(table->buckets[bucket]); + + /* Allow sparse tables, though we don't create them as such at present + */ + if ( thisBucket != NULL) + { + entry = thisBucket->entries; + + /* Search all entries in the bucket and free them up + */ + while (entry != NULL) + { + /* Save next entry - we do not want to access memory in entry after we + * have freed it. + */ + nextEntry = entry->nextEntry; + + /* Free any data pointer, this only happens if the user supplied + * a pointer to a routine that knwos how to free the structure they + * added to the table. + */ + if (entry->free != NULL) + { + entry->free(entry->data); + } + + /* Free the key memory - we know that we allocated this + */ + if (entry->keybase.type == ANTLR3_HASH_TYPE_STR && entry->keybase.key.sKey != NULL) + { + ANTLR3_FREE(entry->keybase.key.sKey); + } + + /* Free this entry + */ + ANTLR3_FREE(entry); + entry = nextEntry; /* Load next pointer to see if we shoud free it */ + } + /* Invalidate the current pointer + */ + thisBucket->entries = NULL; + } + } + + /* Now we can free the bucket memory + */ + ANTLR3_FREE(table->buckets); + } + + /* Now we free teh memory for the table itself + */ + ANTLR3_FREE(table); +} + +/** return the current size of the hash table + */ +static ANTLR3_UINT32 antlr3HashSize (pANTLR3_HASH_TABLE table) +{ + return table->count; +} + +/** Remove a numeric keyed entry from a hash table if it exists, + * no error if it does not exist. + */ +static pANTLR3_HASH_ENTRY antlr3HashRemoveI (pANTLR3_HASH_TABLE table, ANTLR3_INTKEY key) +{ + ANTLR3_UINT32 hash; + pANTLR3_HASH_BUCKET bucket; + pANTLR3_HASH_ENTRY entry; + pANTLR3_HASH_ENTRY * nextPointer; + + /* First we need to know the hash of the provided key + */ + hash = (ANTLR3_UINT32)(key % (ANTLR3_INTKEY)(table->modulo)); + + /* Knowing the hash, we can find the bucket + */ + bucket = table->buckets + hash; + + /* Now, we traverse the entries in the bucket until + * we find the key or the end of the entries in the bucket. + * We track the element prior to the one we are examining + * as we need to set its next pointer to the next pointer + * of the entry we are deleting (if we find it). + */ + entry = bucket->entries; /* Entry to examine */ + nextPointer = & bucket->entries; /* Where to put the next pointer of the deleted entry */ + + while (entry != NULL) + { + /* See if this is the entry we wish to delete + */ + if (entry->keybase.key.iKey == key) + { + /* It was the correct entry, so we set the next pointer + * of the previous entry to the next pointer of this + * located one, which takes it out of the chain. + */ + (*nextPointer) = entry->nextEntry; + + table->count--; + + return entry; + } + else + { + /* We found an entry but it wasn't the one that was wanted, so + * move to the next one, if any. + */ + nextPointer = & (entry->nextEntry); /* Address of the next pointer in the current entry */ + entry = entry->nextEntry; /* Address of the next element in the bucket (if any) */ + } + } + + return NULL; /* Not found */ +} + +/** Remove the element in the hash table for a particular + * key value, if it exists - no error if it does not. + */ +static pANTLR3_HASH_ENTRY +antlr3HashRemove(pANTLR3_HASH_TABLE table, void * key) +{ + ANTLR3_UINT32 hash; + pANTLR3_HASH_BUCKET bucket; + pANTLR3_HASH_ENTRY entry; + pANTLR3_HASH_ENTRY * nextPointer; + + /* First we need to know the hash of the provided key + */ + hash = antlr3Hash(key, (ANTLR3_UINT32)strlen((const char *)key)); + + /* Knowing the hash, we can find the bucket + */ + bucket = table->buckets + (hash % table->modulo); + + /* Now, we traverse the entries in the bucket until + * we find the key or the end of the entires in the bucket. + * We track the element prior to the one we are exmaining + * as we need to set its next pointer to the next pointer + * of the entry we are deleting (if we find it). + */ + entry = bucket->entries; /* Entry to examine */ + nextPointer = & bucket->entries; /* Where to put the next pointer of the deleted entry */ + + while (entry != NULL) + { + /* See if this is the entry we wish to delete + */ + if (strcmp((const char *)key, (const char *)entry->keybase.key.sKey) == 0) + { + /* It was the correct entry, so we set the next pointer + * of the previous entry to the next pointer of this + * located one, which takes it out of the chain. + */ + (*nextPointer) = entry->nextEntry; + + /* Release the key - if we allocated that + */ + if (table->doStrdup == ANTLR3_TRUE) + { + ANTLR3_FREE(entry->keybase.key.sKey); + } + entry->keybase.key.sKey = NULL; + + table->count--; + + return entry; + } + else + { + /* We found an entry but it wasn't the one that was wanted, so + * move to the next one, if any. + */ + nextPointer = & (entry->nextEntry); /* Address of the next pointer in the current entry */ + entry = entry->nextEntry; /* Address of the next element in the bucket (if any) */ + } + } + + return NULL; /* Not found */ +} + +/** Takes the element with the supplied key out of the list, and deletes the data + * calling the supplied free() routine if any. + */ +static void +antlr3HashDeleteI (pANTLR3_HASH_TABLE table, ANTLR3_INTKEY key) +{ + pANTLR3_HASH_ENTRY entry; + + entry = antlr3HashRemoveI(table, key); + + /* Now we can free the elements and the entry in order + */ + if (entry != NULL && entry->free != NULL) + { + /* Call programmer supplied function to release this entry data + */ + entry->free(entry->data); + entry->data = NULL; + } + /* Finally release the space for this entry block. + */ + ANTLR3_FREE(entry); +} + +/** Takes the element with the supplied key out of the list, and deletes the data + * calling the supplied free() routine if any. + */ +static void +antlr3HashDelete (pANTLR3_HASH_TABLE table, void * key) +{ + pANTLR3_HASH_ENTRY entry; + + entry = antlr3HashRemove(table, key); + + /* Now we can free the elements and the entry in order + */ + if (entry != NULL && entry->free != NULL) + { + /* Call programmer supplied function to release this entry data + */ + entry->free(entry->data); + entry->data = NULL; + } + /* Finally release the space for this entry block. + */ + ANTLR3_FREE(entry); +} + +/** Return the element pointer in the hash table for a particular + * key value, or NULL if it don't exist (or was itself NULL). + */ +static void * +antlr3HashGetI(pANTLR3_HASH_TABLE table, ANTLR3_INTKEY key) +{ + ANTLR3_UINT32 hash; + pANTLR3_HASH_BUCKET bucket; + pANTLR3_HASH_ENTRY entry; + + /* First we need to know the hash of the provided key + */ + hash = (ANTLR3_UINT32)(key % (ANTLR3_INTKEY)(table->modulo)); + + /* Knowing the hash, we can find the bucket + */ + bucket = table->buckets + hash; + + /* Now we can inspect the key at each entry in the bucket + * and see if we have a match. + */ + entry = bucket->entries; + + while (entry != NULL) + { + if (entry->keybase.key.iKey == key) + { + /* Match was found, return the data pointer for this entry + */ + return entry->data; + } + entry = entry->nextEntry; + } + + /* If we got here, then we did not find the key + */ + return NULL; +} + +/** Return the element pointer in the hash table for a particular + * key value, or NULL if it don't exist (or was itself NULL). + */ +static void * +antlr3HashGet(pANTLR3_HASH_TABLE table, void * key) +{ + ANTLR3_UINT32 hash; + pANTLR3_HASH_BUCKET bucket; + pANTLR3_HASH_ENTRY entry; + + + /* First we need to know the hash of the provided key + */ + hash = antlr3Hash(key, (ANTLR3_UINT32)strlen((const char *)key)); + + /* Knowing the hash, we can find the bucket + */ + bucket = table->buckets + (hash % table->modulo); + + /* Now we can inspect the key at each entry in the bucket + * and see if we have a match. + */ + entry = bucket->entries; + + while (entry != NULL) + { + if (strcmp((const char *)key, (const char *)entry->keybase.key.sKey) == 0) + { + /* Match was found, return the data pointer for this entry + */ + return entry->data; + } + entry = entry->nextEntry; + } + + /* If we got here, then we did not find the key + */ + return NULL; +} + +/** Add the element pointer in to the table, based upon the + * hash of the provided key. + */ +static ANTLR3_INT32 +antlr3HashPutI(pANTLR3_HASH_TABLE table, ANTLR3_INTKEY key, void * element, void (ANTLR3_CDECL *freeptr)(void *)) +{ + ANTLR3_UINT32 hash; + pANTLR3_HASH_BUCKET bucket; + pANTLR3_HASH_ENTRY entry; + pANTLR3_HASH_ENTRY * newPointer; + + /* First we need to know the hash of the provided key + */ + hash = (ANTLR3_UINT32)(key % (ANTLR3_INTKEY)(table->modulo)); + + /* Knowing the hash, we can find the bucket + */ + bucket = table->buckets + hash; + + /* Knowing the bucket, we can traverse the entries until we + * we find a NULL pointer or we find that this is already + * in the table and duplicates were not allowed. + */ + newPointer = &bucket->entries; + + while (*newPointer != NULL) + { + /* The value at new pointer is pointing to an existing entry. + * If duplicates are allowed then we don't care what it is, but + * must reject this add if the key is the same as the one we are + * supplied with. + */ + if (table->allowDups == ANTLR3_FALSE) + { + if ((*newPointer)->keybase.key.iKey == key) + { + return ANTLR3_ERR_HASHDUP; + } + } + + /* Point to the next entry pointer of the current entry we + * are traversing, if it is NULL we will create our new + * structure and point this to it. + */ + newPointer = &((*newPointer)->nextEntry); + } + + /* newPointer is now pointing at the pointer where we need to + * add our new entry, so let's crate the entry and add it in. + */ + entry = (pANTLR3_HASH_ENTRY)ANTLR3_MALLOC((size_t)sizeof(ANTLR3_HASH_ENTRY)); + + if (entry == NULL) + { + return ANTLR3_ERR_NOMEM; + } + + entry->data = element; /* Install the data element supplied */ + entry->free = freeptr; /* Function that knows how to release the entry */ + entry->keybase.type = ANTLR3_HASH_TYPE_INT; /* Indicate the key type stored here for when we free */ + entry->keybase.key.iKey = key; /* Record the key value */ + entry->nextEntry = NULL; /* Ensure that the forward pointer ends the chain */ + + *newPointer = entry; /* Install the next entry in this bucket */ + + table->count++; + + return ANTLR3_SUCCESS; +} + + +/** Add the element pointer in to the table, based upon the + * hash of the provided key. + */ +static ANTLR3_INT32 +antlr3HashPut(pANTLR3_HASH_TABLE table, void * key, void * element, void (ANTLR3_CDECL *freeptr)(void *)) +{ + ANTLR3_UINT32 hash; + pANTLR3_HASH_BUCKET bucket; + pANTLR3_HASH_ENTRY entry; + pANTLR3_HASH_ENTRY * newPointer; + + /* First we need to know the hash of the provided key + */ + hash = antlr3Hash(key, (ANTLR3_UINT32)strlen((const char *)key)); + + /* Knowing the hash, we can find the bucket + */ + bucket = table->buckets + (hash % table->modulo); + + /* Knowign the bucket, we can traverse the entries until we + * we find a NULL pointer ofr we find that this is already + * in the table and duplicates were not allowed. + */ + newPointer = &bucket->entries; + + while (*newPointer != NULL) + { + /* The value at new pointer is pointing to an existing entry. + * If duplicates are allowed then we don't care what it is, but + * must reject this add if the key is the same as the one we are + * supplied with. + */ + if (table->allowDups == ANTLR3_FALSE) + { + if (strcmp((const char*) key, (const char *)(*newPointer)->keybase.key.sKey) == 0) + { + return ANTLR3_ERR_HASHDUP; + } + } + + /* Point to the next entry pointer of the current entry we + * are traversing, if it is NULL we will create our new + * structure and point this to it. + */ + newPointer = &((*newPointer)->nextEntry); + } + + /* newPointer is now poiting at the pointer where we need to + * add our new entry, so let's crate the entry and add it in. + */ + entry = (pANTLR3_HASH_ENTRY)ANTLR3_MALLOC((size_t)sizeof(ANTLR3_HASH_ENTRY)); + + if (entry == NULL) + { + return ANTLR3_ERR_NOMEM; + } + + entry->data = element; /* Install the data element supplied */ + entry->free = freeptr; /* Function that knows how to release the entry */ + entry->keybase.type = ANTLR3_HASH_TYPE_STR; /* Indicate the key type stored here for free() */ + if (table->doStrdup == ANTLR3_TRUE) + { + entry->keybase.key.sKey = ANTLR3_STRDUP(key); /* Record the key value */ + } + else + { + entry->keybase.key.sKey = key; /* Record the key value */ + } + entry->nextEntry = NULL; /* Ensure that the forward pointer ends the chain */ + + *newPointer = entry; /* Install the next entry in this bucket */ + + table->count++; + + return ANTLR3_SUCCESS; +} + +/** \brief Creates an enumeration structure to traverse the hash table. + * + * \param table Table to enumerate + * \return Pointer to enumeration structure. + */ +pANTLR3_HASH_ENUM +antlr3EnumNew (pANTLR3_HASH_TABLE table) +{ + pANTLR3_HASH_ENUM en; + + /* Allocate structure memory + */ + en = (pANTLR3_HASH_ENUM) ANTLR3_MALLOC((size_t)sizeof(ANTLR3_HASH_ENUM)); + + /* Check that the allocation was good + */ + if (en == NULL) + { + return (pANTLR3_HASH_ENUM) ANTLR3_FUNC_PTR(ANTLR3_ERR_NOMEM); + } + + /* Initialize the start pointers + */ + en->table = table; + en->bucket = 0; /* First bucket */ + en->entry = en->table->buckets->entries; /* First entry to return */ + + /* Special case in that the first bucket may not have anything in it + * but the antlr3EnumNext() function expects that the en->entry is + * set to the next valid pointer. Hence if it is not a valid element + * pointer, attempt to find the next one that is, (table may be empty + * of course. + */ + if (en->entry == NULL) + { + antlr3EnumNextEntry(en); + } + + /* Install the interface + */ + en->free = antlr3EnumFree; + en->next = antlr3EnumNext; + + /* All is good + */ + return en; +} + +/** \brief Return the next entry in the hashtable being traversed by the supplied + * enumeration. + * + * \param[in] en Pointer to the enumeration tracking structure + * \param key Pointer to void pointer, where the key pointer is returned. + * \param data Pointer to void pointer where the data pointer is returned. + * \return + * - ANTLR3_SUCCESS if there was a next key + * - ANTLR3_FAIL if there were no more keys + * + * \remark + * No checking of input structure is performed! + */ +static int +antlr3EnumNext (pANTLR3_HASH_ENUM en, pANTLR3_HASH_KEY * key, void ** data) +{ + /* If the current entry is valid, then use it + */ + if (en->bucket >= en->table->modulo) + { + /* Already exhausted the table + */ + return ANTLR3_FAIL; + } + + /* Pointers are already set to the current entry to return, or + * we would not be at this point in the logic flow. + */ + *key = &(en->entry->keybase); + *data = en->entry->data; + + /* Return pointers are set up, so now we move the element + * pointer to the next in the table (if any). + */ + antlr3EnumNextEntry(en); + + return ANTLR3_SUCCESS; +} + +/** \brief Local function to advance the entry pointer of an enumeration + * structure to the next valid entry (if there is one). + * + * \param[in] enum Pointer to ANTLR3 enumeration structure returned by antlr3EnumNew() + * + * \remark + * - The function always leaves the pointers pointing at a valid entry if there + * is one, so if the entry pointer is NULL when this function exits, there were + * no more entries in the table. + */ +static void +antlr3EnumNextEntry(pANTLR3_HASH_ENUM en) +{ + pANTLR3_HASH_BUCKET bucket; + + /* See if the current entry pointer is valid first of all + */ + if (en->entry != NULL) + { + /* Current entry was a valid point, see if there is another + * one in the chain. + */ + if (en->entry->nextEntry != NULL) + { + /* Next entry in the enumeration is just the next entry + * in the chain. + */ + en->entry = en->entry->nextEntry; + return; + } + } + + /* There were no more entries in the current bucket, if there are + * more buckets then chase them until we find an entry. + */ + en->bucket++; + + while (en->bucket < en->table->modulo) + { + /* There was one more bucket, see if it has any elements in it + */ + bucket = en->table->buckets + en->bucket; + + if (bucket->entries != NULL) + { + /* There was an entry in this bucket, so we can use it + * for the next entry in the enumeration. + */ + en->entry = bucket->entries; + return; + } + + /* There was nothing in the bucket we just examined, move to the + * next one. + */ + en->bucket++; + } + + /* Here we have exhausted all buckets and the enumeration pointer will + * have its bucket count = table->modulo which signifies that we are done. + */ +} + +/** \brief Frees up the memory structures that represent a hash table + * enumeration. + * \param[in] enum Pointer to ANTLR3 enumeration structure returned by antlr3EnumNew() + */ +static void +antlr3EnumFree (pANTLR3_HASH_ENUM en) +{ + /* Nothing to check, we just free it. + */ + ANTLR3_FREE(en); +} + +/** Given an input key of arbitrary length, return a hash value of + * it. This can then be used (with suitable modulo) to index other + * structures. + */ +ANTLR3_API ANTLR3_UINT32 +antlr3Hash(void * key, ANTLR3_UINT32 keylen) +{ + /* Accumulate the hash value of the key + */ + ANTLR3_UINT32 hash; + pANTLR3_UINT8 keyPtr; + ANTLR3_UINT32 i1; + + hash = 0; + keyPtr = (pANTLR3_UINT8) key; + + /* Iterate the key and accumulate the hash + */ + while(keylen > 0) + { + hash = (hash << 4) + (*(keyPtr++)); + + if ((i1=hash&0xf0000000) != 0) + { + hash = hash ^ (i1 >> 24); + hash = hash ^ i1; + } + keylen--; + } + + return hash; +} + +ANTLR3_API pANTLR3_LIST +antlr3ListNew (ANTLR3_UINT32 sizeHint) +{ + pANTLR3_LIST list; + + /* Allocate memory + */ + list = (pANTLR3_LIST)ANTLR3_MALLOC((size_t)sizeof(ANTLR3_LIST)); + + if (list == NULL) + { + return (pANTLR3_LIST)ANTLR3_FUNC_PTR(ANTLR3_ERR_NOMEM); + } + + /* Now we need to add a new table + */ + list->table = antlr3HashTableNew(sizeHint); + + if (list->table == (pANTLR3_HASH_TABLE)ANTLR3_FUNC_PTR(ANTLR3_ERR_NOMEM)) + { + return (pANTLR3_LIST)ANTLR3_FUNC_PTR(ANTLR3_ERR_NOMEM); + } + + /* Allocation was good, install interface + */ + list->free = antlr3ListFree; + list->del = antlr3ListDelete; + list->get = antlr3ListGet; + list->add = antlr3ListAdd; + list->remove = antlr3ListRemove; + list->put = antlr3ListPut; + list->size = antlr3ListSize; + + return list; +} + +static ANTLR3_UINT32 antlr3ListSize (pANTLR3_LIST list) +{ + return list->table->size(list->table); +} + +static void +antlr3ListFree (pANTLR3_LIST list) +{ + /* Free the hashtable that stores the list + */ + list->table->free(list->table); + + /* Free the allocation for the list itself + */ + ANTLR3_FREE(list); +} + +static void +antlr3ListDelete (pANTLR3_LIST list, ANTLR3_INTKEY key) +{ + list->table->delI(list->table, key); +} + +static void * +antlr3ListGet (pANTLR3_LIST list, ANTLR3_INTKEY key) +{ + return list->table->getI(list->table, key); +} + +/** Add the supplied element to the list, at the next available key + */ +static ANTLR3_INT32 antlr3ListAdd (pANTLR3_LIST list, void * element, void (ANTLR3_CDECL *freeptr)(void *)) +{ + ANTLR3_INTKEY key; + + key = list->table->size(list->table) + 1; + return list->put(list, key, element, freeptr); +} + +/** Remove from the list, but don't free the element, just send it back to the + * caller. + */ +static void * +antlr3ListRemove (pANTLR3_LIST list, ANTLR3_INTKEY key) +{ + pANTLR3_HASH_ENTRY entry; + + entry = list->table->removeI(list->table, key); + + if (entry != NULL) + { + return entry->data; + } + else + { + return NULL; + } +} + +static ANTLR3_INT32 +antlr3ListPut (pANTLR3_LIST list, ANTLR3_INTKEY key, void * element, void (ANTLR3_CDECL *freeptr)(void *)) +{ + return list->table->putI(list->table, key, element, freeptr); +} + +ANTLR3_API pANTLR3_STACK +antlr3StackNew (ANTLR3_UINT32 sizeHint) +{ + pANTLR3_STACK stack; + + /* Allocate memory + */ + stack = (pANTLR3_STACK)ANTLR3_MALLOC((size_t)sizeof(ANTLR3_STACK)); + + if (stack == NULL) + { + return (pANTLR3_STACK)ANTLR3_FUNC_PTR(ANTLR3_ERR_NOMEM); + } + + /* Now we need to add a new table + */ + stack->vector = antlr3VectorNew(sizeHint); + stack->top = NULL; + + if (stack->vector == (pANTLR3_VECTOR)ANTLR3_FUNC_PTR(ANTLR3_ERR_NOMEM)) + { + return (pANTLR3_STACK)ANTLR3_FUNC_PTR(ANTLR3_ERR_NOMEM); + } + + /* Looks good, now add the interface + */ + stack->get = antlr3StackGet; + stack->free = antlr3StackFree; + stack->pop = antlr3StackPop; + stack->push = antlr3StackPush; + stack->size = antlr3StackSize; + stack->peek = antlr3StackPeek; + + return stack; +} + +static ANTLR3_UINT32 antlr3StackSize (pANTLR3_STACK stack) +{ + return stack->vector->count; +} + + +static void +antlr3StackFree (pANTLR3_STACK stack) +{ + /* Free the list that supports the stack + */ + stack->vector->free(stack->vector); + stack->vector = NULL; + stack->top = NULL; + + ANTLR3_FREE(stack); +} + +static void * +antlr3StackPop (pANTLR3_STACK stack) +{ + // Delete the element that is currently at the top of the stack + // + stack->vector->del(stack->vector, stack->vector->count - 1); + + // And get the element that is the now the top of the stack (if anything) + // NOTE! This is not quite like a 'real' stack, which would normally return you + // the current top of the stack, then remove it from the stack. + // TODO: Review this, it is correct for follow sets which is what this was done for + // but is not as obvious when using it as a 'real'stack. + // + stack->top = stack->vector->get(stack->vector, stack->vector->count - 1); + return stack->top; +} + +static void * +antlr3StackGet (pANTLR3_STACK stack, ANTLR3_INTKEY key) +{ + return stack->vector->get(stack->vector, (ANTLR3_UINT32)key); +} + +static void * +antlr3StackPeek (pANTLR3_STACK stack) +{ + return stack->top; +} + +static ANTLR3_BOOLEAN +antlr3StackPush (pANTLR3_STACK stack, void * element, void (ANTLR3_CDECL *freeptr)(void *)) +{ + stack->top = element; + return (ANTLR3_BOOLEAN)(stack->vector->add(stack->vector, element, freeptr)); +} + +ANTLR3_API pANTLR3_VECTOR +antlr3VectorNew (ANTLR3_UINT32 sizeHint) +{ + pANTLR3_VECTOR vector; + + + // Allocate memory for the vector structure itself + // + vector = (pANTLR3_VECTOR) ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_VECTOR))); + + if (vector == NULL) + { + return (pANTLR3_VECTOR)ANTLR3_FUNC_PTR(ANTLR3_ERR_NOMEM); + } + + // Now fill in the defaults + // + antlr3SetVectorApi(vector, sizeHint); + + // And everything is hunky dory + // + return vector; +} + +ANTLR3_API void +antlr3SetVectorApi (pANTLR3_VECTOR vector, ANTLR3_UINT32 sizeHint) +{ + ANTLR3_UINT32 initialSize; + + // Allow vectors to be guessed by ourselves, so input size can be zero + // + if (sizeHint > ANTLR3_VECTOR_INTERNAL_SIZE) + { + initialSize = sizeHint; + } + else + { + initialSize = ANTLR3_VECTOR_INTERNAL_SIZE; + } + + if (sizeHint > ANTLR3_VECTOR_INTERNAL_SIZE) + { + vector->elements = (pANTLR3_VECTOR_ELEMENT)ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_VECTOR_ELEMENT) * initialSize)); + } + else + { + vector->elements = vector->internal; + } + + if (vector->elements == NULL) + { + ANTLR3_FREE(vector); + return; + } + + // Memory allocated successfully + // + vector->count = 0; // No entries yet of course + vector->elementsSize = initialSize; // Available entries + + // Now we can install the API + // + vector->add = antlr3VectorAdd; + vector->del = antlr3VectorDel; + vector->get = antlr3VectorGet; + vector->free = antlr3VectorFree; + vector->set = antlr3VectorSet; + vector->remove = antrl3VectorRemove; + vector->clear = antlr3VectorClear; + vector->size = antlr3VectorSize; + + // Assume that this is not a factory made vector + // + vector->factoryMade = ANTLR3_FALSE; +} +// Clear the entries in a vector. +// Clearing the vector leaves its capacity the same but +// it walks the entries first to see if any of them +// have a free routine that must be called. +// +static void +antlr3VectorClear (pANTLR3_VECTOR vector) +{ + ANTLR3_UINT32 entry; + + // We must traverse every entry in the vector and if it has + // a pointer to a free function then we call it with the + // the entry pointer + // + for (entry = 0; entry < vector->count; entry++) + { + if (vector->elements[entry].freeptr != NULL) + { + vector->elements[entry].freeptr(vector->elements[entry].element); + } + vector->elements[entry].freeptr = NULL; + vector->elements[entry].element = NULL; + } + + // Having called any free pointers, we just reset the entry count + // back to zero. + // + vector->count = 0; +} + +static +void ANTLR3_CDECL antlr3VectorFree (pANTLR3_VECTOR vector) +{ + ANTLR3_UINT32 entry; + + // We must traverse every entry in the vector and if it has + // a pointer to a free function then we call it with the + // the entry pointer + // + for (entry = 0; entry < vector->count; entry++) + { + if (vector->elements[entry].freeptr != NULL) + { + vector->elements[entry].freeptr(vector->elements[entry].element); + } + vector->elements[entry].freeptr = NULL; + vector->elements[entry].element = NULL; + } + + if (vector->factoryMade == ANTLR3_FALSE) + { + // The entries are freed, so free the element allocation + // + if (vector->elementsSize > ANTLR3_VECTOR_INTERNAL_SIZE) + { + ANTLR3_FREE(vector->elements); + } + vector->elements = NULL; + + // Finally, free the allocation for the vector itself + // + ANTLR3_FREE(vector); + } +} + +static void antlr3VectorDel (pANTLR3_VECTOR vector, ANTLR3_UINT32 entry) +{ + // Check this is a valid request first + // + if (entry >= vector->count) + { + return; + } + + // Valid request, check for free pointer and call it if present + // + if (vector->elements[entry].freeptr != NULL) + { + vector->elements[entry].freeptr(vector->elements[entry].element); + vector->elements[entry].freeptr = NULL; + } + + if (entry == vector->count - 1) + { + // Ensure the pointer is never reused by accident, but otherwise just + // decrement the pointer. + // + vector->elements[entry].element = NULL; + } + else + { + // Need to shuffle trailing pointers back over the deleted entry + // + ANTLR3_MEMMOVE(vector->elements + entry, vector->elements + entry + 1, sizeof(ANTLR3_VECTOR_ELEMENT) * (vector->count - entry - 1)); + } + + // One less entry in the vector now + // + vector->count--; +} + +static void * antlr3VectorGet (pANTLR3_VECTOR vector, ANTLR3_UINT32 entry) +{ + // Ensure this is a valid request + // + if (entry < vector->count) + { + return vector->elements[entry].element; + } + else + { + // I know nothing, Mr. Fawlty! + // + return NULL; + } +} + +/// Remove the entry from the vector, but do not free any entry, even if it has +/// a free pointer. +/// +static void * antrl3VectorRemove (pANTLR3_VECTOR vector, ANTLR3_UINT32 entry) +{ + void * element; + + // Check this is a valid request first + // + if (entry >= vector->count) + { + return NULL; + } + + // Valid request, return the sorted pointer + // + + element = vector->elements[entry].element; + + if (entry == vector->count - 1) + { + // Ensure the pointer is never reused by accident, but otherwise just + // decrement the pointer. + /// + vector->elements[entry].element = NULL; + vector->elements[entry].freeptr = NULL; + } + else + { + // Need to shuffle trailing pointers back over the deleted entry + // + ANTLR3_MEMMOVE(vector->elements + entry, vector->elements + entry + 1, sizeof(ANTLR3_VECTOR_ELEMENT) * (vector->count - entry - 1)); + } + + // One less entry in the vector now + // + vector->count--; + + return element; +} + +static void +antlr3VectorResize (pANTLR3_VECTOR vector, ANTLR3_UINT32 hint) +{ + ANTLR3_UINT32 newSize; + + // Need to resize the element pointers. We double the allocation + // we already have unless asked for a specific increase. + // + if (hint == 0 || hint < vector->elementsSize) + { + newSize = vector->elementsSize * 2; + } + else + { + newSize = hint * 2; + } + + // Now we know how many we need, so we see if we have just expanded + // past the built in vector elements or were already past that + // + if (vector->elementsSize > ANTLR3_VECTOR_INTERNAL_SIZE) + { + // We were already larger than the internal size, so we just + // use realloc so that the pointers are copied for us + // + vector->elements = (pANTLR3_VECTOR_ELEMENT)ANTLR3_REALLOC(vector->elements, (sizeof(ANTLR3_VECTOR_ELEMENT)* newSize)); + } + else + { + // The current size was less than or equal to the internal array size and as we always start + // with a size that is at least the maximum internal size, then we must need to allocate new memory + // for external pointers. We don't want to take the time to calculate if a requested element + // is part of the internal or external entries, so we copy the internal ones to the new space + // + vector->elements = (pANTLR3_VECTOR_ELEMENT)ANTLR3_MALLOC((sizeof(ANTLR3_VECTOR_ELEMENT)* newSize)); + ANTLR3_MEMCPY(vector->elements, vector->internal, ANTLR3_VECTOR_INTERNAL_SIZE * sizeof(ANTLR3_VECTOR_ELEMENT)); + } + + vector->elementsSize = newSize; +} + +/// Add the supplied pointer and freeing function pointer to the list, +/// expanding the vector if needed. +/// +static ANTLR3_UINT32 antlr3VectorAdd (pANTLR3_VECTOR vector, void * element, void (ANTLR3_CDECL *freeptr)(void *)) +{ + // Do we need to resize the vector table? + // + if (vector->count == vector->elementsSize) + { + antlr3VectorResize(vector, 0); // Give no hint, we let it add 1024 or double it + } + + // Insert the new entry + // + vector->elements[vector->count].element = element; + vector->elements[vector->count].freeptr = freeptr; + + vector->count++; // One more element counted + + return (ANTLR3_UINT32)(vector->count); + +} + +/// Replace the element at the specified entry point with the supplied +/// entry. +/// +static ANTLR3_UINT32 +antlr3VectorSet (pANTLR3_VECTOR vector, ANTLR3_UINT32 entry, void * element, void (ANTLR3_CDECL *freeptr)(void *), ANTLR3_BOOLEAN freeExisting) +{ + + // If the vector is currently not big enough, then we expand it + // + if (entry >= vector->elementsSize) + { + antlr3VectorResize(vector, entry); // We will get at least this many + } + + // Valid request, replace the current one, freeing any prior entry if told to + // + if ( entry < vector->count // If actually replacing an element + && freeExisting // And told to free any existing element + && vector->elements[entry].freeptr != NULL // And the existing element has a free pointer + ) + { + vector->elements[entry].freeptr(vector->elements[entry].element); + } + + // Install the new pointers + // + vector->elements[entry].freeptr = freeptr; + vector->elements[entry].element = element; + + if (entry >= vector->count) + { + vector->count = entry + 1; + } + return (ANTLR3_UINT32)(entry); // Indicates the replacement was successful + +} + +static ANTLR3_UINT32 antlr3VectorSize (pANTLR3_VECTOR vector) +{ + return vector->count; +} + +/// Vector factory creation +/// +ANTLR3_API pANTLR3_VECTOR_FACTORY +antlr3VectorFactoryNew (ANTLR3_UINT32 sizeHint) +{ + pANTLR3_VECTOR_FACTORY factory; + + // Allocate memory for the factory + // + factory = (pANTLR3_VECTOR_FACTORY)ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_VECTOR_FACTORY))); + + if (factory == NULL) + { + return NULL; + } + + // Factory memory is good, so create a new vector pool + // + factory->pools = NULL; + factory->thisPool = -1; + + newPool(factory); + + // Initialize the API, ignore the hint as this algorithm does + // a better job really. + // + antlr3SetVectorApi(&(factory->unTruc), ANTLR3_VECTOR_INTERNAL_SIZE); + + factory->unTruc.factoryMade = ANTLR3_TRUE; + + // Install the factory API + // + factory->close = closeVectorFactory; + factory->newVector = newVector; + + return factory; +} + +static void +newPool(pANTLR3_VECTOR_FACTORY factory) +{ + /* Increment factory count + */ + factory->thisPool++; + + /* Ensure we have enough pointers allocated + */ + factory->pools = (pANTLR3_VECTOR *) + ANTLR3_REALLOC( (void *)factory->pools, /* Current pools pointer (starts at NULL) */ + (ANTLR3_UINT32)((factory->thisPool + 1) * sizeof(pANTLR3_VECTOR *)) /* Memory for new pool pointers */ + ); + + /* Allocate a new pool for the factory + */ + factory->pools[factory->thisPool] = + (pANTLR3_VECTOR) + ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_VECTOR) * ANTLR3_FACTORY_VPOOL_SIZE)); + + + /* Reset the counters + */ + factory->nextVector = 0; + + /* Done + */ + return; +} + +static void +closeVectorFactory (pANTLR3_VECTOR_FACTORY factory) +{ + pANTLR3_VECTOR pool; + ANTLR3_INT32 poolCount; + ANTLR3_UINT32 limit; + ANTLR3_UINT32 vector; + pANTLR3_VECTOR check; + + /* We iterate the vector pools one at a time + */ + for (poolCount = 0; poolCount <= factory->thisPool; poolCount++) + { + /* Pointer to current pool + */ + pool = factory->pools[poolCount]; + + /* Work out how many tokens we need to check in this pool. + */ + limit = (poolCount == factory->thisPool ? factory->nextVector : ANTLR3_FACTORY_VPOOL_SIZE); + + /* Marginal condition, we might be at the start of a brand new pool + * where the nextToken is 0 and nothing has been allocated. + */ + if (limit > 0) + { + /* We have some vectors allocated from this pool + */ + for (vector = 0; vector < limit; vector++) + { + /* Next one in the chain + */ + check = pool + vector; + + // Call the free function on each of the vectors in the pool, + // which in turn will cause any elements it holds that also have a free + // pointer to be freed. However, because any vector may be in any other + // vector, we don't free the element allocations yet. We do that in a + // a specific pass, coming up next. The vector free function knows that + // this is a factory allocated pool vector and so it won't free things it + // should not. + // + check->free(check); + } + } + } + + /* We iterate the vector pools one at a time once again, but this time + * we are going to free up any allocated element pointers. Note that we are doing this + * so that we do not try to release vectors twice. When building ASTs we just copy + * the vectors all over the place and they may be embedded in this vector pool + * numerous times. + */ + for (poolCount = 0; poolCount <= factory->thisPool; poolCount++) + { + /* Pointer to current pool + */ + pool = factory->pools[poolCount]; + + /* Work out how many tokens we need to check in this pool. + */ + limit = (poolCount == factory->thisPool ? factory->nextVector : ANTLR3_FACTORY_VPOOL_SIZE); + + /* Marginal condition, we might be at the start of a brand new pool + * where the nextToken is 0 and nothing has been allocated. + */ + if (limit > 0) + { + /* We have some vectors allocated from this pool + */ + for (vector = 0; vector < limit; vector++) + { + /* Next one in the chain + */ + check = pool + vector; + + // Anything in here should be factory made, but we do this just + // to triple check. We just free up the elements if they were + // allocated beyond the internal size. + // + if (check->factoryMade == ANTLR3_TRUE && check->elementsSize > ANTLR3_VECTOR_INTERNAL_SIZE) + { + ANTLR3_FREE(check->elements); + check->elements = NULL; + } + } + } + + // We can now free this pool allocation as we have called free on every element in every vector + // and freed any memory for pointers the grew beyond the internal size limit. + // + ANTLR3_FREE(factory->pools[poolCount]); + factory->pools[poolCount] = NULL; + } + + /* All the pools are deallocated we can free the pointers to the pools + * now. + */ + ANTLR3_FREE(factory->pools); + + /* Finally, we can free the space for the factory itself + */ + ANTLR3_FREE(factory); + +} + +static pANTLR3_VECTOR +newVector(pANTLR3_VECTOR_FACTORY factory) +{ + pANTLR3_VECTOR vector; + + // See if we need a new vector pool before allocating a new + // one + // + if (factory->nextVector >= ANTLR3_FACTORY_VPOOL_SIZE) + { + // We ran out of vectors in the current pool, so we need a new pool + // + newPool(factory); + } + + // Assuming everything went well (we are trying for performance here so doing minimal + // error checking. Then we can work out what the pointer is to the next vector. + // + vector = factory->pools[factory->thisPool] + factory->nextVector; + factory->nextVector++; + + // We have our token pointer now, so we can initialize it to the predefined model. + // + antlr3SetVectorApi(vector, ANTLR3_VECTOR_INTERNAL_SIZE); + vector->factoryMade = ANTLR3_TRUE; + + // We know that the pool vectors are created at the default size, which means they + // will start off using their internal entry pointers. We must intialize our pool vector + // to point to its own internal entry table and not the pre-made one. + // + vector->elements = vector->internal; + + // And we are done + // + return vector; +} + +/** Array of left most significant bit positions for an 8 bit + * element provides an efficient way to find the highest bit + * that is set in an n byte value (n>0). Assuming the values will all hit the data cache, + * coding without conditional elements should allow branch + * prediction to work well and of course a parallel instruction cache + * will whip through this. Otherwise we must loop shifting a one + * bit and masking. The values we tend to be placing in out integer + * patricia trie are usually a lot lower than the 64 bits we + * allow for the key allows. Hence there is a lot of redundant looping and + * shifting in a while loop. Whereas, the lookup table is just + * a few ands and indirect lookups, while testing for 0. This + * is likely to be done in parallel on many processors available + * when I wrote this. If this code survives as long as yacc, then + * I may already be dead by the time you read this and maybe there is + * a single machine instruction to perform the operation. What + * else are you going to do with all those transistors? Jim 2007 + * + * The table is probably obvious but it is just the number 0..7 + * of the MSB in each integer value 0..256 + */ +static ANTLR3_UINT8 bitIndex[256] = +{ + 0, // 0 - Just for padding + 0, // 1 + 1, 1, // 2..3 + 2, 2, 2, 2, // 4..7 + 3, 3, 3, 3, 3, 3, 3, 3, // 8+ + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 16+ + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // 32+ + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, // 64+ + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 128+ + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 +}; + +/** Rather than use the bit index of a trie node to shift + * 0x01 left that many times, then & with the result, it is + * faster to use the bit index as an index into this table + * which holds precomputed masks for any of the 64 bits + * we need to mask off singly. The data values will stay in + * cache while ever a trie is in heavy use, such as in + * memoization. It is also pretty enough to be ASCII art. + */ +static ANTLR3_UINT64 bitMask[64] = +{ + 0x0000000000000001ULL, 0x0000000000000002ULL, 0x0000000000000004ULL, 0x0000000000000008ULL, + 0x0000000000000010ULL, 0x0000000000000020ULL, 0x0000000000000040ULL, 0x0000000000000080ULL, + 0x0000000000000100ULL, 0x0000000000000200ULL, 0x0000000000000400ULL, 0x0000000000000800ULL, + 0x0000000000001000ULL, 0x0000000000002000ULL, 0x0000000000004000ULL, 0x0000000000008000ULL, + 0x0000000000010000ULL, 0x0000000000020000ULL, 0x0000000000040000ULL, 0x0000000000080000ULL, + 0x0000000000100000ULL, 0x0000000000200000ULL, 0x0000000000400000ULL, 0x0000000000800000ULL, + 0x0000000001000000ULL, 0x0000000002000000ULL, 0x0000000004000000ULL, 0x0000000008000000ULL, + 0x0000000010000000ULL, 0x0000000020000000ULL, 0x0000000040000000ULL, 0x0000000080000000ULL, + 0x0000000100000000ULL, 0x0000000200000000ULL, 0x0000000400000000ULL, 0x0000000800000000ULL, + 0x0000001000000000ULL, 0x0000002000000000ULL, 0x0000004000000000ULL, 0x0000008000000000ULL, + 0x0000010000000000ULL, 0x0000020000000000ULL, 0x0000040000000000ULL, 0x0000080000000000ULL, + 0x0000100000000000ULL, 0x0000200000000000ULL, 0x0000400000000000ULL, 0x0000800000000000ULL, + 0x0001000000000000ULL, 0x0002000000000000ULL, 0x0004000000000000ULL, 0x0008000000000000ULL, + 0x0010000000000000ULL, 0x0020000000000000ULL, 0x0040000000000000ULL, 0x0080000000000000ULL, + 0x0100000000000000ULL, 0x0200000000000000ULL, 0x0400000000000000ULL, 0x0800000000000000ULL, + 0x1000000000000000ULL, 0x2000000000000000ULL, 0x4000000000000000ULL, 0x8000000000000000ULL +}; + +/* INT TRIE Implementation of depth 64 bits, being the number of bits + * in a 64 bit integer. + */ + +pANTLR3_INT_TRIE +antlr3IntTrieNew(ANTLR3_UINT32 depth) +{ + pANTLR3_INT_TRIE trie; + + trie = (pANTLR3_INT_TRIE) ANTLR3_CALLOC(1, sizeof(ANTLR3_INT_TRIE)); /* Base memory required */ + + if (trie == NULL) + { + return (pANTLR3_INT_TRIE) ANTLR3_FUNC_PTR(ANTLR3_ERR_NOMEM); + } + + /* Now we need to allocate the root node. This makes it easier + * to use the tree as we don't have to do anything special + * for the root node. + */ + trie->root = (pANTLR3_INT_TRIE_NODE) ANTLR3_CALLOC(1, sizeof(ANTLR3_INT_TRIE)); + + if (trie->root == NULL) + { + ANTLR3_FREE(trie); + return (pANTLR3_INT_TRIE) ANTLR3_FUNC_PTR(ANTLR3_ERR_NOMEM); + } + + trie->add = intTrieAdd; + trie->del = intTrieDel; + trie->free = intTrieFree; + trie->get = intTrieGet; + + /* Now we seed the root node with the index being the + * highest left most bit we want to test, which limits the + * keys in the trie. This is the trie 'depth'. The limit for + * this implementation is 63 (bits 0..63). + */ + trie->root->bitNum = depth; + + /* And as we have nothing in here yet, we set both child pointers + * of the root node to point back to itself. + */ + trie->root->leftN = trie->root; + trie->root->rightN = trie->root; + trie->count = 0; + + /* Finally, note that the key for this root node is 0 because + * we use calloc() to initialise it. + */ + + return trie; +} + +/** Search the int Trie and return a pointer to the first bucket indexed + * by the key if it is contained in the trie, otherwise NULL. + */ +static pANTLR3_TRIE_ENTRY +intTrieGet (pANTLR3_INT_TRIE trie, ANTLR3_INTKEY key) +{ + pANTLR3_INT_TRIE_NODE thisNode; + pANTLR3_INT_TRIE_NODE nextNode; + + if (trie->count == 0) + { + return NULL; /* Nothing in this trie yet */ + } + /* Starting at the root node in the trie, compare the bit index + * of the current node with its next child node (starts left from root). + * When the bit index of the child node is greater than the bit index of the current node + * then by definition (as the bit index decreases as we descent the trie) + * we have reached a 'backward' pointer. A backward pointer means we + * have reached the only node that can be reached by the bits given us so far + * and it must either be the key we are looking for, or if not then it + * means the entry was not in the trie, and we return NULL. A backward pointer + * points back in to the tree structure rather than down (deeper) within the + * tree branches. + */ + thisNode = trie->root; /* Start at the root node */ + nextNode = thisNode->leftN; /* Examine the left node from the root */ + + /* While we are descending the tree nodes... + */ + while (thisNode->bitNum > nextNode->bitNum) + { + /* Next node now becomes the new 'current' node + */ + thisNode = nextNode; + + /* We now test the bit indicated by the bitmap in the next node + * in the key we are searching for. The new next node is the + * right node if that bit is set and the left node it is not. + */ + if (key & bitMask[nextNode->bitNum]) + { + nextNode = nextNode->rightN; /* 1 is right */ + } + else + { + nextNode = nextNode->leftN; /* 0 is left */ + } + } + + /* Here we have reached a node where the bitMap index is lower than + * its parent. This means it is pointing backward in the tree and + * must therefore be a terminal node, being the only point than can + * be reached with the bits seen so far. It is either the actual key + * we wanted, or if that key is not in the trie it is another key + * that is currently the only one that can be reached by those bits. + * That situation would obviously change if the key was to be added + * to the trie. + * + * Hence it only remains to test whether this is actually the key or not. + */ + if (nextNode->key == key) + { + /* This was the key, so return the entry pointer + */ + return nextNode->buckets; + } + else + { + return NULL; /* That key is not in the trie (note that we set the pointer to -1 if no payload) */ + } +} + + +static ANTLR3_BOOLEAN +intTrieDel (pANTLR3_INT_TRIE trie, ANTLR3_INTKEY key) +{ + pANTLR3_INT_TRIE_NODE p; + + p=trie->root; + key = key; + + return ANTLR3_FALSE; +} + +/** Add an entry into the INT trie. + * Basically we descend the trie as we do when searching it, which will + * locate the only node in the trie that can be reached by the bit pattern of the + * key. If the key is actually at that node, then if the trie accepts duplicates + * we add the supplied data in a new chained bucket to that data node. If it does + * not accept duplicates then we merely return FALSE in case the caller wants to know + * whether the key was already in the trie. + * If the node we locate is not the key we are looking to add, then we insert a new node + * into the trie with a bit index of the leftmost differing bit and the left or right + * node pointing to itself or the data node we are inserting 'before'. + */ +static ANTLR3_BOOLEAN +intTrieAdd (pANTLR3_INT_TRIE trie, ANTLR3_INTKEY key, ANTLR3_UINT32 type, ANTLR3_INTKEY intVal, void * data, void (ANTLR3_CDECL *freeptr)(void *)) +{ + pANTLR3_INT_TRIE_NODE thisNode; + pANTLR3_INT_TRIE_NODE nextNode; + pANTLR3_INT_TRIE_NODE entNode; + ANTLR3_UINT32 depth; + pANTLR3_TRIE_ENTRY newEnt; + pANTLR3_TRIE_ENTRY nextEnt; + ANTLR3_INTKEY xorKey; + + /* Cache the bit depth of this trie, which is always the highest index, + * which is in the root node + */ + depth = trie->root->bitNum; + + thisNode = trie->root; /* Start with the root node */ + nextNode = trie->root->leftN; /* And assume we start to the left */ + + /* Now find the only node that can be currently reached by the bits in the + * key we are being asked to insert. + */ + while (thisNode->bitNum > nextNode->bitNum) + { + /* Still descending the structure, next node becomes current. + */ + thisNode = nextNode; + + if (key & bitMask[nextNode->bitNum]) + { + /* Bit at the required index was 1, so travers the right node from here + */ + nextNode = nextNode->rightN; + } + else + { + /* Bit at the required index was 0, so we traverse to the left + */ + nextNode = nextNode->leftN; + } + } + /* Here we have located the only node that can be reached by the + * bits in the requested key. It could in fact be that key or the node + * we need to use to insert the new key. + */ + if (nextNode->key == key) + { + /* We have located an exact match, but we will only append to the bucket chain + * if this trie accepts duplicate keys. + */ + if (trie->allowDups ==ANTLR3_TRUE) + { + /* Yes, we are accepting duplicates + */ + newEnt = (pANTLR3_TRIE_ENTRY)ANTLR3_CALLOC(1, sizeof(ANTLR3_TRIE_ENTRY)); + + if (newEnt == NULL) + { + /* Out of memory, all we can do is return the fact that the insert failed. + */ + return ANTLR3_FALSE; + } + + /* Otherwise insert this in the chain + */ + newEnt->type = type; + newEnt->freeptr = freeptr; + if (type == ANTLR3_HASH_TYPE_STR) + { + newEnt->data.ptr = data; + } + else + { + newEnt->data.intVal = intVal; + } + + /* We want to be able to traverse the stored elements in the order that they were + * added as duplicate keys. We might need to revise this opinion if we end up having many duplicate keys + * as perhaps reverse order is just as good, so long as it is ordered. + */ + nextEnt = nextNode->buckets; + while (nextEnt->next != NULL) + { + nextEnt = nextEnt->next; + } + nextEnt->next = newEnt; + + trie->count++; + return ANTLR3_TRUE; + } + else + { + /* We found the key is already there and we are not allowed duplicates in this + * trie. + */ + return ANTLR3_FALSE; + } + } + + /* Here we have discovered the only node that can be reached by the bits in the key + * but we have found that this node is not the key we need to insert. We must find the + * the leftmost bit by which the current key for that node and the new key we are going + * to insert, differ. While this nested series of ifs may look a bit strange, experimentation + * showed that it allows a machine code path that works well with predicated execution + */ + xorKey = (key ^ nextNode->key); /* Gives 1 bits only where they differ then we find the left most 1 bit*/ + + /* Most common case is a 32 bit key really + */ +#ifdef ANTLR3_USE_64BIT + if (xorKey & 0xFFFFFFFF00000000) + { + if (xorKey & 0xFFFF000000000000) + { + if (xorKey & 0xFF00000000000000) + { + depth = 56 + bitIndex[((xorKey & 0xFF00000000000000)>>56)]; + } + else + { + depth = 48 + bitIndex[((xorKey & 0x00FF000000000000)>>48)]; + } + } + else + { + if (xorKey & 0x0000FF0000000000) + { + depth = 40 + bitIndex[((xorKey & 0x0000FF0000000000)>>40)]; + } + else + { + depth = 32 + bitIndex[((xorKey & 0x000000FF00000000)>>32)]; + } + } + } + else +#endif + { + if (xorKey & 0x00000000FFFF0000) + { + if (xorKey & 0x00000000FF000000) + { + depth = 24 + bitIndex[((xorKey & 0x00000000FF000000)>>24)]; + } + else + { + depth = 16 + bitIndex[((xorKey & 0x0000000000FF0000)>>16)]; + } + } + else + { + if (xorKey & 0x000000000000FF00) + { + depth = 8 + bitIndex[((xorKey & 0x0000000000000FF00)>>8)]; + } + else + { + depth = bitIndex[xorKey & 0x00000000000000FF]; + } + } + } + + /* We have located the leftmost differing bit, indicated by the depth variable. So, we know what + * bit index we are to insert the new entry at. There are two cases, being where the two keys + * differ at a bit position that is not currently part of the bit testing, where they differ on a bit + * that is currently being skipped in the indexed comparisons, and where they differ on a bit + * that is merely lower down in the current bit search. If the bit index went bit 4, bit 2 and they differ + * at bit 3, then we have the "skipped" bit case. But if that chain was Bit 4, Bit 2 and they differ at bit 1 + * then we have the easy bit <pun>. + * + * So, set up to descend the tree again, but this time looking for the insert point + * according to whether we skip the bit that differs or not. + */ + thisNode = trie->root; + entNode = trie->root->leftN; + + /* Note the slight difference in the checks here to cover both cases + */ + while (thisNode->bitNum > entNode->bitNum && entNode->bitNum > depth) + { + /* Still descending the structure, next node becomes current. + */ + thisNode = entNode; + + if (key & bitMask[entNode->bitNum]) + { + /* Bit at the required index was 1, so traverse the right node from here + */ + entNode = entNode->rightN; + } + else + { + /* Bit at the required index was 0, so we traverse to the left + */ + entNode = entNode->leftN; + } + } + + /* We have located the correct insert point for this new key, so we need + * to allocate our entry and insert it etc. + */ + nextNode = (pANTLR3_INT_TRIE_NODE)ANTLR3_CALLOC(1, sizeof(ANTLR3_INT_TRIE_NODE)); + if (nextNode == NULL) + { + /* All that work and no memory - bummer. + */ + return ANTLR3_FALSE; + } + + /* Build a new entry block for the new node + */ + newEnt = (pANTLR3_TRIE_ENTRY)ANTLR3_CALLOC(1, sizeof(ANTLR3_TRIE_ENTRY)); + + if (newEnt == NULL) + { + /* Out of memory, all we can do is return the fact that the insert failed. + */ + return ANTLR3_FALSE; + } + + /* Otherwise enter this in our new node + */ + newEnt->type = type; + newEnt->freeptr = freeptr; + if (type == ANTLR3_HASH_TYPE_STR) + { + newEnt->data.ptr = data; + } + else + { + newEnt->data.intVal = intVal; + } + /* Install it + */ + nextNode->buckets = newEnt; + nextNode->key = key; + nextNode->bitNum = depth; + + /* Work out the right and left pointers for this new node, which involve + * terminating with the current found node either right or left according + * to whether the current index bit is 1 or 0 + */ + if (key & bitMask[depth]) + { + nextNode->leftN = entNode; /* Terminates at previous position */ + nextNode->rightN = nextNode; /* Terminates with itself */ + } + else + { + nextNode->rightN = entNode; /* Terminates at previous position */ + nextNode->leftN = nextNode; /* Terminates with itself */ + } + + /* Finally, we need to change the pointers at the node we located + * for inserting. If the key bit at its index is set then the right + * pointer for that node becomes the newly created node, otherwise the left + * pointer does. + */ + if (key & bitMask[thisNode->bitNum] ) + { + thisNode->rightN = nextNode; + } + else + { + thisNode->leftN = nextNode; + } + + /* Et voila + */ + trie->count++; + return ANTLR3_TRUE; + +} +/** Release memory allocated to this tree. + * Basic algorithm is that we do a depth first left descent and free + * up any nodes that are not backward pointers. + */ +static void +freeIntNode(pANTLR3_INT_TRIE_NODE node) +{ + pANTLR3_TRIE_ENTRY thisEntry; + pANTLR3_TRIE_ENTRY nextEntry; + + /* If this node has a left pointer that is not a back pointer + * then recursively call to free this + */ + if (node->bitNum > node->leftN->bitNum) + { + /* We have a left node that needs descending, so do it. + */ + freeIntNode(node->leftN); + } + + /* The left nodes from here should now be dealt with, so + * we need to descend any right nodes that are not back pointers + */ + if (node->bitNum > node->rightN->bitNum) + { + /* There are some right nodes to descend and deal with. + */ + freeIntNode(node->rightN); + } + + /* Now all the children are dealt with, we can destroy + * this node too + */ + thisEntry = node->buckets; + + while (thisEntry != NULL) + { + nextEntry = thisEntry->next; + + /* Do we need to call a custom free pointer for this string entry? + */ + if (thisEntry->type == ANTLR3_HASH_TYPE_STR && thisEntry->freeptr != NULL) + { + thisEntry->freeptr(thisEntry->data.ptr); + } + + /* Now free the data for this bucket entry + */ + ANTLR3_FREE(thisEntry); + thisEntry = nextEntry; /* See if there are any more to free */ + } + + /* The bucket entry is now gone, so we can free the memory for + * the entry itself. + */ + ANTLR3_FREE(node); + + /* And that should be it for everything under this node and itself + */ +} + +/** Called to free all nodes and the structure itself. + */ +static void +intTrieFree (pANTLR3_INT_TRIE trie) +{ + /* Descend from the root and free all the nodes + */ + freeIntNode(trie->root); + + /* the nodes are all gone now, so we need only free the memory + * for the structure itself + */ + ANTLR3_FREE(trie); +} diff --git a/antlr-3.1.3/runtime/C/src/antlr3commontoken.c b/antlr-3.1.3/runtime/C/src/antlr3commontoken.c new file mode 100644 index 0000000..b1af07a --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3commontoken.c @@ -0,0 +1,577 @@ +/** + * Contains the default implementation of the common token used within + * java. Custom tokens should create this structure and then append to it using the + * custom pointer to install their own structure and API. + */ + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3.h> + +/* Token API + */ +static pANTLR3_STRING getText (pANTLR3_COMMON_TOKEN token); +static void setText (pANTLR3_COMMON_TOKEN token, pANTLR3_STRING text); +static void setText8 (pANTLR3_COMMON_TOKEN token, pANTLR3_UINT8 text); +static ANTLR3_UINT32 getType (pANTLR3_COMMON_TOKEN token); +static void setType (pANTLR3_COMMON_TOKEN token, ANTLR3_UINT32 type); +static ANTLR3_UINT32 getLine (pANTLR3_COMMON_TOKEN token); +static void setLine (pANTLR3_COMMON_TOKEN token, ANTLR3_UINT32 line); +static ANTLR3_INT32 getCharPositionInLine (pANTLR3_COMMON_TOKEN token); +static void setCharPositionInLine (pANTLR3_COMMON_TOKEN token, ANTLR3_INT32 pos); +static ANTLR3_UINT32 getChannel (pANTLR3_COMMON_TOKEN token); +static void setChannel (pANTLR3_COMMON_TOKEN token, ANTLR3_UINT32 channel); +static ANTLR3_MARKER getTokenIndex (pANTLR3_COMMON_TOKEN token); +static void setTokenIndex (pANTLR3_COMMON_TOKEN token, ANTLR3_MARKER); +static ANTLR3_MARKER getStartIndex (pANTLR3_COMMON_TOKEN token); +static void setStartIndex (pANTLR3_COMMON_TOKEN token, ANTLR3_MARKER index); +static ANTLR3_MARKER getStopIndex (pANTLR3_COMMON_TOKEN token); +static void setStopIndex (pANTLR3_COMMON_TOKEN token, ANTLR3_MARKER index); +static pANTLR3_STRING toString (pANTLR3_COMMON_TOKEN token); + +/* Factory API + */ +static void factoryClose (pANTLR3_TOKEN_FACTORY factory); +static pANTLR3_COMMON_TOKEN newToken (void); +static void setInputStream (pANTLR3_TOKEN_FACTORY factory, pANTLR3_INPUT_STREAM input); + +/* Internal management functions + */ +static void newPool (pANTLR3_TOKEN_FACTORY factory); +static pANTLR3_COMMON_TOKEN newPoolToken (pANTLR3_TOKEN_FACTORY factory); + + + +ANTLR3_API pANTLR3_COMMON_TOKEN +antlr3CommonTokenNew(ANTLR3_UINT32 ttype) +{ + pANTLR3_COMMON_TOKEN token; + + // Create a raw token with the interface installed + // + token = newToken(); + + if (token != NULL) + { + token->setType(token, ttype); + } + + // All good + // + return token; +} + +ANTLR3_API pANTLR3_TOKEN_FACTORY +antlr3TokenFactoryNew(pANTLR3_INPUT_STREAM input) +{ + pANTLR3_TOKEN_FACTORY factory; + + /* allocate memory + */ + factory = (pANTLR3_TOKEN_FACTORY) ANTLR3_MALLOC((size_t)sizeof(ANTLR3_TOKEN_FACTORY)); + + if (factory == NULL) + { + return NULL; + } + + /* Install factory API + */ + factory->newToken = newPoolToken; + factory->close = factoryClose; + factory->setInputStream = setInputStream; + + /* Allocate the initial pool + */ + factory->thisPool = -1; + factory->pools = NULL; + newPool(factory); + + /* Factory space is good, we now want to initialize our cheating token + * which one it is initialized is the model for all tokens we manufacture + */ + antlr3SetTokenAPI(&factory->unTruc); + + /* Set some initial variables for future copying + */ + factory->unTruc.factoryMade = ANTLR3_TRUE; + + // Input stream + // + setInputStream(factory, input); + + return factory; + +} + +static void +setInputStream (pANTLR3_TOKEN_FACTORY factory, pANTLR3_INPUT_STREAM input) +{ + factory->input = input; + factory->unTruc.input = input; + if (input != NULL) + { + factory->unTruc.strFactory = input->strFactory; + } + else + { + factory->unTruc.strFactory = NULL; + } +} + +static void +newPool(pANTLR3_TOKEN_FACTORY factory) +{ + /* Increment factory count + */ + factory->thisPool++; + + /* Ensure we have enough pointers allocated + */ + factory->pools = (pANTLR3_COMMON_TOKEN *) + ANTLR3_REALLOC( (void *)factory->pools, /* Current pools pointer (starts at NULL) */ + (ANTLR3_UINT32)((factory->thisPool + 1) * sizeof(pANTLR3_COMMON_TOKEN *)) /* Memory for new pool pointers */ + ); + + /* Allocate a new pool for the factory + */ + factory->pools[factory->thisPool] = + (pANTLR3_COMMON_TOKEN) + ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_COMMON_TOKEN) * ANTLR3_FACTORY_POOL_SIZE)); + + /* Reset the counters + */ + factory->nextToken = 0; + + /* Done + */ + return; +} + +static pANTLR3_COMMON_TOKEN +newPoolToken(pANTLR3_TOKEN_FACTORY factory) +{ + pANTLR3_COMMON_TOKEN token; + + /* See if we need a new token pool before allocating a new + * one + */ + if (factory->nextToken >= ANTLR3_FACTORY_POOL_SIZE) + { + /* We ran out of tokens in the current pool, so we need a new pool + */ + newPool(factory); + } + + /* Assuming everything went well (we are trying for performance here so doing minimal + * error checking. Then we can work out what the pointer is to the next token. + */ + token = factory->pools[factory->thisPool] + factory->nextToken; + factory->nextToken++; + + /* We have our token pointer now, so we can initialize it to the predefined model. + */ + antlr3SetTokenAPI(token); + + /* It is factory made, and we need to copy the string factory pointer + */ + token->factoryMade = ANTLR3_TRUE; + token->strFactory = factory->input == NULL ? NULL : factory->input->strFactory; + token->input = factory->input; + + /* And we are done + */ + return token; +} + +static void +factoryClose (pANTLR3_TOKEN_FACTORY factory) +{ + pANTLR3_COMMON_TOKEN pool; + ANTLR3_INT32 poolCount; + ANTLR3_UINT32 limit; + ANTLR3_UINT32 token; + pANTLR3_COMMON_TOKEN check; + + /* We iterate the token pools one at a time + */ + for (poolCount = 0; poolCount <= factory->thisPool; poolCount++) + { + /* Pointer to current pool + */ + pool = factory->pools[poolCount]; + + /* Work out how many tokens we need to check in this pool. + */ + limit = (poolCount == factory->thisPool ? factory->nextToken : ANTLR3_FACTORY_POOL_SIZE); + + /* Marginal condition, we might be at the start of a brand new pool + * where the nextToken is 0 and nothing has been allocated. + */ + if (limit > 0) + { + /* We have some tokens allocated from this pool + */ + for (token = 0; token < limit; token++) + { + /* Next one in the chain + */ + check = pool + token; + + /* If the programmer made this a custom token, then + * see if we need to call their free routine. + */ + if (check->custom != NULL && check->freeCustom != NULL) + { + check->freeCustom(check->custom); + check->custom = NULL; + } + } + } + + /* We can now free this pool allocation + */ + ANTLR3_FREE(factory->pools[poolCount]); + factory->pools[poolCount] = NULL; + } + + /* All the pools are deallocated we can free the pointers to the pools + * now. + */ + ANTLR3_FREE(factory->pools); + + /* Finally, we can free the space for the factory itself + */ + ANTLR3_FREE(factory); +} + + +static pANTLR3_COMMON_TOKEN +newToken(void) +{ + pANTLR3_COMMON_TOKEN token; + + /* Allocate memory for this + */ + token = (pANTLR3_COMMON_TOKEN) ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_COMMON_TOKEN))); + + if (token == NULL) + { + return NULL; + } + + // Install the API + // + antlr3SetTokenAPI(token); + token->factoryMade = ANTLR3_FALSE; + + return token; +} + +ANTLR3_API void +antlr3SetTokenAPI(pANTLR3_COMMON_TOKEN token) +{ + token->getText = getText; + token->setText = setText; + token->setText8 = setText8; + token->getType = getType; + token->setType = setType; + token->getLine = getLine; + token->setLine = setLine; + token->setLine = setLine; + token->getCharPositionInLine = getCharPositionInLine; + token->setCharPositionInLine = setCharPositionInLine; + token->getChannel = getChannel; + token->setChannel = setChannel; + token->getTokenIndex = getTokenIndex; + token->setTokenIndex = setTokenIndex; + token->getStartIndex = getStartIndex; + token->setStartIndex = setStartIndex; + token->getStopIndex = getStopIndex; + token->setStopIndex = setStopIndex; + token->toString = toString; + + // Set defaults + // + token->setCharPositionInLine(token, -1); + + token->custom = NULL; + token->freeCustom = NULL; + token->type = ANTLR3_TOKEN_INVALID; + token->textState = ANTLR3_TEXT_NONE; + token->start = 0; + token->stop = 0; + token->channel = ANTLR3_TOKEN_DEFAULT_CHANNEL; + token->line = 0; + token->index = 0; + token->input = NULL; + token->user1 = 0; + token->user2 = 0; + token->user3 = 0; + token->custom = NULL; + + return; +} + +static pANTLR3_STRING getText (pANTLR3_COMMON_TOKEN token) +{ + switch (token->textState) + { + case ANTLR3_TEXT_STRING: + + // Someone already created a string for this token, so we just + // use it. + // + return token->tokText.text; + break; + + case ANTLR3_TEXT_CHARP: + + // We had a straight text pointer installed, now we + // must convert it to a string. Note we have to do this here + // or otherwise setText8() will just install the same char* + // + if (token->strFactory != NULL) + { + token->tokText.text = token->strFactory->newStr8(token->strFactory, (pANTLR3_UINT8)token->tokText.chars); + token->textState = ANTLR3_TEXT_STRING; + return token->tokText.text; + } + else + { + // We cannot do anything here + // + return NULL; + } + break; + + default: + + // EOF is a special case + // + if (token->type == ANTLR3_TOKEN_EOF) + { + token->tokText.text = token->strFactory->newStr8(token->strFactory, (pANTLR3_UINT8)"<EOF>"); + token->textState = ANTLR3_TEXT_STRING; + return token->tokText.text; + } + + + // We had nothing installed in the token, create a new string + // from the input stream + // + + if (token->input != NULL) + { + + return token->input->substr( token->input, + token->getStartIndex(token), + token->getStopIndex(token) + ); + } + + // Nothing to return, there is no input stream + // + return NULL; + break; + } +} +static void setText8 (pANTLR3_COMMON_TOKEN token, pANTLR3_UINT8 text) +{ + // No text to set, so ignore + // + if (text == NULL) return; + + switch (token->textState) + { + case ANTLR3_TEXT_NONE: + case ANTLR3_TEXT_CHARP: // Caller must free before setting again, if it needs to be freed + + // Nothing in there yet, or just a char *, so just set the + // text as a pointer + // + token->textState = ANTLR3_TEXT_CHARP; + token->tokText.chars = (pANTLR3_UCHAR)text; + break; + + default: + + // It was already a pANTLR3_STRING, so just override it + // + token->tokText.text->set8(token->tokText.text, (const char *)text); + break; + } + + // We are done + // + return; +} + +/** \brief Install the supplied text string as teh text for the token. + * The method assumes that the existing text (if any) was created by a factory + * and so does not attempt to release any memory it is using.Text not created + * by a string fctory (not advised) should be released prior to this call. + */ +static void setText (pANTLR3_COMMON_TOKEN token, pANTLR3_STRING text) +{ + // Merely replaces and existing pre-defined text with the supplied + // string + // + token->textState = ANTLR3_TEXT_STRING; + token->tokText.text = text; + + /* We are done + */ + return; +} + +static ANTLR3_UINT32 getType (pANTLR3_COMMON_TOKEN token) +{ + return token->type; +} + +static void setType (pANTLR3_COMMON_TOKEN token, ANTLR3_UINT32 type) +{ + token->type = type; +} + +static ANTLR3_UINT32 getLine (pANTLR3_COMMON_TOKEN token) +{ + return token->line; +} + +static void setLine (pANTLR3_COMMON_TOKEN token, ANTLR3_UINT32 line) +{ + token->line = line; +} + +static ANTLR3_INT32 getCharPositionInLine (pANTLR3_COMMON_TOKEN token) +{ + return token->charPosition; +} + +static void setCharPositionInLine (pANTLR3_COMMON_TOKEN token, ANTLR3_INT32 pos) +{ + token->charPosition = pos; +} + +static ANTLR3_UINT32 getChannel (pANTLR3_COMMON_TOKEN token) +{ + return token->channel; +} + +static void setChannel (pANTLR3_COMMON_TOKEN token, ANTLR3_UINT32 channel) +{ + token->channel = channel; +} + +static ANTLR3_MARKER getTokenIndex (pANTLR3_COMMON_TOKEN token) +{ + return token->index; +} + +static void setTokenIndex (pANTLR3_COMMON_TOKEN token, ANTLR3_MARKER index) +{ + token->index = index; +} + +static ANTLR3_MARKER getStartIndex (pANTLR3_COMMON_TOKEN token) +{ + return token->start == -1 ? (ANTLR3_MARKER)(token->input->data) : token->start; +} + +static void setStartIndex (pANTLR3_COMMON_TOKEN token, ANTLR3_MARKER start) +{ + token->start = start; +} + +static ANTLR3_MARKER getStopIndex (pANTLR3_COMMON_TOKEN token) +{ + return token->stop; +} + +static void setStopIndex (pANTLR3_COMMON_TOKEN token, ANTLR3_MARKER stop) +{ + token->stop = stop; +} + +static pANTLR3_STRING toString (pANTLR3_COMMON_TOKEN token) +{ + pANTLR3_STRING text; + pANTLR3_STRING outtext; + + text = token->getText(token); + + if (text == NULL) + { + return NULL; + } + + if (text->factory == NULL) + { + return text; // This usall ymeans it is the EOF token + } + + /* A new empty string to assemble all the stuff in + */ + outtext = text->factory->newRaw(text->factory); + + /* Now we use our handy dandy string utility to assemble the + * the reporting string + * return "[@"+getTokenIndex()+","+start+":"+stop+"='"+txt+"',<"+type+">"+channelStr+","+line+":"+getCharPositionInLine()+"]"; + */ + outtext->append8(outtext, "[Index: "); + outtext->addi (outtext, (ANTLR3_INT32)token->getTokenIndex(token)); + outtext->append8(outtext, " (Start: "); + outtext->addi (outtext, (ANTLR3_INT32)token->getStartIndex(token)); + outtext->append8(outtext, "-Stop: "); + outtext->addi (outtext, (ANTLR3_INT32)token->getStopIndex(token)); + outtext->append8(outtext, ") ='"); + outtext->appendS(outtext, text); + outtext->append8(outtext, "', type<"); + outtext->addi (outtext, token->type); + outtext->append8(outtext, "> "); + + if (token->getChannel(token) > ANTLR3_TOKEN_DEFAULT_CHANNEL) + { + outtext->append8(outtext, "(channel = "); + outtext->addi (outtext, (ANTLR3_INT32)token->getChannel(token)); + outtext->append8(outtext, ") "); + } + + outtext->append8(outtext, "Line: "); + outtext->addi (outtext, (ANTLR3_INT32)token->getLine(token)); + outtext->append8(outtext, " LinePos:"); + outtext->addi (outtext, token->getCharPositionInLine(token)); + outtext->addc (outtext, ']'); + + return outtext; +} + diff --git a/antlr-3.1.3/runtime/C/src/antlr3commontree.c b/antlr-3.1.3/runtime/C/src/antlr3commontree.c new file mode 100644 index 0000000..ab4aeb6 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3commontree.c @@ -0,0 +1,542 @@ +// \file +// +// Implementation of ANTLR3 CommonTree, which you can use as a +// starting point for your own tree. Though it is often easier just to tag things on +// to the user pointer in the tree unless you are building a different type +// of structure. +// + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3commontree.h> + + +static pANTLR3_COMMON_TOKEN getToken (pANTLR3_BASE_TREE tree); +static pANTLR3_BASE_TREE dupNode (pANTLR3_BASE_TREE tree); +static ANTLR3_BOOLEAN isNilNode (pANTLR3_BASE_TREE tree); +static ANTLR3_UINT32 getType (pANTLR3_BASE_TREE tree); +static pANTLR3_STRING getText (pANTLR3_BASE_TREE tree); +static ANTLR3_UINT32 getLine (pANTLR3_BASE_TREE tree); +static ANTLR3_UINT32 getCharPositionInLine (pANTLR3_BASE_TREE tree); +static pANTLR3_STRING toString (pANTLR3_BASE_TREE tree); +static pANTLR3_BASE_TREE getParent (pANTLR3_BASE_TREE tree); +static void setParent (pANTLR3_BASE_TREE tree, pANTLR3_BASE_TREE parent); +static void setChildIndex (pANTLR3_BASE_TREE tree, ANTLR3_INT32 i); +static ANTLR3_INT32 getChildIndex (pANTLR3_BASE_TREE tree); +static void createChildrenList (pANTLR3_BASE_TREE tree); +static void reuse (pANTLR3_BASE_TREE tree); + +// Factory functions for the Arboretum +// +static void newPool (pANTLR3_ARBORETUM factory); +static pANTLR3_BASE_TREE newPoolTree (pANTLR3_ARBORETUM factory); +static pANTLR3_BASE_TREE newFromTree (pANTLR3_ARBORETUM factory, pANTLR3_COMMON_TREE tree); +static pANTLR3_BASE_TREE newFromToken (pANTLR3_ARBORETUM factory, pANTLR3_COMMON_TOKEN token); +static void factoryClose (pANTLR3_ARBORETUM factory); + +ANTLR3_API pANTLR3_ARBORETUM +antlr3ArboretumNew(pANTLR3_STRING_FACTORY strFactory) +{ + pANTLR3_ARBORETUM factory; + + // Allocate memory + // + factory = (pANTLR3_ARBORETUM) ANTLR3_MALLOC((size_t)sizeof(ANTLR3_ARBORETUM)); + if (factory == NULL) + { + return NULL; + } + + // Install a vector factory to create, track and free() any child + // node lists. + // + factory->vFactory = antlr3VectorFactoryNew(0); + if (factory->vFactory == NULL) + { + free(factory); + return NULL; + } + + // We also keep a reclaim stack, so that any Nil nodes that are + // orphaned are not just left in the pool but are reused, other wise + // we create 6 times as many nilNodes as ordinary nodes and use loads of + // memory. Perhaps at some point, the analysis phase will generate better + // code and we won't need to do this here. + // + factory->nilStack = antlr3StackNew(0); + + // Install factory API + // + factory->newTree = newPoolTree; + factory->newFromTree = newFromTree; + factory->newFromToken = newFromToken; + factory->close = factoryClose; + + // Allocate the initial pool + // + factory->thisPool = -1; + factory->pools = NULL; + newPool(factory); + + // Factory space is good, we now want to initialize our cheating token + // which one it is initialized is the model for all tokens we manufacture + // + antlr3SetCTAPI(&factory->unTruc); + + // Set some initial variables for future copying, including a string factory + // that we can use later for converting trees to strings. + // + factory->unTruc.factory = factory; + factory->unTruc.baseTree.strFactory = strFactory; + + return factory; + +} + +static void +newPool(pANTLR3_ARBORETUM factory) +{ + // Increment factory count + // + factory->thisPool++; + + // Ensure we have enough pointers allocated + // + factory->pools = (pANTLR3_COMMON_TREE *) + ANTLR3_REALLOC( (void *)factory->pools, // Current pools pointer (starts at NULL) + (ANTLR3_UINT32)((factory->thisPool + 1) * sizeof(pANTLR3_COMMON_TREE *)) // Memory for new pool pointers + ); + + // Allocate a new pool for the factory + // + factory->pools[factory->thisPool] = + (pANTLR3_COMMON_TREE) + ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_COMMON_TREE) * ANTLR3_FACTORY_POOL_SIZE)); + + + // Reset the counters + // + factory->nextTree = 0; + + // Done + // + return; +} + +static pANTLR3_BASE_TREE +newPoolTree (pANTLR3_ARBORETUM factory) +{ + pANTLR3_COMMON_TREE tree; + + // If we have anything on the re claim stack, reuse that sucker first + // + tree = factory->nilStack->peek(factory->nilStack); + + if (tree != NULL) + { + // Cool we got something we could reuse, it will have been cleaned up by + // whatever put it back on the stack (for instance if it had a child vector, + // that will have been cleared to hold zero entries and that vector will get reused too. + // It is the basetree pointer that is placed on the stack of course + // + factory->nilStack->pop(factory->nilStack); + return (pANTLR3_BASE_TREE)tree; + + } + // See if we need a new tree pool before allocating a new tree + // + if (factory->nextTree >= ANTLR3_FACTORY_POOL_SIZE) + { + // We ran out of tokens in the current pool, so we need a new pool + // + newPool(factory); + } + + // Assuming everything went well - we are trying for performance here so doing minimal + // error checking - then we can work out what the pointer is to the next commontree. + // + tree = factory->pools[factory->thisPool] + factory->nextTree; + factory->nextTree++; + + // We have our token pointer now, so we can initialize it to the predefined model. + // + antlr3SetCTAPI(tree); + + // Set some initial variables for future copying, including a string factory + // that we can use later for converting trees to strings. + // + tree->factory = factory; + tree->baseTree.strFactory = factory->unTruc.baseTree.strFactory; + + // The super points to the common tree so we must override the one used by + // by the pre-built tree as otherwise we will always poitn to the same initial + // common tree and we might spend 3 hours trying to debug why - this would never + // happen to me of course! :-( + // + tree->baseTree.super = tree; + + // And we are done + // + return &(tree->baseTree); +} + + +static pANTLR3_BASE_TREE +newFromTree(pANTLR3_ARBORETUM factory, pANTLR3_COMMON_TREE tree) +{ + pANTLR3_BASE_TREE newTree; + + newTree = factory->newTree(factory); + + if (newTree == NULL) + { + return NULL; + } + + // Pick up the payload we had in the supplied tree + // + ((pANTLR3_COMMON_TREE)(newTree->super))->token = tree->token; + newTree->u = tree->baseTree.u; // Copy any user pointer + + return newTree; +} + +static pANTLR3_BASE_TREE +newFromToken(pANTLR3_ARBORETUM factory, pANTLR3_COMMON_TOKEN token) +{ + pANTLR3_BASE_TREE newTree; + + newTree = factory->newTree(factory); + + if (newTree == NULL) + { + return NULL; + } + + // Pick up the payload we had in the supplied tree + // + ((pANTLR3_COMMON_TREE)(newTree->super))->token = token; + + return newTree; +} + +static void +factoryClose (pANTLR3_ARBORETUM factory) +{ + ANTLR3_INT32 poolCount; + + // First close the vector factory that supplied all the child pointer + // vectors. + // + factory->vFactory->close(factory->vFactory); + + if (factory->nilStack != NULL) + { + factory->nilStack->free(factory->nilStack); + } + + // We now JUST free the pools because the C runtime CommonToken based tree + // cannot contain anything that was not made by this factory. + // + for (poolCount = 0; poolCount <= factory->thisPool; poolCount++) + { + // We can now free this pool allocation + // + ANTLR3_FREE(factory->pools[poolCount]); + factory->pools[poolCount] = NULL; + } + + // All the pools are deallocated we can free the pointers to the pools + // now. + // + ANTLR3_FREE(factory->pools); + + // Finally, we can free the space for the factory itself + // + ANTLR3_FREE(factory); +} + + +ANTLR3_API void +antlr3SetCTAPI(pANTLR3_COMMON_TREE tree) +{ + // Init base tree + // + antlr3BaseTreeNew(&(tree->baseTree)); + + // We need a pointer to ourselves for + // the payload and few functions that we + // provide. + // + tree->baseTree.super = tree; + + // Common tree overrides + + tree->baseTree.isNilNode = isNilNode; + tree->baseTree.toString = toString; + tree->baseTree.dupNode = (void *(*)(pANTLR3_BASE_TREE))(dupNode); + tree->baseTree.getLine = getLine; + tree->baseTree.getCharPositionInLine = getCharPositionInLine; + tree->baseTree.toString = toString; + tree->baseTree.getType = getType; + tree->baseTree.getText = getText; + tree->baseTree.getToken = getToken; + tree->baseTree.getParent = getParent; + tree->baseTree.setParent = setParent; + tree->baseTree.setChildIndex = setChildIndex; + tree->baseTree.getChildIndex = getChildIndex; + tree->baseTree.createChildrenList = createChildrenList; + tree->baseTree.reuse = reuse; + tree->baseTree.free = NULL; // Factory trees have no free function + + tree->baseTree.children = NULL; + + tree->token = NULL; // No token as yet + tree->startIndex = 0; + tree->stopIndex = 0; + tree->parent = NULL; // No parent yet + tree->childIndex = -1; + + return; +} + +// -------------------------------------- +// Non factory node constructors. +// + +ANTLR3_API pANTLR3_COMMON_TREE +antlr3CommonTreeNew() +{ + pANTLR3_COMMON_TREE tree; + tree = ANTLR3_MALLOC(sizeof(ANTLR3_COMMON_TREE)); + + if (tree == NULL) + { + return NULL; + } + + antlr3SetCTAPI(tree); + + return tree; +} + +ANTLR3_API pANTLR3_COMMON_TREE +antlr3CommonTreeNewFromToken(pANTLR3_COMMON_TOKEN token) +{ + pANTLR3_COMMON_TREE newTree; + + newTree = antlr3CommonTreeNew(); + + if (newTree == NULL) + { + return NULL; + } + + //Pick up the payload we had in the supplied tree + // + newTree->token = token; + return newTree; +} + +/// Create a new vector for holding child nodes using the inbuilt +/// vector factory. +/// +static void +createChildrenList (pANTLR3_BASE_TREE tree) +{ + tree->children = ((pANTLR3_COMMON_TREE)(tree->super))->factory->vFactory->newVector(((pANTLR3_COMMON_TREE)(tree->super))->factory->vFactory); +} + + +static pANTLR3_COMMON_TOKEN +getToken (pANTLR3_BASE_TREE tree) +{ + // The token is the payload of the common tree or other implementor + // so it is stored within ourselves, which is the super pointer.Note + // that whatever the actual token is, it is passed around by its pointer + // to the common token implementation, which it may of course surround + // with its own super structure. + // + return ((pANTLR3_COMMON_TREE)(tree->super))->token; +} + +static pANTLR3_BASE_TREE +dupNode (pANTLR3_BASE_TREE tree) +{ + // The node we are duplicating is in fact the common tree (that's why we are here) + // so we use the super pointer to duplicate. + // + pANTLR3_COMMON_TREE theOld; + + theOld = (pANTLR3_COMMON_TREE)(tree->super); + + // The pointer we return is the base implementation of course + // + return theOld->factory->newFromTree(theOld->factory, theOld); +} + +static ANTLR3_BOOLEAN +isNilNode (pANTLR3_BASE_TREE tree) +{ + // This is a Nil tree if it has no payload (Token in our case) + // + if (((pANTLR3_COMMON_TREE)(tree->super))->token == NULL) + { + return ANTLR3_TRUE; + } + else + { + return ANTLR3_FALSE; + } +} + +static ANTLR3_UINT32 +getType (pANTLR3_BASE_TREE tree) +{ + pANTLR3_COMMON_TREE theTree; + + theTree = (pANTLR3_COMMON_TREE)(tree->super); + + if (theTree->token == NULL) + { + return 0; + } + else + { + return theTree->token->getType(theTree->token); + } +} + +static pANTLR3_STRING +getText (pANTLR3_BASE_TREE tree) +{ + return tree->toString(tree); +} + +static ANTLR3_UINT32 getLine (pANTLR3_BASE_TREE tree) +{ + pANTLR3_COMMON_TREE cTree; + pANTLR3_COMMON_TOKEN token; + + cTree = (pANTLR3_COMMON_TREE)(tree->super); + + token = cTree->token; + + if (token == NULL || token->getLine(token) == 0) + { + if (tree->getChildCount(tree) > 0) + { + pANTLR3_BASE_TREE child; + + child = (pANTLR3_BASE_TREE)tree->getChild(tree, 0); + return child->getLine(child); + } + return 0; + } + return token->getLine(token); +} + +static ANTLR3_UINT32 getCharPositionInLine (pANTLR3_BASE_TREE tree) +{ + pANTLR3_COMMON_TOKEN token; + + token = ((pANTLR3_COMMON_TREE)(tree->super))->token; + + if (token == NULL || token->getCharPositionInLine(token) == -1) + { + if (tree->getChildCount(tree) > 0) + { + pANTLR3_BASE_TREE child; + + child = (pANTLR3_BASE_TREE)tree->getChild(tree, 0); + + return child->getCharPositionInLine(child); + } + return 0; + } + return token->getCharPositionInLine(token); +} + +static pANTLR3_STRING toString (pANTLR3_BASE_TREE tree) +{ + if (tree->isNilNode(tree) == ANTLR3_TRUE) + { + pANTLR3_STRING nilNode; + + nilNode = tree->strFactory->newPtr(tree->strFactory, (pANTLR3_UINT8)"nil", 3); + + return nilNode; + } + + return ((pANTLR3_COMMON_TREE)(tree->super))->token->getText(((pANTLR3_COMMON_TREE)(tree->super))->token); +} + +static pANTLR3_BASE_TREE +getParent (pANTLR3_BASE_TREE tree) +{ + return & (((pANTLR3_COMMON_TREE)(tree->super))->parent->baseTree); +} + +static void +setParent (pANTLR3_BASE_TREE tree, pANTLR3_BASE_TREE parent) +{ + ((pANTLR3_COMMON_TREE)(tree->super))->parent = parent == NULL ? NULL : ((pANTLR3_COMMON_TREE)(parent->super))->parent; +} + +static void +setChildIndex (pANTLR3_BASE_TREE tree, ANTLR3_INT32 i) +{ + ((pANTLR3_COMMON_TREE)(tree->super))->childIndex = i; +} +static ANTLR3_INT32 +getChildIndex (pANTLR3_BASE_TREE tree ) +{ + return ((pANTLR3_COMMON_TREE)(tree->super))->childIndex; +} + +/** Clean up any child vector that the tree might have, so it can be reused, + * then add it into the reuse stack. + */ +static void +reuse (pANTLR3_BASE_TREE tree) +{ + pANTLR3_COMMON_TREE cTree; + + cTree = (pANTLR3_COMMON_TREE)(tree->super); + + if (cTree->factory != NULL) + { + if (cTree->baseTree.children != NULL) + { + cTree->baseTree.children->clear(cTree->baseTree.children); + } + cTree->factory->nilStack->push(cTree->factory->nilStack, tree, NULL); + } +} diff --git a/antlr-3.1.3/runtime/C/src/antlr3commontreeadaptor.c b/antlr-3.1.3/runtime/C/src/antlr3commontreeadaptor.c new file mode 100644 index 0000000..880e15a --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3commontreeadaptor.c @@ -0,0 +1,486 @@ +/** \file + * This is the standard tree adaptor used by the C runtime unless the grammar + * source file says to use anything different. It embeds a BASE_TREE to which + * it adds its own implementation of anything that the base tree is not + * good for, plus a number of methods that any other adaptor type + * needs to implement too. + * \ingroup pANTLR3_COMMON_TREE_ADAPTOR + */ + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3commontreeadaptor.h> + +#ifdef ANTLR3_WINDOWS +#pragma warning( disable : 4100 ) +#endif + +/* BASE_TREE_ADAPTOR overrides... */ +static pANTLR3_BASE_TREE dupNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE treeNode); +static pANTLR3_BASE_TREE create (pANTLR3_BASE_TREE_ADAPTOR adpator, pANTLR3_COMMON_TOKEN payload); +static pANTLR3_BASE_TREE dbgCreate (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_COMMON_TOKEN payload); +static pANTLR3_COMMON_TOKEN createToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text); +static pANTLR3_COMMON_TOKEN createTokenFromToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_COMMON_TOKEN fromToken); +static pANTLR3_COMMON_TOKEN getToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t); +static pANTLR3_STRING getText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t); +static ANTLR3_UINT32 getType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t); +static pANTLR3_BASE_TREE getChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 i); +static ANTLR3_UINT32 getChildCount (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t); +static void replaceChildren (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE t); +static void setDebugEventListener (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_DEBUG_EVENT_LISTENER debugger); +static void setChildIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_INT32 i); +static ANTLR3_INT32 getChildIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t); +static void setParent (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE child, pANTLR3_BASE_TREE parent); +static void setChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 i, pANTLR3_BASE_TREE child); +static void deleteChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 i); +static pANTLR3_BASE_TREE errorNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_TOKEN_STREAM ctnstream, pANTLR3_COMMON_TOKEN startToken, pANTLR3_COMMON_TOKEN stopToken, pANTLR3_EXCEPTION e); +/* Methods specific to each tree adaptor + */ +static void setTokenBoundaries (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN startToken, pANTLR3_COMMON_TOKEN stopToken); +static void dbgSetTokenBoundaries (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN startToken, pANTLR3_COMMON_TOKEN stopToken); +static ANTLR3_MARKER getTokenStartIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t); +static ANTLR3_MARKER getTokenStopIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t); + +static void ctaFree (pANTLR3_BASE_TREE_ADAPTOR adaptor); + +/** Create a new tree adaptor. Note that despite the fact that this is + * creating a new COMMON_TREE adaptor, we return the address of the + * BASE_TREE interface, as should any other adaptor that wishes to be + * used as the tree element of a tree parse/build. It needs to be given the + * address of a valid string factory as we do not know what the originating + * input stream encoding type was. This way we can rely on just using + * the original input stream's string factory or one of the correct type + * which the user supplies us. + */ +ANTLR3_API pANTLR3_BASE_TREE_ADAPTOR +ANTLR3_TREE_ADAPTORNew(pANTLR3_STRING_FACTORY strFactory) +{ + pANTLR3_COMMON_TREE_ADAPTOR cta; + + // First job is to create the memory we need for the tree adaptor interface. + // + cta = (pANTLR3_COMMON_TREE_ADAPTOR) ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_COMMON_TREE_ADAPTOR))); + + if (cta == NULL) + { + return NULL; + } + + // Memory is initialized, so initialize the base tree adaptor + // + antlr3BaseTreeAdaptorInit(&(cta->baseAdaptor), NULL); + + // Install our interface overrides. + // + cta->baseAdaptor.dupNode = (void * (*) (pANTLR3_BASE_TREE_ADAPTOR, void *)) + dupNode; + cta->baseAdaptor.create = (void * (*) (pANTLR3_BASE_TREE_ADAPTOR, pANTLR3_COMMON_TOKEN)) + create; + cta->baseAdaptor.createToken = + createToken; + cta->baseAdaptor.createTokenFromToken = + createTokenFromToken; + cta->baseAdaptor.setTokenBoundaries = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, pANTLR3_COMMON_TOKEN, pANTLR3_COMMON_TOKEN)) + setTokenBoundaries; + cta->baseAdaptor.getTokenStartIndex = (ANTLR3_MARKER (*) (pANTLR3_BASE_TREE_ADAPTOR, void *)) + getTokenStartIndex; + cta->baseAdaptor.getTokenStopIndex = (ANTLR3_MARKER (*) (pANTLR3_BASE_TREE_ADAPTOR, void *)) + getTokenStopIndex; + cta->baseAdaptor.getText = (pANTLR3_STRING (*) (pANTLR3_BASE_TREE_ADAPTOR, void *)) + getText; + cta->baseAdaptor.getType = (ANTLR3_UINT32 (*) (pANTLR3_BASE_TREE_ADAPTOR, void *)) + getType; + cta->baseAdaptor.getChild = (void * (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_UINT32)) + getChild; + cta->baseAdaptor.setChild = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_UINT32, void *)) + setChild; + cta->baseAdaptor.setParent = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, void *)) + setParent; + cta->baseAdaptor.setChildIndex = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_UINT32)) + setChildIndex; + cta->baseAdaptor.deleteChild = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_UINT32)) + deleteChild; + cta->baseAdaptor.getChildCount = (ANTLR3_UINT32 (*) (pANTLR3_BASE_TREE_ADAPTOR, void *)) + getChildCount; + cta->baseAdaptor.getChildIndex = (ANTLR3_INT32 (*) (pANTLR3_BASE_TREE_ADAPTOR, void *)) + getChildIndex; + cta->baseAdaptor.free = (void (*) (pANTLR3_BASE_TREE_ADAPTOR)) + ctaFree; + cta->baseAdaptor.setDebugEventListener = + setDebugEventListener; + cta->baseAdaptor.replaceChildren = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_INT32, ANTLR3_INT32, void *)) + replaceChildren; + cta->baseAdaptor.errorNode = (void * (*) (pANTLR3_BASE_TREE_ADAPTOR, pANTLR3_TOKEN_STREAM, pANTLR3_COMMON_TOKEN, pANTLR3_COMMON_TOKEN, pANTLR3_EXCEPTION)) + errorNode; + + // Install the super class pointer + // + cta->baseAdaptor.super = cta; + + // Install a tree factory for creating new tree nodes + // + cta->arboretum = antlr3ArboretumNew(strFactory); + + // Install a token factory for imaginary tokens, these imaginary + // tokens do not require access to the input stream so we can + // dummy the creation of it, but they will need a string factory. + // + cta->baseAdaptor.tokenFactory = antlr3TokenFactoryNew(NULL); + cta->baseAdaptor.tokenFactory->unTruc.strFactory = strFactory; + + // Allow the base tree adaptor to share the tree factory's string factory. + // + cta->baseAdaptor.strFactory = strFactory; + + // Return the address of the base adaptor interface. + // + return &(cta->baseAdaptor); +} + +/// Debugging version of the tree adaptor (not normally called as generated code +/// calls setDebugEventListener instead which changes a normal token stream to +/// a debugging stream and means that a user's instantiation code does not need +/// to be changed just to debug with AW. +/// +ANTLR3_API pANTLR3_BASE_TREE_ADAPTOR +ANTLR3_TREE_ADAPTORDebugNew(pANTLR3_STRING_FACTORY strFactory, pANTLR3_DEBUG_EVENT_LISTENER debugger) +{ + pANTLR3_BASE_TREE_ADAPTOR ta; + + // Create a normal one first + // + ta = ANTLR3_TREE_ADAPTORNew(strFactory); + + if (ta != NULL) + { + // Reinitialize as a debug version + // + antlr3BaseTreeAdaptorInit(ta, debugger); + ta->create = (void * (*) (pANTLR3_BASE_TREE_ADAPTOR, pANTLR3_COMMON_TOKEN)) + dbgCreate; + ta->setTokenBoundaries = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, pANTLR3_COMMON_TOKEN, pANTLR3_COMMON_TOKEN)) + dbgSetTokenBoundaries; + } + + return ta; +} + +/// Causes an existing common tree adaptor to become a debug version +/// +static void +setDebugEventListener (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_DEBUG_EVENT_LISTENER debugger) +{ + // Reinitialize as a debug version + // + antlr3BaseTreeAdaptorInit(adaptor, debugger); + + adaptor->create = (void * (*) (pANTLR3_BASE_TREE_ADAPTOR, pANTLR3_COMMON_TOKEN)) + dbgCreate; + adaptor->setTokenBoundaries = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, pANTLR3_COMMON_TOKEN, pANTLR3_COMMON_TOKEN)) + dbgSetTokenBoundaries; + +} + +static void +ctaFree(pANTLR3_BASE_TREE_ADAPTOR adaptor) +{ + pANTLR3_COMMON_TREE_ADAPTOR cta; + + cta = (pANTLR3_COMMON_TREE_ADAPTOR)(adaptor->super); + + /* Free the tree factory we created + */ + cta->arboretum->close(((pANTLR3_COMMON_TREE_ADAPTOR)(adaptor->super))->arboretum); + + /* Free the token factory we created + */ + adaptor->tokenFactory->close(adaptor->tokenFactory); + + /* Free the super pointer, as it is this that was allocated + * and is the common tree structure. + */ + ANTLR3_FREE(adaptor->super); +} + +/* BASE_TREE_ADAPTOR overrides */ + +static pANTLR3_BASE_TREE +errorNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_TOKEN_STREAM ctnstream, pANTLR3_COMMON_TOKEN startToken, pANTLR3_COMMON_TOKEN stopToken, pANTLR3_EXCEPTION e) +{ + // Use the supplied common tree node stream to get another tree from the factory + // TODO: Look at creating the erronode as in Java, but this is complicated by the + // need to track and free the memory allocated to it, so for now, we just + // want something in the tree that isn't a NULL pointer. + // + return adaptor->createTypeText(adaptor, ANTLR3_TOKEN_INVALID, (pANTLR3_UINT8)"Tree Error Node"); + +} + +/** Duplicate the supplied node. + */ +static pANTLR3_BASE_TREE +dupNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE treeNode) +{ + return treeNode == NULL ? NULL : treeNode->dupNode(treeNode); +} + +static pANTLR3_BASE_TREE +create (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_COMMON_TOKEN payload) +{ + pANTLR3_BASE_TREE ct; + + /* Create a new common tree as this is what this adaptor deals with + */ + ct = ((pANTLR3_COMMON_TREE_ADAPTOR)(adaptor->super))->arboretum->newFromToken(((pANTLR3_COMMON_TREE_ADAPTOR)(adaptor->super))->arboretum, payload); + + /* But all adaptors return the pointer to the base interface. + */ + return ct; +} +static pANTLR3_BASE_TREE +dbgCreate (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_COMMON_TOKEN payload) +{ + pANTLR3_BASE_TREE ct; + + ct = create(adaptor, payload); + adaptor->debugger->createNode(adaptor->debugger, ct); + + return ct; +} + +/** Tell me how to create a token for use with imaginary token nodes. + * For example, there is probably no input symbol associated with imaginary + * token DECL, but you need to create it as a payload or whatever for + * the DECL node as in ^(DECL type ID). + * + * If you care what the token payload objects' type is, you should + * override this method and any other createToken variant. + */ +static pANTLR3_COMMON_TOKEN +createToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text) +{ + pANTLR3_COMMON_TOKEN newToken; + + newToken = adaptor->tokenFactory->newToken(adaptor->tokenFactory); + + if (newToken != NULL) + { + newToken->textState = ANTLR3_TEXT_CHARP; + newToken->tokText.chars = (pANTLR3_UCHAR)text; + newToken->setType(newToken, tokenType); + newToken->input = adaptor->tokenFactory->input; + } + return newToken; +} + +/** Tell me how to create a token for use with imaginary token nodes. + * For example, there is probably no input symbol associated with imaginary + * token DECL, but you need to create it as a payload or whatever for + * the DECL node as in ^(DECL type ID). + * + * This is a variant of createToken where the new token is derived from + * an actual real input token. Typically this is for converting '{' + * tokens to BLOCK etc... You'll see + * + * r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ; + * + * If you care what the token payload objects' type is, you should + * override this method and any other createToken variant. + * + * NB: this being C it is not so easy to extend the types of creaeteToken. + * We will have to see if anyone needs to do this and add any variants to + * this interface. + */ +static pANTLR3_COMMON_TOKEN +createTokenFromToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_COMMON_TOKEN fromToken) +{ + pANTLR3_COMMON_TOKEN newToken; + + newToken = adaptor->tokenFactory->newToken(adaptor->tokenFactory); + + if (newToken != NULL) + { + // Create the text using our own string factory to avoid complicating + // commontoken. + // + pANTLR3_STRING text; + + newToken->toString = fromToken->toString; + + if (fromToken->textState == ANTLR3_TEXT_CHARP) + { + newToken->textState = ANTLR3_TEXT_CHARP; + newToken->tokText.chars = fromToken->tokText.chars; + } + else + { + text = fromToken->getText(fromToken); + newToken->textState = ANTLR3_TEXT_STRING; + newToken->tokText.text = adaptor->strFactory->newPtr(adaptor->strFactory, text->chars, text->len); + } + + newToken->setLine (newToken, fromToken->getLine(fromToken)); + newToken->setTokenIndex (newToken, fromToken->getTokenIndex(fromToken)); + newToken->setCharPositionInLine (newToken, fromToken->getCharPositionInLine(fromToken)); + newToken->setChannel (newToken, fromToken->getChannel(fromToken)); + newToken->setType (newToken, fromToken->getType(fromToken)); + } + + return newToken; +} + +/* Specific methods for a TreeAdaptor */ + +/** Track start/stop token for subtree root created for a rule. + * Only works with CommonTree nodes. For rules that match nothing, + * seems like this will yield start=i and stop=i-1 in a nil node. + * Might be useful info so I'll not force to be i..i. + */ +static void +setTokenBoundaries (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN startToken, pANTLR3_COMMON_TOKEN stopToken) +{ + ANTLR3_MARKER start; + ANTLR3_MARKER stop; + + pANTLR3_COMMON_TREE ct; + + if (t == NULL) + { + return; + } + + if ( startToken != NULL) + { + start = startToken->getTokenIndex(startToken); + } + else + { + start = 0; + } + + if ( stopToken != NULL) + { + stop = stopToken->getTokenIndex(stopToken); + } + else + { + stop = 0; + } + + ct = (pANTLR3_COMMON_TREE)(t->super); + + ct->startIndex = start; + ct->stopIndex = stop; + +} +static void +dbgSetTokenBoundaries (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN startToken, pANTLR3_COMMON_TOKEN stopToken) +{ + setTokenBoundaries(adaptor, t, startToken, stopToken); + + if (t != NULL && startToken != NULL && stopToken != NULL) + { + adaptor->debugger->setTokenBoundaries(adaptor->debugger, t, startToken->getTokenIndex(startToken), stopToken->getTokenIndex(stopToken)); + } +} + +static ANTLR3_MARKER +getTokenStartIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t) +{ + return ((pANTLR3_COMMON_TREE)(t->super))->startIndex; +} + +static ANTLR3_MARKER +getTokenStopIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t) +{ + return ((pANTLR3_COMMON_TREE)(t->super))->stopIndex; +} + +static pANTLR3_STRING +getText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t) +{ + return t->getText(t); +} + +static ANTLR3_UINT32 +getType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t) +{ + return t->getType(t); +} + +static void +replaceChildren +(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE t) +{ + if (parent != NULL) + { + parent->replaceChildren(parent, startChildIndex, stopChildIndex, t); + } +} + +static pANTLR3_BASE_TREE +getChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 i) +{ + return t->getChild(t, i); +} +static void +setChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 i, pANTLR3_BASE_TREE child) +{ + t->setChild(t, i, child); +} + +static void +deleteChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 i) +{ + t->deleteChild(t, i); +} + +static ANTLR3_UINT32 +getChildCount (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t) +{ + return t->getChildCount(t); +} + +static void +setChildIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_INT32 i) +{ + t->setChildIndex(t, i); +} + +static ANTLR3_INT32 +getChildIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t) +{ + return t->getChildIndex(t); +} +static void +setParent (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE child, pANTLR3_BASE_TREE parent) +{ + child->setParent(child, parent); +} diff --git a/antlr-3.1.3/runtime/C/src/antlr3commontreenodestream.c b/antlr-3.1.3/runtime/C/src/antlr3commontreenodestream.c new file mode 100644 index 0000000..a759d34 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3commontreenodestream.c @@ -0,0 +1,968 @@ +/// \file +/// Defines the implementation of the common node stream the default +/// tree node stream used by ANTLR. +/// + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3commontreenodestream.h> + +#ifdef ANTLR3_WINDOWS +#pragma warning( disable : 4100 ) +#endif + +// COMMON TREE STREAM API +// +static void addNavigationNode (pANTLR3_COMMON_TREE_NODE_STREAM ctns, ANTLR3_UINT32 ttype); +static ANTLR3_BOOLEAN hasUniqueNavigationNodes (pANTLR3_COMMON_TREE_NODE_STREAM ctns); +static pANTLR3_BASE_TREE newDownNode (pANTLR3_COMMON_TREE_NODE_STREAM ctns); +static pANTLR3_BASE_TREE newUpNode (pANTLR3_COMMON_TREE_NODE_STREAM ctns); +static void reset (pANTLR3_COMMON_TREE_NODE_STREAM ctns); +static void push (pANTLR3_COMMON_TREE_NODE_STREAM ctns, ANTLR3_INT32 index); +static ANTLR3_INT32 pop (pANTLR3_COMMON_TREE_NODE_STREAM ctns); +//static ANTLR3_INT32 index (pANTLR3_COMMON_TREE_NODE_STREAM ctns); +static ANTLR3_UINT32 getLookaheadSize (pANTLR3_COMMON_TREE_NODE_STREAM ctns); +// TREE NODE STREAM API +// +static pANTLR3_BASE_TREE_ADAPTOR getTreeAdaptor (pANTLR3_TREE_NODE_STREAM tns); +static pANTLR3_BASE_TREE getTreeSource (pANTLR3_TREE_NODE_STREAM tns); +static pANTLR3_BASE_TREE _LT (pANTLR3_TREE_NODE_STREAM tns, ANTLR3_INT32 k); +static pANTLR3_BASE_TREE get (pANTLR3_TREE_NODE_STREAM tns, ANTLR3_INT32 k); +static void setUniqueNavigationNodes (pANTLR3_TREE_NODE_STREAM tns, ANTLR3_BOOLEAN uniqueNavigationNodes); +static pANTLR3_STRING toString (pANTLR3_TREE_NODE_STREAM tns); +static pANTLR3_STRING toStringSS (pANTLR3_TREE_NODE_STREAM tns, pANTLR3_BASE_TREE start, pANTLR3_BASE_TREE stop); +static void toStringWork (pANTLR3_TREE_NODE_STREAM tns, pANTLR3_BASE_TREE start, pANTLR3_BASE_TREE stop, pANTLR3_STRING buf); +static void replaceChildren (pANTLR3_TREE_NODE_STREAM tns, pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE t); + +// INT STREAM API +// +static void consume (pANTLR3_INT_STREAM is); +static ANTLR3_MARKER tindex (pANTLR3_INT_STREAM is); +static ANTLR3_UINT32 _LA (pANTLR3_INT_STREAM is, ANTLR3_INT32 i); +static ANTLR3_MARKER mark (pANTLR3_INT_STREAM is); +static void release (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker); +static void rewindMark (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker); +static void rewindLast (pANTLR3_INT_STREAM is); +static void seek (pANTLR3_INT_STREAM is, ANTLR3_MARKER index); +static ANTLR3_UINT32 size (pANTLR3_INT_STREAM is); + + +// Helper functions +// +static void fillBuffer (pANTLR3_COMMON_TREE_NODE_STREAM ctns, pANTLR3_BASE_TREE t); +static void fillBufferRoot (pANTLR3_COMMON_TREE_NODE_STREAM ctns); + +// Constructors +// +static void antlr3TreeNodeStreamFree (pANTLR3_TREE_NODE_STREAM tns); +static void antlr3CommonTreeNodeStreamFree (pANTLR3_COMMON_TREE_NODE_STREAM ctns); + +ANTLR3_API pANTLR3_TREE_NODE_STREAM +antlr3TreeNodeStreamNew() +{ + pANTLR3_TREE_NODE_STREAM stream; + + // Memory for the interface structure + // + stream = (pANTLR3_TREE_NODE_STREAM) ANTLR3_CALLOC(1, sizeof(ANTLR3_TREE_NODE_STREAM)); + + if (stream == NULL) + { + return NULL; + } + + // Install basic API + // + stream->replaceChildren = replaceChildren; + stream->free = antlr3TreeNodeStreamFree; + + return stream; +} + +static void +antlr3TreeNodeStreamFree(pANTLR3_TREE_NODE_STREAM stream) +{ + ANTLR3_FREE(stream); +} + +ANTLR3_API pANTLR3_COMMON_TREE_NODE_STREAM +antlr3CommonTreeNodeStreamNewTree(pANTLR3_BASE_TREE tree, ANTLR3_UINT32 hint) +{ + pANTLR3_COMMON_TREE_NODE_STREAM stream; + + stream = antlr3CommonTreeNodeStreamNew(tree->strFactory, hint); + + if (stream == NULL) + { + return NULL; + } + stream->root = tree; + + return stream; +} + +ANTLR3_API pANTLR3_COMMON_TREE_NODE_STREAM +antlr3CommonTreeNodeStreamNewStream(pANTLR3_COMMON_TREE_NODE_STREAM inStream) +{ + pANTLR3_COMMON_TREE_NODE_STREAM stream; + + // Memory for the interface structure + // + stream = (pANTLR3_COMMON_TREE_NODE_STREAM) ANTLR3_CALLOC(1, sizeof(ANTLR3_COMMON_TREE_NODE_STREAM)); + + if (stream == NULL) + { + return NULL; + } + + // Copy in all the reusable parts of the originating stream and create new + // pieces where necessary. + // + + // String factory for tree walker + // + stream->stringFactory = inStream->stringFactory; + + // Create an adaptor for the common tree node stream + // + stream->adaptor = inStream->adaptor; + + // Create space for the tree node stream interface + // + stream->tnstream = antlr3TreeNodeStreamNew(); + + if (stream->tnstream == NULL) + { + stream->free (stream); + + return NULL; + } + + // Create space for the INT_STREAM interface + // + stream->tnstream->istream = antlr3IntStreamNew(); + + if (stream->tnstream->istream == NULL) + { + stream->tnstream->free (stream->tnstream); + stream->free (stream); + + return NULL; + } + + // Install the common tree node stream API + // + stream->addNavigationNode = addNavigationNode; + stream->hasUniqueNavigationNodes = hasUniqueNavigationNodes; + stream->newDownNode = newDownNode; + stream->newUpNode = newUpNode; + stream->reset = reset; + stream->push = push; + stream->pop = pop; + stream->getLookaheadSize = getLookaheadSize; + + stream->free = antlr3CommonTreeNodeStreamFree; + + // Install the tree node stream API + // + stream->tnstream->getTreeAdaptor = getTreeAdaptor; + stream->tnstream->getTreeSource = getTreeSource; + stream->tnstream->_LT = _LT; + stream->tnstream->setUniqueNavigationNodes = setUniqueNavigationNodes; + stream->tnstream->toString = toString; + stream->tnstream->toStringSS = toStringSS; + stream->tnstream->toStringWork = toStringWork; + stream->tnstream->get = get; + + // Install INT_STREAM interface + // + stream->tnstream->istream->consume = consume; + stream->tnstream->istream->index = tindex; + stream->tnstream->istream->_LA = _LA; + stream->tnstream->istream->mark = mark; + stream->tnstream->istream->release = release; + stream->tnstream->istream->rewind = rewindMark; + stream->tnstream->istream->rewindLast = rewindLast; + stream->tnstream->istream->seek = seek; + stream->tnstream->istream->size = size; + + // Initialize data elements of INT stream + // + stream->tnstream->istream->type = ANTLR3_COMMONTREENODE; + stream->tnstream->istream->super = (stream->tnstream); + + // Initialize data elements of TREE stream + // + stream->tnstream->ctns = stream; + + // Initialize data elements of the COMMON TREE NODE stream + // + stream->super = NULL; + stream->uniqueNavigationNodes = ANTLR3_FALSE; + stream->markers = NULL; + stream->nodeStack = inStream->nodeStack; + + // Create the node list map + // + stream->nodes = antlr3VectorNew(DEFAULT_INITIAL_BUFFER_SIZE); + stream->p = -1; + + // Install the navigation nodes + // + + // Install the navigation nodes + // + antlr3SetCTAPI(&(stream->UP)); + antlr3SetCTAPI(&(stream->DOWN)); + antlr3SetCTAPI(&(stream->EOF_NODE)); + antlr3SetCTAPI(&(stream->INVALID_NODE)); + + stream->UP.token = inStream->UP.token; + inStream->UP.token->strFactory = stream->stringFactory; + stream->DOWN.token = inStream->DOWN.token; + inStream->DOWN.token->strFactory = stream->stringFactory; + stream->EOF_NODE.token = inStream->EOF_NODE.token; + inStream->EOF_NODE.token->strFactory = stream->stringFactory; + stream->INVALID_NODE.token = inStream->INVALID_NODE.token; + inStream->INVALID_NODE.token->strFactory= stream->stringFactory; + + // Reuse the root tree of the originating stream + // + stream->root = inStream->root; + + // Signal that this is a rewriting stream so we don't + // free the originating tree. Anything that we rewrite or + // duplicate here will be done through the adaptor or + // the original tree factory. + // + stream->isRewriter = ANTLR3_TRUE; + return stream; +} + +ANTLR3_API pANTLR3_COMMON_TREE_NODE_STREAM +antlr3CommonTreeNodeStreamNew(pANTLR3_STRING_FACTORY strFactory, ANTLR3_UINT32 hint) +{ + pANTLR3_COMMON_TREE_NODE_STREAM stream; + pANTLR3_COMMON_TOKEN token; + + // Memory for the interface structure + // + stream = (pANTLR3_COMMON_TREE_NODE_STREAM) ANTLR3_CALLOC(1, sizeof(ANTLR3_COMMON_TREE_NODE_STREAM)); + + if (stream == NULL) + { + return NULL; + } + + // String factory for tree walker + // + stream->stringFactory = strFactory; + + // Create an adaptor for the common tree node stream + // + stream->adaptor = ANTLR3_TREE_ADAPTORNew(strFactory); + + if (stream->adaptor == NULL) + { + stream->free(stream); + return NULL; + } + + // Create space for the tree node stream interface + // + stream->tnstream = antlr3TreeNodeStreamNew(); + + if (stream->tnstream == NULL) + { + stream->adaptor->free (stream->adaptor); + stream->free (stream); + + return NULL; + } + + // Create space for the INT_STREAM interface + // + stream->tnstream->istream = antlr3IntStreamNew(); + + if (stream->tnstream->istream == NULL) + { + stream->adaptor->free (stream->adaptor); + stream->tnstream->free (stream->tnstream); + stream->free (stream); + + return NULL; + } + + // Install the common tree node stream API + // + stream->addNavigationNode = addNavigationNode; + stream->hasUniqueNavigationNodes = hasUniqueNavigationNodes; + stream->newDownNode = newDownNode; + stream->newUpNode = newUpNode; + stream->reset = reset; + stream->push = push; + stream->pop = pop; + + stream->free = antlr3CommonTreeNodeStreamFree; + + // Install the tree node stream API + // + stream->tnstream->getTreeAdaptor = getTreeAdaptor; + stream->tnstream->getTreeSource = getTreeSource; + stream->tnstream->_LT = _LT; + stream->tnstream->setUniqueNavigationNodes = setUniqueNavigationNodes; + stream->tnstream->toString = toString; + stream->tnstream->toStringSS = toStringSS; + stream->tnstream->toStringWork = toStringWork; + stream->tnstream->get = get; + + // Install INT_STREAM interface + // + stream->tnstream->istream->consume = consume; + stream->tnstream->istream->index = tindex; + stream->tnstream->istream->_LA = _LA; + stream->tnstream->istream->mark = mark; + stream->tnstream->istream->release = release; + stream->tnstream->istream->rewind = rewindMark; + stream->tnstream->istream->rewindLast = rewindLast; + stream->tnstream->istream->seek = seek; + stream->tnstream->istream->size = size; + + // Initialize data elements of INT stream + // + stream->tnstream->istream->type = ANTLR3_COMMONTREENODE; + stream->tnstream->istream->super = (stream->tnstream); + + // Initialize data elements of TREE stream + // + stream->tnstream->ctns = stream; + + // Initialize data elements of the COMMON TREE NODE stream + // + stream->super = NULL; + stream->uniqueNavigationNodes = ANTLR3_FALSE; + stream->markers = NULL; + stream->nodeStack = antlr3StackNew(INITIAL_CALL_STACK_SIZE); + + // Create the node list map + // + if (hint == 0) + { + hint = DEFAULT_INITIAL_BUFFER_SIZE; + } + stream->nodes = antlr3VectorNew(hint); + stream->p = -1; + + // Install the navigation nodes + // + antlr3SetCTAPI(&(stream->UP)); + antlr3SetCTAPI(&(stream->DOWN)); + antlr3SetCTAPI(&(stream->EOF_NODE)); + antlr3SetCTAPI(&(stream->INVALID_NODE)); + + token = antlr3CommonTokenNew(ANTLR3_TOKEN_UP); + token->strFactory = strFactory; + token->textState = ANTLR3_TEXT_CHARP; + token->tokText.chars = (pANTLR3_UCHAR)"UP"; + stream->UP.token = token; + + token = antlr3CommonTokenNew(ANTLR3_TOKEN_DOWN); + token->strFactory = strFactory; + token->textState = ANTLR3_TEXT_CHARP; + token->tokText.chars = (pANTLR3_UCHAR)"DOWN"; + stream->DOWN.token = token; + + token = antlr3CommonTokenNew(ANTLR3_TOKEN_EOF); + token->strFactory = strFactory; + token->textState = ANTLR3_TEXT_CHARP; + token->tokText.chars = (pANTLR3_UCHAR)"EOF"; + stream->EOF_NODE.token = token; + + token = antlr3CommonTokenNew(ANTLR3_TOKEN_INVALID); + token->strFactory = strFactory; + token->textState = ANTLR3_TEXT_CHARP; + token->tokText.chars = (pANTLR3_UCHAR)"INVALID"; + stream->INVALID_NODE.token = token; + + + return stream; +} + +/// Free up any resources that belong to this common tree node stream. +/// +static void antlr3CommonTreeNodeStreamFree (pANTLR3_COMMON_TREE_NODE_STREAM ctns) +{ + + // If this is a rewrting stream, then certain resources + // belong to the originating node stream and we do not + // free them here. + // + if (ctns->isRewriter != ANTLR3_TRUE) + { + ctns->adaptor ->free (ctns->adaptor); + + if (ctns->nodeStack != NULL) + { + ctns->nodeStack->free(ctns->nodeStack); + } + + ANTLR3_FREE(ctns->INVALID_NODE.token); + ANTLR3_FREE(ctns->EOF_NODE.token); + ANTLR3_FREE(ctns->DOWN.token); + ANTLR3_FREE(ctns->UP.token); + } + + if (ctns->nodes != NULL) + { + ctns->nodes ->free (ctns->nodes); + } + ctns->tnstream->istream ->free (ctns->tnstream->istream); + ctns->tnstream ->free (ctns->tnstream); + + + ANTLR3_FREE(ctns); +} + +// ------------------------------------------------------------------------------ +// Local helpers +// + +/// Walk and fill the tree node buffer from the root tree +/// +static void +fillBufferRoot(pANTLR3_COMMON_TREE_NODE_STREAM ctns) +{ + // Call the generic buffer routine with the root as the + // argument + // + fillBuffer(ctns, ctns->root); + ctns->p = 0; // Indicate we are at buffer start +} + +/// Walk tree with depth-first-search and fill nodes buffer. +/// Don't add in DOWN, UP nodes if the supplied tree is a list (t is isNilNode) +// such as the root tree is. +/// +static void +fillBuffer(pANTLR3_COMMON_TREE_NODE_STREAM ctns, pANTLR3_BASE_TREE t) +{ + ANTLR3_BOOLEAN nilNode; + ANTLR3_UINT32 nCount; + ANTLR3_UINT32 c; + + nilNode = ctns->adaptor->isNilNode(ctns->adaptor, t); + + // If the supplied node is not a nil (list) node then we + // add in the node itself to the vector + // + if (nilNode == ANTLR3_FALSE) + { + ctns->nodes->add(ctns->nodes, t, NULL); + } + + // Only add a DOWN node if the tree is not a nil tree and + // the tree does have children. + // + nCount = t->getChildCount(t); + + if (nilNode == ANTLR3_FALSE && nCount>0) + { + ctns->addNavigationNode(ctns, ANTLR3_TOKEN_DOWN); + } + + // We always add any children the tree contains, which is + // a recursive call to this function, which will cause similar + // recursion and implement a depth first addition + // + for (c = 0; c < nCount; c++) + { + fillBuffer(ctns, ctns->adaptor->getChild(ctns->adaptor, t, c)); + } + + // If the tree had children and was not a nil (list) node, then we + // we need to add an UP node here to match the DOWN node + // + if (nilNode == ANTLR3_FALSE && nCount > 0) + { + ctns->addNavigationNode(ctns, ANTLR3_TOKEN_UP); + } +} + + +// ------------------------------------------------------------------------------ +// Interface functions +// + +/// Reset the input stream to the start of the input nodes. +/// +static void +reset (pANTLR3_COMMON_TREE_NODE_STREAM ctns) +{ + if (ctns->p != -1) + { + ctns->p = 0; + } + ctns->tnstream->istream->lastMarker = 0; + + + // Free and reset the node stack only if this is not + // a rewriter, which is going to reuse the originating + // node streams node stack + // + if (ctns->isRewriter != ANTLR3_TRUE) + { + if (ctns->nodeStack != NULL) + { + ctns->nodeStack->free(ctns->nodeStack); + ctns->nodeStack = antlr3StackNew(INITIAL_CALL_STACK_SIZE); + } + } +} + + +static pANTLR3_BASE_TREE +LB(pANTLR3_TREE_NODE_STREAM tns, ANTLR3_INT32 k) +{ + if ( k==0) + { + return &(tns->ctns->INVALID_NODE.baseTree); + } + + if ( (tns->ctns->p - k) < 0) + { + return &(tns->ctns->INVALID_NODE.baseTree); + } + + return tns->ctns->nodes->get(tns->ctns->nodes, tns->ctns->p - k); +} + +/// Get tree node at current input pointer + i ahead where i=1 is next node. +/// i<0 indicates nodes in the past. So -1 is previous node and -2 is +/// two nodes ago. LT(0) is undefined. For i>=n, return null. +/// Return null for LT(0) and any index that results in an absolute address +/// that is negative. +/// +/// This is analogous to the _LT() method of the TokenStream, but this +/// returns a tree node instead of a token. Makes code gen identical +/// for both parser and tree grammars. :) +/// +static pANTLR3_BASE_TREE +_LT (pANTLR3_TREE_NODE_STREAM tns, ANTLR3_INT32 k) +{ + if (tns->ctns->p == -1) + { + fillBufferRoot(tns->ctns); + } + + if (k < 0) + { + return LB(tns, -k); + } + else if (k == 0) + { + return &(tns->ctns->INVALID_NODE.baseTree); + } + + // k was a legitimate request, + // + if (( tns->ctns->p + k - 1) >= (ANTLR3_INT32)(tns->ctns->nodes->count)) + { + return &(tns->ctns->EOF_NODE.baseTree); + } + + return tns->ctns->nodes->get(tns->ctns->nodes, tns->ctns->p + k - 1); +} + +/// Where is this stream pulling nodes from? This is not the name, but +/// the object that provides node objects. +/// +static pANTLR3_BASE_TREE +getTreeSource (pANTLR3_TREE_NODE_STREAM tns) +{ + return tns->ctns->root; +} + +/// Consume the next node from the input stream +/// +static void +consume (pANTLR3_INT_STREAM is) +{ + pANTLR3_TREE_NODE_STREAM tns; + pANTLR3_COMMON_TREE_NODE_STREAM ctns; + + tns = (pANTLR3_TREE_NODE_STREAM)(is->super); + ctns = tns->ctns; + + if (ctns->p == -1) + { + fillBufferRoot(ctns); + } + ctns->p++; +} + +static ANTLR3_UINT32 +_LA (pANTLR3_INT_STREAM is, ANTLR3_INT32 i) +{ + pANTLR3_TREE_NODE_STREAM tns; + pANTLR3_BASE_TREE t; + + tns = (pANTLR3_TREE_NODE_STREAM)(is->super); + + // Ask LT for the 'token' at that position + // + t = tns->_LT(tns, i); + + if (t == NULL) + { + return ANTLR3_TOKEN_INVALID; + } + + // Token node was there so return the type of it + // + return t->getType(t); +} + +/// Mark the state of the input stream so that we can come back to it +/// after a syntactic predicate and so on. +/// +static ANTLR3_MARKER +mark (pANTLR3_INT_STREAM is) +{ + pANTLR3_TREE_NODE_STREAM tns; + pANTLR3_COMMON_TREE_NODE_STREAM ctns; + + tns = (pANTLR3_TREE_NODE_STREAM)(is->super); + ctns = tns->ctns; + + if (tns->ctns->p == -1) + { + fillBufferRoot(tns->ctns); + } + + // Return the current mark point + // + ctns->tnstream->istream->lastMarker = ctns->tnstream->istream->index(ctns->tnstream->istream); + + return ctns->tnstream->istream->lastMarker; +} + +static void +release (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker) +{ +} + +/// Rewind the current state of the tree walk to the state it +/// was in when mark() was called and it returned marker. Also, +/// wipe out the lookahead which will force reloading a few nodes +/// but it is better than making a copy of the lookahead buffer +/// upon mark(). +/// +static void +rewindMark (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker) +{ + is->seek(is, marker); +} + +static void +rewindLast (pANTLR3_INT_STREAM is) +{ + is->seek(is, is->lastMarker); +} + +/// consume() ahead until we hit index. Can't just jump ahead--must +/// spit out the navigation nodes. +/// +static void +seek (pANTLR3_INT_STREAM is, ANTLR3_MARKER index) +{ + pANTLR3_TREE_NODE_STREAM tns; + pANTLR3_COMMON_TREE_NODE_STREAM ctns; + + tns = (pANTLR3_TREE_NODE_STREAM)(is->super); + ctns = tns->ctns; + + ctns->p = ANTLR3_UINT32_CAST(index); +} + +static ANTLR3_MARKER +tindex (pANTLR3_INT_STREAM is) +{ + pANTLR3_TREE_NODE_STREAM tns; + pANTLR3_COMMON_TREE_NODE_STREAM ctns; + + tns = (pANTLR3_TREE_NODE_STREAM)(is->super); + ctns = tns->ctns; + + return (ANTLR3_MARKER)(ctns->p); +} + +/// Expensive to compute the size of the whole tree while parsing. +/// This method only returns how much input has been seen so far. So +/// after parsing it returns true size. +/// +static ANTLR3_UINT32 +size (pANTLR3_INT_STREAM is) +{ + pANTLR3_TREE_NODE_STREAM tns; + pANTLR3_COMMON_TREE_NODE_STREAM ctns; + + tns = (pANTLR3_TREE_NODE_STREAM)(is->super); + ctns = tns->ctns; + + if (ctns->p == -1) + { + fillBufferRoot(ctns); + } + + return ctns->nodes->size(ctns->nodes); +} + +/// As we flatten the tree, we use UP, DOWN nodes to represent +/// the tree structure. When debugging we need unique nodes +/// so instantiate new ones when uniqueNavigationNodes is true. +/// +static void +addNavigationNode (pANTLR3_COMMON_TREE_NODE_STREAM ctns, ANTLR3_UINT32 ttype) +{ + pANTLR3_BASE_TREE node; + + node = NULL; + + if (ttype == ANTLR3_TOKEN_DOWN) + { + if (ctns->hasUniqueNavigationNodes(ctns) == ANTLR3_TRUE) + { + node = ctns->newDownNode(ctns); + } + else + { + node = &(ctns->DOWN.baseTree); + } + } + else + { + if (ctns->hasUniqueNavigationNodes(ctns) == ANTLR3_TRUE) + { + node = ctns->newUpNode(ctns); + } + else + { + node = &(ctns->UP.baseTree); + } + } + + // Now add the node we decided upon. + // + ctns->nodes->add(ctns->nodes, node, NULL); +} + + +static pANTLR3_BASE_TREE_ADAPTOR +getTreeAdaptor (pANTLR3_TREE_NODE_STREAM tns) +{ + return tns->ctns->adaptor; +} + +static ANTLR3_BOOLEAN +hasUniqueNavigationNodes (pANTLR3_COMMON_TREE_NODE_STREAM ctns) +{ + return ctns->uniqueNavigationNodes; +} + +static void +setUniqueNavigationNodes (pANTLR3_TREE_NODE_STREAM tns, ANTLR3_BOOLEAN uniqueNavigationNodes) +{ + tns->ctns->uniqueNavigationNodes = uniqueNavigationNodes; +} + + +/// Print out the entire tree including DOWN/UP nodes. Uses +/// a recursive walk. Mostly useful for testing as it yields +/// the token types not text. +/// +static pANTLR3_STRING +toString (pANTLR3_TREE_NODE_STREAM tns) +{ + + return tns->toStringSS(tns, tns->ctns->root, NULL); +} + +static pANTLR3_STRING +toStringSS (pANTLR3_TREE_NODE_STREAM tns, pANTLR3_BASE_TREE start, pANTLR3_BASE_TREE stop) +{ + pANTLR3_STRING buf; + + buf = tns->ctns->stringFactory->newRaw(tns->ctns->stringFactory); + + tns->toStringWork(tns, start, stop, buf); + + return buf; +} + +static void +toStringWork (pANTLR3_TREE_NODE_STREAM tns, pANTLR3_BASE_TREE p, pANTLR3_BASE_TREE stop, pANTLR3_STRING buf) +{ + + ANTLR3_UINT32 n; + ANTLR3_UINT32 c; + + if (!p->isNilNode(p) ) + { + pANTLR3_STRING text; + + text = p->toString(p); + + if (text == NULL) + { + text = tns->ctns->stringFactory->newRaw(tns->ctns->stringFactory); + + text->addc (text, ' '); + text->addi (text, p->getType(p)); + } + + buf->appendS(buf, text); + } + + if (p == stop) + { + return; /* Finished */ + } + + n = p->getChildCount(p); + + if (n > 0 && ! p->isNilNode(p) ) + { + buf->addc (buf, ' '); + buf->addi (buf, ANTLR3_TOKEN_DOWN); + } + + for (c = 0; c<n ; c++) + { + pANTLR3_BASE_TREE child; + + child = p->getChild(p, c); + tns->toStringWork(tns, child, stop, buf); + } + + if (n > 0 && ! p->isNilNode(p) ) + { + buf->addc (buf, ' '); + buf->addi (buf, ANTLR3_TOKEN_UP); + } +} + +static ANTLR3_UINT32 +getLookaheadSize (pANTLR3_COMMON_TREE_NODE_STREAM ctns) +{ + return ctns->tail < ctns->head + ? (ctns->lookAheadLength - ctns->head + ctns->tail) + : (ctns->tail - ctns->head); +} + +static pANTLR3_BASE_TREE +newDownNode (pANTLR3_COMMON_TREE_NODE_STREAM ctns) +{ + pANTLR3_COMMON_TREE dNode; + pANTLR3_COMMON_TOKEN token; + + token = antlr3CommonTokenNew(ANTLR3_TOKEN_DOWN); + token->textState = ANTLR3_TEXT_CHARP; + token->tokText.chars = (pANTLR3_UCHAR)"DOWN"; + dNode = antlr3CommonTreeNewFromToken(token); + + return &(dNode->baseTree); +} + +static pANTLR3_BASE_TREE +newUpNode (pANTLR3_COMMON_TREE_NODE_STREAM ctns) +{ + pANTLR3_COMMON_TREE uNode; + pANTLR3_COMMON_TOKEN token; + + token = antlr3CommonTokenNew(ANTLR3_TOKEN_UP); + token->textState = ANTLR3_TEXT_CHARP; + token->tokText.chars = (pANTLR3_UCHAR)"UP"; + uNode = antlr3CommonTreeNewFromToken(token); + + return &(uNode->baseTree); +} + +/// Replace from start to stop child index of parent with t, which might +/// be a list. Number of children may be different +/// after this call. The stream is notified because it is walking the +/// tree and might need to know you are monkey-ing with the underlying +/// tree. Also, it might be able to modify the node stream to avoid +/// re-streaming for future phases. +/// +/// If parent is null, don't do anything; must be at root of overall tree. +/// Can't replace whatever points to the parent externally. Do nothing. +/// +static void +replaceChildren (pANTLR3_TREE_NODE_STREAM tns, pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE t) +{ + if (parent != NULL) + { + pANTLR3_BASE_TREE_ADAPTOR adaptor; + pANTLR3_COMMON_TREE_ADAPTOR cta; + + adaptor = tns->getTreeAdaptor(tns); + cta = (pANTLR3_COMMON_TREE_ADAPTOR)(adaptor->super); + + adaptor->replaceChildren(adaptor, parent, startChildIndex, stopChildIndex, t); + } +} + +static pANTLR3_BASE_TREE +get (pANTLR3_TREE_NODE_STREAM tns, ANTLR3_INT32 k) +{ + if (tns->ctns->p == -1) + { + fillBufferRoot(tns->ctns); + } + + return tns->ctns->nodes->get(tns->ctns->nodes, k); +} + +static void +push (pANTLR3_COMMON_TREE_NODE_STREAM ctns, ANTLR3_INT32 index) +{ + ctns->nodeStack->push(ctns->nodeStack, ANTLR3_FUNC_PTR(ctns->p), NULL); // Save current index + ctns->tnstream->istream->seek(ctns->tnstream->istream, index); +} + +static ANTLR3_INT32 +pop (pANTLR3_COMMON_TREE_NODE_STREAM ctns) +{ + ANTLR3_INT32 retVal; + + retVal = ANTLR3_UINT32_CAST(ctns->nodeStack->pop(ctns->nodeStack)); + ctns->tnstream->istream->seek(ctns->tnstream->istream, retVal); + return retVal; +} diff --git a/antlr-3.1.3/runtime/C/src/antlr3convertutf.c b/antlr-3.1.3/runtime/C/src/antlr3convertutf.c new file mode 100644 index 0000000..c6fda21 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3convertutf.c @@ -0,0 +1,542 @@ +/* + * Copyright 2001-2004 Unicode, Inc. + * + * Disclaimer + * + * This source code is provided as is by Unicode, Inc. No claims are + * made as to fitness for any particular purpose. No warranties of any + * kind are expressed or implied. The recipient agrees to determine + * applicability of information provided. If this file has been + * purchased on magnetic or optical media from Unicode, Inc., the + * sole remedy for any claim will be exchange of defective media + * within 90 days of receipt. + * + * Limitations on Rights to Redistribute This Code + * + * Unicode, Inc. hereby grants the right to freely use the information + * supplied in this file in the creation of products supporting the + * Unicode Standard, and to make copies of this file in any form + * for internal or external distribution as long as this notice + * remains attached. + */ + +/* --------------------------------------------------------------------- + + Conversions between UTF32, UTF-16, and UTF-8. Source code file. + Author: Mark E. Davis, 1994. + Rev History: Rick McGowan, fixes & updates May 2001. + Sept 2001: fixed const & error conditions per + mods suggested by S. Parent & A. Lillich. + June 2002: Tim Dodd added detection and handling of incomplete + source sequences, enhanced error detection, added casts + to eliminate compiler warnings. + July 2003: slight mods to back out aggressive FFFE detection. + Jan 2004: updated switches in from-UTF8 conversions. + Oct 2004: updated to use UNI_MAX_LEGAL_UTF32 in UTF-32 conversions. + + See the header file "ConvertUTF.h" for complete documentation. + +------------------------------------------------------------------------ */ + + +#include "antlr3convertutf.h" + +#ifdef CVTUTF_DEBUG +#include <stdio.h> +#endif + +static const int halfShift = 10; /* used for shifting by 10 bits */ + +static const UTF32 halfBase = 0x0010000UL; +static const UTF32 halfMask = 0x3FFUL; + +#define UNI_SUR_HIGH_START (UTF32)0xD800 +#define UNI_SUR_HIGH_END (UTF32)0xDBFF +#define UNI_SUR_LOW_START (UTF32)0xDC00 +#define UNI_SUR_LOW_END (UTF32)0xDFFF +#define false 0 +#define true 1 + +/* --------------------------------------------------------------------- */ + +ConversionResult ConvertUTF32toUTF16 ( + const UTF32** sourceStart, const UTF32* sourceEnd, + UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) { + ConversionResult result = conversionOK; + const UTF32* source = *sourceStart; + UTF16* target = *targetStart; + while (source < sourceEnd) { + UTF32 ch; + if (target >= targetEnd) { + result = targetExhausted; break; + } + ch = *source++; + if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */ + /* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */ + if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { + if (flags == strictConversion) { + --source; /* return to the illegal value itself */ + result = sourceIllegal; + break; + } else { + *target++ = UNI_REPLACEMENT_CHAR; + } + } else { + *target++ = (UTF16)ch; /* normal case */ + } + } else if (ch > UNI_MAX_LEGAL_UTF32) { + if (flags == strictConversion) { + result = sourceIllegal; + } else { + *target++ = UNI_REPLACEMENT_CHAR; + } + } else { + /* target is a character in range 0xFFFF - 0x10FFFF. */ + if (target + 1 >= targetEnd) { + --source; /* Back up source pointer! */ + result = targetExhausted; break; + } + ch -= halfBase; + *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START); + *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START); + } + } + *sourceStart = source; + *targetStart = target; + return result; +} + +/* --------------------------------------------------------------------- */ + +ConversionResult ConvertUTF16toUTF32 ( + const UTF16** sourceStart, const UTF16* sourceEnd, + UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) { + ConversionResult result = conversionOK; + const UTF16* source = *sourceStart; + UTF32* target = *targetStart; + UTF32 ch, ch2; + while (source < sourceEnd) { + const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */ + ch = *source++; + /* If we have a surrogate pair, convert to UTF32 first. */ + if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) { + /* If the 16 bits following the high surrogate are in the source buffer... */ + if (source < sourceEnd) { + ch2 = *source; + /* If it's a low surrogate, convert to UTF32. */ + if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) { + ch = ((ch - UNI_SUR_HIGH_START) << halfShift) + + (ch2 - UNI_SUR_LOW_START) + halfBase; + ++source; + } else if (flags == strictConversion) { /* it's an unpaired high surrogate */ + --source; /* return to the illegal value itself */ + result = sourceIllegal; + break; + } + } else { /* We don't have the 16 bits following the high surrogate. */ + --source; /* return to the high surrogate */ + result = sourceExhausted; + break; + } + } else if (flags == strictConversion) { + /* UTF-16 surrogate values are illegal in UTF-32 */ + if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) { + --source; /* return to the illegal value itself */ + result = sourceIllegal; + break; + } + } + if (target >= targetEnd) { + source = oldSource; /* Back up source pointer! */ + result = targetExhausted; break; + } + *target++ = ch; + } + *sourceStart = source; + *targetStart = target; +#ifdef CVTUTF_DEBUG +if (result == sourceIllegal) { + ANTLR3_FPRINTF(stderr, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x\n", ch, ch2); + fflush(stderr); +} +#endif + return result; +} + +/* --------------------------------------------------------------------- */ + +/* + * Index into the table below with the first byte of a UTF-8 sequence to + * get the number of trailing bytes that are supposed to follow it. + * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is + * left as-is for anyone who may want to do such conversion, which was + * allowed in earlier algorithms. + */ +static const char trailingBytesForUTF8[256] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5 +}; + +/* + * Magic values subtracted from a buffer value during UTF8 conversion. + * This table contains as many values as there might be trailing bytes + * in a UTF-8 sequence. + */ +static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL, + 0x03C82080UL, 0xFA082080UL, 0x82082080UL }; + +/* + * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed + * into the first byte, depending on how many bytes follow. There are + * as many entries in this table as there are UTF-8 sequence types. + * (I.e., one byte sequence, two byte... etc.). Remember that sequencs + * for *legal* UTF-8 will be 4 or fewer bytes total. + */ +static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; + +/* --------------------------------------------------------------------- */ + +/* The interface converts a whole buffer to avoid function-call overhead. + * Constants have been gathered. Loops & conditionals have been removed as + * much as possible for efficiency, in favor of drop-through switches. + * (See "Note A" at the bottom of the file for equivalent code.) + * If your compiler supports it, the "isLegalUTF8" call can be turned + * into an inline function. + */ + +/* --------------------------------------------------------------------- */ + +ConversionResult ConvertUTF16toUTF8 ( + const UTF16** sourceStart, const UTF16* sourceEnd, + UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) { + ConversionResult result = conversionOK; + const UTF16* source = *sourceStart; + UTF8* target = *targetStart; + while (source < sourceEnd) { + UTF32 ch; + unsigned short bytesToWrite = 0; + const UTF32 byteMask = 0xBF; + const UTF32 byteMark = 0x80; + const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */ + ch = *source++; + /* If we have a surrogate pair, convert to UTF32 first. */ + if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) { + /* If the 16 bits following the high surrogate are in the source buffer... */ + if (source < sourceEnd) { + UTF32 ch2 = *source; + /* If it's a low surrogate, convert to UTF32. */ + if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) { + ch = ((ch - UNI_SUR_HIGH_START) << halfShift) + + (ch2 - UNI_SUR_LOW_START) + halfBase; + ++source; + } else if (flags == strictConversion) { /* it's an unpaired high surrogate */ + --source; /* return to the illegal value itself */ + result = sourceIllegal; + break; + } + } else { /* We don't have the 16 bits following the high surrogate. */ + --source; /* return to the high surrogate */ + result = sourceExhausted; + break; + } + } else if (flags == strictConversion) { + /* UTF-16 surrogate values are illegal in UTF-32 */ + if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) { + --source; /* return to the illegal value itself */ + result = sourceIllegal; + break; + } + } + /* Figure out how many bytes the result will require */ + if (ch < (UTF32)0x80) { bytesToWrite = 1; + } else if (ch < (UTF32)0x800) { bytesToWrite = 2; + } else if (ch < (UTF32)0x10000) { bytesToWrite = 3; + } else if (ch < (UTF32)0x110000) { bytesToWrite = 4; + } else { bytesToWrite = 3; + ch = UNI_REPLACEMENT_CHAR; + } + + target += bytesToWrite; + if (target > targetEnd) { + source = oldSource; /* Back up source pointer! */ + target -= bytesToWrite; result = targetExhausted; break; + } + switch (bytesToWrite) { /* note: everything falls through. */ + case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; + case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; + case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; + case 1: *--target = (UTF8)(ch | firstByteMark[bytesToWrite]); + } + target += bytesToWrite; + } + *sourceStart = source; + *targetStart = target; + return result; +} + +/* --------------------------------------------------------------------- */ + +/* + * Utility routine to tell whether a sequence of bytes is legal UTF-8. + * This must be called with the length pre-determined by the first byte. + * If not calling this from ConvertUTF8to*, then the length can be set by: + * length = trailingBytesForUTF8[*source]+1; + * and the sequence is illegal right away if there aren't that many bytes + * available. + * If presented with a length > 4, this returns false. The Unicode + * definition of UTF-8 goes up to 4-byte sequences. + */ + +static ANTLR3_BOOLEAN +isLegalUTF8(const UTF8 *source, int length) { + UTF8 a; + const UTF8 *srcptr = source+length; + switch (length) { + default: return false; + /* Everything else falls through when "true"... */ + case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false; + case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false; + case 2: if ((a = (*--srcptr)) > 0xBF) return false; + + switch (*source) { + /* no fall-through in this inner switch */ + case 0xE0: if (a < 0xA0) return false; break; + case 0xED: if (a > 0x9F) return false; break; + case 0xF0: if (a < 0x90) return false; break; + case 0xF4: if (a > 0x8F) return false; break; + default: if (a < 0x80) return false; + } + + case 1: if (*source >= 0x80 && *source < 0xC2) return false; + } + if (*source > 0xF4) return false; + return true; +} + +/* --------------------------------------------------------------------- */ + +/* + * Exported function to return whether a UTF-8 sequence is legal or not. + * This is not used here; it's just exported. + */ +ANTLR3_BOOLEAN +isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd) { + int length = trailingBytesForUTF8[*source]+1; + if (source+length > sourceEnd) { + return false; + } + return isLegalUTF8(source, length); +} + +/* --------------------------------------------------------------------- */ + +ConversionResult ConvertUTF8toUTF16 ( + const UTF8** sourceStart, const UTF8* sourceEnd, + UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) { + ConversionResult result = conversionOK; + const UTF8* source = *sourceStart; + UTF16* target = *targetStart; + while (source < sourceEnd) { + UTF32 ch = 0; + unsigned short extraBytesToRead = trailingBytesForUTF8[*source]; + if (source + extraBytesToRead >= sourceEnd) { + result = sourceExhausted; break; + } + /* Do this check whether lenient or strict */ + if (! isLegalUTF8(source, extraBytesToRead+1)) { + result = sourceIllegal; + break; + } + /* + * The cases all fall through. See "Note A" below. + */ + switch (extraBytesToRead) { + case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */ + case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */ + case 3: ch += *source++; ch <<= 6; + case 2: ch += *source++; ch <<= 6; + case 1: ch += *source++; ch <<= 6; + case 0: ch += *source++; + } + ch -= offsetsFromUTF8[extraBytesToRead]; + + if (target >= targetEnd) { + source -= (extraBytesToRead+1); /* Back up source pointer! */ + result = targetExhausted; break; + } + if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */ + /* UTF-16 surrogate values are illegal in UTF-32 */ + if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { + if (flags == strictConversion) { + source -= (extraBytesToRead+1); /* return to the illegal value itself */ + result = sourceIllegal; + break; + } else { + *target++ = UNI_REPLACEMENT_CHAR; + } + } else { + *target++ = (UTF16)ch; /* normal case */ + } + } else if (ch > UNI_MAX_UTF16) { + if (flags == strictConversion) { + result = sourceIllegal; + source -= (extraBytesToRead+1); /* return to the start */ + break; /* Bail out; shouldn't continue */ + } else { + *target++ = UNI_REPLACEMENT_CHAR; + } + } else { + /* target is a character in range 0xFFFF - 0x10FFFF. */ + if (target + 1 >= targetEnd) { + source -= (extraBytesToRead+1); /* Back up source pointer! */ + result = targetExhausted; break; + } + ch -= halfBase; + *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START); + *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START); + } + } + *sourceStart = source; + *targetStart = target; + return result; +} + +/* --------------------------------------------------------------------- */ + +ConversionResult ConvertUTF32toUTF8 ( + const UTF32** sourceStart, const UTF32* sourceEnd, + UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) { + ConversionResult result = conversionOK; + const UTF32* source = *sourceStart; + UTF8* target = *targetStart; + while (source < sourceEnd) { + UTF32 ch; + unsigned short bytesToWrite = 0; + const UTF32 byteMask = 0xBF; + const UTF32 byteMark = 0x80; + ch = *source++; + if (flags == strictConversion ) { + /* UTF-16 surrogate values are illegal in UTF-32 */ + if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { + --source; /* return to the illegal value itself */ + result = sourceIllegal; + break; + } + } + /* + * Figure out how many bytes the result will require. Turn any + * illegally large UTF32 things (> Plane 17) into replacement chars. + */ + if (ch < (UTF32)0x80) { bytesToWrite = 1; + } else if (ch < (UTF32)0x800) { bytesToWrite = 2; + } else if (ch < (UTF32)0x10000) { bytesToWrite = 3; + } else if (ch <= UNI_MAX_LEGAL_UTF32) { bytesToWrite = 4; + } else { bytesToWrite = 3; + ch = UNI_REPLACEMENT_CHAR; + result = sourceIllegal; + } + + target += bytesToWrite; + if (target > targetEnd) { + --source; /* Back up source pointer! */ + target -= bytesToWrite; result = targetExhausted; break; + } + switch (bytesToWrite) { /* note: everything falls through. */ + case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; + case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; + case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; + case 1: *--target = (UTF8) (ch | firstByteMark[bytesToWrite]); + } + target += bytesToWrite; + } + *sourceStart = source; + *targetStart = target; + return result; +} + +/* --------------------------------------------------------------------- */ + +ConversionResult ConvertUTF8toUTF32 ( + const UTF8** sourceStart, const UTF8* sourceEnd, + UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) { + ConversionResult result = conversionOK; + const UTF8* source = *sourceStart; + UTF32* target = *targetStart; + while (source < sourceEnd) { + UTF32 ch = 0; + unsigned short extraBytesToRead = trailingBytesForUTF8[*source]; + if (source + extraBytesToRead >= sourceEnd) { + result = sourceExhausted; break; + } + /* Do this check whether lenient or strict */ + if (! isLegalUTF8(source, extraBytesToRead+1)) { + result = sourceIllegal; + break; + } + /* + * The cases all fall through. See "Note A" below. + */ + switch (extraBytesToRead) { + case 5: ch += *source++; ch <<= 6; + case 4: ch += *source++; ch <<= 6; + case 3: ch += *source++; ch <<= 6; + case 2: ch += *source++; ch <<= 6; + case 1: ch += *source++; ch <<= 6; + case 0: ch += *source++; + } + ch -= offsetsFromUTF8[extraBytesToRead]; + + if (target >= targetEnd) { + source -= (extraBytesToRead+1); /* Back up the source pointer! */ + result = targetExhausted; break; + } + if (ch <= UNI_MAX_LEGAL_UTF32) { + /* + * UTF-16 surrogate values are illegal in UTF-32, and anything + * over Plane 17 (> 0x10FFFF) is illegal. + */ + if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { + if (flags == strictConversion) { + source -= (extraBytesToRead+1); /* return to the illegal value itself */ + result = sourceIllegal; + break; + } else { + *target++ = UNI_REPLACEMENT_CHAR; + } + } else { + *target++ = ch; + } + } else { /* i.e., ch > UNI_MAX_LEGAL_UTF32 */ + result = sourceIllegal; + *target++ = UNI_REPLACEMENT_CHAR; + } + } + *sourceStart = source; + *targetStart = target; + return result; +} + +/* --------------------------------------------------------------------- + + Note A. + The fall-through switches in UTF-8 reading code save a + temp variable, some decrements & conditionals. The switches + are equivalent to the following loop: + { + int tmpBytesToRead = extraBytesToRead+1; + do { + ch += *source++; + --tmpBytesToRead; + if (tmpBytesToRead) ch <<= 6; + } while (tmpBytesToRead > 0); + } + In UTF-8 writing code, the switches on "bytesToWrite" are + similarly unrolled loops. + + --------------------------------------------------------------------- */ diff --git a/antlr-3.1.3/runtime/C/src/antlr3cyclicdfa.c b/antlr-3.1.3/runtime/C/src/antlr3cyclicdfa.c new file mode 100644 index 0000000..82e7222 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3cyclicdfa.c @@ -0,0 +1,204 @@ +/** Support functions for traversing cyclic DFA states as laid + * out in static initialized structures by the code generator. + * + * A DFA implemented as a set of transition tables. + * + * Any state that has a semantic predicate edge is special; those states + * are generated with if-then-else structures in a ->specialStateTransition() + * which is generated by cyclicDFA template. + * + * There are at most 32767 states (16-bit signed short). + * Could get away with byte sometimes but would have to generate different + * types and the simulation code too. For a point of reference, the Java + * lexer's Tokens rule DFA has 326 states roughly. + */ + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3defs.h> +#include <antlr3cyclicdfa.h> + +#ifdef ANTLR3_WINDOWS +#pragma warning( disable : 4100 ) +#endif + +static void +noViableAlt(pANTLR3_BASE_RECOGNIZER rec, pANTLR3_CYCLIC_DFA cdfa, ANTLR3_UINT32 s) +{ + // In backtracking mode, we just set the failed flag so that the + // alt can just exit right now. If we are parsing though, then + // we want the exception to be raised. + // + if (rec->state->backtracking > 0) + { + rec->state->failed = ANTLR3_TRUE; + } + else + { + rec->exConstruct(rec); + rec->state->exception->type = ANTLR3_NO_VIABLE_ALT_EXCEPTION; + rec->state->exception->message = cdfa->description; + rec->state->exception->decisionNum = cdfa->decisionNumber; + rec->state->exception->state = s; + } +} + +/** From the input stream, predict what alternative will succeed + * using this DFA (representing the covering regular approximation + * to the underlying CFL). Return an alternative number 1..n. Throw + * an exception upon error. + */ +ANTLR3_API ANTLR3_INT32 +antlr3dfapredict (void * ctx, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_INT_STREAM is, pANTLR3_CYCLIC_DFA cdfa) +{ + ANTLR3_MARKER mark; + ANTLR3_INT32 s; + ANTLR3_INT32 specialState; + ANTLR3_INT32 c; + + mark = is->mark(is); /* Store where we are right now */ + s = 0; /* Always start with state 0 */ + + for (;;) + { + /* Pick out any special state entry for this state + */ + specialState = cdfa->special[s]; + + /* Transition the special state and consume an input token + */ + if (specialState >= 0) + { + s = cdfa->specialStateTransition(ctx, rec, is, cdfa, specialState); + + // Error? + // + if (s<0) + { + // If the predicate/rule raised an exception then we leave it + // in tact, else we have an NVA. + // + if (rec->state->error != ANTLR3_TRUE) + { + noViableAlt(rec,cdfa, s); + } + is->rewind(is, mark); + return 0; + } + is->consume(is); + continue; + } + + /* Accept state? + */ + if (cdfa->accept[s] >= 1) + { + is->rewind(is, mark); + return cdfa->accept[s]; + } + + /* Look for a normal transition state based upon the input token element + */ + c = is->_LA(is, 1); + + /* Check against min and max for this state + */ + if (c>= cdfa->min[s] && c <= cdfa->max[s]) + { + ANTLR3_INT32 snext; + + /* What is the next state? + */ + snext = cdfa->transition[s][c - cdfa->min[s]]; + + if (snext < 0) + { + /* Was in range but not a normal transition + * must check EOT, which is like the else clause. + * eot[s]>=0 indicates that an EOT edge goes to another + * state. + */ + if (cdfa->eot[s] >= 0) + { + s = cdfa->eot[s]; + is->consume(is); + continue; + } + noViableAlt(rec,cdfa, s); + is->rewind(is, mark); + return 0; + } + + /* New current state - move to it + */ + s = snext; + is->consume(is); + continue; + } + /* EOT Transition? + */ + if (cdfa->eot[s] >= 0) + { + s = cdfa->eot[s]; + is->consume(is); + continue; + } + /* EOF transition to accept state? + */ + if ( c == ANTLR3_TOKEN_EOF && cdfa->eof[s] >= 0) + { + is->rewind(is, mark); + return cdfa->accept[cdfa->eof[s]]; + } + + /* No alt, so bomb + */ + noViableAlt(rec, cdfa, s); + is->rewind(is, mark); + return 0; + } + +} + +/** Default special state implementation + */ +ANTLR3_API ANTLR3_INT32 +antlr3dfaspecialStateTransition (void * ctx, pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM is, pANTLR3_CYCLIC_DFA dfa, ANTLR3_INT32 s) +{ + return -1; +} + +/* Default special transition implementation + */ +ANTLR3_API ANTLR3_INT32 +antlr3dfaspecialTransition (void * ctx, pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM is, pANTLR3_CYCLIC_DFA dfa, ANTLR3_INT32 s) +{ + return 0; +} diff --git a/antlr-3.1.3/runtime/C/src/antlr3debughandlers.c b/antlr-3.1.3/runtime/C/src/antlr3debughandlers.c new file mode 100644 index 0000000..14ca4bf --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3debughandlers.c @@ -0,0 +1,1047 @@ +/// \file +/// Provides the debugging functions invoked by a recognizer +/// built using the debug generator mode of the antlr tool. +/// See antlr3debugeventlistener.h for documentation. +/// + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3.h> + +// Not everyone wishes to include the debugger stuff in their final deployment because +// it will then rely on being linked with the socket libraries. Hence if the programmer turns +// off the debugging, we do some dummy stuff that satifies compilers etc but means there is +// no debugger and no reliance on the socket librarires. If you set this flag, then using the -debug +// option to generate your code will produce code that just crashes, but then I presme you are smart +// enough to realize that building the libraries without debugger support means you can't call the +// debugger ;-) +// +#ifdef ANTLR3_NODEBUGGER +ANTLR3_API pANTLR3_DEBUG_EVENT_LISTENER +antlr3DebugListenerNew() +{ + ANTLR3_PRINTF("C runtime was compiled without debugger support. This program will crash!!"); + return NULL; +} +#else + +static ANTLR3_BOOLEAN handshake (pANTLR3_DEBUG_EVENT_LISTENER delboy); +static void enterRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, const char * grammarFileName, const char * ruleName); +static void enterAlt (pANTLR3_DEBUG_EVENT_LISTENER delboy, int alt); +static void exitRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, const char * grammarFileName, const char * ruleName); +static void enterSubRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber); +static void exitSubRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber); +static void enterDecision (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber); +static void exitDecision (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber); +static void consumeToken (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_COMMON_TOKEN t); +static void consumeHiddenToken (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_COMMON_TOKEN t); +static void LT (pANTLR3_DEBUG_EVENT_LISTENER delboy, int i, pANTLR3_COMMON_TOKEN t); +static void mark (pANTLR3_DEBUG_EVENT_LISTENER delboy, ANTLR3_MARKER marker); +static void rewindMark (pANTLR3_DEBUG_EVENT_LISTENER delboy, ANTLR3_MARKER marker); +static void rewindLast (pANTLR3_DEBUG_EVENT_LISTENER delboy); +static void beginBacktrack (pANTLR3_DEBUG_EVENT_LISTENER delboy, int level); +static void endBacktrack (pANTLR3_DEBUG_EVENT_LISTENER delboy, int level, ANTLR3_BOOLEAN successful); +static void location (pANTLR3_DEBUG_EVENT_LISTENER delboy, int line, int pos); +static void recognitionException (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_EXCEPTION e); +static void beginResync (pANTLR3_DEBUG_EVENT_LISTENER delboy); +static void endResync (pANTLR3_DEBUG_EVENT_LISTENER delboy); +static void semanticPredicate (pANTLR3_DEBUG_EVENT_LISTENER delboy, ANTLR3_BOOLEAN result, const char * predicate); +static void commence (pANTLR3_DEBUG_EVENT_LISTENER delboy); +static void terminate (pANTLR3_DEBUG_EVENT_LISTENER delboy); +static void consumeNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t); +static void LTT (pANTLR3_DEBUG_EVENT_LISTENER delboy, int i, pANTLR3_BASE_TREE t); +static void nilNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t); +static void errorNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t); +static void createNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t); +static void createNodeTok (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE node, pANTLR3_COMMON_TOKEN token); +static void becomeRoot (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE newRoot, pANTLR3_BASE_TREE oldRoot); +static void addChild (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE root, pANTLR3_BASE_TREE child); +static void setTokenBoundaries (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t, ANTLR3_MARKER tokenStartIndex, ANTLR3_MARKER tokenStopIndex); +static void ack (pANTLR3_DEBUG_EVENT_LISTENER delboy); + +/// Create and initialize a new debug event listener that can be connected to +/// by ANTLRWorks and any other debugger via a socket. +/// +ANTLR3_API pANTLR3_DEBUG_EVENT_LISTENER +antlr3DebugListenerNew() +{ + pANTLR3_DEBUG_EVENT_LISTENER delboy; + + delboy = ANTLR3_CALLOC(1, sizeof(ANTLR3_DEBUG_EVENT_LISTENER)); + + if (delboy == NULL) + { + return NULL; + } + + // Initialize the API + // + delboy->addChild = addChild; + delboy->becomeRoot = becomeRoot; + delboy->beginBacktrack = beginBacktrack; + delboy->beginResync = beginResync; + delboy->commence = commence; + delboy->consumeHiddenToken = consumeHiddenToken; + delboy->consumeNode = consumeNode; + delboy->consumeToken = consumeToken; + delboy->createNode = createNode; + delboy->createNodeTok = createNodeTok; + delboy->endBacktrack = endBacktrack; + delboy->endResync = endResync; + delboy->enterAlt = enterAlt; + delboy->enterDecision = enterDecision; + delboy->enterRule = enterRule; + delboy->enterSubRule = enterSubRule; + delboy->exitDecision = exitDecision; + delboy->exitRule = exitRule; + delboy->exitSubRule = exitSubRule; + delboy->handshake = handshake; + delboy->location = location; + delboy->LT = LT; + delboy->LTT = LTT; + delboy->mark = mark; + delboy->nilNode = nilNode; + delboy->recognitionException = recognitionException; + delboy->rewind = rewindMark; + delboy->rewindLast = rewindLast; + delboy->semanticPredicate = semanticPredicate; + delboy->setTokenBoundaries = setTokenBoundaries; + delboy->terminate = terminate; + delboy->errorNode = errorNode; + + delboy->PROTOCOL_VERSION = 2; // ANTLR 3.1 is at protocol version 2 + + delboy->port = DEFAULT_DEBUGGER_PORT; + + return delboy; +} + +pANTLR3_DEBUG_EVENT_LISTENER +antlr3DebugListenerNewPort(ANTLR3_UINT32 port) +{ + pANTLR3_DEBUG_EVENT_LISTENER delboy; + + delboy = antlr3DebugListenerNew(); + + if (delboy != NULL) + { + delboy->port = port; + } + + return delboy; +} + +//-------------------------------------------------------------------------------- +// Support functions for sending stuff over the socket interface +// +static int +sockSend(SOCKET sock, const char * ptr, int len) +{ + int sent; + int thisSend; + + sent = 0; + + while (sent < len) + { + // Send as many bytes as we can + // + thisSend = send(sock, ptr, len - sent, 0); + + // Check for errors and tell the user if we got one + // + if (thisSend == -1) + { + return ANTLR3_FALSE; + } + + // Increment our offset by how many we were able to send + // + ptr += thisSend; + sent += thisSend; + } + return ANTLR3_TRUE; +} + +static ANTLR3_BOOLEAN +handshake (pANTLR3_DEBUG_EVENT_LISTENER delboy) +{ + /// Connection structure with which to wait and accept a connection from + /// a debugger. + /// + SOCKET serverSocket; + + // Connection structures to deal with the client after we accept the connection + // and the server while we accept a connection. + // + ANTLR3_SOCKADDRT client; + ANTLR3_SOCKADDRT server; + + // Buffer to construct our message in + // + char message[256]; + + // Specifies the length of the connection structure to accept() + // Windows use int, everyone else uses size_t + // + ANTLR3_SALENT sockaddr_len; + + // Option holder for setsockopt() + // + int optVal; + + if (delboy->initialized == ANTLR3_FALSE) + { + // Windows requires us to initialize WinSock. + // +#ifdef ANTLR3_WINDOWS + { + WORD wVersionRequested; + WSADATA wsaData; + int err; // Return code from WSAStartup + + // We must initialise the Windows socket system when the DLL is loaded. + // We are asking for Winsock 1.1 or better as we don't need anything + // too complicated for this. + // + wVersionRequested = MAKEWORD( 1, 1); + + err = WSAStartup( wVersionRequested, &wsaData ); + + if ( err != 0 ) + { + // Tell the user that we could not find a usable + // WinSock DLL + // + return FALSE; + } + } +#endif + + // Create the server socket, we are the server because we just wait until + // a debugger connects to the port we are listening on. + // + serverSocket = socket(AF_INET, SOCK_STREAM, 0); + + if (serverSocket == INVALID_SOCKET) + { + return ANTLR3_FALSE; + } + + // Set the listening port + // + server.sin_port = htons((unsigned short)delboy->port); + server.sin_family = AF_INET; + server.sin_addr.s_addr = htonl (INADDR_ANY); + + // We could allow a rebind on the same addr/port pair I suppose, but + // I imagine that most people will just want to start debugging one parser at once. + // Maybe change this at some point, but rejecting the bind at this point will ensure + // that people realize they have left something running in the background. + // + if (bind(serverSocket, (pANTLR3_SOCKADDRC)&server, sizeof(server)) == -1) + { + return ANTLR3_FALSE; + } + + // We have bound the socket to the port and address so we now ask the TCP subsystem + // to start listening on that address/port + // + if (listen(serverSocket, 1) == -1) + { + // Some error, just fail + // + return ANTLR3_FALSE; + } + + // Now we can try to accept a connection on the port + // + sockaddr_len = sizeof(client); + delboy->socket = accept(serverSocket, (pANTLR3_SOCKADDRC)&client, &sockaddr_len); + + // Having accepted a connection, we can stop listening and close down the socket + // + shutdown (serverSocket, 0x02); + ANTLR3_CLOSESOCKET (serverSocket); + + if (delboy->socket == -1) + { + return ANTLR3_FALSE; + } + + // Disable Nagle as this is essentially a chat exchange + // + optVal = 1; + setsockopt(delboy->socket, SOL_SOCKET, TCP_NODELAY, (const void *)&optVal, sizeof(optVal)); + + } + + // We now have a good socket connection with the debugging client, so we + // send it the protocol version we are using and what the name of the grammar + // is that we represent. + // + sprintf (message, "ANTLR %d\n", delboy->PROTOCOL_VERSION); + sockSend (delboy->socket, message, (int)strlen(message)); + sprintf (message, "grammar \"%s\n", delboy->grammarFileName->chars); + sockSend (delboy->socket, message, (int)strlen(message)); + ack (delboy); + + delboy->initialized = ANTLR3_TRUE; + + return ANTLR3_TRUE; +} + +// Send the supplied text and wait for an ack from the client +static void +transmit(pANTLR3_DEBUG_EVENT_LISTENER delboy, const char * ptr) +{ + sockSend(delboy->socket, ptr, (int)strlen(ptr)); + ack(delboy); +} + +static void +ack (pANTLR3_DEBUG_EVENT_LISTENER delboy) +{ + // Local buffer to read the next character in to + // + char buffer; + int rCount; + + // Ack terminates in a line feed, so we just wait for + // one of those. Speed is not of the essence so we don't need + // to buffer the input or anything. + // + do + { + rCount = recv(delboy->socket, &buffer, 1, 0); + } + while (rCount == 1 && buffer != '\n'); + + // If the socket ws closed on us, then we will get an error or + // (with a graceful close), 0. We can assume the the debugger stopped for some reason + // (such as Java crashing again). Therefore we just exit the program + // completely if we don't get the terminating '\n' for the ack. + // + if (rCount != 1) + { + ANTLR3_PRINTF("Exiting debugger as remote client closed the socket\n"); + ANTLR3_PRINTF("Received char count was %d, and last char received was %02X\n", rCount, buffer); + exit(0); + } +} + +// Given a buffer string and a source string, serialize the +// text, escaping any newlines and linefeeds. We have no need +// for speed here, this is the debugger. +// +void +serializeText(pANTLR3_STRING buffer, pANTLR3_STRING text) +{ + ANTLR3_UINT32 c; + ANTLR3_UCHAR character; + + // strings lead in with a " + // + buffer->append(buffer, " \""); + + if (text == NULL) + { + return; + } + + // Now we replace linefeeds, newlines and the escape + // leadin character '%' with their hex equivalents + // prefixed by '%' + // + for (c = 0; c < text->len; c++) + { + switch (character = text->charAt(text, c)) + { + case '\n': + + buffer->append(buffer, "%0A"); + break; + + case '\r': + + buffer->append(buffer, "%0D"); + break; + + case '\\': + + buffer->append(buffer, "%25"); + break; + + // Other characters: The Song Remains the Same. + // + default: + + buffer->addc(buffer, character); + break; + } + } +} + +// Given a token, create a stringified version of it, in the supplied +// buffer. We create a string for this in the debug 'object', if there +// is not one there already, and then reuse it here if asked to do this +// again. +// +pANTLR3_STRING +serializeToken(pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_COMMON_TOKEN t) +{ + // Do we already have a serialization buffer? + // + if (delboy->tokenString == NULL) + { + // No, so create one, using the string factory that + // the grammar name used, which is guaranteed to exist. + // 64 bytes will do us here for starters. + // + delboy->tokenString = delboy->grammarFileName->factory->newSize(delboy->grammarFileName->factory, 64); + } + + // Empty string + // + delboy->tokenString->set(delboy->tokenString, (const char *)""); + + // Now we serialize the elements of the token.Note that the debugger only + // uses 32 bits. + // + delboy->tokenString->addi(delboy->tokenString, (ANTLR3_INT32)(t->getTokenIndex(t))); + delboy->tokenString->addc(delboy->tokenString, ' '); + delboy->tokenString->addi(delboy->tokenString, (ANTLR3_INT32)(t->getType(t))); + delboy->tokenString->addc(delboy->tokenString, ' '); + delboy->tokenString->addi(delboy->tokenString, (ANTLR3_INT32)(t->getChannel(t))); + delboy->tokenString->addc(delboy->tokenString, ' '); + delboy->tokenString->addi(delboy->tokenString, (ANTLR3_INT32)(t->getLine(t))); + delboy->tokenString->addc(delboy->tokenString, ' '); + delboy->tokenString->addi(delboy->tokenString, (ANTLR3_INT32)(t->getCharPositionInLine(t))); + + // Now send the text that the token represents. + // + serializeText(delboy->tokenString, t->getText(t)); + + // Finally, as the debugger is a Java program it will expect to get UTF-8 + // encoded strings. We don't use UTF-8 internally to the C runtime, so we + // must force encode it. We have a method to do this in the string class, but + // it returns malloc space that we must free afterwards. + // + return delboy->tokenString->toUTF8(delboy->tokenString); +} + +// Given a tree node, create a stringified version of it in the supplied +// buffer. +// +pANTLR3_STRING +serializeNode(pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE node) +{ + pANTLR3_COMMON_TOKEN token; + + + // Do we already have a serialization buffer? + // + if (delboy->tokenString == NULL) + { + // No, so create one, using the string factory that + // the grammar name used, which is guaranteed to exist. + // 64 bytes will do us here for starters. + // + delboy->tokenString = delboy->grammarFileName->factory->newSize(delboy->grammarFileName->factory, 64); + } + + // Empty string + // + delboy->tokenString->set(delboy->tokenString, (const char *)""); + + // Protect against bugs/errors etc + // + if (node == NULL) + { + return delboy->tokenString; + } + + // Now we serialize the elements of the node.Note that the debugger only + // uses 32 bits. + // + delboy->tokenString->addc(delboy->tokenString, ' '); + + // Adaptor ID + // + delboy->tokenString->addi(delboy->tokenString, delboy->adaptor->getUniqueID(delboy->adaptor, node)); + delboy->tokenString->addc(delboy->tokenString, ' '); + + // Type of the current token (which may be imaginary) + // + delboy->tokenString->addi(delboy->tokenString, delboy->adaptor->getType(delboy->adaptor, node)); + + // See if we have an actual token or just an imaginary + // + token = delboy->adaptor->getToken(delboy->adaptor, node); + + delboy->tokenString->addc(delboy->tokenString, ' '); + if (token != NULL) + { + // Real token + // + delboy->tokenString->addi(delboy->tokenString, (ANTLR3_INT32)(token->getLine(token))); + delboy->tokenString->addc(delboy->tokenString, ' '); + delboy->tokenString->addi(delboy->tokenString, (ANTLR3_INT32)(token->getCharPositionInLine(token))); + } + else + { + // Imaginary tokens have no location + // + delboy->tokenString->addi(delboy->tokenString, -1); + delboy->tokenString->addc(delboy->tokenString, ' '); + delboy->tokenString->addi(delboy->tokenString, -1); + } + + // Start Index of the node + // + delboy->tokenString->addc(delboy->tokenString, ' '); + delboy->tokenString->addi(delboy->tokenString, (ANTLR3_UINT32)(delboy->adaptor->getTokenStartIndex(delboy->adaptor, node))); + + // Now send the text that the node represents. + // + serializeText(delboy->tokenString, delboy->adaptor->getText(delboy->adaptor, node)); + + // Finally, as the debugger is a Java program it will expect to get UTF-8 + // encoded strings. We don't use UTF-8 internally to the C runtime, so we + // must force encode it. We have a method to do this in the string class, but + // there is no utf8 string implementation as of yet + // + return delboy->tokenString->toUTF8(delboy->tokenString); +} + +//------------------------------------------------------------------------------------------------------------------ +// EVENTS +// +static void +enterRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, const char * grammarFileName, const char * ruleName) +{ + char buffer[512]; + + // Create the message (speed is not of the essence) + // + sprintf(buffer, "enterRule %s %s\n", grammarFileName, ruleName); + transmit(delboy, buffer); +} + +static void +enterAlt (pANTLR3_DEBUG_EVENT_LISTENER delboy, int alt) +{ + char buffer[512]; + + // Create the message (speed is not of the essence) + // + sprintf(buffer, "enterAlt %d\n", alt); + transmit(delboy, buffer); +} + +static void +exitRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, const char * grammarFileName, const char * ruleName) +{ + char buffer[512]; + + // Create the message (speed is not of the essence) + // + sprintf(buffer, "enterRule %s %s\n", grammarFileName, ruleName); + transmit(delboy, buffer); +} + +static void +enterSubRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber) +{ + char buffer[512]; + + // Create the message (speed is not of the essence) + // + sprintf(buffer, "enterSubRule %d\n", decisionNumber); + transmit(delboy, buffer); +} + +static void +exitSubRule (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber) +{ + char buffer[512]; + + // Create the message (speed is not of the essence) + // + sprintf(buffer, "exitSubRule %d\n", decisionNumber); + transmit(delboy, buffer); +} + +static void +enterDecision (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber) +{ + char buffer[512]; + + // Create the message (speed is not of the essence) + // + sprintf(buffer, "enterDecision %d\n", decisionNumber); + transmit(delboy, buffer); + +} + +static void +exitDecision (pANTLR3_DEBUG_EVENT_LISTENER delboy, int decisionNumber) +{ + char buffer[512]; + + // Create the message (speed is not of the essence) + // + sprintf(buffer, "exitDecision %d\n", decisionNumber); + transmit(delboy, buffer); +} + +static void +consumeToken (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_COMMON_TOKEN t) +{ + pANTLR3_STRING msg; + + // Create the serialized token + // + msg = serializeToken(delboy, t); + + // Insert the debug event indicator + // + msg->insert8(msg, 0, "consumeToken "); + + msg->addc(msg, '\n'); + + // Transmit the message and wait for ack + // + transmit(delboy, (const char *)(msg->chars)); +} + +static void +consumeHiddenToken (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_COMMON_TOKEN t) +{ + pANTLR3_STRING msg; + + // Create the serialized token + // + msg = serializeToken(delboy, t); + + // Insert the debug event indicator + // + msg->insert8(msg, 0, "consumeHiddenToken "); + + msg->addc(msg, '\n'); + + // Transmit the message and wait for ack + // + transmit(delboy, (const char *)(msg->chars)); +} + +// Looking at the next token event. +// +static void +LT (pANTLR3_DEBUG_EVENT_LISTENER delboy, int i, pANTLR3_COMMON_TOKEN t) +{ + pANTLR3_STRING msg; + + if (t != NULL) + { + // Create the serialized token + // + msg = serializeToken(delboy, t); + + // Insert the index parameter + // + msg->insert8(msg, 0, " "); + msg->inserti(msg, 0, i); + + // Insert the debug event indicator + // + msg->insert8(msg, 0, "LT "); + + msg->addc(msg, '\n'); + + // Transmit the message and wait for ack + // + transmit(delboy, (const char *)(msg->chars)); + } +} + +static void +mark (pANTLR3_DEBUG_EVENT_LISTENER delboy, ANTLR3_MARKER marker) +{ + char buffer[128]; + + sprintf(buffer, "mark %d\n", (ANTLR3_UINT32)(marker & 0xFFFFFFFF)); + + // Transmit the message and wait for ack + // + transmit(delboy, buffer); +} + +static void +rewindMark (pANTLR3_DEBUG_EVENT_LISTENER delboy, ANTLR3_MARKER marker) +{ + char buffer[128]; + + sprintf(buffer, "rewind %d\n", (ANTLR3_UINT32)(marker & 0xFFFFFFFF)); + + // Transmit the message and wait for ack + // + transmit(delboy, buffer); + +} + +static void +rewindLast (pANTLR3_DEBUG_EVENT_LISTENER delboy) +{ + transmit(delboy, (const char *)"rewind\n"); +} + +static void +beginBacktrack (pANTLR3_DEBUG_EVENT_LISTENER delboy, int level) +{ + char buffer[128]; + + sprintf(buffer, "beginBacktrack %d\n", (ANTLR3_UINT32)(level & 0xFFFFFFFF)); + + // Transmit the message and wait for ack + // + transmit(delboy, buffer); +} + +static void +endBacktrack (pANTLR3_DEBUG_EVENT_LISTENER delboy, int level, ANTLR3_BOOLEAN successful) +{ + char buffer[128]; + + sprintf(buffer, "endBacktrack %d %d\n", level, successful); + + // Transmit the message and wait for ack + // + transmit(delboy, buffer); +} + +static void +location (pANTLR3_DEBUG_EVENT_LISTENER delboy, int line, int pos) +{ + char buffer[128]; + + sprintf(buffer, "location %d %d\n", line, pos); + + // Transmit the message and wait for ack + // + transmit(delboy, buffer); +} + +static void +recognitionException (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_EXCEPTION e) +{ + char buffer[256]; + + sprintf(buffer, "exception %s %d %d %d\n", (char *)(e->name), (ANTLR3_INT32)(e->index), e->line, e->charPositionInLine); + + // Transmit the message and wait for ack + // + transmit(delboy, buffer); +} + +static void +beginResync (pANTLR3_DEBUG_EVENT_LISTENER delboy) +{ + transmit(delboy, (const char *)"beginResync\n"); +} + +static void +endResync (pANTLR3_DEBUG_EVENT_LISTENER delboy) +{ + transmit(delboy, (const char *)"endResync\n"); +} + +static void +semanticPredicate (pANTLR3_DEBUG_EVENT_LISTENER delboy, ANTLR3_BOOLEAN result, const char * predicate) +{ + unsigned char * buffer; + unsigned char * out; + + if (predicate != NULL) + { + buffer = (unsigned char *)ANTLR3_MALLOC(64 + 2*strlen(predicate)); + + if (buffer != NULL) + { + out = buffer + sprintf((char *)buffer, "semanticPredicate %s ", result == ANTLR3_TRUE ? "true" : "false"); + + while (*predicate != '\0') + { + switch(*predicate) + { + case '\n': + + *out++ = '%'; + *out++ = '0'; + *out++ = 'A'; + break; + + case '\r': + + *out++ = '%'; + *out++ = '0'; + *out++ = 'D'; + break; + + case '%': + + *out++ = '%'; + *out++ = '0'; + *out++ = 'D'; + break; + + + default: + + *out++ = *predicate; + break; + } + + predicate++; + } + *out++ = '\n'; + *out++ = '\0'; + } + + // Send it and wait for the ack + // + transmit(delboy, (const char *)buffer); + } +} + +#ifdef ANTLR3_WINDOWS +#pragma warning (push) +#pragma warning (disable : 4100) +#endif + +static void +commence (pANTLR3_DEBUG_EVENT_LISTENER delboy) +{ + // Nothing to see here + // +} + +#ifdef ANTLR3_WINDOWS +#pragma warning (pop) +#endif + +static void +terminate (pANTLR3_DEBUG_EVENT_LISTENER delboy) +{ + // Terminate sequence + // + sockSend(delboy->socket, "terminate\n", 10); // Send out the command +} + +//---------------------------------------------------------------- +// Tree parsing events +// +static void +consumeNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t) +{ + pANTLR3_STRING buffer; + + buffer = serializeNode (delboy, t); + + // Now prepend the command + // + buffer->insert8 (buffer, 0, "consumeNode "); + buffer->addc (buffer, '\n'); + + // Send to the debugger and wait for the ack + // + transmit (delboy, (const char *)(delboy->tokenString->toUTF8(delboy->tokenString)->chars)); +} + +static void +LTT (pANTLR3_DEBUG_EVENT_LISTENER delboy, int i, pANTLR3_BASE_TREE t) +{ + pANTLR3_STRING buffer; + + buffer = serializeNode (delboy, t); + + // Now prepend the command + // + buffer->insert8 (buffer, 0, " "); + buffer->inserti (buffer, 0, i); + buffer->insert8 (buffer, 0, "LN "); + buffer->addc (buffer, '\n'); + + // Send to the debugger and wait for the ack + // + transmit (delboy, (const char *)(delboy->tokenString->toUTF8(delboy->tokenString)->chars)); +} + +static void +nilNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t) +{ + char buffer[128]; + sprintf(buffer, "nilNode %d\n", delboy->adaptor->getUniqueID(delboy->adaptor, t)); + transmit(delboy, buffer); +} + +static void +createNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t) +{ + // Do we already have a serialization buffer? + // + if (delboy->tokenString == NULL) + { + // No, so create one, using the string factory that + // the grammar name used, which is guaranteed to exist. + // 64 bytes will do us here for starters. + // + delboy->tokenString = delboy->grammarFileName->factory->newSize(delboy->grammarFileName->factory, 64); + } + + // Empty string + // + delboy->tokenString->set8(delboy->tokenString, (const char *)"createNodeFromTokenElements "); + + // Now we serialize the elements of the node.Note that the debugger only + // uses 32 bits. + // + // Adaptor ID + // + delboy->tokenString->addi(delboy->tokenString, delboy->adaptor->getUniqueID(delboy->adaptor, t)); + delboy->tokenString->addc(delboy->tokenString, ' '); + + // Type of the current token (which may be imaginary) + // + delboy->tokenString->addi(delboy->tokenString, delboy->adaptor->getType(delboy->adaptor, t)); + + // The text that this node represents + // + serializeText(delboy->tokenString, delboy->adaptor->getText(delboy->adaptor, t)); + delboy->tokenString->addc(delboy->tokenString, '\n'); + + // Finally, as the debugger is a Java program it will expect to get UTF-8 + // encoded strings. We don't use UTF-8 internally to the C runtime, so we + // must force encode it. We have a method to do this in the string class, but + // there is no utf8 string implementation as of yet + // + transmit(delboy, (const char *)(delboy->tokenString->toUTF8(delboy->tokenString)->chars)); + +} +static void +errorNode (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t) +{ + // Do we already have a serialization buffer? + // + if (delboy->tokenString == NULL) + { + // No, so create one, using the string factory that + // the grammar name used, which is guaranteed to exist. + // 64 bytes will do us here for starters. + // + delboy->tokenString = delboy->grammarFileName->factory->newSize(delboy->grammarFileName->factory, 64); + } + + // Empty string + // + delboy->tokenString->set8(delboy->tokenString, (const char *)"errorNode "); + + // Now we serialize the elements of the node.Note that the debugger only + // uses 32 bits. + // + // Adaptor ID + // + delboy->tokenString->addi(delboy->tokenString, delboy->adaptor->getUniqueID(delboy->adaptor, t)); + delboy->tokenString->addc(delboy->tokenString, ' '); + + // Type of the current token (which is an error) + // + delboy->tokenString->addi(delboy->tokenString, ANTLR3_TOKEN_INVALID); + + // The text that this node represents + // + serializeText(delboy->tokenString, delboy->adaptor->getText(delboy->adaptor, t)); + delboy->tokenString->addc(delboy->tokenString, '\n'); + + // Finally, as the debugger is a Java program it will expect to get UTF-8 + // encoded strings. We don't use UTF-8 internally to the C runtime, so we + // must force encode it. We have a method to do this in the string class, but + // there is no utf8 string implementation as of yet + // + transmit(delboy, (const char *)(delboy->tokenString->toUTF8(delboy->tokenString)->chars)); + +} + +static void +createNodeTok (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE node, pANTLR3_COMMON_TOKEN token) +{ + char buffer[128]; + + sprintf(buffer, "createNode %d %d\n", delboy->adaptor->getUniqueID(delboy->adaptor, node), (ANTLR3_UINT32)token->getTokenIndex(token)); + + transmit(delboy, buffer); +} + +static void +becomeRoot (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE newRoot, pANTLR3_BASE_TREE oldRoot) +{ + char buffer[128]; + + sprintf(buffer, "becomeRoot %d %d\n", delboy->adaptor->getUniqueID(delboy->adaptor, newRoot), + delboy->adaptor->getUniqueID(delboy->adaptor, oldRoot) + ); + transmit(delboy, buffer); +} + + +static void +addChild (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE root, pANTLR3_BASE_TREE child) +{ + char buffer[128]; + + sprintf(buffer, "addChild %d %d\n", delboy->adaptor->getUniqueID(delboy->adaptor, root), + delboy->adaptor->getUniqueID(delboy->adaptor, child) + ); + transmit(delboy, buffer); +} + +static void +setTokenBoundaries (pANTLR3_DEBUG_EVENT_LISTENER delboy, pANTLR3_BASE_TREE t, ANTLR3_MARKER tokenStartIndex, ANTLR3_MARKER tokenStopIndex) +{ + char buffer[128]; + + sprintf(buffer, "becomeRoot %d %d %d\n", delboy->adaptor->getUniqueID(delboy->adaptor, t), + (ANTLR3_UINT32)tokenStartIndex, + (ANTLR3_UINT32)tokenStopIndex + ); + transmit(delboy, buffer); +} +#endif + diff --git a/antlr-3.1.3/runtime/C/src/antlr3encodings.c b/antlr-3.1.3/runtime/C/src/antlr3encodings.c new file mode 100644 index 0000000..2187426 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3encodings.c @@ -0,0 +1,50 @@ +/** \File + * Provides basic utility functions to convert between + * the various Unicode character conversions. There are of + * course various packages that could be used instead of these + * functions, but then the Antlr 3 C runtime would be dependant + * on the particular package. Using ICU for this is a good idea if + * your project is already dependant on it. + */ + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3.h> + +/// Convert 8 bit character to ANTLR char form. +/// +/// \param[in] inc Input character to transform in 8 bit ASCII form. +/// \return ANTLR3_UCHAR encoding of the character. +/// +ANTLR3_API +ANTLR3_UCHAR antlr3c8toAntlrc(ANTLR3_INT8 inc) +{ + return (ANTLR3_UCHAR)(inc); +} diff --git a/antlr-3.1.3/runtime/C/src/antlr3exception.c b/antlr-3.1.3/runtime/C/src/antlr3exception.c new file mode 100644 index 0000000..339721c --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3exception.c @@ -0,0 +1,190 @@ +/** \file + * Contains default functions for creating and destroying as well as + * otherwise handling ANTLR3 standard exception structures. + */ + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3exception.h> + +static void antlr3ExceptionPrint(pANTLR3_EXCEPTION ex); +static void antlr3ExceptionFree (pANTLR3_EXCEPTION ex); + +/** + * \brief + * Creates a new ANTLR3 exception structure + * + * \param[in] exception + * One of the ANTLR3_xxx_EXCEPTION indicators such as #ANTLR3_RECOGNITION_EXCEPTION + * + * \param[in] message + * Pointer to message string + * + * \param[in] freeMessage + * Set to ANTLR3_TRUE if the message parameter should be freed by a call to + * ANTLR3_FREE() when the exception is destroyed. + * + * \returns + * Pointer to newly initialized exception structure, or an ANTLR3_ERR_xx defined value + * upon failure. + * + * An exception is 'thrown' by a recognizer when input is seen that is not predicted by + * the grammar productions or when some other error condition occurs. In C we do not have + * the luxury of try and catch blocks, so exceptions are added in the order they occur to + * a list in the baserecognizer structure. The last one to be thrown is inserted at the head of + * the list and the one currently installed is pointed to by the newly installed exception. + * + * \remarks + * After an exception is created, you may add a pointer to your own structure and a pointer + * to a function to free this structure when the exception is destroyed. + * + * \see + * ANTLR3_EXCEPTION + */ +pANTLR3_EXCEPTION +antlr3ExceptionNew(ANTLR3_UINT32 exception, void * name, void * message, ANTLR3_BOOLEAN freeMessage) +{ + pANTLR3_EXCEPTION ex; + + /* Allocate memory for the structure + */ + ex = (pANTLR3_EXCEPTION) ANTLR3_CALLOC(1, sizeof(ANTLR3_EXCEPTION)); + + /* Check for memory allocation + */ + if (ex == NULL) + { + return NULL; + } + + ex->name = name; /* Install exception name */ + ex->type = exception; /* Install the exception number */ + ex->message = message; /* Install message string */ + + /* Indicate whether the string should be freed if exception is destroyed + */ + ex->freeMessage = freeMessage; + + /* Install the API + */ + ex->print = antlr3ExceptionPrint; + ex->freeEx = antlr3ExceptionFree; + + return ex; +} + +/** + * \brief + * Prints out the message in all the exceptions in the supplied chain. + * + * \param[in] ex + * Pointer to the exception structure to print. + * + * \remarks + * You may wish to override this function by installing a pointer to a new function + * in the base recognizer context structure. + * + * \see + * ANTLR3_BASE_RECOGNIZER + */ +static void +antlr3ExceptionPrint(pANTLR3_EXCEPTION ex) +{ + /* Ensure valid pointer + */ + while (ex != NULL) + { + /* Number if no message, else the message + */ + if (ex->message == NULL) + { + ANTLR3_FPRINTF(stderr, "ANTLR3_EXCEPTION number %d (%08X).\n", ex->type, ex->type); + } + else + { + ANTLR3_FPRINTF(stderr, "ANTLR3_EXCEPTION: %s\n", (char *)(ex->message)); + } + + /* Move to next in the chain (if any) + */ + ex = ex->nextException; + } + + return; +} + +/** + * \brief + * Frees up a chain of ANTLR3 exceptions + * + * \param[in] ex + * Pointer to the first exception in the chain to free. + * + * \see + * ANTLR3_EXCEPTION + */ +static void +antlr3ExceptionFree(pANTLR3_EXCEPTION ex) +{ + pANTLR3_EXCEPTION next; + + /* Ensure valid pointer + */ + while (ex != NULL) + { + /* Pick up anythign following now, before we free the + * current memory block. + */ + next = ex->nextException; + + /* Free the message pointer if advised to + */ + if (ex->freeMessage == ANTLR3_TRUE) + { + ANTLR3_FREE(ex->message); + } + + /* Call the programmer's custom free routine if advised to + */ + if (ex->freeCustom != NULL) + { + ex->freeCustom(ex->custom); + } + + /* Free the actual structure itself + */ + ANTLR3_FREE(ex); + + ex = next; + } + + return; +} + diff --git a/antlr-3.1.3/runtime/C/src/antlr3filestream.c b/antlr-3.1.3/runtime/C/src/antlr3filestream.c new file mode 100644 index 0000000..5c1eecc --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3filestream.c @@ -0,0 +1,183 @@ +/** \file + * \brief The ANTLR3 C filestream is used when the source character stream + * is a filesystem based input set and all the characters in the filestream + * can be loaded at once into memory and away the lexer goes. + * + * A number of initializers are provided in order that various character + * sets can be supported from input files. The ANTLR3 C runtime expects + * to deal with UTF32 characters only (the reasons for this are to + * do with the simplification of C code when using this form of Unicode + * encoding, though this is not a panacea. More information can be + * found on this by consulting: + * - http://www.unicode.org/versions/Unicode4.0.0/ch02.pdf#G11178 + * Where a well grounded discussion of the encoding formats available + * may be found. + * + */ + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3.h> + + +/** \brief Use the contents of an operating system file as the input + * for an input stream. + * + * \param fileName Name of operating system file to read. + * \return + * - Pointer to new input stream context upon success + * - One of the ANTLR3_ERR_ defines on error. + */ +ANTLR3_API pANTLR3_INPUT_STREAM +antlr3AsciiFileStreamNew(pANTLR3_UINT8 fileName) +{ + // Pointer to the input stream we are going to create + // + pANTLR3_INPUT_STREAM input; + ANTLR3_UINT32 status; + + if (fileName == NULL) + { + return NULL; + } + + // Allocate memory for the input stream structure + // + input = (pANTLR3_INPUT_STREAM) + ANTLR3_CALLOC(1, sizeof(ANTLR3_INPUT_STREAM)); + + if (input == NULL) + { + return NULL; + } + + // Structure was allocated correctly, now we can read the file. + // + status = antlr3readAscii(input, fileName); + + // Call the common 8 bit ASCII input stream handler + // Initializer type thingy doobry function. + // + antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM); + + // Now we can set up the file name + // + input->istream->streamName = input->strFactory->newStr(input->strFactory, fileName); + input->fileName = input->istream->streamName; + + if (status != ANTLR3_SUCCESS) + { + input->close(input); + return NULL; + } + + return input; +} + +ANTLR3_API ANTLR3_UINT32 +antlr3readAscii(pANTLR3_INPUT_STREAM input, pANTLR3_UINT8 fileName) +{ + ANTLR3_FDSC infile; + ANTLR3_UINT32 fSize; + + /* Open the OS file in read binary mode + */ + infile = antlr3Fopen(fileName, "rb"); + + /* Check that it was there + */ + if (infile == NULL) + { + return (ANTLR3_UINT32)ANTLR3_ERR_NOFILE; + } + + /* It was there, so we can read the bytes now + */ + fSize = antlr3Fsize(fileName); /* Size of input file */ + + /* Allocate buffer for this input set + */ + input->data = ANTLR3_MALLOC((size_t)fSize); + input->sizeBuf = fSize; + + if (input->data == NULL) + { + return (ANTLR3_UINT32)ANTLR3_ERR_NOMEM; + } + + input->isAllocated = ANTLR3_TRUE; + + /* Now we read the file. Characters are not converted to + * the internal ANTLR encoding until they are read from the buffer + */ + antlr3Fread(infile, fSize, input->data); + + /* And close the file handle + */ + antlr3Fclose(infile); + + return ANTLR3_SUCCESS; +} + +/** \brief Open an operating system file and return the descriptor + * We just use the common open() and related functions here. + * Later we might find better ways on systems + * such as Windows and OpenVMS for instance. But the idea is to read the + * while file at once anyway, so it may be irrelevant. + */ +ANTLR3_API ANTLR3_FDSC +antlr3Fopen(pANTLR3_UINT8 filename, const char * mode) +{ + return (ANTLR3_FDSC)fopen((const char *)filename, mode); +} + +/** \brief Close an operating system file and free any handles + * etc. + */ +ANTLR3_API void +antlr3Fclose(ANTLR3_FDSC fd) +{ + fclose(fd); +} +ANTLR3_API ANTLR3_UINT32 +antlr3Fsize(pANTLR3_UINT8 fileName) +{ + struct _stat statbuf; + + _stat((const char *)fileName, &statbuf); + + return (ANTLR3_UINT32)statbuf.st_size; +} + +ANTLR3_API ANTLR3_UINT32 +antlr3Fread(ANTLR3_FDSC fdsc, ANTLR3_UINT32 count, void * data) +{ + return (ANTLR3_UINT32)fread(data, (size_t)count, 1, fdsc); +} diff --git a/antlr-3.1.3/runtime/C/src/antlr3inputstream.c b/antlr-3.1.3/runtime/C/src/antlr3inputstream.c new file mode 100644 index 0000000..6595cfb --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3inputstream.c @@ -0,0 +1,619 @@ +/// \file +/// Base functions to initialize and manipulate any input stream +/// + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3input.h> + + +// INT Stream API +// +static void antlr3AsciiConsume (pANTLR3_INT_STREAM is); +static ANTLR3_UCHAR antlr3AsciiLA (pANTLR3_INT_STREAM is, ANTLR3_INT32 la); +static ANTLR3_UCHAR antlr3AsciiLA_ucase (pANTLR3_INT_STREAM is, ANTLR3_INT32 la); +static ANTLR3_MARKER antlr3AsciiIndex (pANTLR3_INT_STREAM is); +static ANTLR3_MARKER antlr3AsciiMark (pANTLR3_INT_STREAM is); +static void antlr3AsciiRewind (pANTLR3_INT_STREAM is, ANTLR3_MARKER mark); +static void antlr3AsciiRewindLast (pANTLR3_INT_STREAM is); +static void antlr3AsciiRelease (pANTLR3_INT_STREAM is, ANTLR3_MARKER mark); +static void antlr3AsciiSeek (pANTLR3_INT_STREAM is, ANTLR3_MARKER seekPoint); +static pANTLR3_STRING antlr3AsciiGetSourceName (pANTLR3_INT_STREAM is); + +// ASCII Charstream API functions +// +static void antlr3InputClose (pANTLR3_INPUT_STREAM input); +static void antlr3InputReset (pANTLR3_INPUT_STREAM input); +static void * antlr3AsciiLT (pANTLR3_INPUT_STREAM input, ANTLR3_INT32 lt); +static ANTLR3_UINT32 antlr3AsciiSize (pANTLR3_INPUT_STREAM input); +static pANTLR3_STRING antlr3AsciiSubstr (pANTLR3_INPUT_STREAM input, ANTLR3_MARKER start, ANTLR3_MARKER stop); +static ANTLR3_UINT32 antlr3AsciiGetLine (pANTLR3_INPUT_STREAM input); +static void * antlr3AsciiGetLineBuf (pANTLR3_INPUT_STREAM input); +static ANTLR3_UINT32 antlr3AsciiGetCharPosition (pANTLR3_INPUT_STREAM input); +static void antlr3AsciiSetLine (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 line); +static void antlr3AsciiSetCharPosition (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 position); +static void antlr3AsciiSetNewLineChar (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 newlineChar); +static void antlr3AsciiSetUcaseLA (pANTLR3_INPUT_STREAM input, ANTLR3_BOOLEAN flag); + +/// \brief Common function to setup function interface for an 8 bit ASCII input stream. +/// +/// \param input Input stream context pointer +/// +/// \remark +/// - Many of the 8 bit ASCII oriented file stream handling functions will be usable +/// by any or at least some other input streams. Therefore it is perfectly acceptable +/// to call this function to install the ASCII handler then override just those functions +/// that would not work for the particular input encoding, such as consume for instance. +/// +void +antlr3AsciiSetupStream (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 type) +{ + // Build a string factory for this stream + // + input->strFactory = antlr3StringFactoryNew(); + + // Default stream set up is for ASCII, therefore there is nothing else + // to do but set it up as such + // + antlr3GenericSetupStream(input, type); +} + + +void +antlr3GenericSetupStream (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 type) +{ + + /* Install function pointers for an 8 bit ASCII input + */ + + /* Allocate stream interface + */ + input->istream = antlr3IntStreamNew(); + input->istream->type = ANTLR3_CHARSTREAM; + input->istream->super = input; + + input->istream->type = type; + + /* Intstream API + */ + input->istream->consume = antlr3AsciiConsume; /* Consume the next 8 bit character in the buffer */ + input->istream->_LA = antlr3AsciiLA; /* Return the UTF32 character at offset n (1 based) */ + input->istream->index = antlr3AsciiIndex; /* Current index (offset from first character */ + input->istream->mark = antlr3AsciiMark; /* Record the current lex state for later restore */ + input->istream->rewind = antlr3AsciiRewind; /* How to rewind the input */ + input->istream->rewindLast = antlr3AsciiRewindLast; /* How to rewind the input */ + input->istream->seek = antlr3AsciiSeek; /* How to seek to a specific point in the stream */ + input->istream->release = antlr3AsciiRelease; /* Reset marks after mark n */ + input->istream->getSourceName = antlr3AsciiGetSourceName; // Return a string that names the input source + + /* Charstream API + */ + input->close = antlr3InputClose; /* Close down the stream completely */ + input->free = antlr3InputClose; /* Synonym for free */ + input->reset = antlr3InputReset; /* Reset input to start */ + input->_LT = antlr3AsciiLT; /* Same as _LA for 8 bit Ascii file */ + input->size = antlr3AsciiSize; /* Return the size of the input buffer */ + input->substr = antlr3AsciiSubstr; /* Return a string from the input stream */ + input->getLine = antlr3AsciiGetLine; /* Return the current line number in the input stream */ + input->getLineBuf = antlr3AsciiGetLineBuf; /* Return a pointer to the start of the current line being consumed */ + input->getCharPositionInLine = antlr3AsciiGetCharPosition; /* Return the offset into the current line of input */ + input->setLine = antlr3AsciiSetLine; /* Set the input stream line number (does not set buffer pointers) */ + input->setCharPositionInLine = antlr3AsciiSetCharPosition; /* Set the offset in to the current line (does not set any pointers ) */ + input->SetNewLineChar = antlr3AsciiSetNewLineChar; /* Set the value of the newline trigger character */ + input->setUcaseLA = antlr3AsciiSetUcaseLA; + + input->charByteSize = 1; // Size in bytes of characters in this stream. + + /* Initialize entries for tables etc + */ + input->markers = NULL; + + /* Set up the input stream brand new + */ + input->reset(input); + + /* Install default line separator character (it can be replaced + * by the grammar programmer later) + */ + input->SetNewLineChar(input, (ANTLR3_UCHAR)'\n'); +} + +static pANTLR3_STRING +antlr3AsciiGetSourceName(pANTLR3_INT_STREAM is) +{ + return is->streamName; +} + +/** \brief Close down an input stream and free any memory allocated by it. + * + * \param input Input stream context pointer + */ +static void +antlr3InputClose(pANTLR3_INPUT_STREAM input) +{ + // Close any markers in the input stream + // + if (input->markers != NULL) + { + input->markers->free(input->markers); + input->markers = NULL; + } + + // Close the string factory + // + if (input->strFactory != NULL) + { + input->strFactory->close(input->strFactory); + } + + // Free the input stream buffer if we allocated it + // + if (input->isAllocated && input->data != NULL) + { + ANTLR3_FREE(input->data); + input->data = NULL; + } + + input->istream->free(input->istream); + + // Finally, free the space for the structure itself + // + ANTLR3_FREE(input); + + // Done + // +} + +static void +antlr3AsciiSetUcaseLA (pANTLR3_INPUT_STREAM input, ANTLR3_BOOLEAN flag) +{ + if (flag) + { + // Return the upper case version of the characters + // + input->istream->_LA = antlr3AsciiLA_ucase; + } + else + { + // Return the raw characters as they are in the buffer + // + input->istream->_LA = antlr3AsciiLA; + } +} + + +/** \brief Reset a re-startable input stream to the start + * + * \param input Input stream context pointer + */ +static void +antlr3InputReset(pANTLR3_INPUT_STREAM input) +{ + + input->nextChar = input->data; /* Input at first character */ + input->line = 1; /* starts at line 1 */ + input->charPositionInLine = -1; + input->currentLine = input->data; + input->markDepth = 0; /* Reset markers */ + + /* Free up the markers table if it is there + */ + if (input->markers != NULL) + { + input->markers->free(input->markers); + } + + /* Install a new markers table + */ + input->markers = antlr3VectorNew(0); +} + +/** \brief Consume the next character in an 8 bit ASCII input stream + * + * \param input Input stream context pointer + */ +static void +antlr3AsciiConsume(pANTLR3_INT_STREAM is) +{ + pANTLR3_INPUT_STREAM input; + + input = ((pANTLR3_INPUT_STREAM) (is->super)); + + if ((pANTLR3_UINT8)(input->nextChar) < (((pANTLR3_UINT8)input->data) + input->sizeBuf)) + { + /* Indicate one more character in this line + */ + input->charPositionInLine++; + + if ((ANTLR3_UCHAR)(*((pANTLR3_UINT8)input->nextChar)) == input->newlineChar) + { + /* Reset for start of a new line of input + */ + input->line++; + input->charPositionInLine = 0; + input->currentLine = (void *)(((pANTLR3_UINT8)input->nextChar) + 1); + } + + /* Increment to next character position + */ + input->nextChar = (void *)(((pANTLR3_UINT8)input->nextChar) + 1); + } +} + +/** \brief Return the input element assuming an 8 bit ascii input + * + * \param[in] input Input stream context pointer + * \param[in] la 1 based offset of next input stream element + * + * \return Next input character in internal ANTLR3 encoding (UTF32) + */ +static ANTLR3_UCHAR +antlr3AsciiLA(pANTLR3_INT_STREAM is, ANTLR3_INT32 la) +{ + pANTLR3_INPUT_STREAM input; + + input = ((pANTLR3_INPUT_STREAM) (is->super)); + + if (( ((pANTLR3_UINT8)input->nextChar) + la - 1) >= (((pANTLR3_UINT8)input->data) + input->sizeBuf)) + { + return ANTLR3_CHARSTREAM_EOF; + } + else + { + return (ANTLR3_UCHAR)(*((pANTLR3_UINT8)input->nextChar + la - 1)); + } +} + +/** \brief Return the input element assuming an 8 bit ASCII input and + * always return the UPPER CASE character. + * Note that this is 8 bit and so we assume that the toupper + * function will use the correct locale for 8 bits. + * + * \param[in] input Input stream context pointer + * \param[in] la 1 based offset of next input stream element + * + * \return Next input character in internal ANTLR3 encoding (UTF32) + */ +static ANTLR3_UCHAR +antlr3AsciiLA_ucase (pANTLR3_INT_STREAM is, ANTLR3_INT32 la) +{ + pANTLR3_INPUT_STREAM input; + + input = ((pANTLR3_INPUT_STREAM) (is->super)); + + if (( ((pANTLR3_UINT8)input->nextChar) + la - 1) >= (((pANTLR3_UINT8)input->data) + input->sizeBuf)) + { + return ANTLR3_CHARSTREAM_EOF; + } + else + { + return (ANTLR3_UCHAR)toupper((*((pANTLR3_UINT8)input->nextChar + la - 1))); + } +} + + +/** \brief Return the input element assuming an 8 bit ascii input + * + * \param[in] input Input stream context pointer + * \param[in] lt 1 based offset of next input stream element + * + * \return Next input character in internal ANTLR3 encoding (UTF32) + */ +static void * +antlr3AsciiLT(pANTLR3_INPUT_STREAM input, ANTLR3_INT32 lt) +{ + /* Casting is horrible but it means no warnings and LT should never be called + * on a character stream anyway I think. If it is then, the void * will need to be + * cast back in a similar manner. Yuck! But this means that LT for Token streams and + * tree streams is correct. + */ + return (ANTLR3_FUNC_PTR(input->istream->_LA(input->istream, lt))); +} + +/** \brief Calculate the current index in the output stream. + * \param[in] input Input stream context pointer + */ +static ANTLR3_MARKER +antlr3AsciiIndex(pANTLR3_INT_STREAM is) +{ + pANTLR3_INPUT_STREAM input; + + input = ((pANTLR3_INPUT_STREAM) (is->super)); + + return (ANTLR3_MARKER)(((pANTLR3_UINT8)input->nextChar)); +} + +/** \brief Return the size of the current input stream, as an Ascii file + * which in this case is the total input. Other implementations may provide + * more sophisticated implementations to deal with non-recoverable streams + * and so on. + * + * \param[in] input Input stream context pointer + */ +static ANTLR3_UINT32 +antlr3AsciiSize(pANTLR3_INPUT_STREAM input) +{ + return input->sizeBuf; +} + +/** \brief Mark the current input point in an Ascii 8 bit stream + * such as a file stream, where all the input is available in the + * buffer. + * + * \param[in] is Input stream context pointer + */ +static ANTLR3_MARKER +antlr3AsciiMark (pANTLR3_INT_STREAM is) +{ + pANTLR3_LEX_STATE state; + pANTLR3_INPUT_STREAM input; + + input = ((pANTLR3_INPUT_STREAM) (is->super)); + + /* New mark point + */ + input->markDepth++; + + /* See if we are revisiting a mark as we can just reuse the vector + * entry if we are, otherwise, we need a new one + */ + if (input->markDepth > input->markers->count) + { + state = ANTLR3_MALLOC(sizeof(ANTLR3_LEX_STATE)); + + /* Add it to the table + */ + input->markers->add(input->markers, state, ANTLR3_FREE_FUNC); /* No special structure, just free() on delete */ + } + else + { + state = (pANTLR3_LEX_STATE)input->markers->get(input->markers, input->markDepth - 1); + + /* Assume no errors for speed, it will just blow up if the table failed + * for some reasons, hence lots of unit tests on the tables ;-) + */ + } + + /* We have created or retrieved the state, so update it with the current + * elements of the lexer state. + */ + state->charPositionInLine = input->charPositionInLine; + state->currentLine = input->currentLine; + state->line = input->line; + state->nextChar = input->nextChar; + + is->lastMarker = input->markDepth; + + /* And that's it + */ + return input->markDepth; +} +/** \brief Rewind the lexer input to the state specified by the last produced mark. + * + * \param[in] input Input stream context pointer + * + * \remark + * Assumes ASCII (or at least, 8 Bit) input stream. + */ +static void +antlr3AsciiRewindLast (pANTLR3_INT_STREAM is) +{ + is->rewind(is, is->lastMarker); +} + +/** \brief Rewind the lexer input to the state specified by the supplied mark. + * + * \param[in] input Input stream context pointer + * + * \remark + * Assumes ASCII (or at least, 8 Bit) input stream. + */ +static void +antlr3AsciiRewind (pANTLR3_INT_STREAM is, ANTLR3_MARKER mark) +{ + pANTLR3_LEX_STATE state; + pANTLR3_INPUT_STREAM input; + + input = ((pANTLR3_INPUT_STREAM) is->super); + + /* Perform any clean up of the marks + */ + input->istream->release(input->istream, mark); + + /* Find the supplied mark state + */ + state = (pANTLR3_LEX_STATE)input->markers->get(input->markers, (ANTLR3_UINT32)(mark - 1)); + + /* Seek input pointer to the requested point (note we supply the void *pointer + * to whatever is implementing the int stream to seek). + */ + antlr3AsciiSeek(is, (ANTLR3_MARKER)(state->nextChar)); + + /* Reset to the reset of the information in the mark + */ + input->charPositionInLine = state->charPositionInLine; + input->currentLine = state->currentLine; + input->line = state->line; + input->nextChar = state->nextChar; + + /* And we are done + */ +} + +/** \brief Rewind the lexer input to the state specified by the supplied mark. + * + * \param[in] input Input stream context pointer + * + * \remark + * Assumes ASCII (or at least, 8 Bit) input stream. + */ +static void +antlr3AsciiRelease (pANTLR3_INT_STREAM is, ANTLR3_MARKER mark) +{ + pANTLR3_INPUT_STREAM input; + + input = ((pANTLR3_INPUT_STREAM) (is->super)); + + /* We don't do much here in fact as we never free any higher marks in + * the hashtable as we just resuse any memory allocated for them. + */ + input->markDepth = (ANTLR3_UINT32)(mark - 1); +} + +/** \brief Rewind the lexer input to the state specified by the supplied mark. + * + * \param[in] input Input stream context pointer + * + * \remark + * Assumes ASCII (or at least, 8 Bit) input stream. + */ +static void +antlr3AsciiSeek (pANTLR3_INT_STREAM is, ANTLR3_MARKER seekPoint) +{ + ANTLR3_INT32 count; + pANTLR3_INPUT_STREAM input; + + input = ANTLR3_FUNC_PTR(((pANTLR3_INPUT_STREAM) is->super)); + + /* If the requested seek point is less than the current + * input point, then we assume that we are resetting from a mark + * and do not need to scan, but can just set to there. + */ + if (seekPoint <= (ANTLR3_MARKER)(input->nextChar)) + { + input->nextChar = ((pANTLR3_UINT8) seekPoint); + } + else + { + count = (ANTLR3_UINT32)(seekPoint - (ANTLR3_MARKER)(input->nextChar)); + + while (count--) + { + is->consume(is); + } + } +} +/** Return a substring of the ASCII (8 bit) input stream in + * newly allocated memory. + * + * \param input Input stream context pointer + * \param start Offset in input stream where the string starts + * \param stop Offset in the input stream where the string ends. + */ +static pANTLR3_STRING +antlr3AsciiSubstr (pANTLR3_INPUT_STREAM input, ANTLR3_MARKER start, ANTLR3_MARKER stop) +{ + return input->strFactory->newPtr(input->strFactory, (pANTLR3_UINT8)start, (ANTLR3_UINT32)(stop - start + 1)); +} + +/** \brief Return the line number as understood by the 8 bit/ASCII input stream. + * + * \param input Input stream context pointer + * \return Line number in input stream that we believe we are working on. + */ +static ANTLR3_UINT32 +antlr3AsciiGetLine (pANTLR3_INPUT_STREAM input) +{ + return input->line; +} + +/** Return a pointer into the input stream that points at the start + * of the current input line as triggered by the end of line character installed + * for the stream ('\n' unless told differently). + * + * \param[in] input + */ +static void * +antlr3AsciiGetLineBuf (pANTLR3_INPUT_STREAM input) +{ + return input->currentLine; +} + +/** Return the current offset in to the current line in the input stream. + * + * \param input Input stream context pointer + * \return Current line offset + */ +static ANTLR3_UINT32 +antlr3AsciiGetCharPosition (pANTLR3_INPUT_STREAM input) +{ + return input->charPositionInLine; +} + +/** Set the current line number as understood by the input stream. + * + * \param input Input stream context pointer + * \param line Line number to tell the input stream we are on + * + * \remark + * This function does not change any pointers, it just allows the programmer to set the + * line number according to some external criterion, such as finding a lexed directive + * like: #nnn "file.c" for instance, such that error reporting and so on in is in sync + * with some original source format. + */ +static void +antlr3AsciiSetLine (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 line) +{ + input->line = line; +} + +/** Set the current offset in the current line to be a particular setting. + * + * \param[in] input Input stream context pointer + * \param[in] position New setting for current offset. + * + * \remark + * This does not set the actual pointers in the input stream, it is purely for reporting + * purposes and so on as per antlr3AsciiSetLine(); + */ +static void +antlr3AsciiSetCharPosition (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 position) +{ + input->charPositionInLine = position; +} + +/** Set the newline trigger character in the input stream to the supplied parameter. + * + * \param[in] input Input stream context pointer + * \param[in] newlineChar Character to set to be the newline trigger. + * + * \remark + * - The supplied newLineChar is in UTF32 encoding (which means ASCII and latin1 etc + * are the same encodings), but the input stream catered to by this function is 8 bit + * only, so it is up to the programmer to ensure that the character supplied is valid. + */ +static void +antlr3AsciiSetNewLineChar (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 newlineChar) +{ + input->newlineChar = newlineChar; +} + + + diff --git a/antlr-3.1.3/runtime/C/src/antlr3intstream.c b/antlr-3.1.3/runtime/C/src/antlr3intstream.c new file mode 100644 index 0000000..b9d49d8 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3intstream.c @@ -0,0 +1,66 @@ +/// \file +/// Implementation of superclass elements of an ANTLR3 int stream. +/// The only methods required are an allocator and a destructor. +/// \addtogroup pANTLR3_INT_STREAM +/// @{ + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3intstream.h> + +static void freeStream (pANTLR3_INT_STREAM stream); + +ANTLR3_API pANTLR3_INT_STREAM +antlr3IntStreamNew() +{ + pANTLR3_INT_STREAM stream; + + // Allocate memory + // + stream = (pANTLR3_INT_STREAM) ANTLR3_CALLOC(1, sizeof(ANTLR3_INT_STREAM)); + + if (stream == NULL) + { + return NULL; + } + + stream->free = freeStream; + + return stream; +} + +static void +freeStream (pANTLR3_INT_STREAM stream) +{ + ANTLR3_FREE(stream); +} + +/// @} +/// diff --git a/antlr-3.1.3/runtime/C/src/antlr3lexer.c b/antlr-3.1.3/runtime/C/src/antlr3lexer.c new file mode 100644 index 0000000..3b82480 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3lexer.c @@ -0,0 +1,880 @@ +/** \file + * + * Base implementation of an antlr 3 lexer. + * + * An ANTLR3 lexer implements a base recongizer, a token source and + * a lexer interface. It constructs a base recognizer with default + * functions, then overrides any of these that are parser specific (usual + * default implementation of base recognizer. + */ + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3lexer.h> + +static void mTokens (pANTLR3_LEXER lexer); +static void setCharStream (pANTLR3_LEXER lexer, pANTLR3_INPUT_STREAM input); +static void pushCharStream (pANTLR3_LEXER lexer, pANTLR3_INPUT_STREAM input); +static void popCharStream (pANTLR3_LEXER lexer); + +static void emitNew (pANTLR3_LEXER lexer, pANTLR3_COMMON_TOKEN token); +static pANTLR3_COMMON_TOKEN emit (pANTLR3_LEXER lexer); +static ANTLR3_BOOLEAN matchs (pANTLR3_LEXER lexer, ANTLR3_UCHAR * string); +static ANTLR3_BOOLEAN matchc (pANTLR3_LEXER lexer, ANTLR3_UCHAR c); +static ANTLR3_BOOLEAN matchRange (pANTLR3_LEXER lexer, ANTLR3_UCHAR low, ANTLR3_UCHAR high); +static void matchAny (pANTLR3_LEXER lexer); +static void recover (pANTLR3_LEXER lexer); +static ANTLR3_UINT32 getLine (pANTLR3_LEXER lexer); +static ANTLR3_MARKER getCharIndex (pANTLR3_LEXER lexer); +static ANTLR3_UINT32 getCharPositionInLine (pANTLR3_LEXER lexer); +static pANTLR3_STRING getText (pANTLR3_LEXER lexer); +static pANTLR3_COMMON_TOKEN nextToken (pANTLR3_TOKEN_SOURCE toksource); + +static void displayRecognitionError (pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 * tokenNames); +static void reportError (pANTLR3_BASE_RECOGNIZER rec); +static void * getCurrentInputSymbol (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM istream); +static void * getMissingSymbol (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM istream, pANTLR3_EXCEPTION e, + ANTLR3_UINT32 expectedTokenType, pANTLR3_BITSET_LIST follow); + +static void reset (pANTLR3_BASE_RECOGNIZER rec); + +static void freeLexer (pANTLR3_LEXER lexer); + + +ANTLR3_API pANTLR3_LEXER +antlr3LexerNew(ANTLR3_UINT32 sizeHint, pANTLR3_RECOGNIZER_SHARED_STATE state) +{ + pANTLR3_LEXER lexer; + pANTLR3_COMMON_TOKEN specialT; + + /* Allocate memory + */ + lexer = (pANTLR3_LEXER) ANTLR3_MALLOC(sizeof(ANTLR3_LEXER)); + + if (lexer == NULL) + { + return NULL; + } + + /* Now we need to create the base recognizer + */ + lexer->rec = antlr3BaseRecognizerNew(ANTLR3_TYPE_LEXER, sizeHint, state); + + if (lexer->rec == NULL) + { + lexer->free(lexer); + return NULL; + } + lexer->rec->super = lexer; + + lexer->rec->displayRecognitionError = displayRecognitionError; + lexer->rec->reportError = reportError; + lexer->rec->reset = reset; + lexer->rec->getCurrentInputSymbol = getCurrentInputSymbol; + lexer->rec->getMissingSymbol = getMissingSymbol; + + /* Now install the token source interface + */ + if (lexer->rec->state->tokSource == NULL) + { + lexer->rec->state->tokSource = (pANTLR3_TOKEN_SOURCE)ANTLR3_MALLOC(sizeof(ANTLR3_TOKEN_SOURCE)); + + if (lexer->rec->state->tokSource == NULL) + { + lexer->rec->free(lexer->rec); + lexer->free(lexer); + + return NULL; + } + lexer->rec->state->tokSource->super = lexer; + + /* Install the default nextToken() method, which may be overridden + * by generated code, or by anything else in fact. + */ + lexer->rec->state->tokSource->nextToken = nextToken; + lexer->rec->state->tokSource->strFactory = NULL; + + lexer->rec->state->tokFactory = NULL; + } + + /* Install the lexer API + */ + lexer->setCharStream = setCharStream; + lexer->mTokens = (void (*)(void *))(mTokens); + lexer->setCharStream = setCharStream; + lexer->pushCharStream = pushCharStream; + lexer->popCharStream = popCharStream; + lexer->emit = emit; + lexer->emitNew = emitNew; + lexer->matchs = matchs; + lexer->matchc = matchc; + lexer->matchRange = matchRange; + lexer->matchAny = matchAny; + lexer->recover = recover; + lexer->getLine = getLine; + lexer->getCharIndex = getCharIndex; + lexer->getCharPositionInLine = getCharPositionInLine; + lexer->getText = getText; + lexer->free = freeLexer; + + /* Initialise the eof token + */ + specialT = &(lexer->rec->state->tokSource->eofToken); + antlr3SetTokenAPI (specialT); + specialT->setType (specialT, ANTLR3_TOKEN_EOF); + specialT->factoryMade = ANTLR3_TRUE; // Prevent things trying to free() it + specialT->strFactory = NULL; + + // Initialize the skip token. + // + specialT = &(lexer->rec->state->tokSource->skipToken); + antlr3SetTokenAPI (specialT); + specialT->setType (specialT, ANTLR3_TOKEN_INVALID); + specialT->factoryMade = ANTLR3_TRUE; // Prevent things trying to free() it + specialT->strFactory = NULL; + return lexer; +} + +static void +reset (pANTLR3_BASE_RECOGNIZER rec) +{ + pANTLR3_LEXER lexer; + + lexer = rec->super; + + lexer->rec->state->token = NULL; + lexer->rec->state->type = ANTLR3_TOKEN_INVALID; + lexer->rec->state->channel = ANTLR3_TOKEN_DEFAULT_CHANNEL; + lexer->rec->state->tokenStartCharIndex = -1; + lexer->rec->state->tokenStartCharPositionInLine = -1; + lexer->rec->state->tokenStartLine = -1; + + lexer->rec->state->text = NULL; + + if (lexer->input != NULL) + { + lexer->input->istream->seek(lexer->input->istream, 0); + } +} + +/// +/// \brief +/// Returns the next available token from the current input stream. +/// +/// \param toksource +/// Points to the implementation of a token source. The lexer is +/// addressed by the super structure pointer. +/// +/// \returns +/// The next token in the current input stream or the EOF token +/// if there are no more tokens. +/// +/// \remarks +/// Write remarks for nextToken here. +/// +/// \see nextToken +/// +ANTLR3_INLINE static pANTLR3_COMMON_TOKEN +nextTokenStr (pANTLR3_TOKEN_SOURCE toksource) +{ + pANTLR3_LEXER lexer; + + lexer = (pANTLR3_LEXER)(toksource->super); + + /// Loop until we get a non skipped token or EOF + /// + for (;;) + { + // Get rid of any previous token (token factory takes care of + // any de-allocation when this token is finally used up. + // + lexer->rec->state->token = NULL; + lexer->rec->state->error = ANTLR3_FALSE; // Start out without an exception + lexer->rec->state->failed = ANTLR3_FALSE; + + + + // Now call the matching rules and see if we can generate a new token + // + for (;;) + { + // Record the start of the token in our input stream. + // + lexer->rec->state->channel = ANTLR3_TOKEN_DEFAULT_CHANNEL; + lexer->rec->state->tokenStartCharIndex = lexer->input->istream->index(lexer->input->istream); + lexer->rec->state->tokenStartCharPositionInLine = lexer->input->getCharPositionInLine(lexer->input); + lexer->rec->state->tokenStartLine = lexer->input->getLine(lexer->input); + lexer->rec->state->text = NULL; + + if (lexer->input->istream->_LA(lexer->input->istream, 1) == ANTLR3_CHARSTREAM_EOF) + { + // Reached the end of the current stream, nothing more to do if this is + // the last in the stack. + // + pANTLR3_COMMON_TOKEN teof = &(toksource->eofToken); + + teof->setStartIndex (teof, lexer->getCharIndex(lexer)); + teof->setStopIndex (teof, lexer->getCharIndex(lexer)); + teof->setLine (teof, lexer->getLine(lexer)); + teof->factoryMade = ANTLR3_TRUE; // This isn't really manufactured but it stops things from trying to free it + return teof; + } + + lexer->rec->state->token = NULL; + lexer->rec->state->error = ANTLR3_FALSE; // Start out without an exception + lexer->rec->state->failed = ANTLR3_FALSE; + + // Call the generated lexer, see if it can get a new token together. + // + lexer->mTokens(lexer->ctx); + + if (lexer->rec->state->error == ANTLR3_TRUE) + { + // Recognition exception, report it and try to recover. + // + lexer->rec->state->failed = ANTLR3_TRUE; + lexer->rec->reportError(lexer->rec); + lexer->recover(lexer); + } + else + { + if (lexer->rec->state->token == NULL) + { + // Emit the real token, which adds it in to the token stream basically + // + emit(lexer); + } + else if (lexer->rec->state->token == &(toksource->skipToken)) + { + // A real token could have been generated, but "Computer say's naaaaah" and it + // it is just something we need to skip altogether. + // + continue; + } + + // Good token, not skipped, not EOF token + // + return lexer->rec->state->token; + } + } + } +} + +/** + * \brief + * Default implementation of the nextToken() call for a lexer. + * + * \param toksource + * Points to the implementation of a token source. The lexer is + * addressed by the super structure pointer. + * + * \returns + * The next token in the current input stream or the EOF token + * if there are no more tokens in any input stream in the stack. + * + * Write detailed description for nextToken here. + * + * \remarks + * Write remarks for nextToken here. + * + * \see nextTokenStr + */ +static pANTLR3_COMMON_TOKEN +nextToken (pANTLR3_TOKEN_SOURCE toksource) +{ + pANTLR3_COMMON_TOKEN tok; + + // Find the next token in the current stream + // + tok = nextTokenStr(toksource); + + // If we got to the EOF token then switch to the previous + // input stream if there were any and just return the + // EOF if there are none. We must check the next token + // in any outstanding input stream we pop into the active + // role to see if it was sitting at EOF after PUSHing the + // stream we just consumed, otherwise we will return EOF + // on the reinstalled input stream, when in actual fact + // there might be more input streams to POP before the + // real EOF of the whole logical inptu stream. Hence we + // use a while loop here until we find somethign in the stream + // that isn't EOF or we reach the actual end of the last input + // stream on the stack. + // + while (tok->type == ANTLR3_TOKEN_EOF) + { + pANTLR3_LEXER lexer; + + lexer = (pANTLR3_LEXER)(toksource->super); + + if (lexer->rec->state->streams != NULL && lexer->rec->state->streams->size(lexer->rec->state->streams) > 0) + { + // We have another input stream in the stack so we + // need to revert to it, then resume the loop to check + // it wasn't sitting at EOF itself. + // + lexer->popCharStream(lexer); + tok = nextTokenStr(toksource); + } + else + { + // There were no more streams on the input stack + // so this EOF is the 'real' logical EOF for + // the input stream. So we just exit the loop and + // return the EOF we have found. + // + break; + } + + } + + // return whatever token we have, which may be EOF + // + return tok; +} + +ANTLR3_API pANTLR3_LEXER +antlr3LexerNewStream(ANTLR3_UINT32 sizeHint, pANTLR3_INPUT_STREAM input, pANTLR3_RECOGNIZER_SHARED_STATE state) +{ + pANTLR3_LEXER lexer; + + // Create a basic lexer first + // + lexer = antlr3LexerNew(sizeHint, state); + + if (lexer != NULL) + { + // Install the input stream and reset the lexer + // + setCharStream(lexer, input); + } + + return lexer; +} + +static void mTokens (pANTLR3_LEXER lexer) +{ + if (lexer) // Fool compiler, avoid pragmas + { + ANTLR3_FPRINTF(stderr, "lexer->mTokens(): Error: No lexer rules were added to the lexer yet!\n"); + } +} + +static void +reportError (pANTLR3_BASE_RECOGNIZER rec) +{ + rec->displayRecognitionError(rec, rec->state->tokenNames); +} + +#ifdef ANTLR3_WINDOWS +#pragma warning( disable : 4100 ) +#endif + +/** Default lexer error handler (works for 8 bit streams only!!!) + */ +static void +displayRecognitionError (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_UINT8 * tokenNames) +{ + pANTLR3_LEXER lexer; + pANTLR3_EXCEPTION ex; + pANTLR3_STRING ftext; + + lexer = (pANTLR3_LEXER)(recognizer->super); + ex = lexer->rec->state->exception; + + // See if there is a 'filename' we can use + // + if (ex->name == NULL) + { + ANTLR3_FPRINTF(stderr, "-unknown source-("); + } + else + { + ftext = ex->streamName->to8(ex->streamName); + ANTLR3_FPRINTF(stderr, "%s(", ftext->chars); + } + + ANTLR3_FPRINTF(stderr, "%d) ", recognizer->state->exception->line); + ANTLR3_FPRINTF(stderr, ": lexer error %d :\n\t%s at offset %d, ", + ex->type, + (pANTLR3_UINT8) (ex->message), + ex->charPositionInLine+1 + ); + { + ANTLR3_INT32 width; + + width = ANTLR3_UINT32_CAST(( (pANTLR3_UINT8)(lexer->input->data) + (lexer->input->size(lexer->input) )) - (pANTLR3_UINT8)(ex->index)); + + if (width >= 1) + { + if (isprint(ex->c)) + { + ANTLR3_FPRINTF(stderr, "near '%c' :\n", ex->c); + } + else + { + ANTLR3_FPRINTF(stderr, "near char(%#02X) :\n", (ANTLR3_UINT8)(ex->c)); + } + ANTLR3_FPRINTF(stderr, "\t%.*s\n", width > 20 ? 20 : width ,((pANTLR3_UINT8)ex->index)); + } + else + { + ANTLR3_FPRINTF(stderr, "(end of input).\n\t This indicates a poorly specified lexer RULE\n\t or unterminated input element such as: \"STRING[\"]\n"); + ANTLR3_FPRINTF(stderr, "\t The lexer was matching from line %d, offset %d, which\n\t ", + (ANTLR3_UINT32)(lexer->rec->state->tokenStartLine), + (ANTLR3_UINT32)(lexer->rec->state->tokenStartCharPositionInLine) + ); + width = ANTLR3_UINT32_CAST(((pANTLR3_UINT8)(lexer->input->data)+(lexer->input->size(lexer->input))) - (pANTLR3_UINT8)(lexer->rec->state->tokenStartCharIndex)); + + if (width >= 1) + { + ANTLR3_FPRINTF(stderr, "looks like this:\n\t\t%.*s\n", width > 20 ? 20 : width ,(pANTLR3_UINT8)(lexer->rec->state->tokenStartCharIndex)); + } + else + { + ANTLR3_FPRINTF(stderr, "is also the end of the line, so you must check your lexer rules\n"); + } + } + } +} + +static void setCharStream (pANTLR3_LEXER lexer, pANTLR3_INPUT_STREAM input) +{ + /* Install the input interface + */ + lexer->input = input; + + /* We may need a token factory for the lexer; we don't destroy any existing factory + * until the lexer is destroyed, as people may still be using the tokens it produced. + * TODO: Later I will provide a dup() method for a token so that it can extract itself + * out of the factory. + */ + if (lexer->rec->state->tokFactory == NULL) + { + lexer->rec->state->tokFactory = antlr3TokenFactoryNew(input); + } + else + { + /* When the input stream is being changed on the fly, rather than + * at the start of a new lexer, then we must tell the tokenFactory + * which input stream to adorn the tokens with so that when they + * are asked to provide their original input strings they can + * do so from the correct text stream. + */ + lexer->rec->state->tokFactory->setInputStream(lexer->rec->state->tokFactory, input); + } + + /* Propagate the string factory so that we preserve the encoding form from + * the input stream. + */ + if (lexer->rec->state->tokSource->strFactory == NULL) + { + lexer->rec->state->tokSource->strFactory = input->strFactory; + + // Set the newly acquired string factory up for our pre-made tokens + // for EOF. + // + if (lexer->rec->state->tokSource->eofToken.strFactory == NULL) + { + lexer->rec->state->tokSource->eofToken.strFactory = input->strFactory; + } + } + + /* This is a lexer, install the appropriate exception creator + */ + lexer->rec->exConstruct = antlr3RecognitionExceptionNew; + + /* Set the current token to nothing + */ + lexer->rec->state->token = NULL; + lexer->rec->state->text = NULL; + lexer->rec->state->tokenStartCharIndex = -1; + + /* Copy the name of the char stream to the token source + */ + lexer->rec->state->tokSource->fileName = input->fileName; +} + +/*! + * \brief + * Change to a new input stream, remembering the old one. + * + * \param lexer + * Pointer to the lexer instance to switch input streams for. + * + * \param input + * New input stream to install as the current one. + * + * Switches the current character input stream to + * a new one, saving the old one, which we will revert to at the end of this + * new one. + */ +static void +pushCharStream (pANTLR3_LEXER lexer, pANTLR3_INPUT_STREAM input) +{ + // Do we need a new input stream stack? + // + if (lexer->rec->state->streams == NULL) + { + // This is the first call to stack a new + // stream and so we must create the stack first. + // + lexer->rec->state->streams = antlr3StackNew(0); + + if (lexer->rec->state->streams == NULL) + { + // Could not do this, we just fail to push it. + // TODO: Consider if this is what we want to do, but then + // any programmer can override this method to do something else. + return; + } + } + + // We have a stack, so we can save the current input stream + // into it. + // + lexer->input->istream->mark(lexer->input->istream); + lexer->rec->state->streams->push(lexer->rec->state->streams, lexer->input, NULL); + + // And now we can install this new one + // + lexer->setCharStream(lexer, input); +} + +/*! + * \brief + * Stops using the current input stream and reverts to any prior + * input stream on the stack. + * + * \param lexer + * Description of parameter lexer. + * + * Pointer to a function that abandons the current input stream, whether it + * is empty or not and reverts to the previous stacked input stream. + * + * \remark + * The function fails silently if there are no prior input streams. + */ +static void +popCharStream (pANTLR3_LEXER lexer) +{ + pANTLR3_INPUT_STREAM input; + + // If we do not have a stream stack or we are already at the + // stack bottom, then do nothing. + // + if (lexer->rec->state->streams != NULL && lexer->rec->state->streams->size(lexer->rec->state->streams) > 0) + { + // We just leave the current stream to its fate, we do not close + // it or anything as we do not know what the programmer intended + // for it. This method can always be overridden of course. + // So just find out what was currently saved on the stack and use + // that now, then pop it from the stack. + // + input = (pANTLR3_INPUT_STREAM)(lexer->rec->state->streams->top); + lexer->rec->state->streams->pop(lexer->rec->state->streams); + + // Now install the stream as the current one. + // + lexer->setCharStream(lexer, input); + lexer->input->istream->rewindLast(lexer->input->istream); + } + return; +} + +static void emitNew (pANTLR3_LEXER lexer, pANTLR3_COMMON_TOKEN token) +{ + lexer->rec->state->token = token; /* Voila! */ +} + +static pANTLR3_COMMON_TOKEN +emit (pANTLR3_LEXER lexer) +{ + pANTLR3_COMMON_TOKEN token; + + /* We could check pointers to token factories and so on, but + * we are in code that we want to run as fast as possible + * so we are not checking any errors. So make sure you have installed an input stream before + * trying to emit a new token. + */ + token = lexer->rec->state->tokFactory->newToken(lexer->rec->state->tokFactory); + + /* Install the supplied information, and some other bits we already know + * get added automatically, such as the input stream it is associated with + * (though it can all be overridden of course) + */ + token->type = lexer->rec->state->type; + token->channel = lexer->rec->state->channel; + token->start = lexer->rec->state->tokenStartCharIndex; + token->stop = lexer->getCharIndex(lexer) - 1; + token->line = lexer->rec->state->tokenStartLine; + token->charPosition = lexer->rec->state->tokenStartCharPositionInLine; + + if (lexer->rec->state->text != NULL) + { + token->textState = ANTLR3_TEXT_STRING; + token->tokText.text = lexer->rec->state->text; + } + else + { + token->textState = ANTLR3_TEXT_NONE; + } + token->lineStart = lexer->input->currentLine; + token->user1 = lexer->rec->state->user1; + token->user2 = lexer->rec->state->user2; + token->user3 = lexer->rec->state->user3; + token->custom = lexer->rec->state->custom; + + lexer->rec->state->token = token; + + return token; +} + +/** + * Free the resources allocated by a lexer + */ +static void +freeLexer (pANTLR3_LEXER lexer) +{ + // This may have ben a delegate or delegator lexer, in which case the + // state may already have been freed (and set to NULL therefore) + // so we ignore the state if we don't have it. + // + if (lexer->rec->state != NULL) + { + if (lexer->rec->state->streams != NULL) + { + lexer->rec->state->streams->free(lexer->rec->state->streams); + } + if (lexer->rec->state->tokFactory != NULL) + { + lexer->rec->state->tokFactory->close(lexer->rec->state->tokFactory); + lexer->rec->state->tokFactory = NULL; + } + if (lexer->rec->state->tokSource != NULL) + { + ANTLR3_FREE(lexer->rec->state->tokSource); + lexer->rec->state->tokSource = NULL; + } + } + if (lexer->rec != NULL) + { + lexer->rec->free(lexer->rec); + lexer->rec = NULL; + } + ANTLR3_FREE(lexer); +} + +/** Implementation of matchs for the lexer, overrides any + * base implementation in the base recognizer. + * + * \remark + * Note that the generated code lays down arrays of ints for constant + * strings so that they are int UTF32 form! + */ +static ANTLR3_BOOLEAN +matchs(pANTLR3_LEXER lexer, ANTLR3_UCHAR * string) +{ + while (*string != ANTLR3_STRING_TERMINATOR) + { + if (lexer->input->istream->_LA(lexer->input->istream, 1) != (*string)) + { + if (lexer->rec->state->backtracking > 0) + { + lexer->rec->state->failed = ANTLR3_TRUE; + return ANTLR3_FALSE; + } + + lexer->rec->exConstruct(lexer->rec); + lexer->rec->state->failed = ANTLR3_TRUE; + + /* TODO: Implement exception creation more fully perhaps + */ + lexer->recover(lexer); + return ANTLR3_FALSE; + } + + /* Matched correctly, do consume it + */ + lexer->input->istream->consume(lexer->input->istream); + string++; + + /* Reset any failed indicator + */ + lexer->rec->state->failed = ANTLR3_FALSE; + } + + + return ANTLR3_TRUE; +} + +/** Implementation of matchc for the lexer, overrides any + * base implementation in the base recognizer. + * + * \remark + * Note that the generated code lays down arrays of ints for constant + * strings so that they are int UTF32 form! + */ +static ANTLR3_BOOLEAN +matchc(pANTLR3_LEXER lexer, ANTLR3_UCHAR c) +{ + if (lexer->input->istream->_LA(lexer->input->istream, 1) == c) + { + /* Matched correctly, do consume it + */ + lexer->input->istream->consume(lexer->input->istream); + + /* Reset any failed indicator + */ + lexer->rec->state->failed = ANTLR3_FALSE; + + return ANTLR3_TRUE; + } + + /* Failed to match, exception and recovery time. + */ + if (lexer->rec->state->backtracking > 0) + { + lexer->rec->state->failed = ANTLR3_TRUE; + return ANTLR3_FALSE; + } + + lexer->rec->exConstruct(lexer->rec); + + /* TODO: Implement exception creation more fully perhaps + */ + lexer->recover(lexer); + + return ANTLR3_FALSE; +} + +/** Implementation of match range for the lexer, overrides any + * base implementation in the base recognizer. + * + * \remark + * Note that the generated code lays down arrays of ints for constant + * strings so that they are int UTF32 form! + */ +static ANTLR3_BOOLEAN +matchRange(pANTLR3_LEXER lexer, ANTLR3_UCHAR low, ANTLR3_UCHAR high) +{ + ANTLR3_UCHAR c; + + /* What is in the stream at the moment? + */ + c = lexer->input->istream->_LA(lexer->input->istream, 1); + if ( c >= low && c <= high) + { + /* Matched correctly, consume it + */ + lexer->input->istream->consume(lexer->input->istream); + + /* Reset any failed indicator + */ + lexer->rec->state->failed = ANTLR3_FALSE; + + return ANTLR3_TRUE; + } + + /* Failed to match, execption and recovery time. + */ + + if (lexer->rec->state->backtracking > 0) + { + lexer->rec->state->failed = ANTLR3_TRUE; + return ANTLR3_FALSE; + } + + lexer->rec->exConstruct(lexer->rec); + + /* TODO: Implement exception creation more fully + */ + lexer->recover(lexer); + + return ANTLR3_FALSE; +} + +static void +matchAny (pANTLR3_LEXER lexer) +{ + lexer->input->istream->consume(lexer->input->istream); +} + +static void +recover (pANTLR3_LEXER lexer) +{ + lexer->input->istream->consume(lexer->input->istream); +} + +static ANTLR3_UINT32 +getLine (pANTLR3_LEXER lexer) +{ + return lexer->input->getLine(lexer->input); +} + +static ANTLR3_UINT32 +getCharPositionInLine (pANTLR3_LEXER lexer) +{ + return lexer->input->getCharPositionInLine(lexer->input); +} + +static ANTLR3_MARKER getCharIndex (pANTLR3_LEXER lexer) +{ + return lexer->input->istream->index(lexer->input->istream); +} + +static pANTLR3_STRING +getText (pANTLR3_LEXER lexer) +{ + if (lexer->rec->state->text) + { + return lexer->rec->state->text; + + } + return lexer->input->substr( + lexer->input, + lexer->rec->state->tokenStartCharIndex, + lexer->getCharIndex(lexer) - lexer->input->charByteSize + ); + +} + +static void * +getCurrentInputSymbol (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM istream) +{ + return NULL; +} + +static void * +getMissingSymbol (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM istream, pANTLR3_EXCEPTION e, + ANTLR3_UINT32 expectedTokenType, pANTLR3_BITSET_LIST follow) +{ + return NULL; +} diff --git a/antlr-3.1.3/runtime/C/src/antlr3parser.c b/antlr-3.1.3/runtime/C/src/antlr3parser.c new file mode 100644 index 0000000..2b7e0e3 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3parser.c @@ -0,0 +1,193 @@ +/** \file + * Implementation of the base functionality for an ANTLR3 parser. + */ + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3parser.h> + +/* Parser API + */ +static void setDebugListener (pANTLR3_PARSER parser, pANTLR3_DEBUG_EVENT_LISTENER dbg); +static void setTokenStream (pANTLR3_PARSER parser, pANTLR3_TOKEN_STREAM); +static pANTLR3_TOKEN_STREAM getTokenStream (pANTLR3_PARSER parser); +static void freeParser (pANTLR3_PARSER parser); + +ANTLR3_API pANTLR3_PARSER +antlr3ParserNewStreamDbg (ANTLR3_UINT32 sizeHint, pANTLR3_TOKEN_STREAM tstream, pANTLR3_DEBUG_EVENT_LISTENER dbg, pANTLR3_RECOGNIZER_SHARED_STATE state) +{ + pANTLR3_PARSER parser; + + parser = antlr3ParserNewStream(sizeHint, tstream, state); + + if (parser == NULL) + { + return NULL; + } + + parser->setDebugListener(parser, dbg); + + return parser; +} + +ANTLR3_API pANTLR3_PARSER +antlr3ParserNew (ANTLR3_UINT32 sizeHint, pANTLR3_RECOGNIZER_SHARED_STATE state) +{ + pANTLR3_PARSER parser; + + /* Allocate memory + */ + parser = (pANTLR3_PARSER) ANTLR3_MALLOC(sizeof(ANTLR3_PARSER)); + + if (parser == NULL) + { + return NULL; + } + + /* Install a base parser + */ + parser->rec = antlr3BaseRecognizerNew(ANTLR3_TYPE_PARSER, sizeHint, state); + + if (parser->rec == NULL) + { + parser->free(parser); + return NULL; + } + + parser->rec->super = parser; + + /* Parser overrides + */ + parser->rec->exConstruct = antlr3MTExceptionNew; + + /* Install the API + */ + parser->setDebugListener = setDebugListener; + parser->setTokenStream = setTokenStream; + parser->getTokenStream = getTokenStream; + + parser->free = freeParser; + + return parser; +} + +ANTLR3_API pANTLR3_PARSER +antlr3ParserNewStream (ANTLR3_UINT32 sizeHint, pANTLR3_TOKEN_STREAM tstream, pANTLR3_RECOGNIZER_SHARED_STATE state) +{ + pANTLR3_PARSER parser; + + parser = antlr3ParserNew(sizeHint, state); + + if (parser == NULL) + { + return NULL; + } + + /* Everything seems to be hunky dory so we can install the + * token stream. + */ + parser->setTokenStream(parser, tstream); + + return parser; +} + +static void +freeParser (pANTLR3_PARSER parser) +{ + if (parser->rec != NULL) + { + // This may have ben a delegate or delegator parser, in which case the + // state may already have been freed (and set to NULL therefore) + // so we ignore the state if we don't have it. + // + if (parser->rec->state != NULL) + { + if (parser->rec->state->following != NULL) + { + parser->rec->state->following->free(parser->rec->state->following); + parser->rec->state->following = NULL; + } + } + parser->rec->free(parser->rec); + parser->rec = NULL; + + } + ANTLR3_FREE(parser); +} + +static void +setDebugListener (pANTLR3_PARSER parser, pANTLR3_DEBUG_EVENT_LISTENER dbg) +{ + // Set the debug listener. There are no methods to override + // because currently the only ones that notify the debugger + // are error reporting and recovery. Hence we can afford to + // check and see if the debugger interface is null or not + // there. If there is ever an occasion for a performance + // sensitive function to use the debugger interface, then + // a replacement function for debug mode should be supplied + // and installed here. + // + parser->rec->debugger = dbg; + + // If there was a tokenstream installed already + // then we need to tell it about the debug interface + // + if (parser->tstream != NULL) + { + parser->tstream->setDebugListener(parser->tstream, dbg); + } +} + +static void +setTokenStream (pANTLR3_PARSER parser, pANTLR3_TOKEN_STREAM tstream) +{ + parser->tstream = tstream; + parser->rec->reset(parser->rec); +} + +static pANTLR3_TOKEN_STREAM +getTokenStream (pANTLR3_PARSER parser) +{ + return parser->tstream; +} + + + + + + + + + + + + + + diff --git a/antlr-3.1.3/runtime/C/src/antlr3rewritestreams.c b/antlr-3.1.3/runtime/C/src/antlr3rewritestreams.c new file mode 100644 index 0000000..416c78c --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3rewritestreams.c @@ -0,0 +1,832 @@ +/// \file +/// Implementation of token/tree streams that are used by the +/// tree re-write rules to manipulate the tokens and trees produced +/// by rules that are subject to rewrite directives. +/// + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3rewritestreams.h> + +// Static support function forward declarations for the stream types. +// +static void reset (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream); +static void add (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream, void * el, void (ANTLR3_CDECL *freePtr)(void *)); +static void * next (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream); +static pANTLR3_BASE_TREE nextTree (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream); +static void * nextToken (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream); +static void * _next (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream); +static void * dupTok (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream, void * el); +static void * dupTree (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream, void * el); +static void * dupTreeNode (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream, void * el); +static pANTLR3_BASE_TREE toTree (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream, void * element); +static pANTLR3_BASE_TREE toTreeNode (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream, void * element); +static ANTLR3_BOOLEAN hasNext (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream); +static pANTLR3_BASE_TREE nextNode (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream); +static pANTLR3_BASE_TREE nextNodeNode (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream); +static pANTLR3_BASE_TREE nextNodeToken (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream); +static ANTLR3_UINT32 size (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream); +static void * getDescription (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream); +static void freeRS (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream); +static void expungeRS (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream); + + +// Place a now unused rewrite stream back on the rewrite stream pool +// so we can reuse it if we need to. +// +static void +freeRS (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream) +{ + // Before placing the stream back in the pool, we + // need to clear any vector it has. This is so any + // free pointers that are associated with the + // entires are called. + // + if (stream->elements != NULL) + { + // Protext factory generated nodes as we cannot clear them, + // the factory is responsible for that. + // + if (stream->elements->factoryMade == ANTLR3_TRUE) + { + stream->elements = NULL; + } + else + { + stream->elements->clear(stream->elements); + stream->freeElements = ANTLR3_TRUE; + } + } + else + { + stream->freeElements = ANTLR3_FALSE; // Just in case + } + + // Add the stream into the recognizer stream stack vector + // adding the stream memory free routine so that + // it is thrown away when the stack vector is destroyed + // + stream->rec->state->rStreams->add(stream->rec->state->rStreams, stream, (void(*)(void *))expungeRS); +} + +/** Do special nilNode reuse detection for node streams. + */ +static void +freeNodeRS(pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream) +{ + pANTLR3_BASE_TREE tree; + + // Before placing the stream back in the pool, we + // need to clear any vector it has. This is so any + // free pointers that are associated with the + // entires are called. However, if this particular function is called + // then we know that the entries in the stream are definately + // tree nodes. Hence we check to see if any of them were nilNodes as + // if they were, we can reuse them. + // + if (stream->elements != NULL) + { + // We have some elements to traverse + // + ANTLR3_UINT32 i; + + for (i = 1; i<= stream->elements->count; i++) + { + tree = (pANTLR3_BASE_TREE)(stream->elements->elements[i-1].element); + if (tree->isNilNode(tree)) + { + tree->reuse(tree); + } + + } + // Protext factory generated nodes as we cannot clear them, + // the factory is responsible for that. + // + if (stream->elements->factoryMade == ANTLR3_TRUE) + { + stream->elements = NULL; + } + else + { + stream->elements->clear(stream->elements); + stream->freeElements = ANTLR3_TRUE; + } + } + else + { + if (stream->singleElement != NULL) + { + tree = (pANTLR3_BASE_TREE)(stream->singleElement); + if (tree->isNilNode(tree)) + { + tree->reuse(tree); + } + } + stream->freeElements = ANTLR3_FALSE; // Just in case + } + + // Add the stream into the recognizer stream stack vector + // adding the stream memory free routine so that + // it is thrown away when the stack vector is destroyed + // + stream->rec->state->rStreams->add(stream->rec->state->rStreams, stream, (void(*)(void *))expungeRS); +} +static void +expungeRS(pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream) +{ + + if (stream->freeElements == ANTLR3_TRUE && stream->elements != NULL) + { + stream->elements->free(stream->elements); + } + ANTLR3_FREE(stream); +} + +// Functions for creating streams +// +static pANTLR3_REWRITE_RULE_ELEMENT_STREAM +antlr3RewriteRuleElementStreamNewAE(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description) +{ + pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream; + + // First - do we already have a rewrite stream that was returned + // to the pool? If we do, then we will just reuse it by resetting + // the generic interface. + // + if (rec->state->rStreams->count > 0) + { + // Remove the entry from the vector. We do not + // cause it to be freed by using remove. + // + stream = rec->state->rStreams->remove(rec->state->rStreams, rec->state->rStreams->count - 1); + + // We found a stream we can reuse. + // If the stream had a vector, then it will have been cleared + // when the freeRS was called that put it in this stack + // + } + else + { + // Ok, we need to allocate a new one as there were none on the stack. + // First job is to create the memory we need. + // + stream = (pANTLR3_REWRITE_RULE_ELEMENT_STREAM) ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_REWRITE_RULE_ELEMENT_STREAM))); + + if (stream == NULL) + { + return NULL; + } + stream->elements = NULL; + stream->freeElements = ANTLR3_FALSE; + } + + // Populate the generic interface + // + stream->rec = rec; + stream->reset = reset; + stream->add = add; + stream->next = next; + stream->nextTree = nextTree; + stream->nextNode = nextNode; + stream->nextToken = nextToken; + stream->_next = _next; + stream->hasNext = hasNext; + stream->size = size; + stream->getDescription = getDescription; + stream->toTree = toTree; + stream->free = freeRS; + stream->singleElement = NULL; + + // Reset the stream to empty. + // + + stream->cursor = 0; + stream->dirty = ANTLR3_FALSE; + + // Install the description + // + stream->elementDescription = description; + + // Install the adaptor + // + stream->adaptor = adaptor; + + return stream; +} + +static pANTLR3_REWRITE_RULE_ELEMENT_STREAM +antlr3RewriteRuleElementStreamNewAEE(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description, void * oneElement) +{ + pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream; + + // First job is to create the memory we need. + // + stream = antlr3RewriteRuleElementStreamNewAE(adaptor, rec, description); + + if (stream == NULL) + { + return NULL; + } + + // Stream seems good so we need to add the supplied element + // + if (oneElement != NULL) + { + stream->add(stream, oneElement, NULL); + } + return stream; +} + +static pANTLR3_REWRITE_RULE_ELEMENT_STREAM +antlr3RewriteRuleElementStreamNewAEV(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description, pANTLR3_VECTOR vector) +{ + pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream; + + // First job is to create the memory we need. + // + stream = antlr3RewriteRuleElementStreamNewAE(adaptor, rec, description); + + if (stream == NULL) + { + return stream; + } + + // Stream seems good so we need to install the vector we were + // given. We assume that someone else is going to free the + // vector. + // + if (stream->elements != NULL && stream->elements->factoryMade == ANTLR3_FALSE && stream->freeElements == ANTLR3_TRUE ) + { + stream->elements->free(stream->elements); + } + stream->elements = vector; + stream->freeElements = ANTLR3_FALSE; + return stream; +} + +// Token rewrite stream ... +// +ANTLR3_API pANTLR3_REWRITE_RULE_TOKEN_STREAM +antlr3RewriteRuleTOKENStreamNewAE(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description) +{ + pANTLR3_REWRITE_RULE_TOKEN_STREAM stream; + + // First job is to create the memory we need. + // + stream = antlr3RewriteRuleElementStreamNewAE(adaptor, rec, description); + + if (stream == NULL) + { + return stream; + } + + // Install the token based overrides + // + stream->dup = dupTok; + stream->nextNode = nextNodeToken; + + // No nextNode implementation for a token rewrite stream + // + return stream; +} + +ANTLR3_API pANTLR3_REWRITE_RULE_TOKEN_STREAM +antlr3RewriteRuleTOKENStreamNewAEE(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description, void * oneElement) +{ + pANTLR3_REWRITE_RULE_TOKEN_STREAM stream; + + // First job is to create the memory we need. + // + stream = antlr3RewriteRuleElementStreamNewAEE(adaptor, rec, description, oneElement); + + // Install the token based overrides + // + stream->dup = dupTok; + stream->nextNode = nextNodeToken; + + // No nextNode implementation for a token rewrite stream + // + return stream; +} + +ANTLR3_API pANTLR3_REWRITE_RULE_TOKEN_STREAM +antlr3RewriteRuleTOKENStreamNewAEV(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description, pANTLR3_VECTOR vector) +{ + pANTLR3_REWRITE_RULE_TOKEN_STREAM stream; + + // First job is to create the memory we need. + // + stream = antlr3RewriteRuleElementStreamNewAEV(adaptor, rec, description, vector); + + // Install the token based overrides + // + stream->dup = dupTok; + stream->nextNode = nextNodeToken; + + // No nextNode implementation for a token rewrite stream + // + return stream; +} + +// Subtree rewrite stream +// +ANTLR3_API pANTLR3_REWRITE_RULE_SUBTREE_STREAM +antlr3RewriteRuleSubtreeStreamNewAE(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description) +{ + pANTLR3_REWRITE_RULE_SUBTREE_STREAM stream; + + // First job is to create the memory we need. + // + stream = antlr3RewriteRuleElementStreamNewAE(adaptor, rec, description); + + if (stream == NULL) + { + return stream; + } + + // Install the subtree based overrides + // + stream->dup = dupTree; + stream->nextNode = nextNode; + stream->free = freeNodeRS; + return stream; + +} +ANTLR3_API pANTLR3_REWRITE_RULE_SUBTREE_STREAM +antlr3RewriteRuleSubtreeStreamNewAEE(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description, void * oneElement) +{ + pANTLR3_REWRITE_RULE_SUBTREE_STREAM stream; + + // First job is to create the memory we need. + // + stream = antlr3RewriteRuleElementStreamNewAEE(adaptor, rec, description, oneElement); + + if (stream == NULL) + { + return stream; + } + + // Install the subtree based overrides + // + stream->dup = dupTree; + stream->nextNode = nextNode; + stream->free = freeNodeRS; + + return stream; +} + +ANTLR3_API pANTLR3_REWRITE_RULE_SUBTREE_STREAM +antlr3RewriteRuleSubtreeStreamNewAEV(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description, pANTLR3_VECTOR vector) +{ + pANTLR3_REWRITE_RULE_SUBTREE_STREAM stream; + + // First job is to create the memory we need. + // + stream = antlr3RewriteRuleElementStreamNewAEV(adaptor, rec, description, vector); + + if (stream == NULL) + { + return NULL; + } + + // Install the subtree based overrides + // + stream->dup = dupTree; + stream->nextNode = nextNode; + stream->free = freeNodeRS; + + return stream; +} +// Node rewrite stream ... +// +ANTLR3_API pANTLR3_REWRITE_RULE_NODE_STREAM +antlr3RewriteRuleNODEStreamNewAE(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description) +{ + pANTLR3_REWRITE_RULE_NODE_STREAM stream; + + // First job is to create the memory we need. + // + stream = antlr3RewriteRuleElementStreamNewAE(adaptor, rec, description); + + if (stream == NULL) + { + return stream; + } + + // Install the node based overrides + // + stream->dup = dupTreeNode; + stream->toTree = toTreeNode; + stream->nextNode = nextNodeNode; + stream->free = freeNodeRS; + + return stream; +} + +ANTLR3_API pANTLR3_REWRITE_RULE_NODE_STREAM +antlr3RewriteRuleNODEStreamNewAEE(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description, void * oneElement) +{ + pANTLR3_REWRITE_RULE_NODE_STREAM stream; + + // First job is to create the memory we need. + // + stream = antlr3RewriteRuleElementStreamNewAEE(adaptor, rec, description, oneElement); + + // Install the node based overrides + // + stream->dup = dupTreeNode; + stream->toTree = toTreeNode; + stream->nextNode = nextNodeNode; + stream->free = freeNodeRS; + + return stream; +} + +ANTLR3_API pANTLR3_REWRITE_RULE_NODE_STREAM +antlr3RewriteRuleNODEStreamNewAEV(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_UINT8 description, pANTLR3_VECTOR vector) +{ + pANTLR3_REWRITE_RULE_NODE_STREAM stream; + + // First job is to create the memory we need. + // + stream = antlr3RewriteRuleElementStreamNewAEV(adaptor, rec, description, vector); + + // Install the Node based overrides + // + stream->dup = dupTreeNode; + stream->toTree = toTreeNode; + stream->nextNode = nextNodeNode; + stream->free = freeNodeRS; + + return stream; +} + +//---------------------------------------------------------------------- +// Static support functions + +/// Reset the condition of this stream so that it appears we have +/// not consumed any of its elements. Elements themselves are untouched. +/// +static void +reset (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream) +{ + stream->dirty = ANTLR3_TRUE; + stream->cursor = 0; +} + +// Add a new pANTLR3_BASE_TREE to this stream +// +static void +add (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream, void * el, void (ANTLR3_CDECL *freePtr)(void *)) +{ + if (el== NULL) + { + return; + } + // As we may be reusing a stream, we may already have allocated + // a rewrite stream vector. If we have then is will be empty if + // we have either zero or just one element in the rewrite stream + // + if (stream->elements != NULL && stream->elements->count > 0) + { + // We already have >1 entries in the stream. So we can just add this new element to the existing + // collection. + // + stream->elements->add(stream->elements, el, freePtr); + return; + } + if (stream->singleElement == NULL) + { + stream->singleElement = el; + return; + } + + // If we got here then we had only the one element so far + // and we must now create a vector to hold a collection of them + // + if (stream->elements == NULL) + { + pANTLR3_VECTOR_FACTORY factory = ((pANTLR3_COMMON_TREE_ADAPTOR)(stream->adaptor->super))->arboretum->vFactory; + + + stream->elements = factory->newVector(factory); + stream->freeElements = ANTLR3_TRUE; // We 'ummed it, so we play it son. + } + + stream->elements->add (stream->elements, stream->singleElement, freePtr); + stream->elements->add (stream->elements, el, freePtr); + stream->singleElement = NULL; + + return; +} + +/// Return the next element in the stream. If out of elements, throw +/// an exception unless size()==1. If size is 1, then return elements[0]. +/// Return a duplicate node/subtree if stream is out of elements and +/// size==1. If we've already used the element, dup (dirty bit set). +/// +static pANTLR3_BASE_TREE +nextTree(pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream) +{ + ANTLR3_UINT32 n; + void * el; + + n = stream->size(stream); + + if ( stream->dirty || (stream->cursor >=n && n==1) ) + { + // if out of elements and size is 1, dup + // + el = stream->_next(stream); + return stream->dup(stream, el); + } + + // test size above then fetch + // + el = stream->_next(stream); + return el; +} + +/// Return the next element for a caller that wants just the token +/// +static void * +nextToken (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream) +{ + return stream->_next(stream); +} + +/// Return the next element in the stream. If out of elements, throw +/// an exception unless size()==1. If size is 1, then return elements[0]. +/// +static void * +next (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream) +{ + ANTLR3_UINT32 s; + + s = stream->size(stream); + if (stream->cursor >= s && s == 1) + { + pANTLR3_BASE_TREE el; + + el = stream->_next(stream); + + return stream->dup(stream, el); + } + + return stream->_next(stream); +} + +/// Do the work of getting the next element, making sure that it's +/// a tree node or subtree. Deal with the optimization of single- +/// element list versus list of size > 1. Throw an exception (or something similar) +/// if the stream is empty or we're out of elements and size>1. +/// You can override in a 'subclass' if necessary. +/// +static void * +_next (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream) +{ + ANTLR3_UINT32 n; + pANTLR3_BASE_TREE t; + + n = stream->size(stream); + + if (n == 0) + { + // This means that the stream is empty + // + return NULL; // Caller must cope with this + } + + // Traversed all the available elements already? + // + if (stream->cursor >= n) + { + if (n == 1) + { + // Special case when size is single element, it will just dup a lot + // + return stream->toTree(stream, stream->singleElement); + } + + // Out of elements and the size is not 1, so we cannot assume + // that we just duplicate the entry n times (such as ID ent+ -> ^(ID ent)+) + // This means we ran out of elements earlier than was expected. + // + return NULL; // Caller must cope with this + } + + // Elements available either for duping or just available + // + if (stream->singleElement != NULL) + { + stream->cursor++; // Cursor advances even for single element as this tells us to dup() + return stream->toTree(stream, stream->singleElement); + } + + // More than just a single element so we extract it from the + // vector. + // + t = stream->toTree(stream, stream->elements->get(stream->elements, stream->cursor)); + stream->cursor++; + return t; +} + +#ifdef ANTLR3_WINDOWS +#pragma warning(push) +#pragma warning(disable : 4100) +#endif +/// When constructing trees, sometimes we need to dup a token or AST +/// subtree. Dup'ing a token means just creating another AST node +/// around it. For trees, you must call the adaptor.dupTree(). +/// +static void * +dupTok (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream, void * el) +{ + ANTLR3_FPRINTF(stderr, "dup() cannot be called on a token rewrite stream!!"); + return NULL; +} +#ifdef ANTLR3_WINDOWS +#pragma warning(pop) +#endif + +/// When constructing trees, sometimes we need to dup a token or AST +/// subtree. Dup'ing a token means just creating another AST node +/// around it. For trees, you must call the adaptor.dupTree(). +/// +static void * +dupTree (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream, void * element) +{ + return stream->adaptor->dupNode(stream->adaptor, (pANTLR3_BASE_TREE)element); +} + +#ifdef ANTLR3_WINDOWS +#pragma warning(push) +#pragma warning(disable : 4100) +#endif +/// When constructing trees, sometimes we need to dup a token or AST +/// subtree. Dup'ing a token means just creating another AST node +/// around it. For trees, you must call the adaptor.dupTree(). +/// +static void * +dupTreeNode (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream, void * element) +{ + ANTLR3_FPRINTF(stderr, "dup() cannot be called on a node rewrite stream!!!"); + return NULL; +} + + +/// We don;t explicitly convert to a tree unless the call goes to +/// nextTree, which means rewrites are heterogeneous +/// +static pANTLR3_BASE_TREE +toTree (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream, void * element) +{ + return (pANTLR3_BASE_TREE)element; +} +#ifdef ANTLR3_WINDOWS +#pragma warning(pop) +#endif + +/// Ensure stream emits trees; tokens must be converted to AST nodes. +/// AST nodes can be passed through unmolested. +/// +#ifdef ANTLR3_WINDOWS +#pragma warning(push) +#pragma warning(disable : 4100) +#endif + +static pANTLR3_BASE_TREE +toTreeNode (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream, void * element) +{ + return stream->adaptor->dupNode(stream->adaptor, (pANTLR3_BASE_TREE)element); +} + +#ifdef ANTLR3_WINDOWS +#pragma warning(pop) +#endif + +/// Returns ANTLR3_TRUE if there is a next element available +/// +static ANTLR3_BOOLEAN +hasNext (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream) +{ + if ( (stream->singleElement != NULL && stream->cursor < 1) + || (stream->elements != NULL && stream->cursor < stream->elements->size(stream->elements))) + { + return ANTLR3_TRUE; + } + else + { + return ANTLR3_FALSE; + } +} + +/// Get the next token from the list and create a node for it +/// This is the implementation for token streams. +/// +static pANTLR3_BASE_TREE +nextNodeToken(pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream) +{ + return stream->adaptor->create(stream->adaptor, stream->_next(stream)); +} + +static pANTLR3_BASE_TREE +nextNodeNode(pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream) +{ + return stream->_next(stream); +} + +/// Treat next element as a single node even if it's a subtree. +/// This is used instead of next() when the result has to be a +/// tree root node. Also prevents us from duplicating recently-added +/// children; e.g., ^(type ID)+ adds ID to type and then 2nd iteration +/// must dup the type node, but ID has been added. +/// +/// Referencing to a rule result twice is ok; dup entire tree as +/// we can't be adding trees; e.g., expr expr. +/// +static pANTLR3_BASE_TREE +nextNode (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream) +{ + + ANTLR3_UINT32 n; + pANTLR3_BASE_TREE el = stream->_next(stream); + + n = stream->size(stream); + if (stream->dirty == ANTLR3_TRUE || (stream->cursor > n && n == 1)) + { + // We are out of elements and the size is 1, which means we just + // dup the node that we have + // + return stream->adaptor->dupNode(stream->adaptor, el); + } + + // We were not out of nodes, so the one we received is the one to return + // + return el; +} + +/// Number of elements available in the stream +/// +static ANTLR3_UINT32 +size (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream) +{ + ANTLR3_UINT32 n = 0; + + /// Should be a count of one if singleElement is set. I copied this + /// logic from the java implementation, which I suspect is just guarding + /// against someone setting singleElement and forgetting to NULL it out + /// + if (stream->singleElement != NULL) + { + n = 1; + } + else + { + if (stream->elements != NULL) + { + return (ANTLR3_UINT32)(stream->elements->count); + } + } + return n; +} + +/// Returns the description string if there is one available (check for NULL). +/// +static void * +getDescription (pANTLR3_REWRITE_RULE_ELEMENT_STREAM stream) +{ + if (stream->elementDescription == NULL) + { + stream->elementDescription = "<unknown source>"; + } + + return stream->elementDescription; +} diff --git a/antlr-3.1.3/runtime/C/src/antlr3string.c b/antlr-3.1.3/runtime/C/src/antlr3string.c new file mode 100644 index 0000000..7e1bd22 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3string.c @@ -0,0 +1,1396 @@ +/** \file + * Implementation of the ANTLR3 string and string factory classes + */ + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3string.h> + +/* Factory API + */ +static pANTLR3_STRING newRaw8 (pANTLR3_STRING_FACTORY factory); +static pANTLR3_STRING newRaw16 (pANTLR3_STRING_FACTORY factory); +static pANTLR3_STRING newSize8 (pANTLR3_STRING_FACTORY factory, ANTLR3_UINT32 size); +static pANTLR3_STRING newSize16 (pANTLR3_STRING_FACTORY factory, ANTLR3_UINT32 size); +static pANTLR3_STRING newPtr8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string, ANTLR3_UINT32 size); +static pANTLR3_STRING newPtr16_8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string, ANTLR3_UINT32 size); +static pANTLR3_STRING newPtr16_16 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string, ANTLR3_UINT32 size); +static pANTLR3_STRING newStr8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string); +static pANTLR3_STRING newStr16_8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string); +static pANTLR3_STRING newStr16_16 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string); +static void destroy (pANTLR3_STRING_FACTORY factory, pANTLR3_STRING string); +static pANTLR3_STRING printable8 (pANTLR3_STRING_FACTORY factory, pANTLR3_STRING string); +static pANTLR3_STRING printable16 (pANTLR3_STRING_FACTORY factory, pANTLR3_STRING string); +static void closeFactory(pANTLR3_STRING_FACTORY factory); + +/* String API + */ +static pANTLR3_UINT8 set8 (pANTLR3_STRING string, const char * chars); +static pANTLR3_UINT8 set16_8 (pANTLR3_STRING string, const char * chars); +static pANTLR3_UINT8 set16_16 (pANTLR3_STRING string, const char * chars); +static pANTLR3_UINT8 append8 (pANTLR3_STRING string, const char * newbit); +static pANTLR3_UINT8 append16_8 (pANTLR3_STRING string, const char * newbit); +static pANTLR3_UINT8 append16_16 (pANTLR3_STRING string, const char * newbit); +static pANTLR3_UINT8 insert8 (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit); +static pANTLR3_UINT8 insert16_8 (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit); +static pANTLR3_UINT8 insert16_16 (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit); + +static pANTLR3_UINT8 setS (pANTLR3_STRING string, pANTLR3_STRING chars); +static pANTLR3_UINT8 appendS (pANTLR3_STRING string, pANTLR3_STRING newbit); +static pANTLR3_UINT8 insertS (pANTLR3_STRING string, ANTLR3_UINT32 point, pANTLR3_STRING newbit); + +static pANTLR3_UINT8 addc8 (pANTLR3_STRING string, ANTLR3_UINT32 c); +static pANTLR3_UINT8 addc16 (pANTLR3_STRING string, ANTLR3_UINT32 c); +static pANTLR3_UINT8 addi8 (pANTLR3_STRING string, ANTLR3_INT32 i); +static pANTLR3_UINT8 addi16 (pANTLR3_STRING string, ANTLR3_INT32 i); +static pANTLR3_UINT8 inserti8 (pANTLR3_STRING string, ANTLR3_UINT32 point, ANTLR3_INT32 i); +static pANTLR3_UINT8 inserti16 (pANTLR3_STRING string, ANTLR3_UINT32 point, ANTLR3_INT32 i); + +static ANTLR3_UINT32 compare8 (pANTLR3_STRING string, const char * compStr); +static ANTLR3_UINT32 compare16_8 (pANTLR3_STRING string, const char * compStr); +static ANTLR3_UINT32 compare16_16(pANTLR3_STRING string, const char * compStr); +static ANTLR3_UINT32 compareS (pANTLR3_STRING string, pANTLR3_STRING compStr); +static ANTLR3_UCHAR charAt8 (pANTLR3_STRING string, ANTLR3_UINT32 offset); +static ANTLR3_UCHAR charAt16 (pANTLR3_STRING string, ANTLR3_UINT32 offset); +static pANTLR3_STRING subString8 (pANTLR3_STRING string, ANTLR3_UINT32 startIndex, ANTLR3_UINT32 endIndex); +static pANTLR3_STRING subString16 (pANTLR3_STRING string, ANTLR3_UINT32 startIndex, ANTLR3_UINT32 endIndex); +static ANTLR3_INT32 toInt32_8 (pANTLR3_STRING string); +static ANTLR3_INT32 toInt32_16 (pANTLR3_STRING string); +static pANTLR3_STRING to8_8 (pANTLR3_STRING string); +static pANTLR3_STRING to8_16 (pANTLR3_STRING string); +static pANTLR3_STRING toUTF8_8 (pANTLR3_STRING string); +static pANTLR3_STRING toUTF8_16 (pANTLR3_STRING string); + +/* Local helpers + */ +static void stringInit8 (pANTLR3_STRING string); +static void stringInit16 (pANTLR3_STRING string); +static void ANTLR3_CDECL stringFree (pANTLR3_STRING string); + +ANTLR3_API pANTLR3_STRING_FACTORY +antlr3StringFactoryNew() +{ + pANTLR3_STRING_FACTORY factory; + + /* Allocate memory + */ + factory = (pANTLR3_STRING_FACTORY) ANTLR3_MALLOC(sizeof(ANTLR3_STRING_FACTORY)); + + if (factory == NULL) + { + return NULL; + } + + /* Now we make a new list to track the strings. + */ + factory->strings = antlr3VectorNew(0); + factory->index = 0; + + if (factory->strings == NULL) + { + ANTLR3_FREE(factory); + return NULL; + } + + /* Install the API (8 bit assumed) + */ + factory->newRaw = newRaw8; + factory->newSize = newSize8; + + factory->newPtr = newPtr8; + factory->newPtr8 = newPtr8; + factory->newStr = newStr8; + factory->newStr8 = newStr8; + factory->destroy = destroy; + factory->printable = printable8; + factory->destroy = destroy; + factory->close = closeFactory; + + return factory; +} + +/** Create a string factory that is UCS2 (16 bit) encoding based + */ +ANTLR3_API pANTLR3_STRING_FACTORY +antlr3UCS2StringFactoryNew() +{ + pANTLR3_STRING_FACTORY factory; + + /* Allocate an 8 bit factory, then override with 16 bit UCS2 functions where we + * need to. + */ + factory = antlr3StringFactoryNew(); + + if (factory == NULL) + { + return NULL; + } + + /* Override the 8 bit API with the UCS2 (mostly just 16 bit) API + */ + factory->newRaw = newRaw16; + factory->newSize = newSize16; + + factory->newPtr = newPtr16_16; + factory->newPtr8 = newPtr16_8; + factory->newStr = newStr16_16; + factory->newStr8 = newStr16_8; + factory->printable = printable16; + + factory->destroy = destroy; + factory->destroy = destroy; + factory->close = closeFactory; + + return factory; +} + +/** + * + * \param factory + * \return + */ +static pANTLR3_STRING +newRaw8 (pANTLR3_STRING_FACTORY factory) +{ + pANTLR3_STRING string; + + string = (pANTLR3_STRING) ANTLR3_MALLOC(sizeof(ANTLR3_STRING)); + + if (string == NULL) + { + return NULL; + } + + /* Structure is allocated, now fill in the API etc. + */ + stringInit8(string); + string->factory = factory; + + /* Add the string into the allocated list + */ + factory->strings->set(factory->strings, factory->index, (void *) string, (void (ANTLR3_CDECL *)(void *))(stringFree), ANTLR3_TRUE); + string->index = factory->index++; + + return string; +} +/** + * + * \param factory + * \return + */ +static pANTLR3_STRING +newRaw16 (pANTLR3_STRING_FACTORY factory) +{ + pANTLR3_STRING string; + + string = (pANTLR3_STRING) ANTLR3_MALLOC(sizeof(ANTLR3_STRING)); + + if (string == NULL) + { + return NULL; + } + + /* Structure is allocated, now fill in the API etc. + */ + stringInit16(string); + string->factory = factory; + + /* Add the string into the allocated list + */ + factory->strings->set(factory->strings, factory->index, (void *) string, (void (ANTLR3_CDECL *)(void *))(stringFree), ANTLR3_TRUE); + string->index = factory->index++; + + return string; +} +static +void ANTLR3_CDECL stringFree (pANTLR3_STRING string) +{ + /* First free the string itself if there was anything in it + */ + if (string->chars) + { + ANTLR3_FREE(string->chars); + } + + /* Now free the space for this string + */ + ANTLR3_FREE(string); + + return; +} +/** + * + * \param string + * \return + */ +static void +stringInit8 (pANTLR3_STRING string) +{ + string->len = 0; + string->size = 0; + string->chars = NULL; + string->encoding = ANTLR3_ENCODING_LATIN1; + + /* API for 8 bit strings*/ + + string->set = set8; + string->set8 = set8; + string->append = append8; + string->append8 = append8; + string->insert = insert8; + string->insert8 = insert8; + string->addi = addi8; + string->inserti = inserti8; + string->addc = addc8; + string->charAt = charAt8; + string->compare = compare8; + string->compare8 = compare8; + string->subString = subString8; + string->toInt32 = toInt32_8; + string->to8 = to8_8; + string->toUTF8 = toUTF8_8; + string->compareS = compareS; + string->setS = setS; + string->appendS = appendS; + string->insertS = insertS; + +} +/** + * + * \param string + * \return + */ +static void +stringInit16 (pANTLR3_STRING string) +{ + string->len = 0; + string->size = 0; + string->chars = NULL; + string->encoding = ANTLR3_ENCODING_UCS2; + + /* API for 16 bit strings */ + + string->set = set16_16; + string->set8 = set16_8; + string->append = append16_16; + string->append8 = append16_8; + string->insert = insert16_16; + string->insert8 = insert16_8; + string->addi = addi16; + string->inserti = inserti16; + string->addc = addc16; + string->charAt = charAt16; + string->compare = compare16_16; + string->compare8 = compare16_8; + string->subString = subString16; + string->toInt32 = toInt32_16; + string->to8 = to8_16; + string->toUTF8 = toUTF8_16; + + string->compareS = compareS; + string->setS = setS; + string->appendS = appendS; + string->insertS = insertS; +} +/** + * + * \param string + * \return + * TODO: Implement UTF-8 + */ +static void +stringInitUTF8 (pANTLR3_STRING string) +{ + string->len = 0; + string->size = 0; + string->chars = NULL; + + /* API */ + +} + +// Convert an 8 bit string into a UTF8 representation, which is in fact just the string itself +// a memcpy as we make no assumptions about the 8 bit encoding. +// +static pANTLR3_STRING +toUTF8_8 (pANTLR3_STRING string) +{ + return string->factory->newPtr(string->factory, (pANTLR3_UINT8)(string->chars), string->len); +} + +// Convert a 16 bit (UCS2) string into a UTF8 representation using the Unicode.org +// supplied C algorithms, which are now contained within the ANTLR3 C runtime +// as permitted by the Unicode license (within the source code antlr3convertutf.c/.h +// UCS2 has the same encoding as UTF16 so we can use UTF16 converter. +// +static pANTLR3_STRING +toUTF8_16 (pANTLR3_STRING string) +{ + + UTF8 * outputEnd; + UTF16 * inputEnd; + pANTLR3_STRING utf8String; + + ConversionResult cResult; + + // Allocate the output buffer, which needs to accommodate potentially + // 3X (in bytes) the input size (in chars). + // + utf8String = string->factory->newStr8(string->factory, (pANTLR3_UINT8)""); + + if (utf8String != NULL) + { + // Free existing allocation + // + ANTLR3_FREE(utf8String->chars); + + // Reallocate according to maximum expected size + // + utf8String->size = string->len *3; + utf8String->chars = (pANTLR3_UINT8)ANTLR3_MALLOC(utf8String->size +1); + + if (utf8String->chars != NULL) + { + inputEnd = (UTF16 *) (string->chars); + outputEnd = (UTF8 *) (utf8String->chars); + + // Call the Unicode converter + // + cResult = ConvertUTF16toUTF8 + ( + (const UTF16**)&inputEnd, + ((const UTF16 *)(string->chars)) + string->len, + &outputEnd, + outputEnd + utf8String->size - 1, + lenientConversion + ); + + // We don't really care if things failed or not here, we just converted + // everything that was vaguely possible and stopped when it wasn't. It is + // up to the grammar programmer to verify that the input is sensible. + // + utf8String->len = ANTLR3_UINT32_CAST(((pANTLR3_UINT8)outputEnd) - utf8String->chars); + + *(outputEnd+1) = '\0'; // Always null terminate + } + } + return utf8String; +} + +/** + * Creates a new string with enough capacity for size 8 bit characters plus a terminator. + * + * \param[in] factory - Pointer to the string factory that owns strings + * \param[in] size - In characters + * \return pointer to the new string. + */ +static pANTLR3_STRING +newSize8 (pANTLR3_STRING_FACTORY factory, ANTLR3_UINT32 size) +{ + pANTLR3_STRING string; + + string = factory->newRaw(factory); + + if (string == NULL) + { + return string; + } + + /* Always add one more byte for a terminator ;-) + */ + string->chars = (pANTLR3_UINT8) ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_UINT8) * (size+1))); + *(string->chars) = '\0'; + string->size = size + 1; + + + return string; +} +/** + * Creates a new string with enough capacity for size 16 bit characters plus a terminator. + * + * \param[in] factory - POitner to the string factory that owns strings + * \param[in] size - In characters + * \return pointer to the new string. + */ +static pANTLR3_STRING +newSize16 (pANTLR3_STRING_FACTORY factory, ANTLR3_UINT32 size) +{ + pANTLR3_STRING string; + + string = factory->newRaw(factory); + + if (string == NULL) + { + return string; + } + + /* Always add one more byte for a terminator ;-) + */ + string->chars = (pANTLR3_UINT8) ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_UINT16) * (size+1))); + *(string->chars) = '\0'; + string->size = size+1; /* Size is always in characters, as is len */ + + return string; +} + +/** Creates a new 8 bit string initialized with the 8 bit characters at the + * supplied ptr, of pre-determined size. + * \param[in] factory - Pointer to the string factory that owns the strings + * \param[in] ptr - Pointer to 8 bit encoded characters + * \return pointer to the new string + */ +static pANTLR3_STRING +newPtr8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 ptr, ANTLR3_UINT32 size) +{ + pANTLR3_STRING string; + + string = factory->newSize(factory, size); + + if (string == NULL) + { + return NULL; + } + + if (size <= 0) + { + return string; + } + + if (ptr != NULL) + { + ANTLR3_MEMMOVE(string->chars, (const void *)ptr, size); + *(string->chars + size) = '\0'; /* Terminate, these strings are usually used for Token streams and printing etc. */ + string->len = size; + } + + return string; +} + +/** Creates a new 16 bit string initialized with the 8 bit characters at the + * supplied 8 bit character ptr, of pre-determined size. + * \param[in] factory - Pointer to the string factory that owns the strings + * \param[in] ptr - Pointer to 8 bit encoded characters + * \return pointer to the new string + */ +static pANTLR3_STRING +newPtr16_8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 ptr, ANTLR3_UINT32 size) +{ + pANTLR3_STRING string; + + /* newSize accepts size in characters, not bytes + */ + string = factory->newSize(factory, size); + + if (string == NULL) + { + return NULL; + } + + if (size <= 0) + { + return string; + } + + if (ptr != NULL) + { + pANTLR3_UINT16 out; + ANTLR3_INT32 inSize; + + out = (pANTLR3_UINT16)(string->chars); + inSize = size; + + while (inSize-- > 0) + { + *out++ = (ANTLR3_UINT16)(*ptr++); + } + + /* Terminate, these strings are usually used for Token streams and printing etc. + */ + *(((pANTLR3_UINT16)(string->chars)) + size) = '\0'; + + string->len = size; + } + + return string; +} + +/** Creates a new 16 bit string initialized with the 16 bit characters at the + * supplied ptr, of pre-determined size. + * \param[in] factory - Pointer to the string factory that owns the strings + * \param[in] ptr - Pointer to 16 bit encoded characters + * \return pointer to the new string + */ +static pANTLR3_STRING +newPtr16_16 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 ptr, ANTLR3_UINT32 size) +{ + pANTLR3_STRING string; + + string = factory->newSize(factory, size); + + if (string == NULL) + { + return NULL; + } + + if (size <= 0) + { + return string; + } + + if (ptr != NULL) + { + ANTLR3_MEMMOVE(string->chars, (const void *)ptr, (size * sizeof(ANTLR3_UINT16))); + + /* Terminate, these strings are usually used for Token streams and printing etc. + */ + *(((pANTLR3_UINT16)(string->chars)) + size) = '\0'; + string->len = size; + } + + return string; +} + +/** Create a new 8 bit string from the supplied, null terminated, 8 bit string pointer. + * \param[in] factory - Pointer to the string factory that owns strings. + * \param[in] ptr - Pointer to the 8 bit encoded string + * \return Pointer to the newly initialized string + */ +static pANTLR3_STRING +newStr8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 ptr) +{ + return factory->newPtr8(factory, ptr, (ANTLR3_UINT32)strlen((const char *)ptr)); +} + +/** Create a new 16 bit string from the supplied, null terminated, 8 bit string pointer. + * \param[in] factory - Pointer to the string factory that owns strings. + * \param[in] ptr - Pointer to the 8 bit encoded string + * \return POinter to the newly initialized string + */ +static pANTLR3_STRING +newStr16_8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 ptr) +{ + return factory->newPtr8(factory, ptr, (ANTLR3_UINT32)strlen((const char *)ptr)); +} + +/** Create a new 16 bit string from the supplied, null terminated, 16 bit string pointer. + * \param[in] factory - Pointer to the string factory that owns strings. + * \param[in] ptr - Pointer to the 16 bit encoded string + * \return Pointer to the newly initialized string + */ +static pANTLR3_STRING +newStr16_16 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 ptr) +{ + pANTLR3_UINT16 in; + ANTLR3_UINT32 count; + + /** First, determine the length of the input string + */ + in = (pANTLR3_UINT16)ptr; + count = 0; + + while (*in++ != '\0') + { + count++; + } + return factory->newPtr(factory, ptr, count); +} + +static void +destroy (pANTLR3_STRING_FACTORY factory, pANTLR3_STRING string) +{ + // Record which string we are deleting + // + ANTLR3_UINT32 strIndex = string->index; + + // Ensure that the string was not factory made, or we would try + // to delete memory that wasn't allocated outside the factory + // block. + // Remove the specific indexed string from the vector + // + factory->strings->del(factory->strings, strIndex); + + // One less string in the vector, so decrement the factory index + // so that the next string allocated is indexed correctly with + // respect to the vector. + // + factory->index--; + + // Now we have to reindex the strings in the vector that followed + // the one we just deleted. We only do this if the one we just deleted + // was not the last one. + // + if (strIndex< factory->index) + { + // We must reindex the strings after the one we just deleted. + // The one that follows the one we just deleted is also out + // of whack, so we start there. + // + ANTLR3_UINT32 i; + + for (i = strIndex; i < factory->index; i++) + { + // Renumber the entry + // + ((pANTLR3_STRING)(factory->strings->elements[i].element))->index = i; + } + } + + // The string has been destroyed and the elements of the factory are reindexed. + // + +} + +static pANTLR3_STRING +printable8(pANTLR3_STRING_FACTORY factory, pANTLR3_STRING instr) +{ + pANTLR3_STRING string; + + /* We don't need to be too efficient here, this is mostly for error messages and so on. + */ + pANTLR3_UINT8 scannedText; + ANTLR3_UINT32 i; + + /* Assume we need as much as twice as much space to parse out the control characters + */ + string = factory->newSize(factory, instr->len *2 + 1); + + /* Scan through and replace unprintable (in terms of this routine) + * characters + */ + scannedText = string->chars; + + for (i = 0; i < instr->len; i++) + { + if (*(instr->chars + i) == '\n') + { + *scannedText++ = '\\'; + *scannedText++ = 'n'; + } + else if (*(instr->chars + i) == '\r') + { + *scannedText++ = '\\'; + *scannedText++ = 'r'; + } + else if (!isprint(*(instr->chars +i))) + { + *scannedText++ = '?'; + } + else + { + *scannedText++ = *(instr->chars + i); + } + } + *scannedText = '\0'; + + string->len = (ANTLR3_UINT32)(scannedText - string->chars); + + return string; +} + +static pANTLR3_STRING +printable16(pANTLR3_STRING_FACTORY factory, pANTLR3_STRING instr) +{ + pANTLR3_STRING string; + + /* We don't need to be too efficient here, this is mostly for error messages and so on. + */ + pANTLR3_UINT16 scannedText; + pANTLR3_UINT16 inText; + ANTLR3_UINT32 i; + ANTLR3_UINT32 outLen; + + /* Assume we need as much as twice as much space to parse out the control characters + */ + string = factory->newSize(factory, instr->len *2 + 1); + + /* Scan through and replace unprintable (in terms of this routine) + * characters + */ + scannedText = (pANTLR3_UINT16)(string->chars); + inText = (pANTLR3_UINT16)(instr->chars); + outLen = 0; + + for (i = 0; i < instr->len; i++) + { + if (*(inText + i) == '\n') + { + *scannedText++ = '\\'; + *scannedText++ = 'n'; + outLen += 2; + } + else if (*(inText + i) == '\r') + { + *scannedText++ = '\\'; + *scannedText++ = 'r'; + outLen += 2; + } + else if (!isprint(*(inText +i))) + { + *scannedText++ = '?'; + outLen++; + } + else + { + *scannedText++ = *(inText + i); + outLen++; + } + } + *scannedText = '\0'; + + string->len = outLen; + + return string; +} + +/** Fascist Capitalist Pig function created + * to oppress the workers comrade. + */ +static void +closeFactory (pANTLR3_STRING_FACTORY factory) +{ + /* Delete the vector we were tracking the strings with, this will + * causes all the allocated strings to be deallocated too + */ + factory->strings->free(factory->strings); + + /* Delete the space for the factory itself + */ + ANTLR3_FREE((void *)factory); +} + +static pANTLR3_UINT8 +append8 (pANTLR3_STRING string, const char * newbit) +{ + ANTLR3_UINT32 len; + + len = (ANTLR3_UINT32)strlen(newbit); + + if (string->size < (string->len + len + 1)) + { + string->chars = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(string->len + len + 1)); + string->size = string->len + len + 1; + } + + /* Note we copy one more byte than the strlen in order to get the trailing + */ + ANTLR3_MEMMOVE((void *)(string->chars + string->len), newbit, (ANTLR3_UINT32)(len+1)); + string->len += len; + + return string->chars; +} + +static pANTLR3_UINT8 +append16_8 (pANTLR3_STRING string, const char * newbit) +{ + ANTLR3_UINT32 len; + pANTLR3_UINT16 apPoint; + ANTLR3_UINT32 count; + + len = (ANTLR3_UINT32)strlen(newbit); + + if (string->size < (string->len + len + 1)) + { + string->chars = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)((sizeof(ANTLR3_UINT16)*(string->len + len + 1)))); + string->size = string->len + len + 1; + } + + apPoint = ((pANTLR3_UINT16)string->chars) + string->len; + string->len += len; + + for (count = 0; count < len; count++) + { + *apPoint++ = *(newbit + count); + } + *apPoint = '\0'; + + return string->chars; +} + +static pANTLR3_UINT8 +append16_16 (pANTLR3_STRING string, const char * newbit) +{ + ANTLR3_UINT32 len; + pANTLR3_UINT16 in; + + /** First, determine the length of the input string + */ + in = (pANTLR3_UINT16)newbit; + len = 0; + + while (*in++ != '\0') + { + len++; + } + + if (string->size < (string->len + len + 1)) + { + string->chars = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)( sizeof(ANTLR3_UINT16) *(string->len + len + 1) )); + string->size = string->len + len + 1; + } + + /* Note we copy one more byte than the strlen in order to get the trailing delimiter + */ + ANTLR3_MEMMOVE((void *)(((pANTLR3_UINT16)string->chars) + string->len), newbit, (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(len+1))); + string->len += len; + + return string->chars; +} + +static pANTLR3_UINT8 +set8 (pANTLR3_STRING string, const char * chars) +{ + ANTLR3_UINT32 len; + + len = (ANTLR3_UINT32)strlen(chars); + if (string->size < len + 1) + { + string->chars = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(len + 1)); + string->size = len + 1; + } + + /* Note we copy one more byte than the strlen in order to get the trailing '\0' + */ + ANTLR3_MEMMOVE((void *)(string->chars), chars, (ANTLR3_UINT32)(len+1)); + string->len = len; + + return string->chars; + +} + +static pANTLR3_UINT8 +set16_8 (pANTLR3_STRING string, const char * chars) +{ + ANTLR3_UINT32 len; + ANTLR3_UINT32 count; + pANTLR3_UINT16 apPoint; + + len = (ANTLR3_UINT32)strlen(chars); + if (string->size < len + 1) + { + string->chars = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(len + 1))); + string->size = len + 1; + } + apPoint = ((pANTLR3_UINT16)string->chars); + string->len = len; + + for (count = 0; count < string->len; count++) + { + *apPoint++ = *(chars + count); + } + *apPoint = '\0'; + + return string->chars; +} + +static pANTLR3_UINT8 +set16_16 (pANTLR3_STRING string, const char * chars) +{ + ANTLR3_UINT32 len; + pANTLR3_UINT16 in; + + /** First, determine the length of the input string + */ + in = (pANTLR3_UINT16)chars; + len = 0; + + while (*in++ != '\0') + { + len++; + } + + if (string->size < len + 1) + { + string->chars = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(len + 1))); + string->size = len + 1; + } + + /* Note we copy one more byte than the strlen in order to get the trailing '\0' + */ + ANTLR3_MEMMOVE((void *)(string->chars), chars, (ANTLR3_UINT32)((len+1) * sizeof(ANTLR3_UINT16))); + string->len = len; + + return string->chars; + +} + +static pANTLR3_UINT8 +addc8 (pANTLR3_STRING string, ANTLR3_UINT32 c) +{ + if (string->size < string->len + 2) + { + string->chars = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(string->len + 2)); + string->size = string->len + 2; + } + *(string->chars + string->len) = (ANTLR3_UINT8)c; + *(string->chars + string->len + 1) = '\0'; + string->len++; + + return string->chars; +} + +static pANTLR3_UINT8 +addc16 (pANTLR3_STRING string, ANTLR3_UINT32 c) +{ + pANTLR3_UINT16 ptr; + + if (string->size < string->len + 2) + { + string->chars = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16) * (string->len + 2))); + string->size = string->len + 2; + } + ptr = (pANTLR3_UINT16)(string->chars); + + *(ptr + string->len) = (ANTLR3_UINT16)c; + *(ptr + string->len + 1) = '\0'; + string->len++; + + return string->chars; +} + +static pANTLR3_UINT8 +addi8 (pANTLR3_STRING string, ANTLR3_INT32 i) +{ + ANTLR3_UINT8 newbit[32]; + + sprintf((char *)newbit, "%d", i); + + return string->append8(string, (const char *)newbit); +} +static pANTLR3_UINT8 +addi16 (pANTLR3_STRING string, ANTLR3_INT32 i) +{ + ANTLR3_UINT8 newbit[32]; + + sprintf((char *)newbit, "%d", i); + + return string->append8(string, (const char *)newbit); +} + +static pANTLR3_UINT8 +inserti8 (pANTLR3_STRING string, ANTLR3_UINT32 point, ANTLR3_INT32 i) +{ + ANTLR3_UINT8 newbit[32]; + + sprintf((char *)newbit, "%d", i); + return string->insert8(string, point, (const char *)newbit); +} +static pANTLR3_UINT8 +inserti16 (pANTLR3_STRING string, ANTLR3_UINT32 point, ANTLR3_INT32 i) +{ + ANTLR3_UINT8 newbit[32]; + + sprintf((char *)newbit, "%d", i); + return string->insert8(string, point, (const char *)newbit); +} + +static pANTLR3_UINT8 +insert8 (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit) +{ + ANTLR3_UINT32 len; + + if (point >= string->len) + { + return string->append(string, newbit); + } + + len = (ANTLR3_UINT32)strlen(newbit); + + if (len == 0) + { + return string->chars; + } + + if (string->size < (string->len + len + 1)) + { + string->chars = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(string->len + len + 1)); + string->size = string->len + len + 1; + } + + /* Move the characters we are inserting before, including the delimiter + */ + ANTLR3_MEMMOVE((void *)(string->chars + point + len), (void *)(string->chars + point), (ANTLR3_UINT32)(string->len - point + 1)); + + /* Note we copy the exact number of bytes + */ + ANTLR3_MEMMOVE((void *)(string->chars + point), newbit, (ANTLR3_UINT32)(len)); + + string->len += len; + + return string->chars; +} + +static pANTLR3_UINT8 +insert16_8 (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit) +{ + ANTLR3_UINT32 len; + ANTLR3_UINT32 count; + pANTLR3_UINT16 inPoint; + + if (point >= string->len) + { + return string->append8(string, newbit); + } + + len = (ANTLR3_UINT32)strlen(newbit); + + if (len == 0) + { + return string->chars; + } + + if (string->size < (string->len + len + 1)) + { + string->chars = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(string->len + len + 1))); + string->size = string->len + len + 1; + } + + /* Move the characters we are inserting before, including the delimiter + */ + ANTLR3_MEMMOVE((void *)(((pANTLR3_UINT16)string->chars) + point + len), (void *)(((pANTLR3_UINT16)string->chars) + point), (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(string->len - point + 1))); + + string->len += len; + + inPoint = ((pANTLR3_UINT16)(string->chars))+point; + for (count = 0; count<len; count++) + { + *(inPoint + count) = (ANTLR3_UINT16)(*(newbit+count)); + } + + return string->chars; +} + +static pANTLR3_UINT8 +insert16_16 (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit) +{ + ANTLR3_UINT32 len; + pANTLR3_UINT16 in; + + if (point >= string->len) + { + return string->append(string, newbit); + } + + /** First, determine the length of the input string + */ + in = (pANTLR3_UINT16)newbit; + len = 0; + + while (*in++ != '\0') + { + len++; + } + + if (len == 0) + { + return string->chars; + } + + if (string->size < (string->len + len + 1)) + { + string->chars = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(string->len + len + 1))); + string->size = string->len + len + 1; + } + + /* Move the characters we are inserting before, including the delimiter + */ + ANTLR3_MEMMOVE((void *)(((pANTLR3_UINT16)string->chars) + point + len), (void *)(((pANTLR3_UINT16)string->chars) + point), (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(string->len - point + 1))); + + + /* Note we copy the exact number of characters + */ + ANTLR3_MEMMOVE((void *)(((pANTLR3_UINT16)string->chars) + point), newbit, (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(len))); + + string->len += len; + + return string->chars; +} + +static pANTLR3_UINT8 setS (pANTLR3_STRING string, pANTLR3_STRING chars) +{ + return string->set(string, (const char *)(chars->chars)); +} + +static pANTLR3_UINT8 appendS (pANTLR3_STRING string, pANTLR3_STRING newbit) +{ + /* We may be passed an empty string, in which case we just return the current pointer + */ + if (newbit == NULL || newbit->len == 0 || newbit->size == 0 || newbit->chars == NULL) + { + return string->chars; + } + else + { + return string->append(string, (const char *)(newbit->chars)); + } +} + +static pANTLR3_UINT8 insertS (pANTLR3_STRING string, ANTLR3_UINT32 point, pANTLR3_STRING newbit) +{ + return string->insert(string, point, (const char *)(newbit->chars)); +} + +/* Function that compares the text of a string to the supplied + * 8 bit character string and returns a result a la strcmp() + */ +static ANTLR3_UINT32 +compare8 (pANTLR3_STRING string, const char * compStr) +{ + return strcmp((const char *)(string->chars), compStr); +} + +/* Function that compares the text of a string with the supplied character string + * (which is assumed to be in the same encoding as the string itself) and returns a result + * a la strcmp() + */ +static ANTLR3_UINT32 +compare16_8 (pANTLR3_STRING string, const char * compStr) +{ + pANTLR3_UINT16 ourString; + ANTLR3_UINT32 charDiff; + + ourString = (pANTLR3_UINT16)(string->chars); + + while (((ANTLR3_UCHAR)(*ourString) != '\0') && ((ANTLR3_UCHAR)(*compStr) != '\0')) + { + charDiff = *ourString - *compStr; + if (charDiff != 0) + { + return charDiff; + } + ourString++; + compStr++; + } + + /* At this point, one of the strings was terminated + */ + return (ANTLR3_UINT32)((ANTLR3_UCHAR)(*ourString) - (ANTLR3_UCHAR)(*compStr)); + +} + +/* Function that compares the text of a string with the supplied character string + * (which is assumed to be in the same encoding as the string itself) and returns a result + * a la strcmp() + */ +static ANTLR3_UINT32 +compare16_16 (pANTLR3_STRING string, const char * compStr8) +{ + pANTLR3_UINT16 ourString; + pANTLR3_UINT16 compStr; + ANTLR3_UINT32 charDiff; + + ourString = (pANTLR3_UINT16)(string->chars); + compStr = (pANTLR3_UINT16)(compStr8); + + while (((ANTLR3_UCHAR)(*ourString) != '\0') && ((ANTLR3_UCHAR)(*((pANTLR3_UINT16)compStr)) != '\0')) + { + charDiff = *ourString - *compStr; + if (charDiff != 0) + { + return charDiff; + } + ourString++; + compStr++; + } + + /* At this point, one of the strings was terminated + */ + return (ANTLR3_UINT32)((ANTLR3_UCHAR)(*ourString) - (ANTLR3_UCHAR)(*compStr)); +} + +/* Function that compares the text of a string with the supplied string + * (which is assumed to be in the same encoding as the string itself) and returns a result + * a la strcmp() + */ +static ANTLR3_UINT32 +compareS (pANTLR3_STRING string, pANTLR3_STRING compStr) +{ + return string->compare(string, (const char *)compStr->chars); +} + + +/* Function that returns the character indexed at the supplied + * offset as a 32 bit character. + */ +static ANTLR3_UCHAR +charAt8 (pANTLR3_STRING string, ANTLR3_UINT32 offset) +{ + if (offset > string->len) + { + return (ANTLR3_UCHAR)'\0'; + } + else + { + return (ANTLR3_UCHAR)(*(string->chars + offset)); + } +} + +/* Function that returns the character indexed at the supplied + * offset as a 32 bit character. + */ +static ANTLR3_UCHAR +charAt16 (pANTLR3_STRING string, ANTLR3_UINT32 offset) +{ + if (offset > string->len) + { + return (ANTLR3_UCHAR)'\0'; + } + else + { + return (ANTLR3_UCHAR)(*((pANTLR3_UINT16)(string->chars) + offset)); + } +} + +/* Function that returns a substring of the supplied string a la .subString(s,e) + * in java runtimes. + */ +static pANTLR3_STRING +subString8 (pANTLR3_STRING string, ANTLR3_UINT32 startIndex, ANTLR3_UINT32 endIndex) +{ + pANTLR3_STRING newStr; + + if (endIndex > string->len) + { + endIndex = string->len + 1; + } + newStr = string->factory->newPtr(string->factory, string->chars + startIndex, endIndex - startIndex); + + return newStr; +} + +/* Returns a substring of the supplied string a la .subString(s,e) + * in java runtimes. + */ +static pANTLR3_STRING +subString16 (pANTLR3_STRING string, ANTLR3_UINT32 startIndex, ANTLR3_UINT32 endIndex) +{ + pANTLR3_STRING newStr; + + if (endIndex > string->len) + { + endIndex = string->len + 1; + } + newStr = string->factory->newPtr(string->factory, (pANTLR3_UINT8)((pANTLR3_UINT16)(string->chars) + startIndex), endIndex - startIndex); + + return newStr; +} + +/* Function that can convert the characters in the string to an integer + */ +static ANTLR3_INT32 +toInt32_8 (struct ANTLR3_STRING_struct * string) +{ + return atoi((const char *)(string->chars)); +} + +/* Function that can convert the characters in the string to an integer + */ +static ANTLR3_INT32 +toInt32_16 (struct ANTLR3_STRING_struct * string) +{ + pANTLR3_UINT16 input; + ANTLR3_INT32 value; + ANTLR3_BOOLEAN negate; + + value = 0; + input = (pANTLR3_UINT16)(string->chars); + negate = ANTLR3_FALSE; + + if (*input == (ANTLR3_UCHAR)'-') + { + negate = ANTLR3_TRUE; + input++; + } + else if (*input == (ANTLR3_UCHAR)'+') + { + input++; + } + + while (*input != '\0' && isdigit(*input)) + { + value = value * 10; + value += ((ANTLR3_UINT32)(*input) - (ANTLR3_UINT32)'0'); + input++; + } + + return negate ? -value : value; +} + +/* Function that returns a pointer to an 8 bit version of the string, + * which in this case is just the string as this is + * 8 bit encodiing anyway. + */ +static pANTLR3_STRING to8_8 (pANTLR3_STRING string) +{ + return string; +} + +/* Function that returns an 8 bit version of the string, + * which in this case is returning all the 16 bit characters + * narrowed back into 8 bits, with characters that are too large + * replaced with '_' + */ +static pANTLR3_STRING to8_16 (pANTLR3_STRING string) +{ + pANTLR3_STRING newStr; + ANTLR3_UINT32 i; + + /* Create a new 8 bit string + */ + newStr = newRaw8(string->factory); + + if (newStr == NULL) + { + return NULL; + } + + /* Always add one more byte for a terminator + */ + newStr->chars = (pANTLR3_UINT8) ANTLR3_MALLOC((size_t)(string->len + 1)); + newStr->size = string->len + 1; + newStr->len = string->len; + + /* Now copy each 16 bit charActer , making it an 8 bit character of + * some sort. + */ + for (i=0; i<string->len; i++) + { + ANTLR3_UCHAR c; + + c = *(((pANTLR3_UINT16)(string->chars)) + i); + + *(newStr->chars + i) = (ANTLR3_UINT8)(c > 255 ? '_' : c); + } + + /* Terminate + */ + *(newStr->chars + newStr->len) = '\0'; + + return newStr; +} diff --git a/antlr-3.1.3/runtime/C/src/antlr3stringstream.c b/antlr-3.1.3/runtime/C/src/antlr3stringstream.c new file mode 100644 index 0000000..a0c3f99 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3stringstream.c @@ -0,0 +1,206 @@ +/// \file +/// Provides implementations of string (or memory) streams as input +/// for ANLTR3 lexers. +/// + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3.h> + +/// \brief Create an in-place ASCII string stream as input to ANTLR 3. +/// +/// An in-place string steam is the preferred method of supplying strings to ANTLR as input +/// for lexing and compiling. This is because we make no copies of the input string but +/// read from it right where it is. +/// +/// \param[in] inString Pointer to the string to be used as the input stream +/// \param[in] size Size (in 8 bit ASCII characters) of the input string +/// \param[in] name NAme to attach the input stream (can be NULL pointer) +/// +/// \return +/// - Pointer to new input stream context upon success +/// - One of the ANTLR3_ERR_ defines on error. +/// +/// \remark +/// - ANTLR does not alter the input string in any way. +/// - String is slightly incorrect in that the passed in pointer can be to any +/// memory in C version of ANTLR3 of course. +//// +ANTLR3_API pANTLR3_INPUT_STREAM +antlr3NewAsciiStringInPlaceStream (pANTLR3_UINT8 inString, ANTLR3_UINT32 size, pANTLR3_UINT8 name) +{ + // Pointer to the input stream we are going to create + // + pANTLR3_INPUT_STREAM input; + + // Allocate memory for the input stream structure + // + input = (pANTLR3_INPUT_STREAM) + ANTLR3_MALLOC(sizeof(ANTLR3_INPUT_STREAM)); + + if (input == NULL) + { + return NULL; + } + + // Structure was allocated correctly, now we can install the pointer. + // + input->isAllocated = ANTLR3_FALSE; + input->data = inString; + input->sizeBuf = size; + + // Call the common 8 bit ASCII input stream handler initializer. + // + antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM); + + // Now we can set up the file name + // + input->istream->streamName = input->strFactory->newStr(input->strFactory, name == NULL ? (pANTLR3_UINT8)"-memory-" : name); + input->fileName = input->istream->streamName; + + return input; +} + +/// \brief Create an in-place UCS2 string stream as input to ANTLR 3. +/// +/// An in-place string steam is the preferred method of supplying strings to ANTLR as input +/// for lexing and compiling. This is because we make no copies of the input string but +/// read from it right where it is. +/// +/// \param[in] inString Pointer to the string to be used as the input stream +/// \param[in] size Size (in 16 bit ASCII characters) of the input string +/// \param[in] name Name to attach the input stream (can be NULL pointer) +/// +/// \return +/// - Pointer to new input stream context upon success +/// - One of the ANTLR3_ERR_ defines on error. +/// +/// \remark +/// - ANTLR does not alter the input string in any way. +/// - String is slightly incorrect in that the passed in pointer can be to any +/// memory in C version of ANTLR3 of course. +//// +ANTLR3_API pANTLR3_INPUT_STREAM +antlr3NewUCS2StringInPlaceStream (pANTLR3_UINT16 inString, ANTLR3_UINT32 size, pANTLR3_UINT16 name) +{ + // Pointer to the input stream we are going to create + // + pANTLR3_INPUT_STREAM input; + + // Layout default file name string in correct encoding + // + ANTLR3_UINT16 defaultName[] = { '-', 'm', 'e', 'm', 'o', 'r', 'y', '-', '\0' }; + + // Allocate memory for the input stream structure + // + input = (pANTLR3_INPUT_STREAM) + ANTLR3_MALLOC(sizeof(ANTLR3_INPUT_STREAM)); + + if (input == NULL) + { + return NULL; + } + + // Structure was allocated correctly, now we can install the pointer. + // + input->isAllocated = ANTLR3_FALSE; + input->data = inString; + input->sizeBuf = size; + + // Call the common 16 bit input stream handler initializer. + // + antlr3UCS2SetupStream (input, ANTLR3_CHARSTREAM); + + input->istream->streamName = input->strFactory->newStr(input->strFactory, name == NULL ? (pANTLR3_UINT8)defaultName : (pANTLR3_UINT8)name); + input->fileName = input->istream->streamName; + + + return input; +} + +/// \brief Create an ASCII string stream as input to ANTLR 3, copying the input string. +/// +/// This string stream first makes a copy of the string at the supplied pointer +/// +/// \param[in] inString Pointer to the string to be copied as the input stream +/// \param[in] size Size (in 8 bit ASCII characters) of the input string +/// \param[in] name NAme to attach the input stream (can be NULL pointer) +/// +/// \return +/// - Pointer to new input stream context upon success +/// - One of the ANTLR3_ERR_ defines on error. +/// +/// \remark +/// - ANTLR does not alter the input string in any way. +/// - String is slightly incorrect in that the passed in pointer can be to any +/// memory in C version of ANTLR3 of course. +//// +pANTLR3_INPUT_STREAM antlr3NewAsciiStringCopyStream (pANTLR3_UINT8 inString, ANTLR3_UINT32 size, pANTLR3_UINT8 name) +{ + // Pointer to the input stream we are going to create + // + pANTLR3_INPUT_STREAM input; + + // Allocate memory for the input stream structure + // + input = (pANTLR3_INPUT_STREAM) + ANTLR3_MALLOC(sizeof(ANTLR3_INPUT_STREAM)); + + if (input == NULL) + { + return NULL; + } + + // Indicate that we allocated this input and allocate it + // + input->isAllocated = ANTLR3_TRUE; + input->data = ANTLR3_MALLOC((size_t)size); + + if (input->data == NULL) + { + return NULL; + } + + // Structure was allocated correctly, now we can install the pointer and set the size. + // + ANTLR3_MEMMOVE(input->data, (const void *)inString, size); + input->sizeBuf = size; + + // Call the common 8 bit ASCII input stream handler + // initializer type thingy doobry function. + // + antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM); + + + input->istream->streamName = input->strFactory->newStr(input->strFactory, name == NULL ? (pANTLR3_UINT8)"-memory-" : name); + input->fileName = input->istream->streamName; + + return input; +} diff --git a/antlr-3.1.3/runtime/C/src/antlr3tokenstream.c b/antlr-3.1.3/runtime/C/src/antlr3tokenstream.c new file mode 100644 index 0000000..4004759 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3tokenstream.c @@ -0,0 +1,1042 @@ +/// \file +/// Default implementation of CommonTokenStream +/// + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3tokenstream.h> + +#ifdef ANTLR3_WINDOWS +#pragma warning( disable : 4100 ) +#endif + +// COMMON_TOKEN_STREAM API +// +static void setTokenTypeChannel (pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_UINT32 ttype, ANTLR3_UINT32 channel); +static void discardTokenType (pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_INT32 ttype); +static void discardOffChannel (pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_BOOLEAN discard); +static pANTLR3_VECTOR getTokens (pANTLR3_COMMON_TOKEN_STREAM cts); +static pANTLR3_LIST getTokenRange (pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_UINT32 start, ANTLR3_UINT32 stop); +static pANTLR3_LIST getTokensSet (pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_UINT32 start, ANTLR3_UINT32 stop, pANTLR3_BITSET types); +static pANTLR3_LIST getTokensList (pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_UINT32 start, ANTLR3_UINT32 stop, pANTLR3_LIST list); +static pANTLR3_LIST getTokensType (pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_UINT32 start, ANTLR3_UINT32 stop, ANTLR3_UINT32 type); + +// TOKEN_STREAM API +// +static pANTLR3_COMMON_TOKEN tokLT (pANTLR3_TOKEN_STREAM ts, ANTLR3_INT32 k); +static pANTLR3_COMMON_TOKEN dbgTokLT (pANTLR3_TOKEN_STREAM ts, ANTLR3_INT32 k); +static pANTLR3_COMMON_TOKEN get (pANTLR3_TOKEN_STREAM ts, ANTLR3_UINT32 i); +static pANTLR3_TOKEN_SOURCE getTokenSource (pANTLR3_TOKEN_STREAM ts); +static void setTokenSource (pANTLR3_TOKEN_STREAM ts, pANTLR3_TOKEN_SOURCE tokenSource); +static pANTLR3_STRING toString (pANTLR3_TOKEN_STREAM ts); +static pANTLR3_STRING toStringSS (pANTLR3_TOKEN_STREAM ts, ANTLR3_UINT32 start, ANTLR3_UINT32 stop); +static pANTLR3_STRING toStringTT (pANTLR3_TOKEN_STREAM ts, pANTLR3_COMMON_TOKEN start, pANTLR3_COMMON_TOKEN stop); +static void setDebugListener (pANTLR3_TOKEN_STREAM ts, pANTLR3_DEBUG_EVENT_LISTENER debugger); + +// INT STREAM API +// +static void consume (pANTLR3_INT_STREAM is); +static void dbgConsume (pANTLR3_INT_STREAM is); +static ANTLR3_UINT32 _LA (pANTLR3_INT_STREAM is, ANTLR3_INT32 i); +static ANTLR3_UINT32 dbgLA (pANTLR3_INT_STREAM is, ANTLR3_INT32 i); +static ANTLR3_MARKER mark (pANTLR3_INT_STREAM is); +static ANTLR3_MARKER dbgMark (pANTLR3_INT_STREAM is); +static void release (pANTLR3_INT_STREAM is, ANTLR3_MARKER mark); +static ANTLR3_UINT32 size (pANTLR3_INT_STREAM is); +static ANTLR3_MARKER tindex (pANTLR3_INT_STREAM is); +static void rewindStream (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker); +static void dbgRewindStream (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker); +static void rewindLast (pANTLR3_INT_STREAM is); +static void dbgRewindLast (pANTLR3_INT_STREAM is); +static void seek (pANTLR3_INT_STREAM is, ANTLR3_MARKER index); +static void dbgSeek (pANTLR3_INT_STREAM is, ANTLR3_MARKER index); +static pANTLR3_STRING getSourceName (pANTLR3_INT_STREAM is); +static void antlr3TokenStreamFree (pANTLR3_TOKEN_STREAM stream); +static void antlr3CTSFree (pANTLR3_COMMON_TOKEN_STREAM stream); + +// Helpers +// +static void fillBuffer (pANTLR3_COMMON_TOKEN_STREAM tokenStream); +static ANTLR3_UINT32 skipOffTokenChannels (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_INT32 i); +static ANTLR3_UINT32 skipOffTokenChannelsReverse (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_INT32 i); +static pANTLR3_COMMON_TOKEN LB (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_INT32 i); + +ANTLR3_API pANTLR3_TOKEN_STREAM +antlr3TokenStreamNew() +{ + pANTLR3_TOKEN_STREAM stream; + + // Memory for the interface structure + // + stream = (pANTLR3_TOKEN_STREAM) ANTLR3_MALLOC(sizeof(ANTLR3_TOKEN_STREAM)); + + if (stream == NULL) + { + return NULL; + } + + // Install basic API + // + stream->free = antlr3TokenStreamFree; + + + return stream; +} + +static void +antlr3TokenStreamFree(pANTLR3_TOKEN_STREAM stream) +{ + ANTLR3_FREE(stream); +} + +static void +antlr3CTSFree (pANTLR3_COMMON_TOKEN_STREAM stream) +{ + // We only free up our subordinate interfaces if they belong + // to us, otherwise we let whoever owns them deal with them. + // + if (stream->tstream->super == stream) + { + if (stream->tstream->istream->super == stream->tstream) + { + stream->tstream->istream->free(stream->tstream->istream); + stream->tstream->istream = NULL; + } + stream->tstream->free(stream->tstream); + } + + // Now we free our own resources + // + if (stream->tokens != NULL) + { + stream->tokens->free(stream->tokens); + stream->tokens = NULL; + } + if (stream->discardSet != NULL) + { + stream->discardSet->free(stream->discardSet); + stream->discardSet = NULL; + } + if (stream->channelOverrides != NULL) + { + stream->channelOverrides->free(stream->channelOverrides); + stream->channelOverrides = NULL; + } + + // Free our memory now + // + ANTLR3_FREE(stream); +} + +ANTLR3_API pANTLR3_COMMON_TOKEN_STREAM +antlr3CommonTokenDebugStreamSourceNew(ANTLR3_UINT32 hint, pANTLR3_TOKEN_SOURCE source, pANTLR3_DEBUG_EVENT_LISTENER debugger) +{ + pANTLR3_COMMON_TOKEN_STREAM stream; + + // Create a standard token stream + // + stream = antlr3CommonTokenStreamSourceNew(hint, source); + + // Install the debugger object + // + stream->tstream->debugger = debugger; + + // Override standard token stream methods with debugging versions + // + stream->tstream->initialStreamState = ANTLR3_FALSE; + + stream->tstream->_LT = dbgTokLT; + + stream->tstream->istream->consume = dbgConsume; + stream->tstream->istream->_LA = dbgLA; + stream->tstream->istream->mark = dbgMark; + stream->tstream->istream->rewind = dbgRewindStream; + stream->tstream->istream->rewindLast = dbgRewindLast; + stream->tstream->istream->seek = dbgSeek; + + return stream; +} + +ANTLR3_API pANTLR3_COMMON_TOKEN_STREAM +antlr3CommonTokenStreamSourceNew(ANTLR3_UINT32 hint, pANTLR3_TOKEN_SOURCE source) +{ + pANTLR3_COMMON_TOKEN_STREAM stream; + + stream = antlr3CommonTokenStreamNew(hint); + + stream->channel = ANTLR3_TOKEN_DEFAULT_CHANNEL; + + stream->channelOverrides = NULL; + stream->discardSet = NULL; + stream->discardOffChannel = ANTLR3_FALSE; + + stream->tstream->setTokenSource(stream->tstream, source); + + stream->free = antlr3CTSFree; + return stream; +} + +ANTLR3_API pANTLR3_COMMON_TOKEN_STREAM +antlr3CommonTokenStreamNew(ANTLR3_UINT32 hint) +{ + pANTLR3_COMMON_TOKEN_STREAM stream; + + /* Memory for the interface structure + */ + stream = (pANTLR3_COMMON_TOKEN_STREAM) ANTLR3_MALLOC(sizeof(ANTLR3_COMMON_TOKEN_STREAM)); + + if (stream == NULL) + { + return NULL; + } + + /* Create space for the token stream interface + */ + stream->tstream = antlr3TokenStreamNew(); + stream->tstream->super = stream; + + /* Create space for the INT_STREAM interfacce + */ + stream->tstream->istream = antlr3IntStreamNew(); + stream->tstream->istream->super = (stream->tstream); + stream->tstream->istream->type = ANTLR3_TOKENSTREAM; + + /* Install the token tracking tables + */ + stream->tokens = antlr3VectorNew(0); + + /* Defaults + */ + stream->p = -1; + + /* Install the common token stream API + */ + stream->setTokenTypeChannel = setTokenTypeChannel; + stream->discardTokenType = discardTokenType; + stream->discardOffChannelToks = discardOffChannel; + stream->getTokens = getTokens; + stream->getTokenRange = getTokenRange; + stream->getTokensSet = getTokensSet; + stream->getTokensList = getTokensList; + stream->getTokensType = getTokensType; + + /* Install the token stream API + */ + stream->tstream->_LT = tokLT; + stream->tstream->get = get; + stream->tstream->getTokenSource = getTokenSource; + stream->tstream->setTokenSource = setTokenSource; + stream->tstream->toString = toString; + stream->tstream->toStringSS = toStringSS; + stream->tstream->toStringTT = toStringTT; + stream->tstream->setDebugListener = setDebugListener; + + /* Install INT_STREAM interface + */ + stream->tstream->istream->_LA = _LA; + stream->tstream->istream->mark = mark; + stream->tstream->istream->release = release; + stream->tstream->istream->size = size; + stream->tstream->istream->index = tindex; + stream->tstream->istream->rewind = rewindStream; + stream->tstream->istream->rewindLast= rewindLast; + stream->tstream->istream->seek = seek; + stream->tstream->istream->consume = consume; + stream->tstream->istream->getSourceName = getSourceName; + + return stream; +} + +// Install a debug listener adn switch to debug mode methods +// +static void +setDebugListener (pANTLR3_TOKEN_STREAM ts, pANTLR3_DEBUG_EVENT_LISTENER debugger) +{ + // Install the debugger object + // + ts->debugger = debugger; + + // Override standard token stream methods with debugging versions + // + ts->initialStreamState = ANTLR3_FALSE; + + ts->_LT = dbgTokLT; + + ts->istream->consume = dbgConsume; + ts->istream->_LA = dbgLA; + ts->istream->mark = dbgMark; + ts->istream->rewind = dbgRewindStream; + ts->istream->rewindLast = dbgRewindLast; + ts->istream->seek = dbgSeek; +} + +/** Get the ith token from the current position 1..n where k=1 is the +* first symbol of lookahead. +*/ +static pANTLR3_COMMON_TOKEN +tokLT (pANTLR3_TOKEN_STREAM ts, ANTLR3_INT32 k) +{ + ANTLR3_INT32 i; + ANTLR3_INT32 n; + pANTLR3_COMMON_TOKEN_STREAM cts; + + cts = (pANTLR3_COMMON_TOKEN_STREAM)ts->super; + + if (k < 0) + { + return LB(cts, -k); + } + + if (cts->p == -1) + { + fillBuffer(cts); + } + if (k == 0) + { + return NULL; + } + + if ((cts->p + k - 1) >= (ANTLR3_INT32)ts->istream->cachedSize) + { + pANTLR3_COMMON_TOKEN teof = &(ts->tokenSource->eofToken); + + teof->setStartIndex (teof, ts->istream->index (ts->istream)); + teof->setStopIndex (teof, ts->istream->index (ts->istream)); + return teof; + } + + i = cts->p; + n = 1; + + /* Need to find k good tokens, skipping ones that are off channel + */ + while ( n < k) + { + /* Skip off-channel tokens */ + i = skipOffTokenChannels(cts, i+1); /* leave p on valid token */ + n++; + } + if ( (ANTLR3_UINT32) i >= ts->istream->cachedSize) + { + pANTLR3_COMMON_TOKEN teof = &(ts->tokenSource->eofToken); + + teof->setStartIndex (teof, ts->istream->index(ts->istream)); + teof->setStopIndex (teof, ts->istream->index(ts->istream)); + return teof; + } + + // Here the token must be in the input vector. Rather then incut + // function call penalty, we jsut return the pointer directly + // from the vector + // + return (pANTLR3_COMMON_TOKEN)cts->tokens->elements[i].element; + //return (pANTLR3_COMMON_TOKEN)cts->tokens->get(cts->tokens, i); +} + +/// Debug only method to flag consumption of initial off-channel +/// tokens in the input stream +/// +static void +consumeInitialHiddenTokens(pANTLR3_INT_STREAM is) +{ + ANTLR3_MARKER first; + ANTLR3_INT32 i; + pANTLR3_TOKEN_STREAM ts; + + ts = (pANTLR3_TOKEN_STREAM) is->super; + first = is->index(is); + + for (i=0; i<first; i++) + { + ts->debugger->consumeHiddenToken(ts->debugger, ts->get(ts, i)); + } + + ts->initialStreamState = ANTLR3_FALSE; + +} + +/// As per the normal tokLT but sends information to the debugger +/// +static pANTLR3_COMMON_TOKEN +dbgTokLT (pANTLR3_TOKEN_STREAM ts, ANTLR3_INT32 k) +{ + if (ts->initialStreamState == ANTLR3_TRUE) + { + consumeInitialHiddenTokens(ts->istream); + } + return tokLT(ts, k); +} + +#ifdef ANTLR3_WINDOWS + /* When fully optimized VC7 complains about non reachable code. + * Not yet sure if this is an optimizer bug, or a bug in the flow analysis + */ +#pragma warning( disable : 4702 ) +#endif + +static pANTLR3_COMMON_TOKEN +LB(pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_INT32 k) +{ + ANTLR3_INT32 i; + ANTLR3_INT32 n; + + if (cts->p == -1) + { + fillBuffer(cts); + } + if (k == 0) + { + return NULL; + } + if ((cts->p - k) < 0) + { + return NULL; + } + + i = cts->p; + n = 1; + + /* Need to find k good tokens, going backwards, skipping ones that are off channel + */ + while (n <= (ANTLR3_INT32) k) + { + /* Skip off-channel tokens + */ + + i = skipOffTokenChannelsReverse(cts, i - 1); /* leave p on valid token */ + n++; + } + if (i < 0) + { + return NULL; + } + // Here the token must be in the input vector. Rather then incut + // function call penalty, we jsut return the pointer directly + // from the vector + // + return (pANTLR3_COMMON_TOKEN)cts->tokens->elements[i].element; +} + +static pANTLR3_COMMON_TOKEN +get (pANTLR3_TOKEN_STREAM ts, ANTLR3_UINT32 i) +{ + pANTLR3_COMMON_TOKEN_STREAM cts; + + cts = (pANTLR3_COMMON_TOKEN_STREAM)ts->super; + + return (pANTLR3_COMMON_TOKEN)(cts->tokens->get(cts->tokens, i)); /* Token index is zero based but vectors are 1 based */ +} + +static pANTLR3_TOKEN_SOURCE +getTokenSource (pANTLR3_TOKEN_STREAM ts) +{ + return ts->tokenSource; +} + +static void +setTokenSource ( pANTLR3_TOKEN_STREAM ts, + pANTLR3_TOKEN_SOURCE tokenSource) +{ + ts->tokenSource = tokenSource; +} + +static pANTLR3_STRING +toString (pANTLR3_TOKEN_STREAM ts) +{ + pANTLR3_COMMON_TOKEN_STREAM cts; + + cts = (pANTLR3_COMMON_TOKEN_STREAM)ts->super; + + if (cts->p == -1) + { + fillBuffer(cts); + } + + return ts->toStringSS(ts, 0, ts->istream->size(ts->istream)); +} + +static pANTLR3_STRING +toStringSS(pANTLR3_TOKEN_STREAM ts, ANTLR3_UINT32 start, ANTLR3_UINT32 stop) +{ + pANTLR3_STRING string; + pANTLR3_TOKEN_SOURCE tsource; + pANTLR3_COMMON_TOKEN tok; + ANTLR3_UINT32 i; + pANTLR3_COMMON_TOKEN_STREAM cts; + + cts = (pANTLR3_COMMON_TOKEN_STREAM) ts->super; + + if (cts->p == -1) + { + fillBuffer(cts); + } + if (stop >= ts->istream->size(ts->istream)) + { + stop = ts->istream->size(ts->istream) - 1; + } + + /* Who is giving us these tokens? + */ + tsource = ts->getTokenSource(ts); + + if (tsource != NULL && cts->tokens != NULL) + { + /* Finally, let's get a string + */ + string = tsource->strFactory->newRaw(tsource->strFactory); + + for (i = start; i <= stop; i++) + { + tok = ts->get(ts, i); + if (tok != NULL) + { + string->appendS(string, tok->getText(tok)); + } + } + + return string; + } + return NULL; + +} + +static pANTLR3_STRING +toStringTT (pANTLR3_TOKEN_STREAM ts, pANTLR3_COMMON_TOKEN start, pANTLR3_COMMON_TOKEN stop) +{ + if (start != NULL && stop != NULL) + { + return ts->toStringSS(ts, (ANTLR3_UINT32)start->getTokenIndex(start), (ANTLR3_UINT32)stop->getTokenIndex(stop)); + } + else + { + return NULL; + } +} + +/** Move the input pointer to the next incoming token. The stream + * must become active with LT(1) available. consume() simply + * moves the input pointer so that LT(1) points at the next + * input symbol. Consume at least one token. + * + * Walk past any token not on the channel the parser is listening to. + */ +static void +consume (pANTLR3_INT_STREAM is) +{ + pANTLR3_COMMON_TOKEN_STREAM cts; + pANTLR3_TOKEN_STREAM ts; + + ts = (pANTLR3_TOKEN_STREAM) is->super; + cts = (pANTLR3_COMMON_TOKEN_STREAM) ts->super; + + if ((ANTLR3_UINT32)cts->p < cts->tokens->size(cts->tokens)) + { + cts->p++; + cts->p = skipOffTokenChannels(cts, cts->p); + } +} + + +/// As per ordinary consume but notifies the debugger about hidden +/// tokens and so on. +/// +static void +dbgConsume (pANTLR3_INT_STREAM is) +{ + pANTLR3_TOKEN_STREAM ts; + ANTLR3_MARKER a; + ANTLR3_MARKER b; + pANTLR3_COMMON_TOKEN t; + + ts = (pANTLR3_TOKEN_STREAM) is->super; + + if (ts->initialStreamState == ANTLR3_TRUE) + { + consumeInitialHiddenTokens(is); + } + + a = is->index(is); // Where are we right now? + t = ts->_LT(ts, 1); // Current token from stream + + consume(is); // Standard consumer + + b = is->index(is); // Where are we after consuming 1 on channel token? + + ts->debugger->consumeToken(ts->debugger, t); // Tell the debugger that we consumed the first token + + if (b>a+1) + { + // The standard consume caused the index to advance by more than 1, + // which can only happen if it skipped some off-channel tokens. + // we need to tell the debugger about those tokens. + // + ANTLR3_MARKER i; + + for (i = a+1; i<b; i++) + { + ts->debugger->consumeHiddenToken(ts->debugger, ts->get(ts, (ANTLR3_UINT32)i)); + } + + } +} + +/** A simple filter mechanism whereby you can tell this token stream + * to force all tokens of type ttype to be on channel. For example, + * when interpreting, we cannot execute actions so we need to tell + * the stream to force all WS and NEWLINE to be a different, ignored, + * channel. + */ +static void +setTokenTypeChannel (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_UINT32 ttype, ANTLR3_UINT32 channel) +{ + if (tokenStream->channelOverrides == NULL) + { + tokenStream->channelOverrides = antlr3ListNew(10); + } + + /* We add one to the channel so we can distinguish NULL as being no entry in the + * table for a particular token type. + */ + tokenStream->channelOverrides->put(tokenStream->channelOverrides, ttype, ANTLR3_FUNC_PTR((ANTLR3_UINT32)channel + 1), NULL); +} + +static void +discardTokenType (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_INT32 ttype) +{ + if (tokenStream->discardSet == NULL) + { + tokenStream->discardSet = antlr3ListNew(31); + } + + /* We add one to the channel so we can distinguish NULL as being no entry in the + * table for a particular token type. We could use bitsets for this I suppose too. + */ + tokenStream->discardSet->put(tokenStream->discardSet, ttype, ANTLR3_FUNC_PTR((ANTLR3_UINT32)ttype + 1), NULL); +} + +static void +discardOffChannel (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_BOOLEAN discard) +{ + tokenStream->discardOffChannel = discard; +} + +static pANTLR3_VECTOR +getTokens (pANTLR3_COMMON_TOKEN_STREAM tokenStream) +{ + if (tokenStream->p == -1) + { + fillBuffer(tokenStream); + } + + return tokenStream->tokens; +} + +static pANTLR3_LIST +getTokenRange (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_UINT32 start, ANTLR3_UINT32 stop) +{ + return tokenStream->getTokensSet(tokenStream, start, stop, NULL); +} +/** Given a start and stop index, return a List of all tokens in + * the token type BitSet. Return null if no tokens were found. This + * method looks at both on and off channel tokens. + */ +static pANTLR3_LIST +getTokensSet (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_UINT32 start, ANTLR3_UINT32 stop, pANTLR3_BITSET types) +{ + pANTLR3_LIST filteredList; + ANTLR3_UINT32 i; + ANTLR3_UINT32 n; + pANTLR3_COMMON_TOKEN tok; + + if (tokenStream->p == -1) + { + fillBuffer(tokenStream); + } + if (stop > tokenStream->tstream->istream->size(tokenStream->tstream->istream)) + { + stop = tokenStream->tstream->istream->size(tokenStream->tstream->istream); + } + if (start > stop) + { + return NULL; + } + + /* We have the range set, now we need to iterate through the + * installed tokens and create a new list with just the ones we want + * in it. We are just moving pointers about really. + */ + filteredList = antlr3ListNew((ANTLR3_UINT32)tokenStream->tstream->istream->size(tokenStream->tstream->istream)); + + for (i = start, n = 0; i<= stop; i++) + { + tok = tokenStream->tstream->get(tokenStream->tstream, i); + + if ( types == NULL + || types->isMember(types, tok->getType(tok) == ANTLR3_TRUE) + ) + { + filteredList->put(filteredList, n++, (void *)tok, NULL); + } + } + + /* Did we get any then? + */ + if (filteredList->size(filteredList) == 0) + { + filteredList->free(filteredList); + filteredList = NULL; + } + + return filteredList; +} + +static pANTLR3_LIST +getTokensList (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_UINT32 start, ANTLR3_UINT32 stop, pANTLR3_LIST list) +{ + pANTLR3_BITSET bitSet; + pANTLR3_LIST newlist; + + bitSet = antlr3BitsetList(list->table); + + newlist = tokenStream->getTokensSet(tokenStream, start, stop, bitSet); + + bitSet->free(bitSet); + + return newlist; + +} + +static pANTLR3_LIST +getTokensType (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_UINT32 start, ANTLR3_UINT32 stop, ANTLR3_UINT32 type) +{ + pANTLR3_BITSET bitSet; + pANTLR3_LIST newlist; + + bitSet = antlr3BitsetOf(type, -1); + newlist = tokenStream->getTokensSet(tokenStream, start, stop, bitSet); + + bitSet->free(bitSet); + + return newlist; +} + +static ANTLR3_UINT32 +_LA (pANTLR3_INT_STREAM is, ANTLR3_INT32 i) +{ + pANTLR3_TOKEN_STREAM ts; + pANTLR3_COMMON_TOKEN tok; + + ts = (pANTLR3_TOKEN_STREAM) is->super; + + tok = ts->_LT(ts, i); + + if (tok != NULL) + { + return tok->getType(tok); + } + else + { + return ANTLR3_TOKEN_INVALID; + } +} + +/// As per _LA() but for debug mode. +/// +static ANTLR3_UINT32 +dbgLA (pANTLR3_INT_STREAM is, ANTLR3_INT32 i) +{ + pANTLR3_TOKEN_STREAM ts; + + ts = (pANTLR3_TOKEN_STREAM) is->super; + + if (ts->initialStreamState == ANTLR3_TRUE) + { + consumeInitialHiddenTokens(is); + } + ts->debugger->LT(ts->debugger, i, tokLT(ts, i)); + return _LA(is, i); +} + +static ANTLR3_MARKER +mark (pANTLR3_INT_STREAM is) +{ + is->lastMarker = is->index(is); + return is->lastMarker; +} + +/// As per mark() but with a call to tell the debugger we are doing this +/// +static ANTLR3_MARKER +dbgMark (pANTLR3_INT_STREAM is) +{ + pANTLR3_TOKEN_STREAM ts; + + ts = (pANTLR3_TOKEN_STREAM) is->super; + + is->lastMarker = is->index(is); + ts->debugger->mark(ts->debugger, is->lastMarker); + + return is->lastMarker; +} + +static void +release (pANTLR3_INT_STREAM is, ANTLR3_MARKER mark) +{ + return; +} + +static ANTLR3_UINT32 +size (pANTLR3_INT_STREAM is) +{ + pANTLR3_COMMON_TOKEN_STREAM cts; + pANTLR3_TOKEN_STREAM ts; + + if (is->cachedSize > 0) + { + return is->cachedSize; + } + ts = (pANTLR3_TOKEN_STREAM) is->super; + cts = (pANTLR3_COMMON_TOKEN_STREAM) ts->super; + + is->cachedSize = cts->tokens->count; + return is->cachedSize; +} + +static ANTLR3_MARKER +tindex (pANTLR3_INT_STREAM is) +{ + pANTLR3_COMMON_TOKEN_STREAM cts; + pANTLR3_TOKEN_STREAM ts; + + ts = (pANTLR3_TOKEN_STREAM) is->super; + cts = (pANTLR3_COMMON_TOKEN_STREAM) ts->super; + + return cts->p; +} + +static void +dbgRewindLast (pANTLR3_INT_STREAM is) +{ + pANTLR3_TOKEN_STREAM ts; + + ts = (pANTLR3_TOKEN_STREAM) is->super; + + ts->debugger->rewindLast(ts->debugger); + + is->rewind(is, is->lastMarker); +} +static void +rewindLast (pANTLR3_INT_STREAM is) +{ + is->rewind(is, is->lastMarker); +} +static void +rewindStream (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker) +{ + is->seek(is, (ANTLR3_UINT32)(marker)); +} +static void +dbgRewindStream (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker) +{ + pANTLR3_TOKEN_STREAM ts; + + ts = (pANTLR3_TOKEN_STREAM) is->super; + + ts->debugger->rewind(ts->debugger, marker); + + is->seek(is, (ANTLR3_UINT32)(marker)); +} + +static void +seek (pANTLR3_INT_STREAM is, ANTLR3_MARKER index) +{ + pANTLR3_COMMON_TOKEN_STREAM cts; + pANTLR3_TOKEN_STREAM ts; + + ts = (pANTLR3_TOKEN_STREAM) is->super; + cts = (pANTLR3_COMMON_TOKEN_STREAM) ts->super; + + cts->p = (ANTLR3_UINT32)index; +} +static void +dbgSeek (pANTLR3_INT_STREAM is, ANTLR3_MARKER index) +{ + // TODO: Implement seek in debugger when Ter adds it to Java + // + seek(is, index); +} +ANTLR3_API void +fillBufferExt(pANTLR3_COMMON_TOKEN_STREAM tokenStream) +{ + fillBuffer(tokenStream); +} +static void +fillBuffer(pANTLR3_COMMON_TOKEN_STREAM tokenStream) { + ANTLR3_UINT32 index; + pANTLR3_COMMON_TOKEN tok; + ANTLR3_BOOLEAN discard; + void * channelI; + + /* Start at index 0 of course + */ + index = 0; + + /* Pick out the next token from the token source + * Remember we just get a pointer (reference if you like) here + * and so if we store it anywhere, we don't set any pointers to auto free it. + */ + tok = tokenStream->tstream->tokenSource->nextToken(tokenStream->tstream->tokenSource); + + while (tok != NULL && tok->type != ANTLR3_TOKEN_EOF) + { + discard = ANTLR3_FALSE; /* Assume we are not discarding */ + + /* I employ a bit of a trick, or perhaps hack here. Rather than + * store a pointer to a structure in the override map and discard set + * we store the value + 1 cast to a void *. Hence on systems where NULL = (void *)0 + * we can distinguish "not being there" from "being channel or type 0" + */ + + if (tokenStream->discardSet != NULL + && tokenStream->discardSet->get(tokenStream->discardSet, tok->getType(tok)) != NULL) + { + discard = ANTLR3_TRUE; + } + else if ( tokenStream->discardOffChannel == ANTLR3_TRUE + && tok->getChannel(tok) != tokenStream->channel + ) + { + discard = ANTLR3_TRUE; + } + else if (tokenStream->channelOverrides != NULL) + { + /* See if this type is in the override map + */ + channelI = tokenStream->channelOverrides->get(tokenStream->channelOverrides, tok->getType(tok) + 1); + + if (channelI != NULL) + { + /* Override found + */ + tok->setChannel(tok, ANTLR3_UINT32_CAST(channelI) - 1); + } + } + + /* If not discarding it, add it to the list at the current index + */ + if (discard == ANTLR3_FALSE) + { + /* Add it, indicating that we will delete it and the table should not + */ + tok->setTokenIndex(tok, index); + tokenStream->p++; + tokenStream->tokens->add(tokenStream->tokens, (void *) tok, NULL); + index++; + } + + tok = tokenStream->tstream->tokenSource->nextToken(tokenStream->tstream->tokenSource); + } + + /* Cache the size so we don't keep doing indirect method calls. We do this as + * early as possible so that anything after this may utilize the cached value. + */ + tokenStream->tstream->istream->cachedSize = tokenStream->tokens->count; + + /* Set the consume pointer to the first token that is on our channel + */ + tokenStream->p = 0; + tokenStream->p = skipOffTokenChannels(tokenStream, tokenStream->p); + +} + +/// Given a starting index, return the index of the first on-channel +/// token. +/// +static ANTLR3_UINT32 +skipOffTokenChannels(pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_INT32 i) { + ANTLR3_INT32 n; + pANTLR3_COMMON_TOKEN tok; + + n = tokenStream->tstream->istream->cachedSize; + + while (i < n) + { + tok = (pANTLR3_COMMON_TOKEN)tokenStream->tokens->elements[i].element; + + if (tok->channel!= tokenStream->channel) + { + i++; + } + else + { + return i; + } + } + return i; +} + +static ANTLR3_UINT32 +skipOffTokenChannelsReverse(pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_INT32 x) +{ + pANTLR3_COMMON_TOKEN tok; + + while (x >= 0) + { + tok = (pANTLR3_COMMON_TOKEN)tokenStream->tokens->elements[x].element; + + if ((tok->channel != tokenStream->channel)) + { + x--; + } + else + { + return x; + } + } + return x; +} + +/// Return a string that represents the name assoicated with the input source +/// +/// /param[in] is The ANTLR3_INT_STREAM interface that is representing this token stream. +/// +/// /returns +/// /implements ANTLR3_INT_STREAM_struct::getSourceName() +/// +static pANTLR3_STRING +getSourceName (pANTLR3_INT_STREAM is) +{ + // Slightly convoluted as we must trace back to the lexer's input source + // via the token source. The streamName that is here is not initialized + // because this is a token stream, not a file or string stream, which are the + // only things that have a context for a source name. + // + return ((pANTLR3_TOKEN_STREAM)(is->super))->tokenSource->fileName; +} diff --git a/antlr-3.1.3/runtime/C/src/antlr3treeparser.c b/antlr-3.1.3/runtime/C/src/antlr3treeparser.c new file mode 100644 index 0000000..4473590 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3treeparser.c @@ -0,0 +1,247 @@ +/** \file + * Implementation of the tree parser and overrides for the base recognizer + */ + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +#include <antlr3treeparser.h> + +/* BASE Recognizer overrides + */ +static void mismatch (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 ttype, pANTLR3_BITSET_LIST follow); + +/* Tree parser API + */ +static void setTreeNodeStream (pANTLR3_TREE_PARSER parser, pANTLR3_COMMON_TREE_NODE_STREAM input); +static pANTLR3_COMMON_TREE_NODE_STREAM + getTreeNodeStream (pANTLR3_TREE_PARSER parser); +static void freeParser (pANTLR3_TREE_PARSER parser); +static void * getCurrentInputSymbol (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM istream); +static void * getMissingSymbol (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM istream, pANTLR3_EXCEPTION e, + ANTLR3_UINT32 expectedTokenType, pANTLR3_BITSET_LIST follow); + + +ANTLR3_API pANTLR3_TREE_PARSER +antlr3TreeParserNewStream(ANTLR3_UINT32 sizeHint, pANTLR3_COMMON_TREE_NODE_STREAM ctnstream, pANTLR3_RECOGNIZER_SHARED_STATE state) +{ + pANTLR3_TREE_PARSER parser; + + /** Allocate tree parser memory + */ + parser =(pANTLR3_TREE_PARSER) ANTLR3_MALLOC(sizeof(ANTLR3_TREE_PARSER)); + + if (parser == NULL) + { + return NULL; + } + + /* Create and install a base recognizer which does most of the work for us + */ + parser->rec = antlr3BaseRecognizerNew(ANTLR3_TYPE_PARSER, sizeHint, state); + + if (parser->rec == NULL) + { + parser->free(parser); + return NULL; + } + + /* Ensure we can track back to the tree parser super structure + * from the base recognizer structure + */ + parser->rec->super = parser; + parser->rec->type = ANTLR3_TYPE_TREE_PARSER; + + /* Install our base recognizer overrides + */ + parser->rec->mismatch = mismatch; + parser->rec->exConstruct = antlr3MTNExceptionNew; + parser->rec->getCurrentInputSymbol = getCurrentInputSymbol; + parser->rec->getMissingSymbol = getMissingSymbol; + + /* Install tree parser API + */ + parser->getTreeNodeStream = getTreeNodeStream; + parser->setTreeNodeStream = setTreeNodeStream; + parser->free = freeParser; + + /* Install the tree node stream + */ + parser->setTreeNodeStream(parser, ctnstream); + + return parser; +} + +/** + * \brief + * Creates a new Mismatched Tree Nde Exception and inserts in the recognizer + * exception stack. + * + * \param recognizer + * Context pointer for this recognizer + * + */ +ANTLR3_API void +antlr3MTNExceptionNew(pANTLR3_BASE_RECOGNIZER recognizer) +{ + /* Create a basic recognition exception structure + */ + antlr3RecognitionExceptionNew(recognizer); + + /* Now update it to indicate this is a Mismatched token exception + */ + recognizer->state->exception->name = ANTLR3_MISMATCHED_TREE_NODE_NAME; + recognizer->state->exception->type = ANTLR3_MISMATCHED_TREE_NODE_EXCEPTION; + + return; +} + + +static void +freeParser (pANTLR3_TREE_PARSER parser) +{ + if (parser->rec != NULL) + { + // This may have ben a delegate or delegator parser, in which case the + // state may already have been freed (and set to NULL therefore) + // so we ignore the state if we don't have it. + // + if (parser->rec->state != NULL) + { + if (parser->rec->state->following != NULL) + { + parser->rec->state->following->free(parser->rec->state->following); + parser->rec->state->following = NULL; + } + } + parser->rec->free(parser->rec); + parser->rec = NULL; + } + + ANTLR3_FREE(parser); +} + +/** Set the input stream and reset the parser + */ +static void +setTreeNodeStream (pANTLR3_TREE_PARSER parser, pANTLR3_COMMON_TREE_NODE_STREAM input) +{ + parser->ctnstream = input; + parser->rec->reset (parser->rec); + parser->ctnstream->reset (parser->ctnstream); +} + +/** Return a pointer to the input stream + */ +static pANTLR3_COMMON_TREE_NODE_STREAM +getTreeNodeStream (pANTLR3_TREE_PARSER parser) +{ + return parser->ctnstream; +} + + +/** Override for standard base recognizer mismatch function + * as we have DOWN/UP nodes in the stream that have no line info, + * plus we want to alter the exception type. + */ +static void +mismatch (pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 ttype, pANTLR3_BITSET_LIST follow) +{ + recognizer->exConstruct(recognizer); + recognizer->recoverFromMismatchedToken(recognizer, ttype, follow); +} + +#ifdef ANTLR3_WINDOWS +#pragma warning (push) +#pragma warning (disable : 4100) +#endif + +// Default implementation is for parser and assumes a token stream as supplied by the runtime. +// You MAY need override this function if the standard TOKEN_STREAM is not what you are using. +// +static void * +getCurrentInputSymbol (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM istream) +{ + pANTLR3_TREE_NODE_STREAM tns; + pANTLR3_COMMON_TREE_NODE_STREAM ctns; + + tns = (pANTLR3_TREE_NODE_STREAM)(istream->super); + ctns = tns->ctns; + return tns->_LT(tns, 1); +} + + +// Default implementation is for parser and assumes a token stream as supplied by the runtime. +// You MAY need override this function if the standard BASE_TREE is not what you are using. +// +static void * +getMissingSymbol (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM istream, pANTLR3_EXCEPTION e, + ANTLR3_UINT32 expectedTokenType, pANTLR3_BITSET_LIST follow) +{ + pANTLR3_TREE_NODE_STREAM tns; + pANTLR3_COMMON_TREE_NODE_STREAM ctns; + pANTLR3_BASE_TREE node; + pANTLR3_BASE_TREE current; + pANTLR3_COMMON_TOKEN token; + pANTLR3_STRING text; + + // Dereference the standard pointers + // + tns = (pANTLR3_TREE_NODE_STREAM)(istream->super); + ctns = tns->ctns; + + // Create a new empty node, by stealing the current one, or the previous one if the current one is EOF + // + current = tns->_LT(tns, 1); + + if (current == &ctns->EOF_NODE.baseTree) + { + current = tns->_LT(tns, -1); + } + node = current->dupNode(current); + + // Find the newly dupicated token + // + token = node->getToken(node); + + // Create the token text that shows it has been inserted + // + token->setText8 (token, (pANTLR3_UINT8)"<missing "); + text = token->getText (token); + text->append8 (text, (const char *)recognizer->state->tokenNames[expectedTokenType]); + text->append8 (text, (const char *)">"); + + // Finally return the pointer to our new node + // + return node; +} +#ifdef ANTLR3_WINDOWS +#pragma warning (pop) +#endif + diff --git a/antlr-3.1.3/runtime/C/src/antlr3ucs2inputstream.c b/antlr-3.1.3/runtime/C/src/antlr3ucs2inputstream.c new file mode 100644 index 0000000..ab1c457 --- /dev/null +++ b/antlr-3.1.3/runtime/C/src/antlr3ucs2inputstream.c @@ -0,0 +1,202 @@ +/// \file +/// Base functions to initialize and manipulate a UCS2 input stream +/// +#include <antlr3input.h> + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC +// http://www.temporal-wave.com +// http://www.linkedin.com/in/jimidle +// +// 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. + +// INT Stream API +// +static void antlr3UCS2Consume (pANTLR3_INT_STREAM is); +static ANTLR3_UCHAR antlr3UCS2LA (pANTLR3_INT_STREAM is, ANTLR3_INT32 la); +static ANTLR3_MARKER antlr3UCS2Index (pANTLR3_INT_STREAM is); +static void antlr3UCS2Seek (pANTLR3_INT_STREAM is, ANTLR3_MARKER seekPoint); + +// ucs2 Charstream API functions +// +static pANTLR3_STRING antlr3UCS2Substr (pANTLR3_INPUT_STREAM input, ANTLR3_MARKER start, ANTLR3_MARKER stop); + +/// \brief Common function to setup function interface for a 16 bit "UCS2" input stream. +/// +/// \param input Input stream context pointer +/// +/// \remark +/// - Strictly speaking, there is no such thing as a UCS2 input stream as the term +/// tends to confuse the notions of character encoding, unicode and so on. However +/// because there will possibly be a need for a UTF-16 stream, I needed to identify 16 bit +/// streams that do not support surrogate encodings and UCS2 is how it is mostly referred to. +/// For instance Java, Oracle and others use a 16 bit encoding of characters and so this type +/// of stream is very common. +/// Take it to mean, therefore, a straight 16 bit uncomplicated encoding of Unicode code points. +/// +void +antlr3UCS2SetupStream (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 type) +{ + // Build a string factory for this stream. This is a 16 bit string "UCS2" factory which is a standard + // part of the ANTLR3 string. The string factory is then passed through the whole chain of lexer->parser->tree->treeparser + // and so on. + // + input->strFactory = antlr3UCS2StringFactoryNew(); + + // Install function pointers for an 8 bit ASCII input, which are good for almost + // all input stream functions. We will then override those that won't work for 16 bit characters. + // + antlr3GenericSetupStream (input, type); + + // Intstream API overrides for UCS2 + // + input->istream->consume = antlr3UCS2Consume; // Consume the next 16 bit character in the buffer + input->istream->_LA = antlr3UCS2LA; // Return the UTF32 character at offset n (1 based) + input->istream->index = antlr3UCS2Index; // Calculate current index in input stream, 16 bit based + input->istream->seek = antlr3UCS2Seek; // How to seek to a specific point in the stream + + // Charstream API overrides for UCS2 + // + input->substr = antlr3UCS2Substr; // Return a string from the input stream + + input->charByteSize = 2; // Size in bytes of characters in this stream. + +} + +/// \brief Consume the next character in an 8 bit ASCII input stream +/// +/// \param input Input stream context pointer +/// +static void +antlr3UCS2Consume(pANTLR3_INT_STREAM is) +{ + pANTLR3_INPUT_STREAM input; + + input = ((pANTLR3_INPUT_STREAM) (is->super)); + + if ((pANTLR3_UINT16)(input->nextChar) < (((pANTLR3_UINT16)input->data) + input->sizeBuf)) + { + // Indicate one more character in this line + // + input->charPositionInLine++; + + if ((ANTLR3_UCHAR)(*((pANTLR3_UINT16)input->nextChar)) == input->newlineChar) + { + // Reset for start of a new line of input + // + input->line++; + input->charPositionInLine = 0; + input->currentLine = (void *)(((pANTLR3_UINT16)input->nextChar) + 1); + } + + // Increment to next character position + // + input->nextChar = (void *)(((pANTLR3_UINT16)input->nextChar) + 1); + } +} + +/// \brief Return the input element assuming an 8 bit ascii input +/// +/// \param[in] input Input stream context pointer +/// \param[in] la 1 based offset of next input stream element +/// +/// \return Next input character in internal ANTLR3 encoding (UTF32) +/// +static ANTLR3_UCHAR +antlr3UCS2LA(pANTLR3_INT_STREAM is, ANTLR3_INT32 la) +{ + pANTLR3_INPUT_STREAM input; + + input = ((pANTLR3_INPUT_STREAM) (is->super)); + + if (( ((pANTLR3_UINT16)input->nextChar) + la - 1) >= (((pANTLR3_UINT16)input->data) + input->sizeBuf)) + { + return ANTLR3_CHARSTREAM_EOF; + } + else + { + return (ANTLR3_UCHAR)(*((pANTLR3_UINT16)input->nextChar + la - 1)); + } +} + + +/// \brief Calculate the current index in the output stream. +/// \param[in] input Input stream context pointer +/// +static ANTLR3_MARKER +antlr3UCS2Index(pANTLR3_INT_STREAM is) +{ + pANTLR3_INPUT_STREAM input; + + input = ((pANTLR3_INPUT_STREAM) (is->super)); + + return (ANTLR3_MARKER)(input->nextChar); +} + +/// \brief Rewind the lexer input to the state specified by the supplied mark. +/// +/// \param[in] input Input stream context pointer +/// +/// \remark +/// Assumes ASCII (or at least, 8 Bit) input stream. +/// +static void +antlr3UCS2Seek (pANTLR3_INT_STREAM is, ANTLR3_MARKER seekPoint) +{ + ANTLR3_INT32 count; + pANTLR3_INPUT_STREAM input; + + input = ((pANTLR3_INPUT_STREAM) is->super); + + // If the requested seek point is less than the current + // input point, then we assume that we are resetting from a mark + // and do not need to scan, but can just set to there. + // + if (seekPoint <= (ANTLR3_MARKER)(input->nextChar)) + { + input->nextChar = (void *)seekPoint; + } + else + { + count = (ANTLR3_UINT32)((seekPoint - (ANTLR3_MARKER)(input->nextChar)) / 2); // 16 bits per character in UCS2 + + while (count--) + { + is->consume(is); + } + } +} +/// \brief Return a substring of the ucs2 (16 bit) input stream in +/// newly allocated memory. +/// +/// \param input Input stream context pointer +/// \param start Offset in input stream where the string starts +/// \param stop Offset in the input stream where the string ends. +/// +static pANTLR3_STRING +antlr3UCS2Substr (pANTLR3_INPUT_STREAM input, ANTLR3_MARKER start, ANTLR3_MARKER stop) +{ + return input->strFactory->newPtr(input->strFactory, (pANTLR3_UINT8)start, ((ANTLR3_UINT32_CAST(stop - start))/2) + 1); +} diff --git a/antlr-3.1.3/runtime/C/vsrulefiles/antlr3lexer.rules b/antlr-3.1.3/runtime/C/vsrulefiles/antlr3lexer.rules new file mode 100644 index 0000000..d80fefa --- /dev/null +++ b/antlr-3.1.3/runtime/C/vsrulefiles/antlr3lexer.rules @@ -0,0 +1,199 @@ +<?xml version="1.0" encoding="utf-8"?> +<VisualStudioToolFile + Name="ANTLR3 Lexer" + Version="8.00" + > + <Rules> + <CustomBuildRule + Name="Antlr3Lexer" + DisplayName="ANTLR 3 Lexer Grammar Translation" + CommandLine="[java] [JavaOptions] [ANTLR3Jar] org.antlr.Tool [LibDir] -[Absolute]o [OutputDirectory] -message-format vs2005 [DFA] [NFA] [Report] [Print] [Debug] [Profile] [AST] [TextDFA] [EBNFExits] [CollapseEdges] [DebugNFA] [MaxRules] [MaxDFAEdges] [DFATimeout] [inputs]" + Outputs="[OutputDirectory]\$(InputName).c;[OutputDirectory]\$(InputName).h" + FileExtensions="*.g3l;*.gl;*.g" + ExecutionDescription="Translating to lexer." + SupportsFileBatching="true" + > + <Properties> + <StringProperty + Name="JavaOptions" + DisplayName="Java VM Options" + PropertyPageName="Java" + Description="Specify any options required to invoke the java VM on this grammar file. Sometimes larger grammars require more memory than the standard allocation and you can specify this here." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="[value]" + Delimited="true" + Inheritable="true" + /> + <BooleanProperty + Name="DFA" + DisplayName="Generate DFA dots" + PropertyPageName="DOT" + Category="DOT Ouputs" + Description="When set to True ANTLR3 will produce a number of .dot files that can be used with dot/graphviz to genreate pictorial representations of the DFAs gnerated for this grammar." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-dfa" + /> + <BooleanProperty + Name="NFA" + DisplayName="Generate NFA DOTs" + Category="DOT Outputs" + Description="When set to True ANTLR3 will produce a number of .dot files that can be used with dot/graphviz to genreate pictorial representations of the NFAs generated for this grammar." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-nfa" + /> + <BooleanProperty + Name="Report" + DisplayName="Generate Report" + PropertyPageName="Reporting" + Category="Reporting" + Description="If set to True then ANTLR3 will generate reports about the grammar file(s) it processes." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-report" + /> + <BooleanProperty + Name="Print" + DisplayName="Print grammar" + PropertyPageName="Reporting" + Category="Reporting" + Description="If set to True ANTLR3 will print out the grammar without the associated actions" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-print" + /> + <BooleanProperty + Name="Debug" + DisplayName="Debug mode" + PropertyPageName="Code Generation" + Category="Output" + Description="If set to True ANTLR3 will generate code that fires debugging events. [JI - Not yet implemented]" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-debug" + /> + <BooleanProperty + Name="Profile" + DisplayName="Generate profile" + Category="Output" + Description="If set to True ANTLR3 will generate code that computes profiling information [JI - not yet implemented]" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-profile" + /> + <BooleanProperty + Name="AST" + DisplayName="Show AST" + PropertyPageName="Extended" + Category="Extended" + Description="If True ANTLR3 will print out the grammar AST" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xgrtree" + /> + <StringProperty + Name="LibDir" + DisplayName="Token directory" + PropertyPageName="Code Generation" + Category="General" + Description="In which directory can ANTLR3 locate grammar token files if not in the same directory as the grammar file." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-lib [value]" + DefaultValue="$(InputDir)" + /> + <BooleanProperty + Name="TextDFA" + DisplayName="Text DFA" + PropertyPageName="Extended" + Category="Extended" + Description="If True ANTLR3 will generate a text version of the DFAsfor this grammar." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xdfa" + /> + <BooleanProperty + Name="EBNFExits" + DisplayName="EBNF Exits" + PropertyPageName="Extended" + Category="Extended" + Description="If True ANTLR3 will not test EBNF exit branches." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xnoprune" + /> + <StringProperty + Name="OutputDirectory" + DisplayName="Output Directory" + PropertyPageName="Code Generation" + Description="Which directory the generated output files be sent to if not the same directory as the grammar file." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="[value]" + DefaultValue="$(InputDir)" + /> + <BooleanProperty + Name="CollapseEdges" + DisplayName="Collapse Edges" + PropertyPageName="Extended" + Category="Extended" + Description="Collapse incident edges into DFA states" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xnocollapse" + /> + <BooleanProperty + Name="DebugNFA" + DisplayName="Debug NFA" + Category="Reporting" + Description="If True, ANTLR3 will dump lots of information to the screen during NFA conversion." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xdbgconversion " + /> + <StringProperty + Name="ANTLR3Jar" + DisplayName="ANTLR3 Jar" + Category="JavaVM" + Description="Specifies the absolute location of the ANTLR3 jar file if it is not in a location covered by %CLASSPATH%. Specify using UNIX directory delimiters to minimize problems." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-jar "[value]"" + /> + <StringProperty + Name="Java" + DisplayName="Java command" + Description="Specifies the command that invokes the java VM. Usually java, but could be something else such as jikes" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="[value]" + DefaultValue="java" + /> + <IntegerProperty + Name="MaxRules" + DisplayName="Max rule call" + PropertyPageName="Extended" + Category="Analysis" + Description="Maximum number of rule invocations during conversion" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xm [value]" + DefaultValue="4" + /> + <IntegerProperty + Name="MaxDFAEdges" + DisplayName="Max DFA Edges" + PropertyPageName="Extended" + Category="Extended" + Description="Maximum "comfortable" number of edges for single DFA state" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xmaxdfaedges [value]" + DefaultValue="65534" + /> + <IntegerProperty + Name="DFATimeout" + DisplayName="DFA Timeout" + PropertyPageName="Extended" + Category="Extended" + Description="DFA conversion timeout period for each decision." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xconversiontimeout [value]" + DefaultValue="1000" + /> + <BooleanProperty + Name="Absolute" + DisplayName="Absolute Directories" + PropertyPageName="Code Generation" + Description="If true, causes ANTLR to assume output directory is always the absolute output path and not to use relative paths as per the intput spec. For visual studio, this should usually be set to true." + Switch="f" + DefaultValue="true" + /> + </Properties> + </CustomBuildRule> + </Rules> +</VisualStudioToolFile> diff --git a/antlr-3.1.3/runtime/C/vsrulefiles/antlr3lexerandparser.rules b/antlr-3.1.3/runtime/C/vsrulefiles/antlr3lexerandparser.rules new file mode 100644 index 0000000..12d100a --- /dev/null +++ b/antlr-3.1.3/runtime/C/vsrulefiles/antlr3lexerandparser.rules @@ -0,0 +1,201 @@ +<?xml version="1.0" encoding="utf-8"?> +<VisualStudioToolFile + Name="ANTLR3 Combo Lexer and Parser" + Version="8.00" + > + <Rules> + <CustomBuildRule + Name="Antlr3ParserLexer" + DisplayName="ANTLR 3 Parser/Lexer Grammar Translation" + CommandLine="[java] [JavaOptions] [ANTLR3Jar] org.antlr.Tool [LibDir] -[Absolute]o [OutputDirectory] -message-format vs2005 [DFA] [NFA] [Report] [Print] [Debug] [Profile] [AST] [TextDFA] [EBNFExits] [CollapseEdges] [DebugNFA] [MaxRules] [MaxDFAEdges] [DFATimeout] [inputs]" + Outputs="[OutputDirectory]\$(InputName)Parser.c;[OutputDirectory]\$(InputName)Parser.h;[OutputDirectory]\$(InputName)Lexer.c;[OutputDirectory]\$(InputName)Lexer.h" + FileExtensions="*.g3pl;*.g3;*.g" + ExecutionDescription="Translating to parser/lexer combination" + SupportsFileBatching="true" + ShowOnlyRuleProperties="false" + > + <Properties> + <StringProperty + Name="JavaOptions" + DisplayName="Java VM Options" + PropertyPageName="Java" + Description="Specify any options required to invoke the java VM on this grammar file. Sometimes larger grammars require more memory than the standard allocation and you can specify this here." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="[value]" + Delimited="true" + Inheritable="true" + /> + <BooleanProperty + Name="DFA" + DisplayName="Generate DFA dots" + PropertyPageName="DOT" + Category="DOT Ouputs" + Description="When set to True ANTLR3 will produce a number of .dot files that can be used with dot/graphviz to genreate pictorial representations of the DFAs gnerated for this grammar." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-dfa" + /> + <BooleanProperty + Name="NFA" + DisplayName="Generate NFA DOTs" + Category="DOT Outputs" + Description="When set to True ANTLR3 will produce a number of .dot files that can be used with dot/graphviz to genreate pictorial representations of the NFAs generated for this grammar." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-nfa" + /> + <BooleanProperty + Name="Report" + DisplayName="Generate Report" + PropertyPageName="Reporting" + Category="Reporting" + Description="If set to True then ANTLR3 will generate reports about the grammar file(s) it processes." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-report" + /> + <BooleanProperty + Name="Print" + DisplayName="Print grammar" + PropertyPageName="Reporting" + Category="Reporting" + Description="If set to True ANTLR3 will print out the grammar without the associated actions" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-print" + /> + <BooleanProperty + Name="Debug" + DisplayName="Debug mode" + PropertyPageName="Code Generation" + Category="Output" + Description="If set to True ANTLR3 will generate code that fires debugging events. [JI - Not yet implemented]" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-debug" + /> + <BooleanProperty + Name="Profile" + DisplayName="Generate profile" + Category="Output" + Description="If set to True ANTLR3 will generate code that computes profiling information [JI - not yet implemented]" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-profile" + /> + <BooleanProperty + Name="AST" + DisplayName="Show AST" + PropertyPageName="Extended" + Category="Extended" + Description="If True ANTLR3 will print out the grammar AST" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xgrtree" + /> + <StringProperty + Name="LibDir" + DisplayName="Token directory" + PropertyPageName="Code Generation" + Category="General" + Description="In which directory can ANTLR3 locate grammar token files if not in the same directory as the grammar file." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-lib [value]" + DefaultValue="$(InputDir)" + /> + <BooleanProperty + Name="TextDFA" + DisplayName="Text DFA" + PropertyPageName="Extended" + Category="Extended" + Description="If True ANTLR3 will generate a text version of the DFAsfor this grammar." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xdfa" + /> + <BooleanProperty + Name="EBNFExits" + DisplayName="EBNF Exits" + PropertyPageName="Extended" + Category="Extended" + Description="If True ANTLR3 will not test EBNF exit branches." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xnoprune" + /> + <StringProperty + Name="OutputDirectory" + DisplayName="Output Directory" + PropertyPageName="Code Generation" + Description="Which directory the generated output files be sent to if not the same directory as the grammar file." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="[value]" + DefaultValue="$(InputDir)" + /> + <BooleanProperty + Name="CollapseEdges" + DisplayName="Collapse Edges" + PropertyPageName="Extended" + Category="Extended" + Description="Collapse incident edges into DFA states" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xnocollapse" + /> + <BooleanProperty + Name="DebugNFA" + DisplayName="Debug NFA" + Category="Reporting" + Description="If True, ANTLR3 will dump lots of information to the screen during NFA conversion." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xdbgconversion " + /> + <StringProperty + Name="ANTLR3Jar" + DisplayName="ANTLR3 Jar" + PropertyPageName="Java" + Category="JavaVM" + Description="Specifies the absolute location of the ANTLR3 jar file if it is not in a location covered by %CLASSPATH%. Specify using UNIX directory delimiters to minimize problems." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-jar "[value]"" + /> + <StringProperty + Name="Java" + DisplayName="Java command" + Description="Specifies the command that invokes the java VM. Usually java, but could be something else such as jikes" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="[value]" + DefaultValue="java" + /> + <IntegerProperty + Name="MaxRules" + DisplayName="Max rule call" + PropertyPageName="Extended" + Category="Analysis" + Description="Maximum number of rule invocations during conversion" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xm [value]" + DefaultValue="4" + /> + <IntegerProperty + Name="MaxDFAEdges" + DisplayName="Max DFA Edges" + PropertyPageName="Extended" + Category="Extended" + Description="Maximum "comfortable" number of edges for single DFA state" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xmaxdfaedges [value]" + DefaultValue="65534" + /> + <IntegerProperty + Name="DFATimeout" + DisplayName="DFA Timeout" + PropertyPageName="Extended" + Category="Extended" + Description="DFA conversion timeout period for each decision." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xconversiontimeout [value]" + DefaultValue="1000" + /> + <BooleanProperty + Name="Absolute" + DisplayName="Absolute Directories" + PropertyPageName="Code Generation" + Description="If true, causes ANTLR to assume output directory is always the absolute output path and not to use relative paths as per the intput spec. For visual studio, this should usually be set to true." + Switch="f" + DefaultValue="true" + /> + </Properties> + </CustomBuildRule> + </Rules> +</VisualStudioToolFile> diff --git a/antlr-3.1.3/runtime/C/vsrulefiles/antlr3parser.rules b/antlr-3.1.3/runtime/C/vsrulefiles/antlr3parser.rules new file mode 100644 index 0000000..6332946 --- /dev/null +++ b/antlr-3.1.3/runtime/C/vsrulefiles/antlr3parser.rules @@ -0,0 +1,201 @@ +<?xml version="1.0" encoding="utf-8"?> +<VisualStudioToolFile + Name="ANTLR3 Parser" + Version="8.00" + > + <Rules> + <CustomBuildRule + Name="Antlr3Parser" + DisplayName="ANTLR 3 Parser Grammar Translation" + CommandLine="[java] [JavaOptions] [ANTLR3Jar] org.antlr.Tool [LibDir] -[Absolute]o [OutputDirectory] -message-format vs2005 [DFA] [NFA] [Report] [Print] [Debug] [Profile] [AST] [TextDFA] [EBNFExits] [CollapseEdges] [DebugNFA] [MaxRules] [MaxDFAEdges] [DFATimeout] [inputs]" + Outputs="[OutputDirectory]\$(InputName).c;[OutputDirectory]\$(InputName).h" + FileExtensions="*.g3p;*.gp;*.g" + ExecutionDescription="Translating to parser." + SupportsFileBatching="true" + > + <Properties> + <StringProperty + Name="JavaOptions" + DisplayName="Java VM Options" + PropertyPageName="Java" + Description="Specify any options required to invoke the java VM on this grammar file. Sometimes larger grammars require more memory than the standard allocation and you can specify this here." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="[value]" + Delimited="true" + Inheritable="true" + /> + <BooleanProperty + Name="DFA" + DisplayName="Generate DFA dots" + PropertyPageName="DOT" + Category="DOT Ouputs" + Description="When set to True ANTLR3 will produce a number of .dot files that can be used with dot/graphviz to genreate pictorial representations of the DFAs gnerated for this grammar." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-dfa" + /> + <BooleanProperty + Name="NFA" + DisplayName="Generate NFA DOTs" + Category="DOT Outputs" + Description="When set to True ANTLR3 will produce a number of .dot files that can be used with dot/graphviz to genreate pictorial representations of the NFAs generated for this grammar." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-nfa" + /> + <BooleanProperty + Name="Report" + DisplayName="Generate Report" + PropertyPageName="Reporting" + Category="Reporting" + Description="If set to True then ANTLR3 will generate reports about the grammar file(s) it processes." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-report" + /> + <BooleanProperty + Name="Print" + DisplayName="Print grammar" + PropertyPageName="Reporting" + Category="Reporting" + Description="If set to True ANTLR3 will print out the grammar without the associated actions" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-print" + /> + <BooleanProperty + Name="Debug" + DisplayName="Debug mode" + PropertyPageName="Code Generation" + Category="Output" + Description="If set to True ANTLR3 will generate code that fires debugging events. [JI - Not yet implemented]" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-debug" + /> + <BooleanProperty + Name="Profile" + DisplayName="Generate profile" + Category="Output" + Description="If set to True ANTLR3 will generate code that computes profiling information [JI - not yet implemented]" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-profile" + /> + <BooleanProperty + Name="AST" + DisplayName="Show AST" + PropertyPageName="Extended" + Category="Extended" + Description="If True ANTLR3 will print out the grammar AST" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xgrtree" + /> + <StringProperty + Name="LibDir" + DisplayName="Token directory" + PropertyPageName="Code Generation" + Category="General" + Description="In which directory can ANTLR3 locate grammar token files if not in the same directory as the grammar file." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-lib [value]" + DefaultValue="$(InputDir)" + /> + <BooleanProperty + Name="TextDFA" + DisplayName="Text DFA" + PropertyPageName="Extended" + Category="Extended" + Description="If True ANTLR3 will generate a text version of the DFAsfor this grammar." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xdfa" + /> + <BooleanProperty + Name="EBNFExits" + DisplayName="EBNF Exits" + PropertyPageName="Extended" + Category="Extended" + Description="If True ANTLR3 will not test EBNF exit branches." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xnoprune" + /> + <StringProperty + Name="OutputDirectory" + DisplayName="Output Directory" + PropertyPageName="Code Generation" + Description="Which directory the generated output files be sent to if not the same directory as the grammar file." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="[value]" + DefaultValue="$(InputDir)" + /> + <BooleanProperty + Name="CollapseEdges" + DisplayName="Collapse Edges" + PropertyPageName="Extended" + Category="Extended" + Description="Collapse incident edges into DFA states" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xnocollapse" + /> + <BooleanProperty + Name="DebugNFA" + DisplayName="Debug NFA" + Category="Reporting" + Description="If True, ANTLR3 will dump lots of information to the screen during NFA conversion." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xdbgconversion " + /> + <StringProperty + Name="ANTLR3Jar" + DisplayName="ANTLR3 Jar" + PropertyPageName="Java" + Category="JavaVM" + Description="Specifies the absolute location of the ANTLR3 jar file if it is not in a location covered by %CLASSPATH%. Specify using UNIX directory delimiters to minimize problems." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-jar "[value]"" + /> + <StringProperty + Name="Java" + DisplayName="Java command" + PropertyPageName="Java" + Description="Specifies the command that invokes the java VM. Usually java, but could be something else such as jikes" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="[value]" + DefaultValue="java" + /> + <IntegerProperty + Name="MaxRules" + DisplayName="Max rule call" + PropertyPageName="Extended" + Category="Analysis" + Description="Maximum number of rule invocations during conversion" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xm [value]" + DefaultValue="4" + /> + <IntegerProperty + Name="MaxDFAEdges" + DisplayName="Max DFA Edges" + PropertyPageName="Extended" + Category="Extended" + Description="Maximum "comfortable" number of edges for single DFA state" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xmaxdfaedges [value]" + DefaultValue="65534" + /> + <IntegerProperty + Name="DFATimeout" + DisplayName="DFA Timeout" + PropertyPageName="Extended" + Category="Extended" + Description="DFA conversion timeout period for each decision." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xconversiontimeout [value]" + DefaultValue="1000" + /> + <BooleanProperty + Name="Absolute" + DisplayName="Absolute paths" + PropertyPageName="Code Generation" + Description="If true, causes ANTLR to assume output directory is always the absolute output path and not to use relative paths as per the intput spec. For visual studio, this should usually be set to true." + Switch="f" + DefaultValue="true" + /> + </Properties> + </CustomBuildRule> + </Rules> +</VisualStudioToolFile> diff --git a/antlr-3.1.3/runtime/C/vsrulefiles/antlr3treeparser.rules b/antlr-3.1.3/runtime/C/vsrulefiles/antlr3treeparser.rules new file mode 100644 index 0000000..1f60f77 --- /dev/null +++ b/antlr-3.1.3/runtime/C/vsrulefiles/antlr3treeparser.rules @@ -0,0 +1,201 @@ +<?xml version="1.0" encoding="utf-8"?> +<VisualStudioToolFile + Name="ANTLR3 Tree Parser" + Version="8.00" + > + <Rules> + <CustomBuildRule + Name="Antlr3TreeParser" + DisplayName="ANTLR 3 Tree Parser Grammar Translation" + CommandLine="[java] [JavaOptions] [ANTLR3Jar] org.antlr.Tool [LibDir] -[Absolute]o [OutputDirectory] -message-format vs2005 [DFA] [NFA] [Report] [Print] [Debug] [Profile] [AST] [TextDFA] [EBNFExits] [CollapseEdges] [DebugNFA] [MaxRules] [MaxDFAEdges] [DFATimeout] [inputs]" + Outputs="[OutputDirectory]\$(InputName).c;[OutputDirectory]\$(InputName).h" + FileExtensions="*.g3t;*.gt;*.g" + ExecutionDescription="Translating to tree parser." + SupportsFileBatching="true" + > + <Properties> + <StringProperty + Name="JavaOptions" + DisplayName="Java VM Options" + PropertyPageName="Java" + Description="Specify any options required to invoke the java VM on this grammar file. Sometimes larger grammars require more memory than the standard allocation and you can specify this here." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="[value]" + Delimited="true" + Inheritable="true" + /> + <BooleanProperty + Name="DFA" + DisplayName="Generate DFA dots" + PropertyPageName="DOT" + Category="DOT Ouputs" + Description="When set to True ANTLR3 will produce a number of .dot files that can be used with dot/graphviz to genreate pictorial representations of the DFAs gnerated for this grammar." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-dfa" + /> + <BooleanProperty + Name="NFA" + DisplayName="Generate NFA DOTs" + Category="DOT Outputs" + Description="When set to True ANTLR3 will produce a number of .dot files that can be used with dot/graphviz to genreate pictorial representations of the NFAs generated for this grammar." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-nfa" + /> + <BooleanProperty + Name="Report" + DisplayName="Generate Report" + PropertyPageName="Reporting" + Category="Reporting" + Description="If set to True then ANTLR3 will generate reports about the grammar file(s) it processes." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-report" + /> + <BooleanProperty + Name="Print" + DisplayName="Print grammar" + PropertyPageName="Reporting" + Category="Reporting" + Description="If set to True ANTLR3 will print out the grammar without the associated actions" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-print" + /> + <BooleanProperty + Name="Debug" + DisplayName="Debug mode" + PropertyPageName="Code Generation" + Category="Output" + Description="If set to True ANTLR3 will generate code that fires debugging events. [JI - Not yet implemented]" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-debug" + /> + <BooleanProperty + Name="Profile" + DisplayName="Generate profile" + Category="Output" + Description="If set to True ANTLR3 will generate code that computes profiling information [JI - not yet implemented]" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-profile" + /> + <BooleanProperty + Name="AST" + DisplayName="Show AST" + PropertyPageName="Extended" + Category="Extended" + Description="If True ANTLR3 will print out the grammar AST" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xgrtree" + /> + <StringProperty + Name="LibDir" + DisplayName="Token directory" + PropertyPageName="Code Generation" + Category="General" + Description="In which directory can ANTLR3 locate grammar token files if not in the same directory as the grammar file." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-lib [value]" + DefaultValue="$(InputDir)" + /> + <BooleanProperty + Name="TextDFA" + DisplayName="Text DFA" + PropertyPageName="Extended" + Category="Extended" + Description="If True ANTLR3 will generate a text version of the DFAsfor this grammar." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xdfa" + /> + <BooleanProperty + Name="EBNFExits" + DisplayName="EBNF Exits" + PropertyPageName="Extended" + Category="Extended" + Description="If True ANTLR3 will not test EBNF exit branches." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xnoprune" + /> + <StringProperty + Name="OutputDirectory" + DisplayName="Output Directory" + PropertyPageName="Code Generation" + Description="Which directory the generated output files be sent to if not the same directory as the grammar file." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="[value]" + DefaultValue="$(InputDir)" + /> + <BooleanProperty + Name="CollapseEdges" + DisplayName="Collapse Edges" + PropertyPageName="Extended" + Category="Extended" + Description="Collapse incident edges into DFA states" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xnocollapse" + /> + <BooleanProperty + Name="DebugNFA" + DisplayName="Debug NFA" + Category="Reporting" + Description="If True, ANTLR3 will dump lots of information to the screen during NFA conversion." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xdbgconversion " + /> + <StringProperty + Name="ANTLR3Jar" + DisplayName="ANTLR3 Jar" + PropertyPageName="Java" + Category="JavaVM" + Description="Specifies the absolute location of the ANTLR3 jar file if it is not in a location covered by %CLASSPATH%. Specify using UNIX directory delimiters to minimize problems." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-jar "[value]"" + /> + <StringProperty + Name="Java" + DisplayName="Java command" + PropertyPageName="Java" + Description="Specifies the command that invokes the java VM. Usually java, but could be something else such as jikes" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="[value]" + DefaultValue="java" + /> + <IntegerProperty + Name="MaxRules" + DisplayName="Max rule call" + PropertyPageName="Extended" + Category="Analysis" + Description="Maximum number of rule invocations during conversion" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xm [value]" + DefaultValue="4" + /> + <IntegerProperty + Name="MaxDFAEdges" + DisplayName="Max DFA Edges" + PropertyPageName="Extended" + Category="Extended" + Description="Maximum "comfortable" number of edges for single DFA state" + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xmaxdfaedges [value]" + DefaultValue="65534" + /> + <IntegerProperty + Name="DFATimeout" + DisplayName="DFA Timeout" + PropertyPageName="Extended" + Category="Extended" + Description="DFA conversion timeout period for each decision." + HelpURL="http://www.antlr.org/wiki/display/ANTLR3/Command+line+options" + Switch="-Xconversiontimeout [value]" + DefaultValue="1000" + /> + <BooleanProperty + Name="Absolute" + DisplayName="Absolute directories" + PropertyPageName="Code Generation" + Description="If true, causes ANTLR to assume output directory is always the absolute output path and not to use relative paths as per the intput spec. For visual studio, this should usually be set to true." + Switch="f" + DefaultValue="true" + /> + </Properties> + </CustomBuildRule> + </Rules> +</VisualStudioToolFile> diff --git a/antlr-3.1.3/runtime/CSharp/Cutting a new release.txt b/antlr-3.1.3/runtime/CSharp/Cutting a new release.txt new file mode 100644 index 0000000..dbc4e3c --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Cutting a new release.txt @@ -0,0 +1,43 @@ +===================================================================== +NOTE: The following top-level directory names: + - build + - bin + - docs + are *reserved* for use by the build system. The 'nant clean' + command will remove directories with any of these names + without warning!! +===================================================================== + +To cut a new release, I need to do the following: + +a) Change the version numbers in + + all.antlr3.runtime.net.build + README.TXT + Sources/Antlr3.Runtime/default.build + Sources/Antlr3.Runtime/AssemblyInfo.cs + Sources/Antlr3.Runtime/Antlr.Runtime/Constants.cs + Sources/Antlr3.Utility/default.build + Sources/Antlr3.Utility/AssemblyInfo.cs + Sources/Antlr3.Runtime.Tests/default.build + +b) Update the contents of + + README.TXT + CHANGES.TXT + +c) Clean the build output directories: + + nant clean + +d) Build the release assemblies (for .NET v1.1 and .NET v2.0) + + nant release -t:net-1.1 + nant release -t:net-2.0 + +e) Build the release distro archives (for .NET v1.1 and .NET v2.0) + + nant dist + + + \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/LICENSE.TXT b/antlr-3.1.3/runtime/CSharp/LICENSE.TXT new file mode 100644 index 0000000..1a9b1dd --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/LICENSE.TXT @@ -0,0 +1,30 @@ +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner shall be + under the terms and conditions of this license, without any + additional terms or conditions. + +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. diff --git a/antlr-3.1.3/runtime/CSharp/Libraries/MbUnit/MbUnit.Framework.dll b/antlr-3.1.3/runtime/CSharp/Libraries/MbUnit/MbUnit.Framework.dll new file mode 100644 index 0000000..18108b9 Binary files /dev/null and b/antlr-3.1.3/runtime/CSharp/Libraries/MbUnit/MbUnit.Framework.dll differ diff --git a/antlr-3.1.3/runtime/CSharp/Libraries/MbUnit/MbUnit.MSBuild.Tasks.dll b/antlr-3.1.3/runtime/CSharp/Libraries/MbUnit/MbUnit.MSBuild.Tasks.dll new file mode 100644 index 0000000..2de3b3f Binary files /dev/null and b/antlr-3.1.3/runtime/CSharp/Libraries/MbUnit/MbUnit.MSBuild.Tasks.dll differ diff --git a/antlr-3.1.3/runtime/CSharp/Libraries/MbUnit/MbUnit.Tasks.dll b/antlr-3.1.3/runtime/CSharp/Libraries/MbUnit/MbUnit.Tasks.dll new file mode 100644 index 0000000..47fab28 Binary files /dev/null and b/antlr-3.1.3/runtime/CSharp/Libraries/MbUnit/MbUnit.Tasks.dll differ diff --git a/antlr-3.1.3/runtime/CSharp/Libraries/MbUnit/QuickGraph.Algorithms.dll b/antlr-3.1.3/runtime/CSharp/Libraries/MbUnit/QuickGraph.Algorithms.dll new file mode 100644 index 0000000..b70672e Binary files /dev/null and b/antlr-3.1.3/runtime/CSharp/Libraries/MbUnit/QuickGraph.Algorithms.dll differ diff --git a/antlr-3.1.3/runtime/CSharp/Libraries/MbUnit/QuickGraph.dll b/antlr-3.1.3/runtime/CSharp/Libraries/MbUnit/QuickGraph.dll new file mode 100644 index 0000000..5f7dbbf Binary files /dev/null and b/antlr-3.1.3/runtime/CSharp/Libraries/MbUnit/QuickGraph.dll differ diff --git a/antlr-3.1.3/runtime/CSharp/Libraries/StringTemplate.NET/net-1.1/StringTemplate.dll b/antlr-3.1.3/runtime/CSharp/Libraries/StringTemplate.NET/net-1.1/StringTemplate.dll new file mode 100644 index 0000000..bb586d4 Binary files /dev/null and b/antlr-3.1.3/runtime/CSharp/Libraries/StringTemplate.NET/net-1.1/StringTemplate.dll differ diff --git a/antlr-3.1.3/runtime/CSharp/Libraries/StringTemplate.NET/net-1.1/antlr.runtime.dll b/antlr-3.1.3/runtime/CSharp/Libraries/StringTemplate.NET/net-1.1/antlr.runtime.dll new file mode 100644 index 0000000..cb95c6a Binary files /dev/null and b/antlr-3.1.3/runtime/CSharp/Libraries/StringTemplate.NET/net-1.1/antlr.runtime.dll differ diff --git a/antlr-3.1.3/runtime/CSharp/Libraries/StringTemplate.NET/net-2.0/StringTemplate.dll b/antlr-3.1.3/runtime/CSharp/Libraries/StringTemplate.NET/net-2.0/StringTemplate.dll new file mode 100644 index 0000000..b79f892 Binary files /dev/null and b/antlr-3.1.3/runtime/CSharp/Libraries/StringTemplate.NET/net-2.0/StringTemplate.dll differ diff --git a/antlr-3.1.3/runtime/CSharp/Libraries/StringTemplate.NET/net-2.0/antlr.runtime.dll b/antlr-3.1.3/runtime/CSharp/Libraries/StringTemplate.NET/net-2.0/antlr.runtime.dll new file mode 100644 index 0000000..cb95c6a Binary files /dev/null and b/antlr-3.1.3/runtime/CSharp/Libraries/StringTemplate.NET/net-2.0/antlr.runtime.dll differ diff --git a/antlr-3.1.3/runtime/CSharp/NOTICE.TXT b/antlr-3.1.3/runtime/CSharp/NOTICE.TXT new file mode 100644 index 0000000..9dda784 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/NOTICE.TXT @@ -0,0 +1,35 @@ +============================================================================= + +This software contains code derived from the ANTLR V3 Java Runtime Library +developed by Terence Parr in accordance with it's license: + + --------------------------------------------------------------------- + +[The "BSD licence"] +Copyright (c) 2003-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. + +============================================================================= \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/README.TXT b/antlr-3.1.3/runtime/CSharp/README.TXT new file mode 100644 index 0000000..5676d84 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/README.TXT @@ -0,0 +1,103 @@ +ANTLR v3.1 .NET Runtime Library (for us with the ANTLR C# Code Generator) + +29 September, 2007 + +Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com +Micheal Jordan + +1. INTRODUCTION + +The ANTLR v3.1 .NET Runtime Library extend the ANTLR language processing +tools generator to the C#/CLI platforms such as Microsoft .NET, +Novell/Ximian Mono and dotGNU. It is written in the C# programming language +and was designed specifically for use with the ANTLR C# Code Generation +target but, it would work equally well with a VB.NET, C++/CLI or indeed +IronPython code generator were such a thing to be developed for ANTLR v3.1. + +We hope you find the ANTLR v3.1 .NET Runtime Library delightful and useful +even but, as per the license under which you may use it, this software is not +guaranteed to work. + +Please see LICENSE.TXT for the full text of the license and NOTICE.TXT +for attribution notices. + + +2. WHAT'S IN THE PACK? + +This distribution contains three projects, the project files needed to +build them with Microsoft Visual Studio 2003 & 2005 and, Nant build files. + +The projects are: + + 1. Antlr3.Runtime - the ANTLR v3.1 .NET Runtime Library + + 2. Antlr3.Utility - the ANTLR v3.1 .NET Runtime Utility Library + + 3. Antlr3.Runtime.Tests - the ANTLR v3.1 .NET Runtime Library Tests + +In addition the Libraries sub-directory contains externals dependencies. + +2.1 Dependencies + + 1. Antlr3.Runtime - none + + 2. Antlr3.Utility - Antlr3.Runtime.dll + StringTemplate.dll + antlr.runtime.dll + + 3. Antlr3.Runtime.Tests - Antlr3.Runtime.dll + StringTemplate.dll + antlr.runtime.dll + +In addition, Antlr3.Runtime.Tests has a dependency on the MbUnit v2.4 dlls. + + +3. USING The ANTLR v3.1 .NET Runtime Library + +Tou use the ANTLR v3.1 .NET Runtime Library in your projects, add a +reference to the following file in your projects: + - Antlr3.Runtime.dll + +If you are using StringTemplate out in your grammar, add the following +files too: + - StringTemplate.dll + - antlr.runtime.dll + +You can find examples of using ANTLR v3.1 and the ANTLR v3.1 .NET Runtime +Library at: + http://www.antlr.org/download/examples-v3.tar.gz + + +4. BUILDING The ANTLR v3.1 .NET Runtime Library + +If you wish to re-build The ANTLR v3.1 .NET Runtime Library for any reason, this +is what you need to know. + + nant clean + nant release -t:net-1.1 +or + nant clean + nant release -t:net-2.0 + + +5. ANTLR v3.1 .NET Runtime Library STATUS + +This release of the ANTLR v3.1 .NET Runtime Library is a stable beta release. + +There are currently 0 failures in the unit test suite. + + +Don't forget to visit the www.antlr.org for further info. The mailing lists +are (currently) low volume and have a very high Signal-to-Noise ratio. We'd +like to hear about how you're using ANTLR v3.1 and the .NET Runtime Library. + + +7. CONTRIBUTORS + +Kunle Odutola +Micheal Jordan + + +Enjoy! + +Kunle Odutola diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime (VS2003).sln b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime (VS2003).sln new file mode 100644 index 0000000..c4d2f6c --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime (VS2003).sln @@ -0,0 +1,37 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Antlr3.Runtime (VS2003)", "Antlr3.Runtime\Antlr3.Runtime (VS2003).csproj", "{38F943D7-937F-4911-9882-24955F6AD0CC}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Antlr3.Utility (VS2003)", "Antlr3.Utility\Antlr3.Utility (VS2003).csproj", "{B8845D7E-8201-4904-87E7-85508F58B2F0}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Antlr.Runtime.Tests (VS2003)", "Antlr3.Runtime.Tests\Antlr.Runtime.Tests (VS2003).csproj", "{B51F118A-098F-4432-98A6-8C69B83BA3B9}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + Debug = Debug + Release = Release + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {38F943D7-937F-4911-9882-24955F6AD0CC}.Debug.ActiveCfg = Debug|.NET + {38F943D7-937F-4911-9882-24955F6AD0CC}.Debug.Build.0 = Debug|.NET + {38F943D7-937F-4911-9882-24955F6AD0CC}.Release.ActiveCfg = Release|.NET + {38F943D7-937F-4911-9882-24955F6AD0CC}.Release.Build.0 = Release|.NET + {B8845D7E-8201-4904-87E7-85508F58B2F0}.Debug.ActiveCfg = Debug|.NET + {B8845D7E-8201-4904-87E7-85508F58B2F0}.Debug.Build.0 = Debug|.NET + {B8845D7E-8201-4904-87E7-85508F58B2F0}.Release.ActiveCfg = Release|.NET + {B8845D7E-8201-4904-87E7-85508F58B2F0}.Release.Build.0 = Release|.NET + {B51F118A-098F-4432-98A6-8C69B83BA3B9}.Debug.ActiveCfg = Debug|.NET + {B51F118A-098F-4432-98A6-8C69B83BA3B9}.Debug.Build.0 = Debug|.NET + {B51F118A-098F-4432-98A6-8C69B83BA3B9}.Release.ActiveCfg = Release|.NET + {B51F118A-098F-4432-98A6-8C69B83BA3B9}.Release.Build.0 = Release|.NET + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime (VS2005).sln b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime (VS2005).sln new file mode 100644 index 0000000..e542995 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime (VS2005).sln @@ -0,0 +1,41 @@ + +Microsoft Visual Studio Solution File, Format Version 9.00 +# Visual Studio 2005 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Antlr3.Runtime (VS2005)", "Antlr3.Runtime\Antlr3.Runtime (VS2005).csproj", "{CF15D0D5-BE72-4F98-B70F-229ABA1DF0E8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Antlr3.Utility (VS2005)", "Antlr3.Utility\Antlr3.Utility (VS2005).csproj", "{BEB27DCC-ABFB-4951-B618-2B639EC65A46}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Antlr3.Runtime.Tests (VS2005)", "Antlr3.Runtime.Tests\Antlr3.Runtime.Tests (VS2005).csproj", "{784B3027-5E9C-4BF2-BFE6-B5002CAE30AB}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {784B3027-5E9C-4BF2-BFE6-B5002CAE30AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {784B3027-5E9C-4BF2-BFE6-B5002CAE30AB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {784B3027-5E9C-4BF2-BFE6-B5002CAE30AB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {784B3027-5E9C-4BF2-BFE6-B5002CAE30AB}.Release|Any CPU.Build.0 = Release|Any CPU + {BEB27DCC-ABFB-4951-B618-2B639EC65A46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BEB27DCC-ABFB-4951-B618-2B639EC65A46}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BEB27DCC-ABFB-4951-B618-2B639EC65A46}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BEB27DCC-ABFB-4951-B618-2B639EC65A46}.Release|Any CPU.Build.0 = Release|Any CPU + {CF15D0D5-BE72-4F98-B70F-229ABA1DF0E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CF15D0D5-BE72-4F98-B70F-229ABA1DF0E8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CF15D0D5-BE72-4F98-B70F-229ABA1DF0E8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CF15D0D5-BE72-4F98-B70F-229ABA1DF0E8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + name = Antlr3.Runtime (VS2005) + version = 0.1 + StartupItem = Antlr3.Runtime\Antlr3.Runtime (VS2005).csproj + Policies = $0 + $0.DotNetNamingPolicy = $1 + $1.DirectoryNamespaceAssociation = None + $1.ResourceNamePolicy = FileName + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/ANTLRxxxxStreamFixture.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/ANTLRxxxxStreamFixture.cs new file mode 100644 index 0000000..c91ef69 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/ANTLRxxxxStreamFixture.cs @@ -0,0 +1,423 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +#pragma warning disable 219 // No unused variable warnings + +namespace Antlr.Runtime.Tests +{ + using System; + using Stream = System.IO.Stream; + using FileStream = System.IO.FileStream; + using MemoryStream = System.IO.MemoryStream; + using FileMode = System.IO.FileMode; + using Encoding = System.Text.Encoding; + using Encoder = System.Text.Encoder; + + using ANTLRInputStream = Antlr.Runtime.ANTLRInputStream; + + using MbUnit.Framework; + + [TestFixture] + public class ANTLRxxxxStreamFixture : TestFixtureBase + { + private static readonly string grammarStr = "" + + "parser grammar p;" + NL + + "prog : WHILE ID LCURLY (assign)* RCURLY EOF;" + NL + + "assign : ID ASSIGN expr SEMI ;" + NL + + "expr : INT | FLOAT | ID ;" + NL; + + + #region ANTLRInputStream Tests + + [Test] + public void TestANTLRInputStreamConstructorDoesNotHang() + { + Encoding encoding = Encoding.Unicode; + byte[] grammarStrBuffer = encoding.GetBytes(grammarStr); + MemoryStream grammarStream = new MemoryStream(grammarStrBuffer); + + ANTLRInputStream input = new ANTLRInputStream(grammarStream, encoding); + } + + [Test] + public void TestSizeOnEmptyANTLRInputStream() + { + MemoryStream grammarStream = new MemoryStream(new byte[] { }); + + ANTLRInputStream inputStream = new ANTLRInputStream(grammarStream, Encoding.Unicode); + Assert.AreEqual(0, inputStream.Count); + } + + [Test] + public void TestSizeOnANTLRInputStream() + { + Encoding encoding = Encoding.Unicode; + byte[] grammarStrBuffer = encoding.GetBytes(grammarStr); + MemoryStream grammarStream = new MemoryStream(grammarStrBuffer); + + ANTLRInputStream inputStream = new ANTLRInputStream(grammarStream, Encoding.Unicode); + Assert.AreEqual(grammarStr.Length, inputStream.Count); + } + + [Test] + public void TestConsumeAndIndexOnANTLRInputStream() + { + Encoding encoding = Encoding.Unicode; + byte[] grammarStrBuffer = encoding.GetBytes(grammarStr); + MemoryStream grammarStream = new MemoryStream(grammarStrBuffer); + + ANTLRInputStream inputStream = new ANTLRInputStream(grammarStream, Encoding.Unicode); + Assert.AreEqual(0, inputStream.Index()); + + inputStream.Consume(); + Assert.AreEqual(1, inputStream.Index()); + + inputStream.Consume(); + Assert.AreEqual(2, inputStream.Index()); + + while (inputStream.Index() < inputStream.Count) + { + inputStream.Consume(); + } + Assert.AreEqual(inputStream.Index(), inputStream.Count); + } + + [Test] + public void TestConsumeAllCharactersInAnANTLRInputStream() + { + Encoding encoding = Encoding.Unicode; + byte[] grammarStrBuffer = encoding.GetBytes(grammarStr); + MemoryStream grammarStream = new MemoryStream(grammarStrBuffer); + + ANTLRInputStream inputStream = new ANTLRInputStream(grammarStream, Encoding.Unicode); + while (inputStream.Index() < inputStream.Count) + { + Console.Out.Write((char)inputStream.LA(1)); + inputStream.Consume(); + } + Assert.AreEqual(inputStream.Index(), inputStream.Count); + } + + [Test] + public void TestConsumeOnANTLRInputStream() + { + Encoding encoding = Encoding.Unicode; + byte[] buffer = encoding.GetBytes("One\r\nTwo"); + MemoryStream grammarStream = new MemoryStream(buffer); + + ANTLRInputStream inputStream = new ANTLRInputStream(grammarStream, Encoding.Unicode); + Assert.AreEqual(0, inputStream.Index()); + Assert.AreEqual(0, inputStream.CharPositionInLine); + Assert.AreEqual(1, inputStream.Line); + + inputStream.Consume(); // O + Assert.AreEqual(1, inputStream.Index()); + Assert.AreEqual(1, inputStream.CharPositionInLine); + Assert.AreEqual(1, inputStream.Line); + + inputStream.Consume(); // n + Assert.AreEqual(2, inputStream.Index()); + Assert.AreEqual(2, inputStream.CharPositionInLine); + Assert.AreEqual(1, inputStream.Line); + + inputStream.Consume(); // e + Assert.AreEqual(3, inputStream.Index()); + Assert.AreEqual(3, inputStream.CharPositionInLine); + Assert.AreEqual(1, inputStream.Line); + + inputStream.Consume(); // \r + Assert.AreEqual(4, inputStream.Index()); + Assert.AreEqual(4, inputStream.CharPositionInLine); + Assert.AreEqual(1, inputStream.Line); + + inputStream.Consume(); // \n + Assert.AreEqual(5, inputStream.Index()); + Assert.AreEqual(0, inputStream.CharPositionInLine); + Assert.AreEqual(2, inputStream.Line); + + inputStream.Consume(); // T + Assert.AreEqual(6, inputStream.Index()); + Assert.AreEqual(1, inputStream.CharPositionInLine); + Assert.AreEqual(2, inputStream.Line); + + inputStream.Consume(); // w + Assert.AreEqual(7, inputStream.Index()); + Assert.AreEqual(2, inputStream.CharPositionInLine); + Assert.AreEqual(2, inputStream.Line); + + inputStream.Consume(); // o + Assert.AreEqual(8, inputStream.Index()); + Assert.AreEqual(3, inputStream.CharPositionInLine); + Assert.AreEqual(2, inputStream.Line); + + inputStream.Consume(); // EOF + Assert.AreEqual(8, inputStream.Index()); + Assert.AreEqual(3, inputStream.CharPositionInLine); + Assert.AreEqual(2, inputStream.Line); + + inputStream.Consume(); // EOF + Assert.AreEqual(8, inputStream.Index()); + Assert.AreEqual(3, inputStream.CharPositionInLine); + Assert.AreEqual(2, inputStream.Line); + } + + [Test] + public void TestResetOnANTLRInputStream() + { + Encoding encoding = Encoding.Unicode; + byte[] buffer = encoding.GetBytes("One\r\nTwo"); + MemoryStream grammarStream = new MemoryStream(buffer); + + ANTLRInputStream inputStream = new ANTLRInputStream(grammarStream, encoding); + Assert.AreEqual(0, inputStream.Index()); + Assert.AreEqual(0, inputStream.CharPositionInLine); + Assert.AreEqual(1, inputStream.Line); + + inputStream.Consume(); // O + inputStream.Consume(); // n + + Assert.AreEqual('e', inputStream.LA(1)); + Assert.AreEqual(2, inputStream.Index()); + + inputStream.Reset(); + Assert.AreEqual('O', inputStream.LA(1)); + Assert.AreEqual(0, inputStream.Index()); + Assert.AreEqual(0, inputStream.CharPositionInLine); + Assert.AreEqual(1, inputStream.Line); + inputStream.Consume(); // O + + Assert.AreEqual('n', inputStream.LA(1)); + Assert.AreEqual(1, inputStream.Index()); + Assert.AreEqual(1, inputStream.CharPositionInLine); + Assert.AreEqual(1, inputStream.Line); + inputStream.Consume(); // n + + Assert.AreEqual('e', inputStream.LA(1)); + Assert.AreEqual(2, inputStream.Index()); + Assert.AreEqual(2, inputStream.CharPositionInLine); + Assert.AreEqual(1, inputStream.Line); + inputStream.Consume(); // e + } + + [Test] + public void TestSubstringOnANTLRInputStream() + { + Encoding encoding = Encoding.Unicode; + byte[] buffer = encoding.GetBytes("One\r\nTwo\r\nThree"); + MemoryStream grammarStream = new MemoryStream(buffer); + + ANTLRInputStream stream = new ANTLRInputStream(grammarStream, encoding); + + Assert.AreEqual("Two", stream.Substring(5, 7)); + Assert.AreEqual("One", stream.Substring(0, 2)); + Assert.AreEqual("Three", stream.Substring(10, 14)); + + stream.Consume(); + + Assert.AreEqual("Two", stream.Substring(5, 7)); + Assert.AreEqual("One", stream.Substring(0, 2)); + Assert.AreEqual("Three", stream.Substring(10, 14)); + } + + [Test] + public void TestSeekOnANTLRInputStream() + { + Encoding encoding = Encoding.Unicode; + byte[] buffer = encoding.GetBytes("One\r\nTwo\r\nThree"); + MemoryStream grammarStream = new MemoryStream(buffer); + + ANTLRInputStream stream = new ANTLRInputStream(grammarStream, encoding); + Assert.AreEqual('O', stream.LA(1)); + Assert.AreEqual(0, stream.Index()); + Assert.AreEqual(0, stream.CharPositionInLine); + Assert.AreEqual(1, stream.Line); + + stream.Seek(6); + Assert.AreEqual('w', stream.LA(1)); + Assert.AreEqual(6, stream.Index()); + Assert.AreEqual(1, stream.CharPositionInLine); + Assert.AreEqual(2, stream.Line); + + stream.Seek(11); + Assert.AreEqual('h', stream.LA(1)); + Assert.AreEqual(11, stream.Index()); + Assert.AreEqual(1, stream.CharPositionInLine); + Assert.AreEqual(3, stream.Line); + + // seeking backwards leaves state info (other than index in stream) unchanged + stream.Seek(1); + Assert.AreEqual('n', stream.LA(1)); + Assert.AreEqual(1, stream.Index()); + Assert.AreEqual(1, stream.CharPositionInLine); + Assert.AreEqual(3, stream.Line); + } + + #endregion + + + #region ANTLRStringStream Tests + + [Test] + public void TestSizeOnEmptyANTLRStringStream() + { + ANTLRStringStream s1 = new ANTLRStringStream(""); + Assert.AreEqual(0, s1.Count); + Assert.AreEqual(0, s1.Index()); + } + + [Test] + public void TestSizeOnANTLRStringStream() + { + ANTLRStringStream s1 = new ANTLRStringStream("lexer\r\n"); + Assert.AreEqual(7, s1.Count); + + ANTLRStringStream s2 = new ANTLRStringStream(grammarStr); + Assert.AreEqual(grammarStr.Length, s2.Count); + + ANTLRStringStream s3 = new ANTLRStringStream("grammar P;"); + Assert.AreEqual(10, s3.Count); + } + + [Test] + public void TestConsumeOnANTLRStringStream() + { + ANTLRStringStream stream = new ANTLRStringStream("One\r\nTwo"); + Assert.AreEqual(0, stream.Index()); + Assert.AreEqual(0, stream.CharPositionInLine); + Assert.AreEqual(1, stream.Line); + + stream.Consume(); // O + Assert.AreEqual(1, stream.Index()); + Assert.AreEqual(1, stream.CharPositionInLine); + Assert.AreEqual(1, stream.Line); + + stream.Consume(); // n + Assert.AreEqual(2, stream.Index()); + Assert.AreEqual(2, stream.CharPositionInLine); + Assert.AreEqual(1, stream.Line); + + stream.Consume(); // e + Assert.AreEqual(3, stream.Index()); + Assert.AreEqual(3, stream.CharPositionInLine); + Assert.AreEqual(1, stream.Line); + + stream.Consume(); // \r + Assert.AreEqual(4, stream.Index()); + Assert.AreEqual(4, stream.CharPositionInLine); + Assert.AreEqual(1, stream.Line); + + stream.Consume(); // \n + Assert.AreEqual(5, stream.Index()); + Assert.AreEqual(0, stream.CharPositionInLine); + Assert.AreEqual(2, stream.Line); + + stream.Consume(); // T + Assert.AreEqual(6, stream.Index()); + Assert.AreEqual(1, stream.CharPositionInLine); + Assert.AreEqual(2, stream.Line); + + stream.Consume(); // w + Assert.AreEqual(7, stream.Index()); + Assert.AreEqual(2, stream.CharPositionInLine); + Assert.AreEqual(2, stream.Line); + + stream.Consume(); // o + Assert.AreEqual(8, stream.Index()); + Assert.AreEqual(3, stream.CharPositionInLine); + Assert.AreEqual(2, stream.Line); + + stream.Consume(); // EOF + Assert.AreEqual(8, stream.Index()); + Assert.AreEqual(3, stream.CharPositionInLine); + Assert.AreEqual(2, stream.Line); + + stream.Consume(); // EOF + Assert.AreEqual(8, stream.Index()); + Assert.AreEqual(3, stream.CharPositionInLine); + Assert.AreEqual(2, stream.Line); + } + + [Test] + public void TestResetOnANTLRStringStream() + { + ANTLRStringStream stream = new ANTLRStringStream("One\r\nTwo"); + Assert.AreEqual(0, stream.Index()); + Assert.AreEqual(0, stream.CharPositionInLine); + Assert.AreEqual(1, stream.Line); + + stream.Consume(); // O + stream.Consume(); // n + + Assert.AreEqual('e', stream.LA(1)); + Assert.AreEqual(2, stream.Index()); + + stream.Reset(); + Assert.AreEqual('O', stream.LA(1)); + Assert.AreEqual(0, stream.Index()); + Assert.AreEqual(0, stream.CharPositionInLine); + Assert.AreEqual(1, stream.Line); + stream.Consume(); // O + + Assert.AreEqual('n', stream.LA(1)); + Assert.AreEqual(1, stream.Index()); + Assert.AreEqual(1, stream.CharPositionInLine); + Assert.AreEqual(1, stream.Line); + stream.Consume(); // n + + Assert.AreEqual('e', stream.LA(1)); + Assert.AreEqual(2, stream.Index()); + Assert.AreEqual(2, stream.CharPositionInLine); + Assert.AreEqual(1, stream.Line); + stream.Consume(); // e + } + + [Test] + public void TestSubstringOnANTLRStringStream() + { + ANTLRStringStream stream = new ANTLRStringStream("One\r\nTwo\r\nThree"); + + Assert.AreEqual("Two", stream.Substring(5, 7)); + Assert.AreEqual("One", stream.Substring(0, 2)); + Assert.AreEqual("Three", stream.Substring(10, 14)); + + stream.Consume(); + + Assert.AreEqual("Two", stream.Substring(5, 7)); + Assert.AreEqual("One", stream.Substring(0, 2)); + Assert.AreEqual("Three", stream.Substring(10, 14)); + } + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/Antlr.Runtime.Tests (VS2003).csproj b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/Antlr.Runtime.Tests (VS2003).csproj new file mode 100644 index 0000000..0d919bb --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/Antlr.Runtime.Tests (VS2003).csproj @@ -0,0 +1,120 @@ +<VisualStudioProject> + <CSHARP + ProjectType = "Local" + ProductVersion = "7.10.3077" + SchemaVersion = "2.0" + ProjectGuid = "{B51F118A-098F-4432-98A6-8C69B83BA3B9}" + > + <Build> + <Settings + ApplicationIcon = "" + AssemblyKeyContainerName = "" + AssemblyName = "Antlr.Runtime.Tests" + AssemblyOriginatorKeyFile = "" + DefaultClientScript = "JScript" + DefaultHTMLPageLayout = "Grid" + DefaultTargetSchema = "IE50" + DelaySign = "false" + OutputType = "Exe" + PreBuildEvent = "" + PostBuildEvent = "" + RootNamespace = "Antlr.Runtime.Tests" + RunPostBuildEvent = "OnBuildSuccess" + StartupObject = "" + > + <Config + Name = "Debug" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "DEBUG;STRONG_NAME;DOTNET1" + DocumentationFile = "" + DebugSymbols = "true" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "false" + OutputPath = "bin\Debug\net-1.1\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + <Config + Name = "Release" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "STRONG_NAME;DOTNET1" + DocumentationFile = "" + DebugSymbols = "false" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "false" + OutputPath = "bin\Release\net-1.1\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + </Settings> + <References> + <Reference + Name = "Antlr3.Runtime (VS2003)" + Project = "{38F943D7-937F-4911-9882-24955F6AD0CC}" + Package = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}" + /> + <Reference + Name = "MbUnit.Framework" + AssemblyName = "MbUnit.Framework" + HintPath = "..\..\Libraries\MbUnit\MbUnit.Framework.dll" + /> + </References> + </Build> + <Files> + <Include> + <File + RelPath = "ANTLRxxxxStreamFixture.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "ITreeFixture.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "ITreeNodeStreamFixture.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "TestDriver.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "TestFixtureBase.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "TreeWizardFixture.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Properties\AssemblyInfo.cs" + SubType = "Code" + BuildAction = "Compile" + /> + </Include> + </Files> + </CSHARP> +</VisualStudioProject> + diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/Antlr3.Runtime.Tests (VS2005).csproj b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/Antlr3.Runtime.Tests (VS2005).csproj new file mode 100644 index 0000000..0b60862 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/Antlr3.Runtime.Tests (VS2005).csproj @@ -0,0 +1,66 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProductVersion>8.0.50727</ProductVersion> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{784B3027-5E9C-4BF2-BFE6-B5002CAE30AB}</ProjectGuid> + <OutputType>Exe</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>Antlr.Runtime.Tests</RootNamespace> + <AssemblyName>Antlr3.Runtime.Tests</AssemblyName> + <StartupObject> + </StartupObject> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\net-2.0\</OutputPath> + <DefineConstants>TRACE;DEBUG;STRONG_NAME;DOTNET2</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\net-2.0\</OutputPath> + <DefineConstants>TRACE;STRONG_NAME;DOTNET2</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <UseVSHostingProcess>false</UseVSHostingProcess> + <DocumentationFile> + </DocumentationFile> + </PropertyGroup> + <ItemGroup> + <Compile Include="ANTLRxxxxStreamFixture.cs" /> + <Compile Include="RewriteRuleXxxxStreamFixture.cs" /> + <Compile Include="TestDriver.cs" /> + <Compile Include="TreeWizardFixture.cs" /> + <Compile Include="ITreeFixture.cs" /> + <Compile Include="ITreeNodeStreamFixture.cs" /> + <Compile Include="TestFixtureBase.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <Reference Include="MbUnit.Framework, Version=1.0.2700.29885, Culture=neutral, PublicKeyToken=5e72ecd30bc408d5"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\..\Libraries\MbUnit\MbUnit.Framework.dll</HintPath> + </Reference> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Antlr3.Runtime\Antlr3.Runtime (VS2005).csproj"> + <Project>{CF15D0D5-BE72-4F98-B70F-229ABA1DF0E8}</Project> + <Name>Antlr3.Runtime (VS2005)</Name> + </ProjectReference> + </ItemGroup> + <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/ITreeFixture.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/ITreeFixture.cs new file mode 100644 index 0000000..7c5fed0 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/ITreeFixture.cs @@ -0,0 +1,457 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tests +{ + using System; + using StringBuilder = System.Text.StringBuilder; + + using IToken = Antlr.Runtime.IToken; + using Token = Antlr.Runtime.Token; + using CommonToken = Antlr.Runtime.CommonToken; + using ITree = Antlr.Runtime.Tree.ITree; + using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; + using CommonTree = Antlr.Runtime.Tree.CommonTree; + using CommonTreeAdaptor = Antlr.Runtime.Tree.CommonTreeAdaptor; + + using MbUnit.Framework; + + [TestFixture] + public class ITreeFixture : TestFixtureBase + { + #region CommonTree Tests + + [Test] + public void testSingleNode() + { + CommonTree t = new CommonTree(new CommonToken(101)); + Assert.IsNull(t.parent); + Assert.AreEqual(-1, t.childIndex); + } + + [Test] + public void test4Nodes() + { + // ^(101 ^(102 103) 104) + CommonTree r0 = new CommonTree(new CommonToken(101)); + r0.AddChild(new CommonTree(new CommonToken(102))); + r0.GetChild(0).AddChild(new CommonTree(new CommonToken(103))); + r0.AddChild(new CommonTree(new CommonToken(104))); + + Assert.IsNull(r0.parent); + Assert.AreEqual(-1, r0.childIndex); + } + + [Test] + public void testList() + { + // ^(nil 101 102 103) + CommonTree r0 = new CommonTree((IToken)null); + CommonTree c0, c1, c2; + r0.AddChild(c0 = new CommonTree(new CommonToken(101))); + r0.AddChild(c1 = new CommonTree(new CommonToken(102))); + r0.AddChild(c2 = new CommonTree(new CommonToken(103))); + + Assert.IsNull(r0.parent); + Assert.AreEqual(-1, r0.childIndex); + Assert.AreEqual(r0, c0.parent); + Assert.AreEqual(0, c0.childIndex); + Assert.AreEqual(r0, c1.parent); + Assert.AreEqual(1, c1.childIndex); + Assert.AreEqual(r0, c2.parent); + Assert.AreEqual(2, c2.childIndex); + } + + [Test] + public void testList2() + { + // Add child ^(nil 101 102 103) to root 5 + // should pull 101 102 103 directly to become 5's child list + CommonTree root = new CommonTree(new CommonToken(5)); + + // child tree + CommonTree r0 = new CommonTree((IToken)null); + CommonTree c0, c1, c2; + r0.AddChild(c0 = new CommonTree(new CommonToken(101))); + r0.AddChild(c1 = new CommonTree(new CommonToken(102))); + r0.AddChild(c2 = new CommonTree(new CommonToken(103))); + + root.AddChild(r0); + + Assert.IsNull(root.parent); + Assert.AreEqual(-1, root.childIndex); + // check children of root all point at root + Assert.AreEqual(root, c0.parent); + Assert.AreEqual(0, c0.childIndex); + Assert.AreEqual(root, c0.parent); + Assert.AreEqual(1, c1.childIndex); + Assert.AreEqual(root, c0.parent); + Assert.AreEqual(2, c2.childIndex); + } + + [Test] + public void testAddListToExistChildren() + { + // Add child ^(nil 101 102 103) to root ^(5 6) + // should add 101 102 103 to end of 5's child list + CommonTree root = new CommonTree(new CommonToken(5)); + root.AddChild(new CommonTree(new CommonToken(6))); + + // child tree + CommonTree r0 = new CommonTree((IToken)null); + CommonTree c0, c1, c2; + r0.AddChild(c0 = new CommonTree(new CommonToken(101))); + r0.AddChild(c1 = new CommonTree(new CommonToken(102))); + r0.AddChild(c2 = new CommonTree(new CommonToken(103))); + + root.AddChild(r0); + + Assert.IsNull(root.parent); + Assert.AreEqual(-1, root.childIndex); + // check children of root all point at root + Assert.AreEqual(root, c0.parent); + Assert.AreEqual(1, c0.childIndex); + Assert.AreEqual(root, c0.parent); + Assert.AreEqual(2, c1.childIndex); + Assert.AreEqual(root, c0.parent); + Assert.AreEqual(3, c2.childIndex); + } + + [Test] + public void testDupTree() + { + // ^(101 ^(102 103 ^(106 107) ) 104 105) + CommonTree r0 = new CommonTree(new CommonToken(101)); + CommonTree r1 = new CommonTree(new CommonToken(102)); + r0.AddChild(r1); + r1.AddChild(new CommonTree(new CommonToken(103))); + ITree r2 = new CommonTree(new CommonToken(106)); + r2.AddChild(new CommonTree(new CommonToken(107))); + r1.AddChild(r2); + r0.AddChild(new CommonTree(new CommonToken(104))); + r0.AddChild(new CommonTree(new CommonToken(105))); + + CommonTree dup = (CommonTree)(new CommonTreeAdaptor()).DupTree(r0); + + Assert.IsNull(dup.parent); + Assert.AreEqual(-1, dup.childIndex); + dup.SanityCheckParentAndChildIndexes(); + } + + [Test] + public void testBecomeRoot() + { + // 5 becomes new root of ^(nil 101 102 103) + CommonTree newRoot = new CommonTree(new CommonToken(5)); + + CommonTree oldRoot = new CommonTree((IToken)null); + oldRoot.AddChild(new CommonTree(new CommonToken(101))); + oldRoot.AddChild(new CommonTree(new CommonToken(102))); + oldRoot.AddChild(new CommonTree(new CommonToken(103))); + + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + adaptor.BecomeRoot(newRoot, oldRoot); + newRoot.SanityCheckParentAndChildIndexes(); + } + + [Test] + public void testBecomeRoot2() + { + // 5 becomes new root of ^(101 102 103) + CommonTree newRoot = new CommonTree(new CommonToken(5)); + + CommonTree oldRoot = new CommonTree(new CommonToken(101)); + oldRoot.AddChild(new CommonTree(new CommonToken(102))); + oldRoot.AddChild(new CommonTree(new CommonToken(103))); + + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + adaptor.BecomeRoot(newRoot, oldRoot); + newRoot.SanityCheckParentAndChildIndexes(); + } + + [Test] + public void testBecomeRoot3() + { + // ^(nil 5) becomes new root of ^(nil 101 102 103) + CommonTree newRoot = new CommonTree((IToken)null); + newRoot.AddChild(new CommonTree(new CommonToken(5))); + + CommonTree oldRoot = new CommonTree((IToken)null); + oldRoot.AddChild(new CommonTree(new CommonToken(101))); + oldRoot.AddChild(new CommonTree(new CommonToken(102))); + oldRoot.AddChild(new CommonTree(new CommonToken(103))); + + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + adaptor.BecomeRoot(newRoot, oldRoot); + newRoot.SanityCheckParentAndChildIndexes(); + } + + [Test] + public void testBecomeRoot5() + { + // ^(nil 5) becomes new root of ^(101 102 103) + CommonTree newRoot = new CommonTree((IToken)null); + newRoot.AddChild(new CommonTree(new CommonToken(5))); + + CommonTree oldRoot = new CommonTree(new CommonToken(101)); + oldRoot.AddChild(new CommonTree(new CommonToken(102))); + oldRoot.AddChild(new CommonTree(new CommonToken(103))); + + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + adaptor.BecomeRoot(newRoot, oldRoot); + newRoot.SanityCheckParentAndChildIndexes(); + } + + [Test] + public void testBecomeRoot6() + { + // emulates construction of ^(5 6) + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + CommonTree root_0 = (CommonTree)adaptor.GetNilNode(); + CommonTree root_1 = (CommonTree)adaptor.GetNilNode(); + root_1 = (CommonTree)adaptor.BecomeRoot(new CommonTree(new CommonToken(5)), root_1); + + adaptor.AddChild(root_1, new CommonTree(new CommonToken(6))); + + adaptor.AddChild(root_0, root_1); + + root_0.SanityCheckParentAndChildIndexes(); + } + + // Test replaceChildren + + [Test] + public void testReplaceWithNoChildren() + { + CommonTree t = new CommonTree(new CommonToken(101)); + CommonTree newChild = new CommonTree(new CommonToken(5)); + bool error = false; + try + { + t.ReplaceChildren(0, 0, newChild); + } + catch (Exception) + { + error = true; + } + Assert.IsTrue(error); + } + + [Test] + public void testReplaceWithOneChildren() + { + // assume token type 99 and use text + CommonTree t = new CommonTree(new CommonToken(99, "a")); + CommonTree c0 = new CommonTree(new CommonToken(99, "b")); + t.AddChild(c0); + + CommonTree newChild = new CommonTree(new CommonToken(99, "c")); + t.ReplaceChildren(0, 0, newChild); + String expected = "(a c)"; + Assert.AreEqual(expected, t.ToStringTree()); + t.SanityCheckParentAndChildIndexes(); + } + + [Test] + public void testReplaceInMiddle() + { + CommonTree t = new CommonTree(new CommonToken(99, "a")); + t.AddChild(new CommonTree(new CommonToken(99, "b"))); + t.AddChild(new CommonTree(new CommonToken(99, "c"))); // index 1 + t.AddChild(new CommonTree(new CommonToken(99, "d"))); + + CommonTree newChild = new CommonTree(new CommonToken(99, "x")); + t.ReplaceChildren(1, 1, newChild); + String expected = "(a b x d)"; + Assert.AreEqual(expected, t.ToStringTree()); + t.SanityCheckParentAndChildIndexes(); + } + + [Test] + public void testReplaceAtLeft() + { + CommonTree t = new CommonTree(new CommonToken(99, "a")); + t.AddChild(new CommonTree(new CommonToken(99, "b"))); // index 0 + t.AddChild(new CommonTree(new CommonToken(99, "c"))); + t.AddChild(new CommonTree(new CommonToken(99, "d"))); + + CommonTree newChild = new CommonTree(new CommonToken(99, "x")); + t.ReplaceChildren(0, 0, newChild); + String expected = "(a x c d)"; + Assert.AreEqual(expected, t.ToStringTree()); + t.SanityCheckParentAndChildIndexes(); + } + + [Test] + public void testReplaceAtRight() + { + CommonTree t = new CommonTree(new CommonToken(99, "a")); + t.AddChild(new CommonTree(new CommonToken(99, "b"))); + t.AddChild(new CommonTree(new CommonToken(99, "c"))); + t.AddChild(new CommonTree(new CommonToken(99, "d"))); // index 2 + + CommonTree newChild = new CommonTree(new CommonToken(99, "x")); + t.ReplaceChildren(2, 2, newChild); + String expected = "(a b c x)"; + Assert.AreEqual(expected, t.ToStringTree()); + t.SanityCheckParentAndChildIndexes(); + } + + [Test] + public void testReplaceOneWithTwoAtLeft() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + CommonTree t = new CommonTree(new CommonToken(99, "a")); + t.AddChild(new CommonTree(new CommonToken(99, "b"))); + t.AddChild(new CommonTree(new CommonToken(99, "c"))); + t.AddChild(new CommonTree(new CommonToken(99, "d"))); + + CommonTree newChildren = (CommonTree)adaptor.GetNilNode(); + newChildren.AddChild(new CommonTree(new CommonToken(99, "x"))); + newChildren.AddChild(new CommonTree(new CommonToken(99, "y"))); + + t.ReplaceChildren(0, 0, newChildren); + String expected = "(a x y c d)"; + Assert.AreEqual(expected, t.ToStringTree()); + t.SanityCheckParentAndChildIndexes(); + } + + [Test] + public void testReplaceOneWithTwoAtRight() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + CommonTree t = new CommonTree(new CommonToken(99, "a")); + t.AddChild(new CommonTree(new CommonToken(99, "b"))); + t.AddChild(new CommonTree(new CommonToken(99, "c"))); + t.AddChild(new CommonTree(new CommonToken(99, "d"))); + + CommonTree newChildren = (CommonTree)adaptor.GetNilNode(); + newChildren.AddChild(new CommonTree(new CommonToken(99, "x"))); + newChildren.AddChild(new CommonTree(new CommonToken(99, "y"))); + + t.ReplaceChildren(2, 2, newChildren); + String expected = "(a b c x y)"; + Assert.AreEqual(expected, t.ToStringTree()); + t.SanityCheckParentAndChildIndexes(); + } + + [Test] + public void testReplaceOneWithTwoInMiddle() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + CommonTree t = new CommonTree(new CommonToken(99, "a")); + t.AddChild(new CommonTree(new CommonToken(99, "b"))); + t.AddChild(new CommonTree(new CommonToken(99, "c"))); + t.AddChild(new CommonTree(new CommonToken(99, "d"))); + + CommonTree newChildren = (CommonTree)adaptor.GetNilNode(); + newChildren.AddChild(new CommonTree(new CommonToken(99, "x"))); + newChildren.AddChild(new CommonTree(new CommonToken(99, "y"))); + + t.ReplaceChildren(1, 1, newChildren); + String expected = "(a b x y d)"; + Assert.AreEqual(expected, t.ToStringTree()); + t.SanityCheckParentAndChildIndexes(); + } + + [Test] + public void testReplaceTwoWithOneAtLeft() + { + CommonTree t = new CommonTree(new CommonToken(99, "a")); + t.AddChild(new CommonTree(new CommonToken(99, "b"))); + t.AddChild(new CommonTree(new CommonToken(99, "c"))); + t.AddChild(new CommonTree(new CommonToken(99, "d"))); + + CommonTree newChild = new CommonTree(new CommonToken(99, "x")); + + t.ReplaceChildren(0, 1, newChild); + String expected = "(a x d)"; + Assert.AreEqual(expected, t.ToStringTree()); + t.SanityCheckParentAndChildIndexes(); + } + + [Test] + public void testReplaceTwoWithOneAtRight() + { + CommonTree t = new CommonTree(new CommonToken(99, "a")); + t.AddChild(new CommonTree(new CommonToken(99, "b"))); + t.AddChild(new CommonTree(new CommonToken(99, "c"))); + t.AddChild(new CommonTree(new CommonToken(99, "d"))); + + CommonTree newChild = new CommonTree(new CommonToken(99, "x")); + + t.ReplaceChildren(1, 2, newChild); + String expected = "(a b x)"; + Assert.AreEqual(expected, t.ToStringTree()); + t.SanityCheckParentAndChildIndexes(); + } + + [Test] + public void testReplaceAllWithOne() + { + CommonTree t = new CommonTree(new CommonToken(99, "a")); + t.AddChild(new CommonTree(new CommonToken(99, "b"))); + t.AddChild(new CommonTree(new CommonToken(99, "c"))); + t.AddChild(new CommonTree(new CommonToken(99, "d"))); + + CommonTree newChild = new CommonTree(new CommonToken(99, "x")); + + t.ReplaceChildren(0, 2, newChild); + String expected = "(a x)"; + Assert.AreEqual(expected, t.ToStringTree()); + t.SanityCheckParentAndChildIndexes(); + } + + [Test] + public void testReplaceAllWithTwo() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + CommonTree t = new CommonTree(new CommonToken(99, "a")); + t.AddChild(new CommonTree(new CommonToken(99, "b"))); + t.AddChild(new CommonTree(new CommonToken(99, "c"))); + t.AddChild(new CommonTree(new CommonToken(99, "d"))); + + CommonTree newChildren = (CommonTree)adaptor.GetNilNode(); + newChildren.AddChild(new CommonTree(new CommonToken(99, "x"))); + newChildren.AddChild(new CommonTree(new CommonToken(99, "y"))); + + t.ReplaceChildren(0, 2, newChildren); + String expected = "(a x y)"; + Assert.AreEqual(expected, t.ToStringTree()); + t.SanityCheckParentAndChildIndexes(); + } + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/ITreeNodeStreamFixture.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/ITreeNodeStreamFixture.cs new file mode 100644 index 0000000..7969200 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/ITreeNodeStreamFixture.cs @@ -0,0 +1,660 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tests +{ + using System; + using StringBuilder = System.Text.StringBuilder; + + using IToken = Antlr.Runtime.IToken; + using Token = Antlr.Runtime.Token; + using CommonToken = Antlr.Runtime.CommonToken; + using ITree = Antlr.Runtime.Tree.ITree; + using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream; + using CommonTree = Antlr.Runtime.Tree.CommonTree; + using CommonTreeNodeStream = Antlr.Runtime.Tree.CommonTreeNodeStream; + using UnBufferedTreeNodeStream = Antlr.Runtime.Tree.UnBufferedTreeNodeStream; + + using MbUnit.Framework; + + [TestFixture] + public class ITreeNodeStreamFixture : TestFixtureBase + { + #region CommonTreeNodeStream Tests + + [Test] + public void testSingleNode() + { + ITree t = new CommonTree(new CommonToken(101)); + + ITreeNodeStream stream = CreateCommonTreeNodeStream(t); + string expected = " 101"; + string actual = GetStringOfEntireStreamContentsWithNodeTypesOnly(stream); + Assert.AreEqual(expected, actual); + + expected = " 101"; + actual = stream.ToString(); + Assert.AreEqual(expected, actual); + } + + [Test] + /// <summary> + /// Test a tree with four nodes - ^(101 ^(102 103) 104) + /// </summary> + public void test4Nodes() + { + ITree t = new CommonTree(new CommonToken(101)); + t.AddChild(new CommonTree(new CommonToken(102))); + t.GetChild(0).AddChild(new CommonTree(new CommonToken(103))); + t.AddChild(new CommonTree(new CommonToken(104))); + + ITreeNodeStream stream = CreateCommonTreeNodeStream(t); + string expected = " 101 102 103 104"; + string actual = GetStringOfEntireStreamContentsWithNodeTypesOnly(stream); + Assert.AreEqual(expected, actual); + + expected = " 101 2 102 2 103 3 104 3"; + actual = stream.ToString(); + Assert.AreEqual(expected, actual); + } + + [Test] + public void testList() + { + ITree root = new CommonTree((IToken)null); + + ITree t = new CommonTree(new CommonToken(101)); + t.AddChild(new CommonTree(new CommonToken(102))); + t.GetChild(0).AddChild(new CommonTree(new CommonToken(103))); + t.AddChild(new CommonTree(new CommonToken(104))); + + ITree u = new CommonTree(new CommonToken(105)); + + root.AddChild(t); + root.AddChild(u); + + CommonTreeNodeStream stream = new CommonTreeNodeStream(root); + string expected = " 101 102 103 104 105"; + string actual = GetStringOfEntireStreamContentsWithNodeTypesOnly(stream); + Assert.AreEqual(expected, actual); + + expected = " 101 2 102 2 103 3 104 3 105"; + actual = stream.ToString(); + Assert.AreEqual(expected, actual); + } + + [Test] + public void testFlatList() + { + ITree root = new CommonTree((IToken)null); + + root.AddChild(new CommonTree(new CommonToken(101))); + root.AddChild(new CommonTree(new CommonToken(102))); + root.AddChild(new CommonTree(new CommonToken(103))); + + CommonTreeNodeStream stream = new CommonTreeNodeStream(root); + string expected = " 101 102 103"; + string actual = GetStringOfEntireStreamContentsWithNodeTypesOnly(stream); + Assert.AreEqual(expected, actual); + + expected = " 101 102 103"; + actual = stream.ToString(); + Assert.AreEqual(expected, actual); + } + + [Test] + public void testListWithOneNode() + { + ITree root = new CommonTree((IToken)null); + + root.AddChild(new CommonTree(new CommonToken(101))); + + CommonTreeNodeStream stream = new CommonTreeNodeStream(root); + string expected = " 101"; + string actual = GetStringOfEntireStreamContentsWithNodeTypesOnly(stream); + Assert.AreEqual(expected, actual); + + expected = " 101"; + actual = stream.ToString(); + Assert.AreEqual(expected, actual); + } + + [Test] + public void testAoverB() + { + ITree t = new CommonTree(new CommonToken(101)); + t.AddChild(new CommonTree(new CommonToken(102))); + + ITreeNodeStream stream = CreateCommonTreeNodeStream(t); + string expected = " 101 102"; + string actual = GetStringOfEntireStreamContentsWithNodeTypesOnly(stream); + Assert.AreEqual(expected, actual); + + expected = " 101 2 102 3"; + actual = stream.ToString(); + Assert.AreEqual(expected, actual); + } + + [Test] + public void testLT() + { + // ^(101 ^(102 103) 104) + ITree t = new CommonTree(new CommonToken(101)); + t.AddChild(new CommonTree(new CommonToken(102))); + t.GetChild(0).AddChild(new CommonTree(new CommonToken(103))); + t.AddChild(new CommonTree(new CommonToken(104))); + + ITreeNodeStream stream = CreateCommonTreeNodeStream(t); + Assert.AreEqual(101, ((ITree)stream.LT(1)).Type); + Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(2)).Type); + Assert.AreEqual(102, ((ITree)stream.LT(3)).Type); + Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(4)).Type); + Assert.AreEqual(103, ((ITree)stream.LT(5)).Type); + Assert.AreEqual(Token.UP, ((ITree)stream.LT(6)).Type); + Assert.AreEqual(104, ((ITree)stream.LT(7)).Type); + Assert.AreEqual(Token.UP, ((ITree)stream.LT(8)).Type); + Assert.AreEqual(Token.EOF, ((ITree)stream.LT(9)).Type); + // check way ahead + Assert.AreEqual(Token.EOF, ((ITree)stream.LT(100)).Type); + } + + [Test] + public void testMarkRewindEntire() + { + // ^(101 ^(102 103 ^(106 107) ) 104 105) + // stream has 7 real + 6 nav nodes + // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF + ITree r0 = new CommonTree(new CommonToken(101)); + ITree r1 = new CommonTree(new CommonToken(102)); + r0.AddChild(r1); + r1.AddChild(new CommonTree(new CommonToken(103))); + ITree r2 = new CommonTree(new CommonToken(106)); + r2.AddChild(new CommonTree(new CommonToken(107))); + r1.AddChild(r2); + r0.AddChild(new CommonTree(new CommonToken(104))); + r0.AddChild(new CommonTree(new CommonToken(105))); + + CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); + int m = stream.Mark(); // MARK + for (int k = 1; k <= 13; k++) + { // consume til end + stream.LT(1); + stream.Consume(); + } + Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); + Assert.AreEqual(Token.UP, ((ITree)stream.LT(-1)).Type); + stream.Rewind(m); // REWIND + + // consume til end again :) + for (int k = 1; k <= 13; k++) + { // consume til end + stream.LT(1); + stream.Consume(); + } + Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); + Assert.AreEqual(Token.UP, ((ITree)stream.LT(-1)).Type); + } + + [Test] + public void testMarkRewindInMiddle() + { + // ^(101 ^(102 103 ^(106 107) ) 104 105) + // stream has 7 real + 6 nav nodes + // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF + ITree r0 = new CommonTree(new CommonToken(101)); + ITree r1 = new CommonTree(new CommonToken(102)); + r0.AddChild(r1); + r1.AddChild(new CommonTree(new CommonToken(103))); + ITree r2 = new CommonTree(new CommonToken(106)); + r2.AddChild(new CommonTree(new CommonToken(107))); + r1.AddChild(r2); + r0.AddChild(new CommonTree(new CommonToken(104))); + r0.AddChild(new CommonTree(new CommonToken(105))); + + CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); + for (int k = 1; k <= 7; k++) + { // consume til middle + //System.out.println(((ITree)stream.LT(1)).Type); + stream.Consume(); + } + Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); + int m = stream.Mark(); // MARK + stream.Consume(); // consume 107 + stream.Consume(); // consume UP + stream.Consume(); // consume UP + stream.Consume(); // consume 104 + stream.Rewind(m); // REWIND + + Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); + stream.Consume(); + Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); + stream.Consume(); + Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); + stream.Consume(); + Assert.AreEqual(104, ((ITree)stream.LT(1)).Type); + stream.Consume(); + // now we're past rewind position + Assert.AreEqual(105, ((ITree)stream.LT(1)).Type); + stream.Consume(); + Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); + stream.Consume(); + Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); + Assert.AreEqual(Token.UP, ((ITree)stream.LT(-1)).Type); + } + + [Test] + public void testMarkRewindNested() + { + // ^(101 ^(102 103 ^(106 107) ) 104 105) + // stream has 7 real + 6 nav nodes + // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF + ITree r0 = new CommonTree(new CommonToken(101)); + ITree r1 = new CommonTree(new CommonToken(102)); + r0.AddChild(r1); + r1.AddChild(new CommonTree(new CommonToken(103))); + ITree r2 = new CommonTree(new CommonToken(106)); + r2.AddChild(new CommonTree(new CommonToken(107))); + r1.AddChild(r2); + r0.AddChild(new CommonTree(new CommonToken(104))); + r0.AddChild(new CommonTree(new CommonToken(105))); + + CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); + int m = stream.Mark(); // MARK at start + stream.Consume(); // consume 101 + stream.Consume(); // consume DN + int m2 = stream.Mark(); // MARK on 102 + stream.Consume(); // consume 102 + stream.Consume(); // consume DN + stream.Consume(); // consume 103 + stream.Consume(); // consume 106 + stream.Rewind(m2); // REWIND to 102 + Assert.AreEqual(102, ((ITree)stream.LT(1)).Type); + stream.Consume(); + Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); + stream.Consume(); + // stop at 103 and rewind to start + stream.Rewind(m); // REWIND to 101 + Assert.AreEqual(101, ((ITree)stream.LT(1)).Type); + stream.Consume(); + Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); + stream.Consume(); + Assert.AreEqual(102, ((ITree)stream.LT(1)).Type); + stream.Consume(); + Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); + } + + [Test] + public void testSeek() + { + // ^(101 ^(102 103 ^(106 107) ) 104 105) + // stream has 7 real + 6 nav nodes + // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF + ITree r0 = new CommonTree(new CommonToken(101)); + ITree r1 = new CommonTree(new CommonToken(102)); + r0.AddChild(r1); + r1.AddChild(new CommonTree(new CommonToken(103))); + ITree r2 = new CommonTree(new CommonToken(106)); + r2.AddChild(new CommonTree(new CommonToken(107))); + r1.AddChild(r2); + r0.AddChild(new CommonTree(new CommonToken(104))); + r0.AddChild(new CommonTree(new CommonToken(105))); + + CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); + stream.Consume(); // consume 101 + stream.Consume(); // consume DN + stream.Consume(); // consume 102 + stream.Seek(7); // seek to 107 + Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume 107 + stream.Consume(); // consume UP + stream.Consume(); // consume UP + Assert.AreEqual(104, ((ITree)stream.LT(1)).Type); + } + + [Test] + public void testSeekFromStart() + { + // ^(101 ^(102 103 ^(106 107) ) 104 105) + // stream has 7 real + 6 nav nodes + // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF + ITree r0 = new CommonTree(new CommonToken(101)); + ITree r1 = new CommonTree(new CommonToken(102)); + r0.AddChild(r1); + r1.AddChild(new CommonTree(new CommonToken(103))); + ITree r2 = new CommonTree(new CommonToken(106)); + r2.AddChild(new CommonTree(new CommonToken(107))); + r1.AddChild(r2); + r0.AddChild(new CommonTree(new CommonToken(104))); + r0.AddChild(new CommonTree(new CommonToken(105))); + + CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); + stream.Seek(7); // seek to 107 + Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume 107 + stream.Consume(); // consume UP + stream.Consume(); // consume UP + Assert.AreEqual(104, ((ITree)stream.LT(1)).Type); + } + + [Test] + public void testPushPop() + { + // ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109) + // stream has 9 real + 8 nav nodes + // Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP + ITree r0 = new CommonTree(new CommonToken(101)); + ITree r1 = new CommonTree(new CommonToken(102)); + r1.AddChild(new CommonTree(new CommonToken(103))); + r0.AddChild(r1); + ITree r2 = new CommonTree(new CommonToken(104)); + r2.AddChild(new CommonTree(new CommonToken(105))); + r0.AddChild(r2); + ITree r3 = new CommonTree(new CommonToken(106)); + r3.AddChild(new CommonTree(new CommonToken(107))); + r0.AddChild(r3); + r0.AddChild(new CommonTree(new CommonToken(108))); + r0.AddChild(new CommonTree(new CommonToken(109))); + + CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); + String expecting = " 101 2 102 2 103 3 104 2 105 3 106 2 107 3 108 109 3"; + String found = stream.ToString(); + Assert.AreEqual(expecting, found); + + // Assume we want to hit node 107 and then "call 102" then return + + int indexOf102 = 2; + int indexOf107 = 12; + for (int k = 1; k <= indexOf107; k++) + { // consume til 107 node + stream.Consume(); + } + // CALL 102 + Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); + stream.Push(indexOf102); + Assert.AreEqual(102, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume 102 + Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume DN + Assert.AreEqual(103, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume 103 + Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); + // RETURN + stream.Pop(); + Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); + } + + [Test] + public void testNestedPushPop() + { + // ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109) + // stream has 9 real + 8 nav nodes + // Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP + ITree r0 = new CommonTree(new CommonToken(101)); + ITree r1 = new CommonTree(new CommonToken(102)); + r1.AddChild(new CommonTree(new CommonToken(103))); + r0.AddChild(r1); + ITree r2 = new CommonTree(new CommonToken(104)); + r2.AddChild(new CommonTree(new CommonToken(105))); + r0.AddChild(r2); + ITree r3 = new CommonTree(new CommonToken(106)); + r3.AddChild(new CommonTree(new CommonToken(107))); + r0.AddChild(r3); + r0.AddChild(new CommonTree(new CommonToken(108))); + r0.AddChild(new CommonTree(new CommonToken(109))); + + CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); + + // Assume we want to hit node 107 and then "call 102", which + // calls 104, then return + + int indexOf102 = 2; + int indexOf107 = 12; + for (int k = 1; k <= indexOf107; k++) + { // consume til 107 node + stream.Consume(); + } + Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); + // CALL 102 + stream.Push(indexOf102); + Assert.AreEqual(102, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume 102 + Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume DN + Assert.AreEqual(103, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume 103 + + // CALL 104 + int indexOf104 = 6; + stream.Push(indexOf104); + Assert.AreEqual(104, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume 102 + Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume DN + Assert.AreEqual(105, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume 103 + Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); + // RETURN (to UP node in 102 subtree) + stream.Pop(); + + Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); + // RETURN (to empty stack) + stream.Pop(); + Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); + } + + [Test] + public void testPushPopFromEOF() + { + // ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109) + // stream has 9 real + 8 nav nodes + // Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP + ITree r0 = new CommonTree(new CommonToken(101)); + ITree r1 = new CommonTree(new CommonToken(102)); + r1.AddChild(new CommonTree(new CommonToken(103))); + r0.AddChild(r1); + ITree r2 = new CommonTree(new CommonToken(104)); + r2.AddChild(new CommonTree(new CommonToken(105))); + r0.AddChild(r2); + ITree r3 = new CommonTree(new CommonToken(106)); + r3.AddChild(new CommonTree(new CommonToken(107))); + r0.AddChild(r3); + r0.AddChild(new CommonTree(new CommonToken(108))); + r0.AddChild(new CommonTree(new CommonToken(109))); + + CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); + + while (stream.LA(1) != Token.EOF) + { + stream.Consume(); + } + int indexOf102 = 2; + int indexOf104 = 6; + Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); + + // CALL 102 + stream.Push(indexOf102); + Assert.AreEqual(102, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume 102 + Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume DN + Assert.AreEqual(103, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume 103 + Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); + // RETURN (to empty stack) + stream.Pop(); + Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); + + // CALL 104 + stream.Push(indexOf104); + Assert.AreEqual(104, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume 102 + Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume DN + Assert.AreEqual(105, ((ITree)stream.LT(1)).Type); + stream.Consume(); // consume 103 + Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); + // RETURN (to empty stack) + stream.Pop(); + Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); + } + + [Test] + public void testStackStretch() + { + // make more than INITIAL_CALL_STACK_SIZE pushes + ITree r0 = new CommonTree(new CommonToken(101)); + CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); + // go 1 over initial size + for (int i = 1; i <= CommonTreeNodeStream.INITIAL_CALL_STACK_SIZE + 1; i++) + { + stream.Push(i); + } + Assert.AreEqual(10, stream.Pop()); + Assert.AreEqual(9, stream.Pop()); + } + + #endregion + + + #region UnBufferedTreeNodeStream Tests + + [Test] + public void testBufferOverflow() + { + StringBuilder buf = new StringBuilder(); + StringBuilder buf2 = new StringBuilder(); + // make ^(101 102 ... n) + ITree t = new CommonTree(new CommonToken(101)); + buf.Append(" 101"); + buf2.Append(" 101"); + buf2.Append(" "); + buf2.Append(Token.DOWN); + for (int i = 0; i <= UnBufferedTreeNodeStream.INITIAL_LOOKAHEAD_BUFFER_SIZE + 10; i++) + { + t.AddChild(new CommonTree(new CommonToken(102 + i))); + buf.Append(" "); + buf.Append(102 + i); + buf2.Append(" "); + buf2.Append(102 + i); + } + buf2.Append(" "); + buf2.Append(Token.UP); + + ITreeNodeStream stream = CreateUnBufferedTreeNodeStream(t); + String expecting = buf.ToString(); + String found = GetStringOfEntireStreamContentsWithNodeTypesOnly(stream); + Assert.AreEqual(expecting, found); + + expecting = buf2.ToString(); + found = stream.ToString(); + Assert.AreEqual(expecting, found); + } + + /// <summary> + /// Test what happens when tail hits the end of the buffer, but there + /// is more room left. + /// </summary> + /// <remarks> + /// Specifically that would mean that head is not at 0 but has + /// advanced somewhere to the middle of the lookahead buffer. + /// + /// Use Consume() to advance N nodes into lookahead. Then use LT() + /// to load at least INITIAL_LOOKAHEAD_BUFFER_SIZE-N nodes so the + /// buffer has to wrap. + /// </remarks> + [Test] + public void testBufferWrap() + { + int N = 10; + // make tree with types: 1 2 ... INITIAL_LOOKAHEAD_BUFFER_SIZE+N + ITree t = new CommonTree((IToken)null); + for (int i = 0; i < UnBufferedTreeNodeStream.INITIAL_LOOKAHEAD_BUFFER_SIZE + N; i++) + { + t.AddChild(new CommonTree(new CommonToken(i + 1))); + } + + // move head to index N + ITreeNodeStream stream = CreateUnBufferedTreeNodeStream(t); + for (int i = 1; i <= N; i++) + { // consume N + ITree node = (ITree)stream.LT(1); + Assert.AreEqual(i, node.Type); + stream.Consume(); + } + + // now use LT to lookahead past end of buffer + int remaining = UnBufferedTreeNodeStream.INITIAL_LOOKAHEAD_BUFFER_SIZE - N; + int wrapBy = 4; // wrap around by 4 nodes + Assert.IsTrue(wrapBy < N, "bad test code; wrapBy must be less than N"); + for (int i = 1; i <= remaining + wrapBy; i++) + { // wrap past end of buffer + ITree node = (ITree)stream.LT(i); // look ahead to ith token + Assert.AreEqual(N + i, node.Type); + } + } + + #endregion + + + #region Helper Methods + + protected ITreeNodeStream CreateCommonTreeNodeStream(object t) + { + return new CommonTreeNodeStream(t); + } + + protected ITreeNodeStream CreateUnBufferedTreeNodeStream(object t) + { + return new UnBufferedTreeNodeStream(t); + } + + public string GetStringOfEntireStreamContentsWithNodeTypesOnly(ITreeNodeStream nodes) + { + StringBuilder buf = new StringBuilder(); + for (int i = 0; i < nodes.Count; i++) + { + object t = nodes.LT(i + 1); + int type = nodes.TreeAdaptor.GetNodeType(t); + if (!((type == Token.DOWN) || (type == Token.UP))) + { + buf.Append(" "); + buf.Append(type); + } + } + return buf.ToString(); + } + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/Properties/AssemblyInfo.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..fc419c9 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,69 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Antlr3.Runtime.Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Toshiba")] +[assembly: AssemblyProduct("Antlr3.Runtime.Tests")] +[assembly: AssemblyCopyright("Copyright © Toshiba 2007")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("e7a97455-a3e3-4877-a90b-b1b12606dac1")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Revision and Build Numbers +// by using the '*' as shown below: +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/RewriteRuleXxxxStreamFixture.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/RewriteRuleXxxxStreamFixture.cs new file mode 100644 index 0000000..e673685 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/RewriteRuleXxxxStreamFixture.cs @@ -0,0 +1,392 @@ +/* +[The "BSD licence"] +Copyright (c) 2007 Johannes Luber +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + +#pragma warning disable 219 // No unused variable warnings + +namespace Antlr.Runtime.Tests { + using System; + using System.Collections.Generic; + using Antlr.Runtime.Tree; + + using MbUnit.Framework; + + [TestFixture] + public class RewriteRuleXxxxStreamFixture : TestFixtureBase { + #region Check Constructors + + [Test] + public void CheckRewriteRuleTokenStreamConstructors() { + RewriteRuleTokenStream tokenTest1 = new RewriteRuleTokenStream(CreateTreeAdaptor(), + "RewriteRuleTokenStream test1"); + + RewriteRuleTokenStream tokenTest2 = new RewriteRuleTokenStream(CreateTreeAdaptor(), + "RewriteRuleTokenStream test2", CreateToken(1, + "test token without any real context")); + + RewriteRuleTokenStream tokenTest3 = new RewriteRuleTokenStream(CreateTreeAdaptor(), + "RewriteRuleTokenStream test3", CreateTokenList(4)); + } + + [Test] + public void CheckRewriteRuleSubtreeStreamConstructors() { + RewriteRuleSubtreeStream subtreeTest1 = + new RewriteRuleSubtreeStream(CreateTreeAdaptor(), + "RewriteRuleSubtreeStream test1"); + + RewriteRuleSubtreeStream subtreeTest2 = + new RewriteRuleSubtreeStream(CreateTreeAdaptor(), + "RewriteRuleSubtreeStream test2", CreateToken(1, + "test token without any real context")); + + RewriteRuleSubtreeStream subtreeTest3 = + new RewriteRuleSubtreeStream(CreateTreeAdaptor(), + "RewriteRuleSubtreeStream test3", CreateTokenList(4)); + } + + [Test] + public void CheckRewriteRuleNodeStreamConstructors() { + RewriteRuleNodeStream nodeTest1 = new RewriteRuleNodeStream(CreateTreeAdaptor(), + "RewriteRuleNodeStream test1"); + + RewriteRuleNodeStream nodeTest2 = new RewriteRuleNodeStream(CreateTreeAdaptor(), + "RewriteRuleNodeStream test2", CreateToken(1, + "test token without any real context")); + + RewriteRuleNodeStream nodeTest3 = new RewriteRuleNodeStream(CreateTreeAdaptor(), + "RewriteRuleNodeStream test3", CreateTokenList(4)); + } + #endregion + + #region Method Tests + + #region Empty Behaviour + [Test] + [ExpectedException(typeof(RewriteEmptyStreamException), "RewriteRuleTokenStream test")] + public void CheckRRTokenStreamBehaviourWhileEmpty1() { + string description = "RewriteRuleTokenStream test"; + RewriteRuleTokenStream tokenTest = + new RewriteRuleTokenStream(CreateTreeAdaptor(), description); + + Assert.IsFalse(tokenTest.HasNext(), "tokenTest has to give back false here."); + Assert.AreEqual(description.ToString(), tokenTest.Description, + "Description strings should be equal."); + Assert.AreEqual(0, tokenTest.Size(), "The number of elements should be zero."); + tokenTest.Reset(); + Assert.IsTrue(true, "Reset() shouldn't make any problems here."); + Assert.AreEqual(0, tokenTest.Size(), + "The number of elements should be still zero."); + tokenTest.NextNode(); + } + + [Test] + [ExpectedException(typeof(RewriteEmptyStreamException), + "RewriteRuleSubtreeStream test")] + public void CheckRRSubtreeStreamBehaviourWhileEmpty1() { + string description = "RewriteRuleSubtreeStream test"; + RewriteRuleSubtreeStream subtreeTest = + new RewriteRuleSubtreeStream(CreateTreeAdaptor(), description); + + Assert.IsFalse(subtreeTest.HasNext(), "HasNext() has to give back false here."); + Assert.AreEqual(description.ToString(), subtreeTest.Description, + "Description strings should be equal."); + Assert.AreEqual(0, subtreeTest.Size(), "The number of elements should be zero."); + subtreeTest.Reset(); + Assert.IsTrue(true, "Reset() shouldn't make any problems here."); + Assert.AreEqual(0, subtreeTest.Size(), + "The number of elements should be still zero."); + subtreeTest.NextNode(); + } + + [Test] + [ExpectedException(typeof(RewriteEmptyStreamException), "RewriteRuleNodeStream test")] + public void CheckRRNodeStreamBehaviourWhileEmpty1() { + string description = "RewriteRuleNodeStream test"; + RewriteRuleNodeStream nodeTest = + new RewriteRuleNodeStream(CreateTreeAdaptor(), description); + + Assert.IsFalse(nodeTest.HasNext(), "HasNext() has to give back false here."); + Assert.AreEqual(description.ToString(), nodeTest.Description, + "Description strings should be equal."); + Assert.AreEqual(0, nodeTest.Size(), "The number of elements should be zero."); + nodeTest.Reset(); + Assert.IsTrue(true, "Reset() shouldn't make any problems here."); + Assert.AreEqual(0, nodeTest.Size(), + "The number of elements should be still zero."); + nodeTest.NextNode(); + } + + [Test] + [ExpectedException(typeof(RewriteEmptyStreamException), "RewriteRuleTokenStream test")] + public void CheckRRTokenStreamBehaviourWhileEmpty2() { + RewriteRuleTokenStream tokenTest = new RewriteRuleTokenStream(CreateTreeAdaptor(), + "RewriteRuleTokenStream test"); + + tokenTest.NextTree(); + } + + [Test] + [ExpectedException(typeof(RewriteEmptyStreamException), + "RewriteRuleSubtreeStream test")] + public void CheckRRSubtreeStreamBehaviourWhileEmpty2() { + RewriteRuleSubtreeStream subtreeTest = new RewriteRuleSubtreeStream( + CreateTreeAdaptor(), "RewriteRuleSubtreeStream test"); + + subtreeTest.NextTree(); + } + + [Test] + [ExpectedException(typeof(RewriteEmptyStreamException), "RewriteRuleNodeStream test")] + public void CheckRRNodeStreamBehaviourWhileEmpty2() { + RewriteRuleNodeStream nodeTest = new RewriteRuleNodeStream(CreateTreeAdaptor(), + "RewriteRuleNodeStream test"); + + nodeTest.NextTree(); + } + + [Test] + [ExpectedException(typeof(RewriteEmptyStreamException), "RewriteRuleTokenStream test")] + public void CheckRRTokenStreamBehaviourWhileEmpty3() { + RewriteRuleTokenStream tokenTest = new RewriteRuleTokenStream(CreateTreeAdaptor(), + "RewriteRuleTokenStream test"); + + tokenTest.NextToken(); + } + + #endregion + + #region Behaviour with Elements + [Test] + [ExpectedException(typeof(RewriteCardinalityException), "RewriteRuleTokenStream test")] + public void CheckRRTokenStreamBehaviourWithElements() { + RewriteRuleTokenStream tokenTest = new RewriteRuleTokenStream(CreateTreeAdaptor(), + "RewriteRuleTokenStream test"); + + IToken token1 = CreateToken(1, "test token without any real context"); + + // Test Add() + tokenTest.Add(token1); + Assert.AreEqual(1, tokenTest.Size(), "tokenTest should have the size 1 (1)."); + Assert.IsTrue(tokenTest.HasNext(), "HasNext() should be true here (1)."); + + // Test NextNode() + CommonTree tree = (CommonTree) tokenTest.NextNode(); + Assert.AreEqual(token1, tree.Token, + "The returned token should be equal to the given token (1)."); + Assert.AreEqual(1, tokenTest.Size(), "tokenTest should have the size 1 (2)."); + Assert.IsFalse(tokenTest.HasNext(), "HasNext() should be false here (1)."); + tokenTest.Reset(); + Assert.AreEqual(1, tokenTest.Size(), "tokenTest should have the size 1 (3)."); + Assert.IsTrue(tokenTest.HasNext(), "HasNext() should be true here (2)."); + + // Test NextToken() + IToken returnedToken = tokenTest.NextToken(); + Assert.AreEqual(token1, returnedToken, + "The returned token should be equal to the given token (2)."); + Assert.AreEqual(1, tokenTest.Size(), "tokenTest should have the size 1 (4)."); + Assert.IsFalse(tokenTest.HasNext(), "HasNext() should be false here (2)."); + tokenTest.Reset(); + Assert.AreEqual(1, tokenTest.Size(), "tokenTest should have the size 1 (5)."); + Assert.IsTrue(tokenTest.HasNext(), "HasNext() should be true here (3)."); + + // Test NextTree() + returnedToken = (IToken) tokenTest.NextTree(); + Assert.AreEqual(token1, returnedToken, + "The returned token should be equal to the given token (3)."); + Assert.AreEqual(1, tokenTest.Size(), "tokenTest should have the size 1 (6)."); + Assert.IsFalse(tokenTest.HasNext(), "HasNext() should be false here (2)."); + tokenTest.Reset(); + Assert.AreEqual(1, tokenTest.Size(), "tokenTest should have the size 1 (7)."); + Assert.IsTrue(tokenTest.HasNext(), "HasNext() should be true here (3)."); + + // Test, what happens with two elements + IToken token2 = CreateToken(2, "test token without any real context"); + + tokenTest.Add(token2); + Assert.AreEqual(2, tokenTest.Size(), "tokenTest should have the size 2 (1)."); + Assert.IsTrue(tokenTest.HasNext(), "HasNext() should be true here (4)."); + returnedToken = tokenTest.NextToken(); + Assert.AreEqual(token1, returnedToken, + "The returned token should be equal to the given token (4)."); + Assert.AreEqual(2, tokenTest.Size(), "tokenTest should have the size 2 (2)."); + Assert.IsTrue(tokenTest.HasNext(), "HasNext() should be true here (5)."); + returnedToken = tokenTest.NextToken(); + Assert.AreEqual(token2, returnedToken, + "The returned token should be equal to the given token (5)."); + Assert.IsFalse(tokenTest.HasNext(), "HasNext() should be false here (3)."); + + // Test exception + tokenTest.NextToken(); + } + + [Test] + [ExpectedException(typeof(RewriteCardinalityException), + "RewriteRuleSubtreeStream test")] + public void CheckRRSubtreeStreamBehaviourWithElements() { + RewriteRuleSubtreeStream subtreeTest = + new RewriteRuleSubtreeStream(CreateTreeAdaptor(), + "RewriteRuleSubtreeStream test"); + + IToken token1 = CreateToken(1, "test token without any real context"); + ITree tree1 = CreateTree(token1); + + // Test Add() + subtreeTest.Add(tree1); + Assert.AreEqual(1, subtreeTest.Size(), "subtreeTest should have the size 1 (1)."); + Assert.IsTrue(subtreeTest.HasNext(), "HasNext() should be true here (1)."); + + // Test NextNode() + Assert.AreEqual(tree1, (ITree) subtreeTest.NextNode(), + "The returned tree should be equal to the given tree (1)."); + Assert.AreEqual(1, subtreeTest.Size(), "subtreeTest should have the size 1 (2)."); + Assert.IsFalse(subtreeTest.HasNext(), "HasNext() should be false here (1)."); + subtreeTest.Reset(); + Assert.AreEqual(1, subtreeTest.Size(), "subtreeTest should have the size 1 (3)."); + Assert.IsTrue(subtreeTest.HasNext(), "HasNext() should be true here (2)."); + + // Test NextTree() + CommonTree returnedTree = (CommonTree) subtreeTest.NextTree(); + Assert.AreEqual(token1, returnedTree.Token, + "The returned token should be equal to the given token (3)."); + Assert.AreEqual(1, subtreeTest.Size(), "subtreeTest should have the size 1 (4)."); + Assert.IsFalse(subtreeTest.HasNext(), "HasNext() should be false here (2)."); + subtreeTest.Reset(); + Assert.AreEqual(1, subtreeTest.Size(), "subtreeTest should have the size 1 (5)."); + Assert.IsTrue(subtreeTest.HasNext(), "HasNext() should be true here (3)."); + + // Test, what happens with two elements + IToken token2 = CreateToken(2, "test token without any real context"); + ITree tree2 = CreateTree(token2); + + subtreeTest.Add(tree2); + Assert.AreEqual(2, subtreeTest.Size(), "subtreeTest should have the size 2 (1)."); + Assert.IsTrue(subtreeTest.HasNext(), "HasNext() should be true here (4)."); + returnedTree = (CommonTree) subtreeTest.NextTree(); + Assert.AreEqual(token1, returnedTree.Token, + "The returned token should be equal to the given token (4)."); + Assert.AreEqual(2, subtreeTest.Size(), "subtreeTest should have the size 2 (2)."); + Assert.IsTrue(subtreeTest.HasNext(), "HasNext() should be true here (5)."); + returnedTree = (CommonTree) subtreeTest.NextTree(); + Assert.AreEqual(token2, returnedTree.Token, + "The returned token should be equal to the given token (5)."); + Assert.IsFalse(subtreeTest.HasNext(), "HasNext() should be false here (3)."); + + // Test exception + subtreeTest.NextTree(); + } + + [Test] + [ExpectedException(typeof(RewriteCardinalityException), "RewriteRuleNodeStream test")] + public void CheckRRNodeStreamBehaviourWithElements() { + RewriteRuleNodeStream nodeTest = new RewriteRuleNodeStream(CreateTreeAdaptor(), + "RewriteRuleNodeStream test"); + + IToken token1 = CreateToken(1, "test token without any real context"); + ITree tree1 = CreateTree(token1); + + // Test Add() + nodeTest.Add(tree1); + Assert.AreEqual(1, nodeTest.Size(), "nodeTest should have the size 1 (1)."); + Assert.IsTrue(nodeTest.HasNext(), "HasNext() should be true here (1)."); + + // Test NextNode() + CommonTree returnedTree = (CommonTree) nodeTest.NextNode(); + Assert.AreEqual(tree1.Type, returnedTree.Type, + "The returned tree should be equal to the given tree (1)."); + Assert.AreEqual(1, nodeTest.Size(), "nodeTest should have the size 1 (2)."); + Assert.IsFalse(nodeTest.HasNext(), "HasNext() should be false here (1)."); + nodeTest.Reset(); + Assert.AreEqual(1, nodeTest.Size(), "nodeTest should have the size 1 (3)."); + Assert.IsTrue(nodeTest.HasNext(), "HasNext() should be true here (2)."); + + // Test NextTree() + returnedTree = (CommonTree) nodeTest.NextTree(); + Assert.AreEqual(token1, returnedTree.Token, + "The returned token should be equal to the given token (3)."); + Assert.AreEqual(1, nodeTest.Size(), "nodeTest should have the size 1 (4)."); + Assert.IsFalse(nodeTest.HasNext(), "HasNext() should be false here (2)."); + nodeTest.Reset(); + Assert.AreEqual(1, nodeTest.Size(), "nodeTest should have the size 1 (5)."); + Assert.IsTrue(nodeTest.HasNext(), "HasNext() should be true here (3)."); + + // Test, what happens with two elements + IToken token2 = CreateToken(2, "test token without any real context"); + ITree tree2 = CreateTree(token2); + + nodeTest.Add(tree2); + Assert.AreEqual(2, nodeTest.Size(), "nodeTest should have the size 2 (1)."); + Assert.IsTrue(nodeTest.HasNext(), "HasNext() should be true here (4)."); + returnedTree = (CommonTree) nodeTest.NextTree(); + Assert.AreEqual(token1, returnedTree.Token, + "The returned token should be equal to the given token (4)."); + Assert.AreEqual(2, nodeTest.Size(), "nodeTest should have the size 2 (2)."); + Assert.IsTrue(nodeTest.HasNext(), "HasNext() should be true here (5)."); + returnedTree = (CommonTree) nodeTest.NextTree(); + Assert.AreEqual(token2, returnedTree.Token, + "The returned token should be equal to the given token (5)."); + Assert.IsFalse(nodeTest.HasNext(), "HasNext() should be false here (3)."); + + // Test exception + nodeTest.NextTree(); + } + + #endregion + + #endregion + + + #region Helper Methods + + private ITreeAdaptor CreateTreeAdaptor() { + return new CommonTreeAdaptor(); + } + + private ITree CreateTree(IToken token) { + return new CommonTree(token); + } + + private IToken CreateToken(int type, string text) { + return new CommonToken(type, text); + } + + private IList<IToken> CreateTokenList(int count) { + IList<IToken> list = new List<IToken>(); + for (int i = 0; i < count; i++) { + list.Add(new CommonToken((i+1), "test token " + (i+1).ToString() + + " without any real context")); + } + return list; + } + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/TestDriver.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/TestDriver.cs new file mode 100644 index 0000000..730ece9 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/TestDriver.cs @@ -0,0 +1,64 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tests +{ + using System; + using MbUnit.Framework; + using MbUnit.Core; + + public class TestDriver + { + public static void Main(string[] args) + { + // Change 'NOT_DEBUGGING' --> '!NOT_DEBUGGING' to run individual fixtures +#if !NOT_DEBUGGING + //ITreeFixture fixture = new ITreeFixture(); + ANTLRxxxxStreamFixture fixture = new ANTLRxxxxStreamFixture(); + fixture.TestConsumeAllCharactersInAnANTLRInputStream(); +#else + StartAutoRunner(); +#endif + } + + public static void StartAutoRunner() + { + using (AutoRunner auto = new AutoRunner()) + { + auto.Run(); + auto.ReportToHtml(); + } + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/TestFixtureBase.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/TestFixtureBase.cs new file mode 100644 index 0000000..2bfdc86 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/TestFixtureBase.cs @@ -0,0 +1,44 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tests +{ + using System; + using MbUnit.Framework; + + abstract public class TestFixtureBase + { + public static readonly string NL = Environment.NewLine; + } +} diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/TreeWizardFixture.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/TreeWizardFixture.cs new file mode 100644 index 0000000..e08d43e --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime.Tests/TreeWizardFixture.cs @@ -0,0 +1,515 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tests +{ + using System; + using IList = System.Collections.IList; + using IDictionary = System.Collections.IDictionary; + using ArrayList = System.Collections.ArrayList; + using Hashtable = System.Collections.Hashtable; + using StringBuilder = System.Text.StringBuilder; + + using IToken = Antlr.Runtime.IToken; + using Token = Antlr.Runtime.Token; + using CommonToken = Antlr.Runtime.CommonToken; + using ITree = Antlr.Runtime.Tree.ITree; + using TreeWizard = Antlr.Runtime.Tree.TreeWizard; + using CommonTree = Antlr.Runtime.Tree.CommonTree; + using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; + using CommonTreeAdaptor = Antlr.Runtime.Tree.CommonTreeAdaptor; + using CollectionUtils = Antlr.Runtime.Collections.CollectionUtils; + + using MbUnit.Framework; + + [TestFixture] + public class TreeWizardFixture : TestFixtureBase + { + protected static readonly String[] tokens = + new String[] { "", "", "", "", "", "A", "B", "C", "D", "E", "ID", "VAR" }; + + private sealed class RecordAllElementsVisitor : TreeWizard.Visitor + { + private IList list; + + public RecordAllElementsVisitor(IList list) + { + this.list = list; + } + + override public void Visit(object t) + { + list.Add(t); + } + } + + private sealed class Test1ContextVisitor : TreeWizard.ContextVisitor + { + private ITreeAdaptor adaptor; + private IList list; + + public Test1ContextVisitor(ITreeAdaptor adaptor, IList list) + { + this.adaptor = adaptor; + this.list = list; + } + + public void Visit(object t, object parent, int childIndex, IDictionary labels) + { + list.Add(adaptor.GetNodeText(t) + + "@" + ((parent != null) ? adaptor.GetNodeText(parent) : "nil") + + "[" + childIndex + "]"); + } + } + + private sealed class Test2ContextVisitor : TreeWizard.ContextVisitor + { + private ITreeAdaptor adaptor; + private IList list; + + public Test2ContextVisitor(ITreeAdaptor adaptor, IList list) + { + this.adaptor = adaptor; + this.list = list; + } + + public void Visit(object t, object parent, int childIndex, IDictionary labels) + { + + list.Add(adaptor.GetNodeText(t) + + "@" + ((parent != null) ? adaptor.GetNodeText(parent) : "nil") + + "[" + childIndex + "]" + labels["a"] + "&" + labels["b"]); + } + } + + #region TreeWizard Tests + + [Test] + public void testSingleNode() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("ID"); + string actual = t.ToStringTree(); + string expected = "ID"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testSingleNodeWithArg() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("ID[foo]"); + string actual = t.ToStringTree(); + string expected = "foo"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testSingleNodeTree() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A)"); + string actual = t.ToStringTree(); + string expected = "A"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testSingleLevelTree() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B C D)"); + string actual = t.ToStringTree(); + string expected = "(A B C D)"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testListTree() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(nil A B C)"); + string actual = t.ToStringTree(); + string expected = "A B C"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testInvalidListTree() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("A B C"); + Assert.IsTrue(t == null); + } + + [Test] + public void testDoubleLevelTree() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A (B C) (B D) E)"); + string actual = t.ToStringTree(); + string expected = "(A (B C) (B D) E)"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testSingleNodeIndex() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("ID"); + IDictionary m = wiz.Index(t); + string actual = CollectionUtils.DictionaryToString(m); + string expected = "{10=[ID]}"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testNoRepeatsIndex() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B C D)"); + IDictionary m = wiz.Index(t); + string actual = CollectionUtils.DictionaryToString(m); + //string expected = "{8=[D], 6=[B], 7=[C], 5=[A]}"; + string expected = "{8=[D], 7=[C], 6=[B], 5=[A]}"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testRepeatsIndex() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B (A C B) B D D)"); + IDictionary m = wiz.Index(t); + string actual = CollectionUtils.DictionaryToString(m); + //string expected = "{8=[D, D], 6=[B, B, B], 7=[C], 5=[A, A]}"; + string expected = "{8=[D, D], 7=[C], 6=[B, B, B], 5=[A, A]}"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testNoRepeatsVisit() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B C D)"); + IList elements = new ArrayList(); + wiz.Visit(t, wiz.GetTokenType("B"), new RecordAllElementsVisitor(elements)); + string actual = CollectionUtils.ListToString(elements); + string expected = "[B]"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testNoRepeatsVisit2() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B (A C B) B D D)"); + IList elements = new ArrayList(); + wiz.Visit(t, wiz.GetTokenType("C"), new RecordAllElementsVisitor(elements)); + string actual = CollectionUtils.ListToString(elements); + string expected = "[C]"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testRepeatsVisit() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B (A C B) B D D)"); + IList elements = new ArrayList(); + wiz.Visit(t, wiz.GetTokenType("B"), new RecordAllElementsVisitor(elements)); + string actual = CollectionUtils.ListToString(elements); + string expected = "[B, B, B]"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testRepeatsVisit2() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B (A C B) B D D)"); + IList elements = new ArrayList(); + wiz.Visit(t, wiz.GetTokenType("A"), new RecordAllElementsVisitor(elements)); + string actual = CollectionUtils.ListToString(elements); + string expected = "[A, A]"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testRepeatsVisitWithContext() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B (A C B) B D D)"); + IList elements = new ArrayList(); + wiz.Visit(t, wiz.GetTokenType("B"), new Test1ContextVisitor(adaptor, elements)); + string actual = CollectionUtils.ListToString(elements); + string expected = "[B@A[0], B@A[1], B@A[2]]"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testRepeatsVisitWithNullParentAndContext() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B (A C B) B D D)"); + IList elements = new ArrayList(); + wiz.Visit(t, wiz.GetTokenType("A"), new Test1ContextVisitor(adaptor, elements)); + string actual = CollectionUtils.ListToString(elements); + string expected = "[A@nil[0], A@A[1]]"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testVisitPattern() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B C (A B) D)"); + IList elements = new ArrayList(); + wiz.Visit(t, "(A B)", new RecordAllElementsVisitor(elements)); + string actual = CollectionUtils.ListToString(elements); + string expected = "[A]"; // shouldn't match overall root, just (A B) + Assert.AreEqual(expected, actual); + } + + [Test] + public void testVisitPatternMultiple() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B C (A B) (D (A B)))"); + IList elements = new ArrayList(); + wiz.Visit(t, "(A B)", new Test1ContextVisitor(adaptor, elements)); + string actual = CollectionUtils.ListToString(elements); + string expected = "[A@A[2], A@D[0]]"; // shouldn't match overall root, just (A B) + Assert.AreEqual(expected, actual); + } + + [Test] + public void testVisitPatternMultipleWithLabels() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))"); + IList elements = new ArrayList(); + wiz.Visit(t, "(%a:A %b:B)", new Test2ContextVisitor(adaptor, elements)); + string actual = CollectionUtils.ListToString(elements); + string expected = "[foo@A[2]foo&bar, big@D[0]big&dog]"; + Assert.AreEqual(expected, actual); + } + + [Test] + public void testParse() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B C)"); + bool valid = wiz.Parse(t, "(A B C)"); + Assert.IsTrue(valid); + } + + [Test] + public void testParseSingleNode() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("A"); + bool valid = wiz.Parse(t, "A"); + Assert.IsTrue(valid); + } + + [Test] + public void testParseFlatTree() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(nil A B C)"); + bool valid = wiz.Parse(t, "(nil A B C)"); + Assert.IsTrue(valid); + } + + [Test] + public void testWildcard() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B C)"); + bool valid = wiz.Parse(t, "(A . .)"); + Assert.IsTrue(valid); + } + + [Test] + public void testParseWithText() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B[foo] C[bar])"); + // C pattern has no text arg so despite [bar] in t, no need + // to match text--check structure only. + bool valid = wiz.Parse(t, "(A B[foo] C)"); + Assert.IsTrue(valid); + } + + [Test] + public void testParseWithTextFails() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B C)"); + bool valid = wiz.Parse(t, "(A[foo] B C)"); + Assert.IsTrue(!valid); // fails + } + + [Test] + public void testParseLabels() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B C)"); + IDictionary labels = new Hashtable(); + bool valid = wiz.Parse(t, "(%a:A %b:B %c:C)", labels); + Assert.IsTrue(valid); + Assert.AreEqual("A", labels["a"].ToString()); + Assert.AreEqual("B", labels["b"].ToString()); + Assert.AreEqual("C", labels["c"].ToString()); + } + + [Test] + public void testParseWithWildcardLabels() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B C)"); + IDictionary labels = new Hashtable(); + bool valid = wiz.Parse(t, "(A %b:. %c:.)", labels); + Assert.IsTrue(valid); + Assert.AreEqual("B", labels["b"].ToString()); + Assert.AreEqual("C", labels["c"].ToString()); + } + + [Test] + public void testParseLabelsAndTestText() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B[foo] C)"); + IDictionary labels = new Hashtable(); + bool valid = wiz.Parse(t, "(%a:A %b:B[foo] %c:C)", labels); + Assert.IsTrue(valid); + Assert.AreEqual("A", labels["a"].ToString()); + Assert.AreEqual("foo", labels["b"].ToString()); + Assert.AreEqual("C", labels["c"].ToString()); + } + + [Test] + public void testParseLabelsInNestedTree() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A (B C) (D E))"); + IDictionary labels = new Hashtable(); + bool valid = wiz.Parse(t, "(%a:A (%b:B %c:C) (%d:D %e:E) )", labels); + Assert.IsTrue(valid); + Assert.AreEqual("A", labels["a"].ToString()); + Assert.AreEqual("B", labels["b"].ToString()); + Assert.AreEqual("C", labels["c"].ToString()); + Assert.AreEqual("D", labels["d"].ToString()); + Assert.AreEqual("E", labels["e"].ToString()); + } + + [Test] + public void testEquals() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t1 = (CommonTree)wiz.Create("(A B C)"); + CommonTree t2 = (CommonTree)wiz.Create("(A B C)"); + bool same = TreeWizard.Equals(t1, t2, adaptor); + Assert.IsTrue(same); + } + + [Test] + public void testEqualsWithText() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t1 = (CommonTree)wiz.Create("(A B[foo] C)"); + CommonTree t2 = (CommonTree)wiz.Create("(A B[foo] C)"); + bool same = TreeWizard.Equals(t1, t2, adaptor); + Assert.IsTrue(same); + } + + [Test] + public void testEqualsWithMismatchedText() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t1 = (CommonTree)wiz.Create("(A B[foo] C)"); + CommonTree t2 = (CommonTree)wiz.Create("(A B C)"); + bool same = TreeWizard.Equals(t1, t2, adaptor); + Assert.IsTrue(!same); + } + + [Test] + public void testFindPattern() + { + ITreeAdaptor adaptor = new CommonTreeAdaptor(); + TreeWizard wiz = new TreeWizard(adaptor, tokens); + CommonTree t = (CommonTree)wiz.Create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))"); + IList subtrees = wiz.Find(t, "(A B)"); + IList elements = subtrees; + string actual = CollectionUtils.ListToString(elements); + string expected = "[foo, big]"; + Assert.AreEqual(expected, actual); + } + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Collections/CollectionUtils.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Collections/CollectionUtils.cs new file mode 100644 index 0000000..2afb16d --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Collections/CollectionUtils.cs @@ -0,0 +1,129 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Collections +{ + using System; + using IList = System.Collections.IList; + using IDictionary = System.Collections.IDictionary; + using DictionaryEntry = System.Collections.DictionaryEntry; + using IEnumerator = System.Collections.IEnumerator; + using StringBuilder = System.Text.StringBuilder; + + public class CollectionUtils + { + /// <summary> + /// Returns a string representation of this IList. + /// </summary> + /// <remarks> + /// The string representation is a list of the collection's elements in the order + /// they are returned by its IEnumerator, enclosed in square brackets ("[]"). + /// The separator is a comma followed by a space i.e. ", ". + /// </remarks> + /// <param name="coll">Collection whose string representation will be returned</param> + /// <returns>A string representation of the specified collection or "null"</returns> + public static string ListToString(IList coll) + { + StringBuilder sb = new StringBuilder(); + + if (coll != null) + { + sb.Append("["); + for (int i = 0; i < coll.Count; i++) + { + if (i > 0) + sb.Append(", "); + + object element = coll[i]; + if (element == null) + sb.Append("null"); + else if (element is IDictionary) + sb.Append(DictionaryToString((IDictionary)element)); + else if (element is IList) + sb.Append(ListToString((IList)element)); + else + sb.Append(element.ToString()); + + } + sb.Append("]"); + } + else + sb.Insert(0, "null"); + + return sb.ToString(); + } + + + /// <summary> + /// Returns a string representation of this IDictionary. + /// </summary> + /// <remarks> + /// The string representation is a list of the collection's elements in the order + /// they are returned by its IEnumerator, enclosed in curly brackets ("{}"). + /// The separator is a comma followed by a space i.e. ", ". + /// </remarks> + /// <param name="dict">Dictionary whose string representation will be returned</param> + /// <returns>A string representation of the specified dictionary or "null"</returns> + public static string DictionaryToString(IDictionary dict) + { + StringBuilder sb = new StringBuilder(); + + if (dict != null) + { + sb.Append("{"); + int i = 0; + foreach (DictionaryEntry e in dict) + { + if (i > 0) + { + sb.Append(", "); + } + + if (e.Value is IDictionary) + sb.AppendFormat("{0}={1}", e.Key.ToString(), DictionaryToString((IDictionary)e.Value)); + else if (e.Value is IList) + sb.AppendFormat("{0}={1}", e.Key.ToString(), ListToString((IList)e.Value)); + else + sb.AppendFormat("{0}={1}", e.Key.ToString(), e.Value.ToString()); + i++; + } + sb.Append("}"); + } + else + sb.Insert(0, "null"); + + return sb.ToString(); + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Collections/HashList.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Collections/HashList.cs new file mode 100644 index 0000000..fd4872c --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Collections/HashList.cs @@ -0,0 +1,501 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Collections +{ + using System; + using IDictionary = System.Collections.IDictionary; + using IDictionaryEnumerator = System.Collections.IDictionaryEnumerator; + using ICollection = System.Collections.ICollection; + using IEnumerator = System.Collections.IEnumerator; + using Hashtable = System.Collections.Hashtable; + using ArrayList = System.Collections.ArrayList; + using DictionaryEntry = System.Collections.DictionaryEntry; + using StringBuilder = System.Text.StringBuilder; + + /// <summary> + /// An Hashtable-backed dictionary that enumerates Keys and Values in + /// insertion order. + /// </summary> + public sealed class HashList : IDictionary + { + #region Helper classes + private sealed class HashListEnumerator : IDictionaryEnumerator + { + internal enum EnumerationMode + { + Key, + Value, + Entry + } + private HashList _hashList; + private ArrayList _orderList; + private EnumerationMode _mode; + private int _index; + private int _version; + private object _key; + private object _value; + + #region Constructors + + internal HashListEnumerator() + { + _index = 0; + _key = null; + _value = null; + } + + internal HashListEnumerator(HashList hashList, EnumerationMode mode) + { + _hashList = hashList; + _mode = mode; + _version = hashList._version; + _orderList = hashList._insertionOrderList; + _index = 0; + _key = null; + _value = null; + } + + #endregion + + #region IDictionaryEnumerator Members + + public object Key + { + get + { + if (_key == null) + { + throw new InvalidOperationException("Enumeration has either not started or has already finished."); + } + return _key; + } + } + + public object Value + { + get + { + if (_key == null) + { + throw new InvalidOperationException("Enumeration has either not started or has already finished."); + } + return _value; + } + } + + public DictionaryEntry Entry + { + get + { + if (_key == null) + { + throw new InvalidOperationException("Enumeration has either not started or has already finished."); + } + return new DictionaryEntry(_key, _value); + } + } + + #endregion + + #region IEnumerator Members + + public void Reset() + { + if (_version != _hashList._version) + { + throw new InvalidOperationException("Collection was modified; enumeration operation may not execute."); + } + _index = 0; + _key = null; + _value = null; + } + + public object Current + { + get + { + if (_key == null) + { + throw new InvalidOperationException("Enumeration has either not started or has already finished."); + } + + if (_mode == EnumerationMode.Key) + return _key; + else if (_mode == EnumerationMode.Value) + return _value; + + return new DictionaryEntry(_key, _value); + } + } + + public bool MoveNext() + { + if (_version != _hashList._version) + { + throw new InvalidOperationException("Collection was modified; enumeration operation may not execute."); + } + + if (_index < _orderList.Count) + { + _key = _orderList[_index]; + _value = _hashList[_key]; + _index++; + return true; + } + _key = null; + return false; + } + + #endregion + } + + private sealed class KeyCollection : ICollection + { + private HashList _hashList; + + #region Constructors + + internal KeyCollection() + { + } + + internal KeyCollection(HashList hashList) + { + _hashList = hashList; + } + + #endregion + + public override string ToString() + { + StringBuilder result = new StringBuilder(); + + result.Append("["); + ArrayList keys = _hashList._insertionOrderList; + for (int i = 0; i < keys.Count; i++) + { + if (i > 0) + { + result.Append(", "); + } + result.Append(keys[i]); + } + result.Append("]"); + + return result.ToString(); + } + + public override bool Equals(object o) + { + if (o is KeyCollection) + { + KeyCollection other = (KeyCollection) o; + if ((Count == 0) && (other.Count == 0)) + return true; + else if (Count == other.Count) + { + for (int i = 0; i < Count; i++) + { + if ((_hashList._insertionOrderList[i] == other._hashList._insertionOrderList[i]) || + (_hashList._insertionOrderList[i].Equals(other._hashList._insertionOrderList[i]))) + return true; + } + } + } + return false; + } + + public override int GetHashCode() + { + return _hashList._insertionOrderList.GetHashCode(); + } + + #region ICollection Members + + public bool IsSynchronized + { + get { return _hashList.IsSynchronized; } + } + + public int Count + { + get { return _hashList.Count; } + } + + public void CopyTo(Array array, int index) + { + _hashList.CopyKeysTo(array, index); + } + + public object SyncRoot + { + get { return _hashList.SyncRoot; } + } + + #endregion + + #region IEnumerable Members + + public IEnumerator GetEnumerator() + { + return new HashListEnumerator(_hashList, HashListEnumerator.EnumerationMode.Key); + } + + #endregion + } + + private sealed class ValueCollection : ICollection + { + private HashList _hashList; + + #region Constructors + + internal ValueCollection() + { + } + + internal ValueCollection(HashList hashList) + { + _hashList = hashList; + } + + #endregion + + public override string ToString() + { + StringBuilder result = new StringBuilder(); + + result.Append("["); + IEnumerator iter = new HashListEnumerator(_hashList, HashListEnumerator.EnumerationMode.Value); + if (iter.MoveNext()) + { + result.Append((iter.Current == null) ? "null" : iter.Current); + while (iter.MoveNext()) + { + result.Append(", "); + result.Append((iter.Current == null) ? "null" : iter.Current); + } + } + result.Append("]"); + + return result.ToString(); + } + + #region ICollection Members + + public bool IsSynchronized + { + get { return _hashList.IsSynchronized; } + } + + public int Count + { + get { return _hashList.Count; } + } + + public void CopyTo(Array array, int index) + { + _hashList.CopyValuesTo(array, index); + } + + public object SyncRoot + { + get { return _hashList.SyncRoot; } + } + + #endregion + + #region IEnumerable Members + + public IEnumerator GetEnumerator() + { + return new HashListEnumerator(_hashList, HashListEnumerator.EnumerationMode.Value); + } + + #endregion + } + + #endregion + + private Hashtable _dictionary = new Hashtable(); + private ArrayList _insertionOrderList = new ArrayList(); + private int _version; + + #region Constructors + + public HashList() : this(-1) + { + } + + public HashList(int capacity) + { + if (capacity < 0) + { + _dictionary = new Hashtable(); + _insertionOrderList = new ArrayList(); + } + else + { + _dictionary = new Hashtable(capacity); + _insertionOrderList = new ArrayList(capacity); + } + _version = 0; + } + + #endregion + + #region IDictionary Members + + public bool IsReadOnly { get { return _dictionary.IsReadOnly; } } + + public IDictionaryEnumerator GetEnumerator() + { + return new HashListEnumerator(this, HashListEnumerator.EnumerationMode.Entry); + } + + public object this[object key] + { + get { return _dictionary[key]; } + set + { + bool isNewEntry = !_dictionary.Contains(key); + _dictionary[key] = value; + if (isNewEntry) + _insertionOrderList.Add(key); + _version++; + } + } + + public void Remove(object key) + { + _dictionary.Remove(key); + _insertionOrderList.Remove(key); + _version++; + } + + public bool Contains(object key) + { + return _dictionary.Contains(key); + } + + public void Clear() + { + _dictionary.Clear(); + _insertionOrderList.Clear(); + _version++; + } + + public ICollection Values + { + get { return new ValueCollection(this); } + } + + public void Add(object key, object value) + { + _dictionary.Add(key, value); + _insertionOrderList.Add(key); + _version++; + } + + public ICollection Keys + { + get { return new KeyCollection(this); } + } + + public bool IsFixedSize + { + get { return _dictionary.IsFixedSize; } + } + + #endregion + + #region ICollection Members + + public bool IsSynchronized + { + get { return _dictionary.IsSynchronized; } + } + + public int Count + { + get { return _dictionary.Count; } + } + + public void CopyTo(Array array, int index) + { + int len = _insertionOrderList.Count; + for (int i = 0; i < len; i++) + { + DictionaryEntry e = new DictionaryEntry(_insertionOrderList[i], _dictionary[_insertionOrderList[i]]); + array.SetValue(e, index++); + } + } + + public object SyncRoot + { + get { return _dictionary.SyncRoot; } + } + + #endregion + + #region IEnumerable Members + + IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return new HashListEnumerator(this, HashListEnumerator.EnumerationMode.Entry); + } + + #endregion + + private void CopyKeysTo(Array array, int index) + { + int len = _insertionOrderList.Count; + for (int i = 0; i < len; i++) + { + array.SetValue(_insertionOrderList[i], index++); + } + } + + private void CopyValuesTo(Array array, int index) + { + int len = _insertionOrderList.Count; + for (int i = 0; i < len; i++) + { + array.SetValue(_dictionary[_insertionOrderList[i]], index++); + } + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Collections/StackList.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Collections/StackList.cs new file mode 100644 index 0000000..938d11f --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Collections/StackList.cs @@ -0,0 +1,77 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Collections +{ + using System; + using ArrayList = System.Collections.ArrayList; + + /// <summary> + /// Stack abstraction that also supports the IList interface + /// </summary> + public class StackList : ArrayList + { + public StackList() : base() + { + } + + /// <summary> + /// Adds an element to the top of the stack list. + /// </summary> + public void Push(object item) + { + Add(item); + } + + /// <summary> + /// Removes the element at the top of the stack list and returns it. + /// </summary> + /// <returns>The element at the top of the stack.</returns> + public object Pop() + { + object poppedItem = this[this.Count - 1]; + RemoveAt(this.Count - 1); + return poppedItem; + } + + /// <summary> + /// Removes the element at the top of the stack list without removing it. + /// </summary> + /// <returns>The element at the top of the stack.</returns> + public object Peek() + { + return this[this.Count - 1]; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/BlankDebugEventListener.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/BlankDebugEventListener.cs new file mode 100644 index 0000000..78fc0ab --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/BlankDebugEventListener.cs @@ -0,0 +1,95 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +Copyright (c) 2007-2008 Johannes Luber +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Debug +{ + using System; + using Antlr.Runtime; + using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; + using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream; + + + /// <summary> + /// A blank listener that does nothing; useful for real classes so + /// they don't have to have lots of blank methods and are less + /// sensitive to updates to debug interface. + /// </summary> + public class BlankDebugEventListener : IDebugEventListener + { + public virtual void EnterRule(string grammarFileName, string ruleName) { } + public virtual void ExitRule(string grammarFileName, string ruleName) { } + public virtual void EnterAlt(int alt) { } + public virtual void EnterSubRule(int decisionNumber) { } + public virtual void ExitSubRule(int decisionNumber) { } + public virtual void EnterDecision(int decisionNumber) { } + public virtual void ExitDecision(int decisionNumber) { } + public virtual void Location(int line, int pos) { } + public virtual void ConsumeToken(IToken token) { } + public virtual void ConsumeHiddenToken(IToken token) { } + public virtual void LT(int i, IToken t) { } + public virtual void Mark(int i) { } + public virtual void Rewind(int i) { } + public virtual void Rewind() { } + public virtual void BeginBacktrack(int level) { } + public virtual void EndBacktrack(int level, bool successful) { } + public virtual void RecognitionException(RecognitionException e) { } + public virtual void BeginResync() { } + public virtual void EndResync() { } + public virtual void SemanticPredicate(bool result, string predicate) { } + public virtual void Commence() { } + public virtual void Terminate() { } + + + #region Tree Parsing Stuff + + public virtual void ConsumeNode(object t) { } + public virtual void LT(int i, object t) { } + + #endregion + + + #region AST Stuff + + public virtual void GetNilNode(object t) { } + public virtual void ErrorNode(object t) {} + public virtual void CreateNode(object t) { } + public virtual void CreateNode(object node, IToken token) { } + public virtual void BecomeRoot(object newRoot, object oldRoot) { } + public virtual void AddChild(object root, object child) { } + public virtual void SetTokenBoundaries(object t, int tokenStartIndex, int tokenStopIndex) { } + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugEventHub.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugEventHub.cs new file mode 100644 index 0000000..d25a1f7 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugEventHub.cs @@ -0,0 +1,370 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Debug +{ + using System; + using IList = System.Collections.IList; + using ArrayList = System.Collections.ArrayList; + using IToken = Antlr.Runtime.IToken; + using RecognitionException = Antlr.Runtime.RecognitionException; + + /// <summary> + /// Broadcast debug events to multiple listeners. + /// </summary> + /// <remarks> + /// Lets you debug and still use the event mechanism to build + /// parse trees etc... + /// Not thread-safe. Don't add events in one thread while parser + /// fires events in another. + /// </remarks> + public class DebugEventHub : IDebugEventListener + { + protected IList listeners = new ArrayList(); + + public DebugEventHub(IDebugEventListener listener) + { + listeners.Add(listener); + } + + public DebugEventHub(params IDebugEventListener[] listeners) + { + + foreach (IDebugEventListener listener in listeners) + { + this.listeners.Add(listener); + } + } + + /// <summary> + /// Add another listener to broadcast events too. + /// </summary> + /// <remarks> + /// Not thread-safe. Don't add events in one thread while parser + /// fires events in another. + /// </remarks> + public void AddListener(IDebugEventListener listener) + { + listeners.Add(listener); + } + + public void EnterRule(string grammarFileName, string ruleName) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.EnterRule(grammarFileName, ruleName); + } + } + + public void ExitRule(string grammarFileName, string ruleName) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.ExitRule(grammarFileName, ruleName); + } + } + + public void EnterAlt(int alt) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.EnterAlt(alt); + } + } + + public void EnterSubRule(int decisionNumber) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.EnterSubRule(decisionNumber); + } + } + + public void ExitSubRule(int decisionNumber) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.ExitSubRule(decisionNumber); + } + } + + public void EnterDecision(int decisionNumber) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.EnterDecision(decisionNumber); + } + } + + public void ExitDecision(int decisionNumber) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.ExitDecision(decisionNumber); + } + } + + public void Location(int line, int pos) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.Location(line, pos); + } + } + + public void ConsumeToken(IToken token) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.ConsumeToken(token); + } + } + + public void ConsumeHiddenToken(IToken token) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.ConsumeHiddenToken(token); + } + } + + public void LT(int index, IToken t) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.LT(index, t); + } + } + + public void Mark(int index) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.Mark(index); + } + } + + public void Rewind(int index) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.Rewind(index); + } + } + + public void Rewind() + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.Rewind(); + } + } + + public void BeginBacktrack(int level) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.BeginBacktrack(level); + } + } + + public void EndBacktrack(int level, bool successful) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.EndBacktrack(level, successful); + } + } + + public void RecognitionException(RecognitionException e) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.RecognitionException(e); + } + } + + public void BeginResync() + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.BeginResync(); + } + } + + public void EndResync() + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.EndResync(); + } + } + + public void SemanticPredicate(bool result, string predicate) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.SemanticPredicate(result, predicate); + } + } + + public void Commence() + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.Commence(); + } + } + + public void Terminate() + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.Terminate(); + } + } + + + #region Tree parsing stuff + + public void ConsumeNode(object t) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.ConsumeNode(t); + } + } + + public void LT(int index, object t) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.LT(index, t); + } + } + + #endregion + + + #region AST Stuff + + public void GetNilNode(object t) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.GetNilNode(t); + } + } + + public void ErrorNode(object t) { + for (int i = 0; i < listeners.Count; i++) { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.ErrorNode(t); + } + } + + public void CreateNode(object t) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.CreateNode(t); + } + } + + public void CreateNode(object node, IToken token) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.CreateNode(node, token); + } + } + + public void BecomeRoot(object newRoot, object oldRoot) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.BecomeRoot(newRoot, oldRoot); + } + } + + public void AddChild(object root, object child) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.AddChild(root, child); + } + } + + public void SetTokenBoundaries(object t, int tokenStartIndex, int tokenStopIndex) + { + for (int i = 0; i < listeners.Count; i++) + { + IDebugEventListener listener = (IDebugEventListener)listeners[i]; + listener.SetTokenBoundaries(t, tokenStartIndex, tokenStopIndex); + } + } + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugEventRepeater.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugEventRepeater.cs new file mode 100644 index 0000000..e61811e --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugEventRepeater.cs @@ -0,0 +1,102 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Debug +{ + using System; + using IToken = Antlr.Runtime.IToken; + using RecognitionException = Antlr.Runtime.RecognitionException; + + /// <summary> + /// A simple event repeater (proxy) that delegates all functionality to + /// the listener sent into the ctor. + /// </summary> + /// <remarks> + /// Useful if you want to listen in on a few debug events w/o + /// interrupting the debugger. Just subclass the repeater and override + /// the methods you want to listen in on. Remember to call the method + /// in this class so the event will continue on to the original recipient. + /// </remarks> + public class DebugEventRepeater : IDebugEventListener + { + protected IDebugEventListener listener; + + public DebugEventRepeater(IDebugEventListener listener) + { + this.listener = listener; + } + + public void EnterRule(string grammarFileName, string ruleName) { listener.EnterRule(grammarFileName, ruleName); } + public void ExitRule(string grammarFileName, string ruleName) { listener.ExitRule(grammarFileName, ruleName); } + public void EnterAlt(int alt) { listener.EnterAlt(alt); } + public void EnterSubRule(int decisionNumber) { listener.EnterSubRule(decisionNumber); } + public void ExitSubRule(int decisionNumber) { listener.ExitSubRule(decisionNumber); } + public void EnterDecision(int decisionNumber) { listener.EnterDecision(decisionNumber); } + public void ExitDecision(int decisionNumber) { listener.ExitDecision(decisionNumber); } + public void Location(int line, int pos) { listener.Location(line, pos); } + public void ConsumeToken(IToken token) { listener.ConsumeToken(token); } + public void ConsumeHiddenToken(IToken token) { listener.ConsumeHiddenToken(token); } + public void LT(int i, IToken t) { listener.LT(i, t); } + public void Mark(int i) { listener.Mark(i); } + public void Rewind(int i) { listener.Rewind(i); } + public void Rewind() { listener.Rewind(); } + public void BeginBacktrack(int level) { listener.BeginBacktrack(level); } + public void EndBacktrack(int level, bool successful) { listener.EndBacktrack(level, successful); } + public void RecognitionException(RecognitionException e) { listener.RecognitionException(e); } + public void BeginResync() { listener.BeginResync(); } + public void EndResync() { listener.EndResync(); } + public void SemanticPredicate(bool result, string predicate) { listener.SemanticPredicate(result, predicate); } + public void Commence() { listener.Commence(); } + public void Terminate() { listener.Terminate(); } + + // Tree parsing stuff + + public void ConsumeNode(object t) { listener.ConsumeNode(t); } + public void LT(int i, object t) { listener.LT(i, t); } + + // AST Stuff + + public void GetNilNode(object t) { listener.GetNilNode(t); } + public void ErrorNode(object t) { listener.ErrorNode(t); } + public void CreateNode(object t) { listener.CreateNode(t); } + public void CreateNode(object node, IToken token) { listener.CreateNode(node, token); } + public void BecomeRoot(object newRoot, object oldRoot) { listener.BecomeRoot(newRoot, oldRoot); } + public void AddChild(object root, object child) { listener.AddChild(root, child); } + public void SetTokenBoundaries(object t, int tokenStartIndex, int tokenStopIndex) + { + listener.SetTokenBoundaries(t, tokenStartIndex, tokenStopIndex); + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugEventSocketProxy.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugEventSocketProxy.cs new file mode 100644 index 0000000..55a2a08 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugEventSocketProxy.cs @@ -0,0 +1,422 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Debug +{ + using System; + using StreamReader = System.IO.StreamReader; + using StreamWriter = System.IO.StreamWriter; + using Encoding = System.Text.Encoding; + using StringBuilder = System.Text.StringBuilder; + using TcpClient = System.Net.Sockets.TcpClient; + using TcpListener = System.Net.Sockets.TcpListener; + using IToken = Antlr.Runtime.IToken; + using RecognitionException = Antlr.Runtime.RecognitionException; + using BaseRecognizer = Antlr.Runtime.BaseRecognizer; + using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; + + /// <summary> + /// A proxy debug event listener that forwards events over a socket to + /// debugger (or any other listener) using a simple text-based protocol; + /// one event per line. + /// </summary> + /// <remarks> + /// ANTLRWorks listens on server socket with a + /// RemoteDebugEventSocketListener instance. These two objects must therefore + /// be kept in sync. New events must be handled on both sides of socket. + /// </remarks> + public class DebugEventSocketProxy : BlankDebugEventListener + { + public const int DEFAULT_DEBUGGER_PORT = 0xBFCC; + protected int port = DEFAULT_DEBUGGER_PORT; + protected TcpListener serverSocket; + protected TcpClient socket; + protected string grammarFileName; + protected StreamWriter writer; + protected StreamReader reader; + protected BaseRecognizer recognizer; + + /// <summary> + /// Almost certainly the recognizer will have adaptor set, but + /// we don't know how to cast it (Parser or TreeParser) to get + /// the adaptor field. Must be set with a constructor. :( + /// </summary> + protected ITreeAdaptor adaptor; + + public DebugEventSocketProxy(BaseRecognizer recognizer, ITreeAdaptor adaptor) + : this(recognizer, DEFAULT_DEBUGGER_PORT, adaptor) + { + } + + public DebugEventSocketProxy(BaseRecognizer recognizer, int port, ITreeAdaptor adaptor) + { + this.grammarFileName = recognizer.GrammarFileName; + this.port = port; + this.adaptor = adaptor; + } + + public virtual void Handshake() + { + if (serverSocket == null) + { + serverSocket = new TcpListener(port); + serverSocket.Start(); + socket = serverSocket.AcceptTcpClient(); + socket.NoDelay = true; + + reader = new StreamReader(socket.GetStream(), Encoding.UTF8); + writer = new StreamWriter(socket.GetStream(), Encoding.UTF8); + + writer.WriteLine("ANTLR " + Constants.DEBUG_PROTOCOL_VERSION); + writer.WriteLine("grammar \"" + grammarFileName); + writer.Flush(); + Ack(); + } + } + + public override void Commence() + { + // don't bother sending event; listener will trigger upon connection + } + + public override void Terminate() + { + Transmit("terminate"); + writer.Close(); + try + { + socket.Close(); + } + catch (System.IO.IOException ioe) + { + Console.Error.WriteLine(ioe.StackTrace); + } + } + + protected internal virtual void Ack() + { + try + { + reader.ReadLine(); + } + catch (System.IO.IOException ioe) + { + Console.Error.WriteLine(ioe.StackTrace); + } + } + + protected internal virtual void Transmit(string eventLabel) + { + writer.WriteLine(eventLabel); + writer.Flush(); + Ack(); + } + + public override void EnterRule(string grammarFileName, string ruleName) + { + Transmit("enterRule\t" + grammarFileName + "\t" + ruleName); + } + + public override void EnterAlt(int alt) + { + Transmit("enterAlt\t" + alt); + } + + public override void ExitRule(string grammarFileName, string ruleName) + { + Transmit("exitRule\t" + grammarFileName + "\t" + ruleName); + } + + public override void EnterSubRule(int decisionNumber) + { + Transmit("enterSubRule\t" + decisionNumber); + } + + public override void ExitSubRule(int decisionNumber) + { + Transmit("exitSubRule\t" + decisionNumber); + } + + public override void EnterDecision(int decisionNumber) + { + Transmit("enterDecision\t" + decisionNumber); + } + + public override void ExitDecision(int decisionNumber) + { + Transmit("exitDecision\t" + decisionNumber); + } + + public override void ConsumeToken(IToken t) + { + string buf = SerializeToken(t); + Transmit("consumeToken\t" + buf); + } + + public override void ConsumeHiddenToken(IToken t) + { + string buf = SerializeToken(t); + Transmit("consumeHiddenToken\t" + buf); + } + + public override void LT(int i, IToken t) + { + if (t != null) + Transmit("LT\t" + i + "\t" + SerializeToken(t)); + } + + public override void Mark(int i) + { + Transmit("mark\t" + i); + } + + public override void Rewind(int i) + { + Transmit("rewind\t" + i); + } + + public override void Rewind() + { + Transmit("rewind"); + } + + public override void BeginBacktrack(int level) + { + Transmit("beginBacktrack\t" + level); + } + + public override void EndBacktrack(int level, bool successful) + { + Transmit("endBacktrack\t" + level + "\t" + (successful ? true.ToString() : false.ToString())); + } + + public override void Location(int line, int pos) + { + Transmit("location\t" + line + "\t" + pos); + } + + public override void RecognitionException(RecognitionException e) + { + StringBuilder buf = new StringBuilder(50); + buf.Append("exception\t"); + buf.Append(e.GetType().FullName); + // dump only the data common to all exceptions for now + buf.Append("\t"); + buf.Append(e.Index); + buf.Append("\t"); + buf.Append(e.Line); + buf.Append("\t"); + buf.Append(e.CharPositionInLine); + Transmit(buf.ToString()); + } + + public override void BeginResync() + { + Transmit("beginResync"); + } + + public override void EndResync() + { + Transmit("endResync"); + } + + public override void SemanticPredicate(bool result, string predicate) + { + StringBuilder buf = new StringBuilder(50); + buf.Append("semanticPredicate\t"); + buf.Append(result); + SerializeText(buf, predicate); + Transmit(buf.ToString()); + } + + + #region A S T P a r s i n g E v e n t s + + public override void ConsumeNode(object t) + { + StringBuilder buf = new StringBuilder(50); + buf.Append("consumeNode\t"); + SerializeNode(buf, t); + Transmit(buf.ToString()); + } + + public override void LT(int i, object t) + { + int ID = adaptor.GetUniqueID(t); + string text = adaptor.GetNodeText(t); + int type = adaptor.GetNodeType(t); + StringBuilder buf = new StringBuilder(50); + buf.Append("LN\t"); // lookahead node; distinguish from LT in protocol + buf.Append(i); + SerializeNode(buf, t); + Transmit(buf.ToString()); + } + + #endregion + + + #region A S T E v e n t s + + public override void GetNilNode(object t) + { + int ID = adaptor.GetUniqueID(t); + Transmit("nilNode\t" + ID); + } + + public override void ErrorNode(object t) { + int ID = adaptor.GetUniqueID(t); + String text = t.ToString(); + StringBuilder buf = new StringBuilder(50); + buf.Append("errorNode\t"); + buf.Append(ID); + buf.Append("\t"); + buf.Append(Token.INVALID_TOKEN_TYPE); + SerializeText(buf, text); + Transmit(buf.ToString()); + } + + public override void CreateNode(object t) + { + int ID = adaptor.GetUniqueID(t); + string text = adaptor.GetNodeText(t); + int type = adaptor.GetNodeType(t); + StringBuilder buf = new StringBuilder(50); + buf.Append("createNodeFromTokenElements\t"); + buf.Append(ID); + buf.Append("\t"); + buf.Append(type); + SerializeText(buf, text); + Transmit(buf.ToString()); + } + + public override void CreateNode(object node, IToken token) + { + int ID = adaptor.GetUniqueID(node); + int tokenIndex = token.TokenIndex; + Transmit("createNode\t" + ID + "\t" + tokenIndex); + } + + public override void BecomeRoot(object newRoot, object oldRoot) + { + int newRootID = adaptor.GetUniqueID(newRoot); + int oldRootID = adaptor.GetUniqueID(oldRoot); + Transmit("becomeRoot\t" + newRootID + "\t" + oldRootID); + } + + public override void AddChild(object root, object child) + { + int rootID = adaptor.GetUniqueID(root); + int childID = adaptor.GetUniqueID(child); + Transmit("addChild\t" + rootID + "\t" + childID); + } + + public override void SetTokenBoundaries(object t, int tokenStartIndex, int tokenStopIndex) + { + int ID = adaptor.GetUniqueID(t); + Transmit("setTokenBoundaries\t" + ID + "\t" + tokenStartIndex + "\t" + tokenStopIndex); + } + + #endregion + + #region Support + + public ITreeAdaptor TreeAdaptor { + set { this.adaptor = value; } + get { return adaptor; } + } + + protected internal virtual string SerializeToken(IToken t) + { + StringBuilder buf = new StringBuilder(50); + buf.Append(t.TokenIndex); buf.Append('\t'); + buf.Append(t.Type); buf.Append('\t'); + buf.Append(t.Channel); buf.Append('\t'); + buf.Append(t.Line); buf.Append('\t'); + buf.Append(t.CharPositionInLine); + SerializeText(buf, t.Text); + return buf.ToString(); + } + + protected internal virtual string EscapeNewlines(string txt) + { + txt = txt.Replace("%", "%25"); // escape all escape char ;) + txt = txt.Replace("\n", "%0A"); // escape \n + txt = txt.Replace("\r", "%0D"); // escape \r + return txt; + } + + protected internal void SerializeNode(StringBuilder buf, object t) + { + int ID = adaptor.GetUniqueID(t); + string text = adaptor.GetNodeText(t); + int type = adaptor.GetNodeType(t); + buf.Append("\t"); + buf.Append(ID); + buf.Append("\t"); + buf.Append(type); + IToken token = adaptor.GetToken(t); + int line = -1; + int pos = -1; + if (token != null) + { + line = token.Line; + pos = token.CharPositionInLine; + } + buf.Append("\t"); + buf.Append(line); + buf.Append("\t"); + buf.Append(pos); + int tokenIndex = adaptor.GetTokenStartIndex(t); + buf.Append("\t"); + buf.Append(tokenIndex); + SerializeText(buf, text); + } + + protected void SerializeText(StringBuilder buf, string text) + { + buf.Append("\t\""); + if (text == null) + { + text = ""; + } + // escape \n and \r all text for token appears to exist on one line + // this escape is slow but easy to understand + text = EscapeNewlines(text); + buf.Append(text); + } + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugParser.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugParser.cs new file mode 100644 index 0000000..1fc97b8 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugParser.cs @@ -0,0 +1,112 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Debug +{ + using System; + using Antlr.Runtime; + using ErrorManager = Antlr.Runtime.Misc.ErrorManager; + + public class DebugParser : Parser + { + /// <summary> + /// Provide a new debug event listener for this parser. Notify the + /// input stream too that it should send events to this listener. + /// </summary> + virtual public IDebugEventListener DebugListener + { + get + { + return dbg; + } + + set + { + if (input is DebugTokenStream) + { + ((DebugTokenStream)input).DebugListener = value; + } + this.dbg = value; + } + + } + /// <summary>Who to notify when events in the parser occur. </summary> + protected internal IDebugEventListener dbg = null; + + /// <summary> + /// Used to differentiate between fixed lookahead and cyclic DFA decisions + /// while profiling. + /// </summary> + public bool isCyclicDecision = false; + + /// <summary> + /// Create a normal parser except wrap the token stream in a debug + /// proxy that fires consume events. + /// </summary> + public DebugParser(ITokenStream input, IDebugEventListener dbg, RecognizerSharedState state) + : base((input is DebugTokenStream ? input : new DebugTokenStream(input, dbg)), state) + { + DebugListener = dbg; + } + + public DebugParser(ITokenStream input, RecognizerSharedState state) + : base((input is DebugTokenStream ? input : new DebugTokenStream(input, null)), state) { + } + + public DebugParser(ITokenStream input, IDebugEventListener dbg) + : this((input is DebugTokenStream ? input : new DebugTokenStream(input, dbg)), dbg, null) + { + } + + public virtual void ReportError(System.IO.IOException e) + { + ErrorManager.InternalError(e); + } + + override public void BeginResync() + { + dbg.BeginResync(); + } + + override public void EndResync() + { + dbg.EndResync(); + } + + public override void ReportError(RecognitionException e) { + dbg.RecognitionException(e); + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugTokenStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugTokenStream.cs new file mode 100644 index 0000000..4cc858f --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugTokenStream.cs @@ -0,0 +1,189 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Debug +{ + using System; + using Antlr.Runtime; + + public class DebugTokenStream : ITokenStream + { + protected internal IDebugEventListener dbg; + public ITokenStream input; + protected internal bool initialStreamState = true; + /// <summary> + /// Track the last Mark() call result value for use in Rewind(). + /// </summary> + protected int lastMarker; + + virtual public IDebugEventListener DebugListener + { + set { this.dbg = value; } + } + + public DebugTokenStream(ITokenStream input, IDebugEventListener dbg) + { + this.input = input; + DebugListener = dbg; + // force TokenStream to get at least first valid token + // so we know if there are any hidden tokens first in the stream + input.LT(1); + } + + public virtual void Consume() + { + if (initialStreamState) + { + ConsumeInitialHiddenTokens(); + } + int a = input.Index(); + IToken t = input.LT(1); + input.Consume(); + int b = input.Index(); + dbg.ConsumeToken(t); + if (b > a + 1) + { + // then we consumed more than one token; must be off channel tokens + for (int i = a + 1; i < b; i++) + { + dbg.ConsumeHiddenToken(input.Get(i)); + } + } + } + + /// <summary>consume all initial off-channel tokens</summary> + protected internal virtual void ConsumeInitialHiddenTokens() + { + int firstOnChannelTokenIndex = input.Index(); + for (int i = 0; i < firstOnChannelTokenIndex; i++) + { + dbg.ConsumeHiddenToken(input.Get(i)); + } + initialStreamState = false; + } + + public virtual IToken LT(int i) + { + if (initialStreamState) + { + ConsumeInitialHiddenTokens(); + } + dbg.LT(i, input.LT(i)); + return input.LT(i); + } + + public virtual int LA(int i) + { + if (initialStreamState) + { + ConsumeInitialHiddenTokens(); + } + dbg.LT(i, input.LT(i)); + return input.LA(i); + } + + public virtual IToken Get(int i) + { + return input.Get(i); + } + + public virtual int Mark() + { + lastMarker = input.Mark(); + dbg.Mark(lastMarker); + return lastMarker; + } + + public virtual int Index() + { + return input.Index(); + } + + public virtual void Rewind(int marker) + { + dbg.Rewind(marker); + input.Rewind(marker); + } + + public virtual void Rewind() + { + dbg.Rewind(); + input.Rewind(lastMarker); + } + + public virtual void Release(int marker) + { + } + + public virtual void Seek(int index) + { + input.Seek(index); + } + + [Obsolete("Please use property Count instead.")] + public virtual int Size() + { + return Count; + } + + public virtual int Count { + get { return input.Count; } + } + + public virtual ITokenSource TokenSource + { + get { return input.TokenSource; } + } + + public virtual string SourceName { + get { return TokenSource.SourceName; } + } + + public override string ToString() + { + return input.ToString(); + } + + public virtual string ToString(int start, int stop) + { + return input.ToString(start, stop); + } + + public virtual string ToString(IToken start, IToken stop) + { + return input.ToString(start, stop); + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugTreeAdaptor.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugTreeAdaptor.cs new file mode 100644 index 0000000..63aa4c3 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugTreeAdaptor.cs @@ -0,0 +1,294 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Debug +{ + using System; + using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; + + /// <summary> + /// A TreeAdaptor proxy that fires debugging events to a DebugEventListener + /// delegate and uses the TreeAdaptor delegate to do the actual work. All + /// AST events are triggered by this adaptor; no code gen changes are needed + /// in generated rules. Debugging events are triggered *after* invoking + /// tree adaptor routines. + /// + /// Trees created with actions in rewrite actions like "-> ^(ADD {foo} {bar})" + /// cannot be tracked as they might not use the adaptor to create foo, bar. + /// The debug listener has to deal with tree node IDs for which it did + /// not see a CreateNode event. A single <unknown> node is sufficient even + /// if it represents a whole tree. + /// </summary> + public class DebugTreeAdaptor : ITreeAdaptor + { + protected IDebugEventListener dbg; + protected ITreeAdaptor adaptor; + + public DebugTreeAdaptor(IDebugEventListener dbg, ITreeAdaptor adaptor) + { + this.dbg = dbg; + this.adaptor = adaptor; + } + + public object Create(IToken payload) + { + if (payload.TokenIndex < 0) { + // could be token conjured up during error recovery + return Create(payload.Type, payload.Text); + } + object node = adaptor.Create(payload); + dbg.CreateNode(node, payload); + return node; + } + + public Object ErrorNode(ITokenStream input, IToken start, IToken stop, + RecognitionException e) + { + Object node = adaptor.ErrorNode(input, start, stop, e); + if (node != null) { + dbg.ErrorNode(node); + } + return node; + } + + public object DupTree(object tree) + { + Object t = adaptor.DupTree(tree); + // walk the tree and emit create and add child events + // to simulate what DupTree has done. DupTree does not call this debug + // adapter so I must simulate. + SimulateTreeConstruction(t); + return t; + } + + /** ^(A B C): emit create A, create B, add child, ...*/ + protected void SimulateTreeConstruction(Object t) { + dbg.CreateNode(t); + int n = adaptor.GetChildCount(t); + for (int i=0; i<n; i++) { + Object child = adaptor.GetChild(t, i); + SimulateTreeConstruction(child); + dbg.AddChild(t, child); + } + } + + public object DupNode(object treeNode) + { + Object d = adaptor.DupNode(treeNode); + dbg.CreateNode(d); + return d; + } + + public object GetNilNode() + { + object node = adaptor.GetNilNode(); + dbg.GetNilNode(node); + return node; + } + + public bool IsNil(object tree) + { + return adaptor.IsNil(tree); + } + + public void AddChild(object t, object child) + { + if ((t == null) || (child == null)) + { + return; + } + adaptor.AddChild(t, child); + dbg.AddChild(t, child); + } + + public object BecomeRoot(object newRoot, object oldRoot) + { + object n = adaptor.BecomeRoot(newRoot, oldRoot); + dbg.BecomeRoot(newRoot, oldRoot); + return n; + } + + public object RulePostProcessing(object root) + { + return adaptor.RulePostProcessing(root); + } + + public void AddChild(object t, IToken child) + { + object n = this.Create(child); + this.AddChild(t, n); + } + + public object BecomeRoot(IToken newRoot, object oldRoot) + { + object n = this.Create(newRoot); + adaptor.BecomeRoot(n, oldRoot); + dbg.BecomeRoot(newRoot, oldRoot); + return n; + } + + public object Create(int tokenType, IToken fromToken) + { + object node = adaptor.Create(tokenType, fromToken); + dbg.CreateNode(node); + return node; + } + + public object Create(int tokenType, IToken fromToken, string text) + { + object node = adaptor.Create(tokenType, fromToken, text); + dbg.CreateNode(node); + return node; + } + + public object Create(int tokenType, string text) + { + object node = adaptor.Create(tokenType, text); + dbg.CreateNode(node); + return node; + } + + public int GetNodeType(object t) + { + return adaptor.GetNodeType(t); + } + + public void SetNodeType(object t, int type) + { + adaptor.SetNodeType(t, type); + } + + public string GetNodeText(object t) + { + return adaptor.GetNodeText(t); + } + + public void SetNodeText(object t, string text) + { + adaptor.SetNodeText(t, text); + } + + public IToken GetToken(object treeNode) + { + return adaptor.GetToken(treeNode); + } + + public void SetTokenBoundaries(object t, IToken startToken, IToken stopToken) + { + adaptor.SetTokenBoundaries(t, startToken, stopToken); + if ( (t != null) && (startToken != null) && (stopToken != null) ) + { + dbg.SetTokenBoundaries(t, + startToken.TokenIndex, + stopToken.TokenIndex); + } + } + + public int GetTokenStartIndex(object t) + { + return adaptor.GetTokenStartIndex(t); + } + + public int GetTokenStopIndex(object t) + { + return adaptor.GetTokenStopIndex(t); + } + + public object GetChild(object t, int i) + { + return adaptor.GetChild(t, i); + } + + public void SetChild(object t, int i, object child) + { + adaptor.SetChild(t, i, child); + } + + public object DeleteChild(object t, int i) + { + return adaptor.DeleteChild(t, i); + } + + public int GetChildCount(object t) + { + return adaptor.GetChildCount(t); + } + + public int GetUniqueID(object node) + { + return adaptor.GetUniqueID(node); + } + + public object GetParent(object t) + { + return adaptor.GetParent(t); + } + + public int GetChildIndex(object t) + { + return adaptor.GetChildIndex(t); + } + + public void SetParent(object t, object parent) + { + adaptor.SetParent(t, parent); + } + + public void SetChildIndex(object t, int index) + { + adaptor.SetChildIndex(t, index); + } + + public void ReplaceChildren(object parent, int startChildIndex, int stopChildIndex, object t) + { + adaptor.ReplaceChildren(parent, startChildIndex, stopChildIndex, t); + } + + #region Support + + public IDebugEventListener DebugListener + { + get { return dbg; } + set { dbg = value; } + } + + public ITreeAdaptor TreeAdaptor + { + get { return adaptor; } + } + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugTreeNodeStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugTreeNodeStream.cs new file mode 100644 index 0000000..8b25f55 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugTreeNodeStream.cs @@ -0,0 +1,183 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Debug +{ + using System; + using Antlr.Runtime; + using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; + using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream; + using ITokenStream = Antlr.Runtime.ITokenStream; + + + /// <summary> + /// Debug any tree node stream. The constructor accepts the stream + /// and a debug listener. As node stream calls come in, debug events + /// are triggered. + /// </summary> + public class DebugTreeNodeStream : ITreeNodeStream + { + protected IDebugEventListener dbg; + protected ITreeAdaptor adaptor; + protected ITreeNodeStream input; + protected bool initialStreamState = true; + + /// <summary>Track the last mark() call result value for use in rewind().</summary> + protected int lastMarker; + + public DebugTreeNodeStream(ITreeNodeStream input, IDebugEventListener dbg) + { + this.input = input; + this.adaptor = input.TreeAdaptor; + this.input.HasUniqueNavigationNodes = true; + SetDebugListener(dbg); + } + + public void SetDebugListener(IDebugEventListener dbg) + { + this.dbg = dbg; + } + + public ITokenStream TokenStream + { + get { return input.TokenStream; } + } + + public string SourceName { + get { return TokenStream.SourceName; } + } + + public ITreeAdaptor TreeAdaptor + { + get { return adaptor; } + } + + public void Consume() + { + object node = input.LT(1); + input.Consume(); + dbg.ConsumeNode(node); + } + + public object Get(int i) + { + return input.Get(i); + } + + public object LT(int i) + { + object node = input.LT(i); + dbg.LT(i, node); + return node; + } + + public int LA(int i) + { + object node = input.LT(i); + int type = adaptor.GetNodeType(node); + dbg.LT(i, node); + return type; + } + + public int Mark() + { + lastMarker = input.Mark(); + dbg.Mark(lastMarker); + return lastMarker; + } + + public int Index() + { + return input.Index(); + } + + public void Rewind(int marker) + { + dbg.Rewind(marker); + input.Rewind(marker); + } + + public void Rewind() + { + dbg.Rewind(); + input.Rewind(lastMarker); + } + + public void Release(int marker) + { + } + + public void Seek(int index) + { + input.Seek(index); + } + + [Obsolete("Please use property Count instead.")] + public int Size() + { + return Count; + } + + public int Count { + get { return input.Count; } + } + + public object TreeSource + { + get { return input; } + } + + /// <summary> + /// It is normally this object that instructs the node stream to + /// create unique nav nodes, but to satisfy interface, we have to + /// define it. It might be better to ignore the parameter but + /// there might be a use for it later, so I'll leave. + /// </summary> + public virtual bool HasUniqueNavigationNodes + { + set { input.HasUniqueNavigationNodes = value; } + } + + public void ReplaceChildren(object parent, int startChildIndex, int stopChildIndex, object t) + { + input.ReplaceChildren(parent, startChildIndex, stopChildIndex, t); + } + + public string ToString(object start, object stop) + { + return input.ToString(start, stop); + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugTreeParser.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugTreeParser.cs new file mode 100644 index 0000000..5650cc2 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/DebugTreeParser.cs @@ -0,0 +1,133 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Debug +{ + using System; + using System.IO; + using Antlr.Runtime; + using TreeParser = Antlr.Runtime.Tree.TreeParser; + using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; + using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream; + using ErrorManager = Antlr.Runtime.Misc.ErrorManager; + + public class DebugTreeParser : TreeParser + { + /// <summary>Who to notify when events in the parser occur.</summary> + protected IDebugEventListener dbg = null; + + /// <summary> + /// Used to differentiate between fixed lookahead and cyclic DFA decisions + /// while profiling. + /// </summary> + public bool isCyclicDecision = false; + + /// <summary> + /// Create a normal parser except wrap the token stream in a debug + /// proxy that fires consume events. + /// </summary> + public DebugTreeParser(ITreeNodeStream input, IDebugEventListener dbg, RecognizerSharedState state) + : base((input is DebugTreeNodeStream ? input : new DebugTreeNodeStream(input, dbg)), state) + { + + DebugListener = dbg; + } + + public DebugTreeParser(ITreeNodeStream input, RecognizerSharedState state) + : base((input is DebugTreeNodeStream ? input : new DebugTreeNodeStream(input, null)), state) + { + } + + public DebugTreeParser(ITreeNodeStream input, IDebugEventListener dbg) + : this((input is DebugTreeNodeStream ? input : new DebugTreeNodeStream(input, dbg)), dbg, null) + { + } + + /// <summary> + /// Provide a new debug event listener for this parser. Notify the + /// input stream too that it should send events to this listener. + /// </summary> + public IDebugEventListener DebugListener + { + get { return dbg; } + set + { + if (input is DebugTreeNodeStream) + { + ((DebugTreeNodeStream)input).SetDebugListener(value); + } + this.dbg = value; + } + } + + public virtual void ReportError(IOException e) + { + ErrorManager.InternalError(e); + } + + public override void ReportError(RecognitionException e) { + dbg.RecognitionException(e); + } + + protected override object GetMissingSymbol(IIntStream input, + RecognitionException e, + int expectedTokenType, + BitSet follow) { + object o = base.GetMissingSymbol(input, e, expectedTokenType, follow); + dbg.ConsumeNode(o); + return o; + } + + override public void BeginResync() + { + dbg.BeginResync(); + } + + override public void EndResync() + { + dbg.EndResync(); + } + + override public void BeginBacktrack(int level) + { + dbg.BeginBacktrack(level); + } + + override public void EndBacktrack(int level, bool successful) + { + dbg.EndBacktrack(level, successful); + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/IDebugEventListener.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/IDebugEventListener.cs new file mode 100644 index 0000000..a68cb81 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/IDebugEventListener.cs @@ -0,0 +1,367 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Debug +{ + using System; + using Antlr.Runtime; + + /// <summary> + /// All debugging events that a recognizer can trigger. + /// </summary> + /// + /// <remarks> + /// I did not create a separate AST debugging interface as it would create + /// lots of extra classes and DebugParser has a dbg var defined, which makes + /// it hard to change to ASTDebugEventListener. I looked hard at this issue + /// and it is easier to understand as one monolithic event interface for all + /// possible events. Hopefully, adding ST debugging stuff won't be bad. Leave + /// for future. 4/26/2006. + /// </remarks> + public interface IDebugEventListener + { + /// <summary> + /// The parser has just entered a rule. No decision has been made about + /// which alt is predicted. This is fired AFTER init actions have been + /// executed. Attributes are defined and available etc... + /// The grammarFileName allows composite grammars to jump around among + /// multiple grammar files. + /// </summary> + void EnterRule(string grammarFileName, string ruleName); + + /// <summary> + /// Because rules can have lots of alternatives, it is very useful to + /// know which alt you are entering. This is 1..n for n alts. + /// </summary> + void EnterAlt(int alt); + + /// <summary> + /// This is the last thing executed before leaving a rule. It is + /// executed even if an exception is thrown. This is triggered after + /// error reporting and recovery have occurred (unless the exception is + /// not caught in this rule). This implies an "exitAlt" event. + /// The grammarFileName allows composite grammars to jump around among + /// multiple grammar files. + /// </summary> + void ExitRule(string grammarFileName, string ruleName); + + /// <summary>Track entry into any (...) subrule other EBNF construct </summary> + void EnterSubRule(int decisionNumber); + + void ExitSubRule(int decisionNumber); + + /// <summary> + /// Every decision, fixed k or arbitrary, has an enter/exit event + /// so that a GUI can easily track what LT/Consume events are + /// associated with prediction. You will see a single enter/exit + /// subrule but multiple enter/exit decision events, one for each + /// loop iteration. + /// </summary> + void EnterDecision(int decisionNumber); + + void ExitDecision(int decisionNumber); + + /// <summary> + /// An input token was consumed; matched by any kind of element. + /// Trigger after the token was matched by things like Match(), MatchAny(). + /// </summary> + void ConsumeToken(IToken t); + + /// <summary> + /// An off-channel input token was consumed. + /// Trigger after the token was matched by things like Match(), MatchAny(). + /// (unless of course the hidden token is first stuff in the input stream). + /// </summary> + void ConsumeHiddenToken(IToken t); + + /// <summary> + /// Somebody (anybody) looked ahead. Note that this actually gets + /// triggered by both LA and LT calls. The debugger will want to know + /// which Token object was examined. Like ConsumeToken, this indicates + /// what token was seen at that depth. A remote debugger cannot look + /// ahead into a file it doesn't have so LT events must pass the token + /// even if the info is redundant. + /// </summary> + void LT(int i, IToken t); + + /// <summary> + /// The parser is going to look arbitrarily ahead; mark this location, + /// the token stream's marker is sent in case you need it. + /// </summary> + void Mark(int marker); + + /// <summary> + /// After an arbitrairly long lookahead as with a cyclic DFA (or with + /// any backtrack), this informs the debugger that stream should be + /// rewound to the position associated with marker. + /// </summary> + void Rewind(int marker); + + /// <summary> + /// Rewind to the input position of the last marker. + /// Used currently only after a cyclic DFA and just + /// before starting a sem/syn predicate to get the + /// input position back to the start of the decision. + /// Do not "pop" the marker off the state. Mark(i) + /// and Rewind(i) should balance still. + /// </summary> + void Rewind(); + + void BeginBacktrack(int level); + + void EndBacktrack(int level, bool successful); + + /// <summary> + /// To watch a parser move through the grammar, the parser needs to + /// inform the debugger what line/charPos it is passing in the grammar. + /// For now, this does not know how to switch from one grammar to the + /// other and back for island grammars etc... + /// + /// This should also allow breakpoints because the debugger can stop + /// the parser whenever it hits this line/pos. + /// </summary> + void Location(int line, int pos); + + /// <summary> + /// A recognition exception occurred such as NoViableAltException. I made + /// this a generic event so that I can alter the exception hierachy later + /// without having to alter all the debug objects. + /// + /// Upon error, the stack of enter rule/subrule must be properly unwound. + /// If no viable alt occurs it is within an enter/exit decision, which + /// also must be rewound. Even the rewind for each mark must be unwount. + /// In the C# target this is pretty easy using try/finally, if a bit + /// ugly in the generated code. The rewind is generated in DFA.Predict() + /// actually so no code needs to be generated for that. For languages + /// w/o this "finally" feature (C++?), the target implementor will have + /// to build an event stack or something. + /// + /// Across a socket for remote debugging, only the RecognitionException + /// data fields are transmitted. The token object or whatever that + /// caused the problem was the last object referenced by LT. The + /// immediately preceding LT event should hold the unexpected Token or + /// char. + /// + /// Here is a sample event trace for grammar: + /// + /// b : C ({;}A|B) // {;} is there to prevent A|B becoming a set + /// | D + /// ; + /// + /// The sequence for this rule (with no viable alt in the subrule) for + /// input 'c c' (there are 3 tokens) is: + /// + /// Commence + /// LT(1) + /// EnterRule b + /// Location 7 1 + /// enter decision 3 + /// LT(1) + /// exit decision 3 + /// enterAlt1 + /// Location 7 5 + /// LT(1) + /// ConsumeToken <![CDATA[[c/<4>,1:0]]]> + /// Location 7 7 + /// EnterSubRule 2 + /// enter decision 2 + /// LT(1) + /// LT(1) + /// RecognitionException NoViableAltException 2 1 2 + /// exit decision 2 + /// ExitSubRule 2 + /// BeginResync + /// LT(1) + /// ConsumeToken <![CDATA[[c/<4>,1:1]]]> + /// LT(1) + /// EndResync + /// LT(-1) + /// ExitRule b + /// Terminate + /// </summary> + void RecognitionException(RecognitionException e); + + /// <summary> + /// Indicates the recognizer is about to consume tokens to resynchronize + /// the parser. Any Consume events from here until the recovered event + /// are not part of the parse--they are dead tokens. + /// </summary> + void BeginResync(); + + /// <summary> + /// Indicates that the recognizer has finished consuming tokens in order + /// to resychronize. There may be multiple BeginResync/EndResync pairs + /// before the recognizer comes out of errorRecovery mode (in which + /// multiple errors are suppressed). This will be useful + /// in a gui where you want to probably grey out tokens that are consumed + /// but not matched to anything in grammar. Anything between + /// a BeginResync/EndResync pair was tossed out by the parser. + /// </summary> + void EndResync(); + + /// <summary> + /// A semantic predicate was evaluate with this result and action text + /// </summary> + void SemanticPredicate(bool result, string predicate); + + /// <summary> + /// Announce that parsing has begun. Not technically useful except for + /// sending events over a socket. A GUI for example will launch a thread + /// to connect and communicate with a remote parser. The thread will want + /// to notify the GUI when a connection is made. ANTLR parsers + /// trigger this upon entry to the first rule (the ruleLevel is used to + /// figure this out). + /// </summary> + void Commence(); + + /// <summary> + /// Parsing is over; successfully or not. Mostly useful for telling + /// remote debugging listeners that it's time to quit. When the rule + /// invocation level goes to zero at the end of a rule, we are done + /// parsing. + /// </summary> + void Terminate(); + + + #region T r e e P a r s i n g + + /// <summary> + /// Input for a tree parser is an AST, but we know nothing for sure + /// about a node except its type and text (obtained from the adaptor). + /// This is the analog of the ConsumeToken method. Again, the ID is + /// the hashCode usually of the node so it only works if hashCode is + /// not implemented. If the type is UP or DOWN, then + /// the ID is not really meaningful as it's fixed--there is + /// just one UP node and one DOWN navigation node. + /// </summary> + void ConsumeNode(object t); + + /// <summary> + /// The tree parser lookedahead. If the type is UP or DOWN, + /// then the ID is not really meaningful as it's fixed--there is + /// just one UP node and one DOWN navigation node. + /// </summary> + void LT(int i, object t); + + #endregion + + + #region A S T E v e n t s + + /// <summary> + /// Announce the creation of a nil node + /// </summary> + /// <remarks> + /// A nil was created (even nil nodes have a unique ID... + /// they are not "null" per se). As of 4/28/2006, this + /// seems to be uniquely triggered when starting a new subtree + /// such as when entering a subrule in automatic mode and when + /// building a tree in rewrite mode. + /// + /// If you are receiving this event over a socket via + /// RemoteDebugEventSocketListener then only t.ID is set. + /// </remarks> + void GetNilNode(object t); + + /// <summary> + /// Upon syntax error, recognizers bracket the error with an error node + /// if they are building ASTs. + /// </summary> + /// <param name="t">The object</param> + void ErrorNode(object t); + + /// <summary> + /// Announce a new node built from token elements such as type etc... + /// </summary> + /// <remarks> + /// If you are receiving this event over a socket via + /// RemoteDebugEventSocketListener then only t.ID, type, + /// text are set. + /// </remarks> + void CreateNode(object t); + + /// <summary> + /// Announce a new node built from an existing token. + /// </summary> + /// <remarks> + /// If you are receiving this event over a socket via + /// RemoteDebugEventSocketListener then only node.ID + /// and token.tokenIndex are set. + /// </remarks> + void CreateNode(object node, IToken token); + + /// <summary> + /// Make a node the new root of an existing root. + /// </summary> + /// + /// <remarks> + /// Note: the newRootID parameter is possibly different + /// than the TreeAdaptor.BecomeRoot() newRoot parameter. + /// In our case, it will always be the result of calling + /// TreeAdaptor.BecomeRoot() and not root_n or whatever. + /// + /// The listener should assume that this event occurs + /// only when the current subrule (or rule) subtree is + /// being reset to newRootID. + /// + /// <see cref="Antlr.Runtime.Tree.ITreeAdaptor.BecomeRoot(object, object)"/> + /// + /// If you are receiving this event over a socket via + /// RemoteDebugEventSocketListener then only IDs are set. + /// </remarks> + void BecomeRoot(object newRoot, object oldRoot); + + /// <summary> + /// Make childID a child of rootID. + /// </summary> + /// <remarks> + /// If you are receiving this event over a socket via + /// RemoteDebugEventSocketListener then only IDs are set. + /// </remarks> + /// <see cref="Antlr.Runtime.Tree.ITreeAdaptor.AddChild(object,object)"/> + void AddChild(object root, object child); + + /// <summary> + /// Set the token start/stop token index for a subtree root or node + /// </summary> + /// <remarks> + /// If you are receiving this event over a socket via + /// RemoteDebugEventSocketListener then only IDs are set. + /// </remarks> + void SetTokenBoundaries(object t, int tokenStartIndex, int tokenStopIndex); + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/ParseTreeBuilder.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/ParseTreeBuilder.cs new file mode 100644 index 0000000..4f40d50 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/ParseTreeBuilder.cs @@ -0,0 +1,126 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Debug +{ + using System; + using System.Collections; + using Stack = System.Collections.Stack; + using IToken = Antlr.Runtime.IToken; + using RecognitionException = Antlr.Runtime.RecognitionException; + using ParseTree = Antlr.Runtime.Tree.ParseTree; + + + /// <summary> + /// This parser listener tracks rule entry/exit and token matches + /// to build a simple parse tree using ParseTree nodes. + /// </summary> + public class ParseTreeBuilder : BlankDebugEventListener + { + public static readonly String EPSILON_PAYLOAD = "<epsilon>"; + + Stack callStack = new Stack(); + IList hiddenTokens = new ArrayList(); + int backtracking = 0; + + public ParseTreeBuilder(string grammarName) + { + ParseTree root = Create("<grammar " + grammarName + ">"); + callStack.Push(root); + } + + public ParseTree Tree { + get { return (ParseTree)callStack.Peek(); } + } + + /// <summary> + /// What kind of node to create. You might want to override + /// so I factored out creation here. + /// </summary> + public ParseTree Create(object payload) + { + return new ParseTree(payload); + } + + public ParseTree EpsilonNode() { + return Create(EPSILON_PAYLOAD); + } + + /** Backtracking or cyclic DFA, don't want to add nodes to tree */ + override public void EnterDecision(int d) { backtracking++; } + override public void ExitDecision(int i) { backtracking--; } + + override public void EnterRule(string filename, string ruleName) { + if ( backtracking>0 ) return; + ParseTree parentRuleNode = (ParseTree)callStack.Peek(); + ParseTree ruleNode = Create(ruleName); + parentRuleNode.AddChild(ruleNode); + callStack.Push(ruleNode); + } + + override public void ExitRule(string filename, string ruleName) + { + if ( backtracking>0 ) return; + ParseTree ruleNode = (ParseTree)callStack.Peek(); + if ( ruleNode.ChildCount==0 ) { + ruleNode.AddChild(EpsilonNode()); + } + callStack.Pop(); + } + + override public void ConsumeToken(IToken token) + { + if ( backtracking>0 ) return; + ParseTree ruleNode = (ParseTree)callStack.Peek(); + ParseTree elementNode = Create(token); + elementNode.hiddenTokens = this.hiddenTokens; + this.hiddenTokens = new ArrayList(); + ruleNode.AddChild(elementNode); + } + + override public void ConsumeHiddenToken(IToken token) { + if ( backtracking>0 ) return; + hiddenTokens.Add(token); + } + + override public void RecognitionException(RecognitionException e) + { + if ( backtracking>0 ) return; + ParseTree ruleNode = (ParseTree)callStack.Peek(); + ParseTree errorNode = Create(e); + ruleNode.AddChild(errorNode); + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/Profiler.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/Profiler.cs new file mode 100644 index 0000000..962c511 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/Profiler.cs @@ -0,0 +1,522 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Debug +{ + using System; + using IList = System.Collections.IList; + using ArrayList = System.Collections.ArrayList; + using StringBuilder = System.Text.StringBuilder; + using IOException = System.IO.IOException; + using IToken = Antlr.Runtime.IToken; + using Token = Antlr.Runtime.Token; + using CommonToken = Antlr.Runtime.CommonToken; + using IIntStream = Antlr.Runtime.IIntStream; + using ITokenStream = Antlr.Runtime.ITokenStream; + using BaseRecognizer = Antlr.Runtime.BaseRecognizer; + using RecognitionException = Antlr.Runtime.RecognitionException; + using Stats = Antlr.Runtime.Misc.Stats; + + /// <summary> + /// Using the debug event interface, track what is happening in the parser + /// and record statistics about the runtime. + /// </summary> + public class Profiler : BlankDebugEventListener + { + /// <summary> + /// Because I may change the stats, I need to track that for later + /// computations to be consistent. + /// </summary> + public const string Version = "2"; + public const string RUNTIME_STATS_FILENAME = "runtime.stats"; + public const int NUM_RUNTIME_STATS = 29; + + public DebugParser parser = null; + + // working variables + + protected internal int ruleLevel = 0; + protected internal int decisionLevel = 0; + protected internal int maxLookaheadInCurrentDecision = 0; + protected internal CommonToken lastTokenConsumed = null; + + protected IList lookaheadStack = new ArrayList(); + + // stats variables + + public int numRuleInvocations = 0; + public int numGuessingRuleInvocations = 0; + public int maxRuleInvocationDepth = 0; + public int numFixedDecisions = 0; + public int numCyclicDecisions = 0; + public int numBacktrackDecisions = 0; + public int[] decisionMaxFixedLookaheads = new int[200]; // TODO: make List + public int[] decisionMaxCyclicLookaheads = new int[200]; + public IList decisionMaxSynPredLookaheads = new ArrayList(); + public int numHiddenTokens = 0; + public int numCharsMatched = 0; + public int numHiddenCharsMatched = 0; + public int numSemanticPredicates = 0; + public int numSyntacticPredicates = 0; + protected int numberReportedErrors = 0; + public int numMemoizationCacheMisses = 0; + public int numMemoizationCacheHits = 0; + public int numMemoizationCacheEntries = 0; + + public Profiler() + { + } + + public Profiler(DebugParser parser) + { + this.parser = parser; + } + + public override void EnterRule(string grammarFileName, string ruleName) + { + ruleLevel++; + numRuleInvocations++; + if (ruleLevel > maxRuleInvocationDepth) + { + maxRuleInvocationDepth = ruleLevel; + } + } + + /// <summary>Track memoization</summary> + /// <remarks> + /// This is not part of standard debug interface but is triggered by + /// profiling. Code gen inserts an override for this method in the + /// recognizer, which triggers this method. + /// </remarks> + public void ExamineRuleMemoization(IIntStream input, int ruleIndex, string ruleName) + { + int stopIndex = parser.GetRuleMemoization(ruleIndex, input.Index()); + if (stopIndex == BaseRecognizer.MEMO_RULE_UNKNOWN) + { + numMemoizationCacheMisses++; + numGuessingRuleInvocations++; // we'll have to enter + } + else + { + // regardless of rule success/failure, if in cache, we have a cache hit + numMemoizationCacheHits++; + } + } + + public void Memoize(IIntStream input, int ruleIndex, int ruleStartIndex, string ruleName) + { + // count how many entries go into table + numMemoizationCacheEntries++; + } + + public override void ExitRule(string grammarFileName, string ruleName) + { + ruleLevel--; + } + + public override void EnterDecision(int decisionNumber) + { + decisionLevel++; + int startingLookaheadIndex = parser.TokenStream.Index(); + lookaheadStack.Add(startingLookaheadIndex); + } + + public override void ExitDecision(int decisionNumber) + { + // track how many of acyclic, cyclic here as we don't know what kind + // yet in enterDecision event. + if (parser.isCyclicDecision) + { + numCyclicDecisions++; + } + else + { + numFixedDecisions++; + } + lookaheadStack.Remove(lookaheadStack.Count - 1); // pop lookahead depth counter + decisionLevel--; + if (parser.isCyclicDecision) + { + if (numCyclicDecisions >= decisionMaxCyclicLookaheads.Length) + { + int[] bigger = new int[decisionMaxCyclicLookaheads.Length * 2]; + Array.Copy(decisionMaxCyclicLookaheads, 0, bigger, 0, decisionMaxCyclicLookaheads.Length); + decisionMaxCyclicLookaheads = bigger; + } + decisionMaxCyclicLookaheads[numCyclicDecisions - 1] = maxLookaheadInCurrentDecision; + } + else + { + if (numFixedDecisions >= decisionMaxFixedLookaheads.Length) + { + int[] bigger = new int[decisionMaxFixedLookaheads.Length * 2]; + Array.Copy(decisionMaxFixedLookaheads, 0, bigger, 0, decisionMaxFixedLookaheads.Length); + decisionMaxFixedLookaheads = bigger; + } + decisionMaxFixedLookaheads[numFixedDecisions - 1] = maxLookaheadInCurrentDecision; + } + parser.isCyclicDecision = false; // can't nest so just reset to false + maxLookaheadInCurrentDecision = 0; + } + + public override void ConsumeToken(IToken token) + { + lastTokenConsumed = (CommonToken)token; + } + + /// <summary> + /// The parser is in a decision if the decision depth > 0. This works + /// for backtracking also, which can have nested decisions. + /// </summary> + public bool InDecision() + { + return decisionLevel > 0; + } + + public override void ConsumeHiddenToken(IToken token) + { + lastTokenConsumed = (CommonToken)token; + } + + /// <summary> + /// Track refs to lookahead if in a fixed/nonfixed decision. + /// </summary> + public override void LT(int i, IToken t) + { + if (InDecision()) + { + // get starting index off stack + int stackTop = lookaheadStack.Count - 1; + int startingIndex = (int)lookaheadStack[stackTop]; + // compute lookahead depth + int thisRefIndex = parser.TokenStream.Index(); + int numHidden = GetNumberOfHiddenTokens(startingIndex, thisRefIndex); + int depth = i + thisRefIndex - startingIndex - numHidden; + + if (depth > maxLookaheadInCurrentDecision) + { + maxLookaheadInCurrentDecision = depth; + } + } + } + + /// <summary> + /// Track backtracking decisions. You'll see a fixed or cyclic decision + /// and then a backtrack. + /// </summary> + /// <remarks> + /// enter rule + /// ... + /// enter decision + /// LA and possibly consumes (for cyclic DFAs) + /// begin backtrack level + /// mark m + /// rewind m + /// end backtrack level, success + /// exit decision + /// ... + /// exit rule + /// </remarks> + public override void BeginBacktrack(int level) + { + numBacktrackDecisions++; + } + + /// <summary>Successful or not, track how much lookahead synpreds use</summary> + public override void EndBacktrack(int level, bool successful) + { + decisionMaxSynPredLookaheads.Add(maxLookaheadInCurrentDecision); + } + + public override void RecognitionException(RecognitionException e) + { + numberReportedErrors++; + } + + public override void SemanticPredicate(bool result, string predicate) + { + if (InDecision()) + { + numSemanticPredicates++; + } + } + + public override void Terminate() + { + string stats = ToNotifyString(); + try + { + Stats.WriteReport(RUNTIME_STATS_FILENAME, stats); + } + catch (IOException ex) + { + Console.Error.WriteLine(ex); + Console.Error.WriteLine(ex.StackTrace); + } + Console.Out.WriteLine(Profiler.ToString(stats)); + } + + virtual public DebugParser Parser + { + set { this.parser = value; } + } + + // R E P O R T I N G + + + public virtual string ToNotifyString() + { + ITokenStream input = parser.TokenStream; + for (int i = 0; (i < input.Count) && (lastTokenConsumed != null) && (i <= lastTokenConsumed.TokenIndex); i++) + { + IToken t = input.Get(i); + if (t.Channel != Token.DEFAULT_CHANNEL) + { + numHiddenTokens++; + numHiddenCharsMatched += t.Text.Length; + } + } + numCharsMatched = lastTokenConsumed.StopIndex + 1; + decisionMaxFixedLookaheads = Trim(decisionMaxFixedLookaheads, numFixedDecisions); + decisionMaxCyclicLookaheads = Trim(decisionMaxCyclicLookaheads, numCyclicDecisions); + StringBuilder buf = new StringBuilder(); + buf.Append(Version); + buf.Append('\t'); + buf.Append(parser.GetType().FullName); + buf.Append('\t'); + buf.Append(numRuleInvocations); + buf.Append('\t'); + buf.Append(maxRuleInvocationDepth); + buf.Append('\t'); + buf.Append(numFixedDecisions); + buf.Append('\t'); + buf.Append(Stats.Min(decisionMaxFixedLookaheads)); + buf.Append('\t'); + buf.Append(Stats.Max(decisionMaxFixedLookaheads)); + buf.Append('\t'); + buf.Append(Stats.Avg(decisionMaxFixedLookaheads)); + buf.Append('\t'); + buf.Append(Stats.Stddev(decisionMaxFixedLookaheads)); + buf.Append('\t'); + buf.Append(numCyclicDecisions); + buf.Append('\t'); + buf.Append(Stats.Min(decisionMaxCyclicLookaheads)); + buf.Append('\t'); + buf.Append(Stats.Max(decisionMaxCyclicLookaheads)); + buf.Append('\t'); + buf.Append(Stats.Avg(decisionMaxCyclicLookaheads)); + buf.Append('\t'); + buf.Append(Stats.Stddev(decisionMaxCyclicLookaheads)); + buf.Append('\t'); + buf.Append(numBacktrackDecisions); + buf.Append('\t'); + buf.Append(Stats.Min(ToArray(decisionMaxSynPredLookaheads))); + buf.Append('\t'); + buf.Append(Stats.Max(ToArray(decisionMaxSynPredLookaheads))); + buf.Append('\t'); + buf.Append(Stats.Avg(ToArray(decisionMaxSynPredLookaheads))); + buf.Append('\t'); + buf.Append(Stats.Stddev(ToArray(decisionMaxSynPredLookaheads))); + buf.Append('\t'); + buf.Append(numSemanticPredicates); + buf.Append('\t'); + buf.Append(parser.TokenStream.Count); + buf.Append('\t'); + buf.Append(numHiddenTokens); + buf.Append('\t'); + buf.Append(numCharsMatched); + buf.Append('\t'); + buf.Append(numHiddenCharsMatched); + buf.Append('\t'); + buf.Append(numberReportedErrors); + buf.Append('\t'); + buf.Append(numMemoizationCacheHits); + buf.Append('\t'); + buf.Append(numMemoizationCacheMisses); + buf.Append('\t'); + buf.Append(numGuessingRuleInvocations); + buf.Append('\t'); + buf.Append(numMemoizationCacheEntries); + return buf.ToString(); + } + + public override string ToString() + { + return Profiler.ToString(ToNotifyString()); + } + + protected static string[] DecodeReportData(string data) + { + string[] fields = data.Split(new char[] { '\t' }); + if (fields.Length != NUM_RUNTIME_STATS) + { + return null; + } + return fields; + } + + public static string ToString(string notifyDataLine) + { + string[] fields = DecodeReportData(notifyDataLine); + if (fields == null) + { + return null; + } + StringBuilder buf = new StringBuilder(); + buf.Append("ANTLR Runtime Report; Profile Version "); + buf.Append(fields[0]); + buf.Append('\n'); + buf.Append("parser name "); + buf.Append(fields[1]); + buf.Append('\n'); + buf.Append("Number of rule invocations "); + buf.Append(fields[2]); + buf.Append('\n'); + buf.Append("Number of rule invocations in \"guessing\" mode "); + buf.Append(fields[27]); + buf.Append('\n'); + buf.Append("max rule invocation nesting depth "); + buf.Append(fields[3]); + buf.Append('\n'); + buf.Append("number of fixed lookahead decisions "); + buf.Append(fields[4]); + buf.Append('\n'); + buf.Append("min lookahead used in a fixed lookahead decision "); + buf.Append(fields[5]); + buf.Append('\n'); + buf.Append("max lookahead used in a fixed lookahead decision "); + buf.Append(fields[6]); + buf.Append('\n'); + buf.Append("average lookahead depth used in fixed lookahead decisions "); + buf.Append(fields[7]); + buf.Append('\n'); + buf.Append("standard deviation of depth used in fixed lookahead decisions "); + buf.Append(fields[8]); + buf.Append('\n'); + buf.Append("number of arbitrary lookahead decisions "); + buf.Append(fields[9]); + buf.Append('\n'); + buf.Append("min lookahead used in an arbitrary lookahead decision "); + buf.Append(fields[10]); + buf.Append('\n'); + buf.Append("max lookahead used in an arbitrary lookahead decision "); + buf.Append(fields[11]); + buf.Append('\n'); + buf.Append("average lookahead depth used in arbitrary lookahead decisions "); + buf.Append(fields[12]); + buf.Append('\n'); + buf.Append("standard deviation of depth used in arbitrary lookahead decisions "); + buf.Append(fields[13]); + buf.Append('\n'); + buf.Append("number of evaluated syntactic predicates "); + buf.Append(fields[14]); + buf.Append('\n'); + buf.Append("min lookahead used in a syntactic predicate "); + buf.Append(fields[15]); + buf.Append('\n'); + buf.Append("max lookahead used in a syntactic predicate "); + buf.Append(fields[16]); + buf.Append('\n'); + buf.Append("average lookahead depth used in syntactic predicates "); + buf.Append(fields[17]); + buf.Append('\n'); + buf.Append("standard deviation of depth used in syntactic predicates "); + buf.Append(fields[18]); + buf.Append('\n'); + buf.Append("rule memoization cache size "); + buf.Append(fields[28]); + buf.Append('\n'); + buf.Append("number of rule memoization cache hits "); + buf.Append(fields[25]); + buf.Append('\n'); + buf.Append("number of rule memoization cache misses "); + buf.Append(fields[26]); + buf.Append('\n'); + buf.Append("number of evaluated semantic predicates "); + buf.Append(fields[19]); + buf.Append('\n'); + buf.Append("number of tokens "); + buf.Append(fields[20]); + buf.Append('\n'); + buf.Append("number of hidden tokens "); + buf.Append(fields[21]); + buf.Append('\n'); + buf.Append("number of char "); + buf.Append(fields[22]); + buf.Append('\n'); + buf.Append("number of hidden char "); + buf.Append(fields[23]); + buf.Append('\n'); + buf.Append("number of syntax errors "); + buf.Append(fields[24]); + buf.Append('\n'); + return buf.ToString(); + } + + protected int[] Trim(int[] X, int n) + { + if (n < X.Length) + { + int[] trimmed = new int[n]; + Array.Copy(X, 0, trimmed, 0, n); + X = trimmed; + } + return X; + } + + protected int[] ToArray(IList a) + { + int[] x = new int[a.Count]; + a.CopyTo(x, 0); + return x; + } + + /// <summary>Get num hidden tokens between i..j inclusive</summary> + public int GetNumberOfHiddenTokens(int i, int j) + { + int n = 0; + ITokenStream input = parser.TokenStream; + for (int ti = i; ti < input.Count && ti <= j; ti++) + { + IToken t = input.Get(ti); + if (t.Channel != Token.DEFAULT_CHANNEL) + { + n++; + } + } + return n; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/RemoteDebugEventSocketListener.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/RemoteDebugEventSocketListener.cs new file mode 100644 index 0000000..bbae960 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/RemoteDebugEventSocketListener.cs @@ -0,0 +1,633 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Debug +{ + using System; + using System.Globalization; + using System.Threading; + using StreamReader = System.IO.StreamReader; + using StreamWriter = System.IO.StreamWriter; + using IOException = System.IO.IOException; + using Encoding = System.Text.Encoding; + using StringBuilder = System.Text.StringBuilder; + using TcpClient = System.Net.Sockets.TcpClient; + using TcpListener = System.Net.Sockets.TcpListener; + using Antlr.Runtime; + using ITree = Antlr.Runtime.Tree.ITree; + using BaseTree = Antlr.Runtime.Tree.BaseTree; + + + public class RemoteDebugEventSocketListener + { + internal const int MAX_EVENT_ELEMENTS = 8; + internal IDebugEventListener listener; + internal string hostName; + internal int port; + internal TcpClient channel = null; + internal StreamWriter writer; + internal StreamReader reader; + internal string eventLabel; + /// <summary>Version of ANTLR (dictates events)</summary> + public string version; + public string grammarFileName; + /// <summary> + /// Track the last token index we saw during a consume. If same, then + /// set a flag that we have a problem. + /// </summary> + int previousTokenIndex = -1; + bool tokenIndexesInvalid = false; + + #region ProxyToken Class + + public class ProxyToken : IToken + { + internal int index; + internal int type; + internal int channel; + internal int line; + internal int charPos; + internal string text; + + public ProxyToken(int index) + { + this.index = index; + } + + public ProxyToken(int index, int type, int channel, int line, int charPos, string text) + { + this.index = index; + this.type = type; + this.channel = channel; + this.line = line; + this.charPos = charPos; + this.text = text; + } + + public int Type + { + get { return this.type; } + set { this.type = value; } + } + + public int Line + { + get { return this.line; } + set { this.line = value; } + } + + public int CharPositionInLine + { + get { return this.charPos; } + set { this.charPos = value; } + } + + public int Channel + { + get { return this.channel; } + set { this.channel = value; } + } + + public int TokenIndex + { + get { return this.index; } + set { this.index = value; } + } + + public string Text + { + get { return this.text; } + set { this.text = value; } + } + + public ICharStream InputStream + { + get { return null; } + set { ; } + } + + public override string ToString() + { + string channelStr = ""; + if (channel != Token.DEFAULT_CHANNEL) + { + channelStr = ",channel=" + channel; + } + return "[" + Text + "/<" + type + ">" + channelStr + "," + line + ":" + CharPositionInLine + ",@" + index + "]"; + } + } + + #endregion + + #region ProxyTree Class + + public class ProxyTree : BaseTree + { + public int ID; + public int type; + public int line = 0; + public int charPos = -1; + public int tokenIndex = -1; + public string text; + + public ProxyTree(int ID) + { + this.ID = ID; + } + + public ProxyTree(int ID, int type, int line, int charPos, int tokenIndex, string text) + { + this.ID = ID; + this.type = type; + this.line = line; + this.charPos = charPos; + this.tokenIndex = tokenIndex; + this.text = text; + } + + override public int TokenStartIndex + { + get { return tokenIndex; } + set { ; } + } + override public int TokenStopIndex + { + get { return 0; } + set { ; } + } + override public ITree DupNode() + { + return null; + } + override public int Type + { + get { return type; } + } + override public string Text + { + get { return text; } + } + + override public string ToString() + { + return "fix this"; + } + } + + #endregion + + public RemoteDebugEventSocketListener(IDebugEventListener listener, string hostName, int port) + { + this.listener = listener; + this.hostName = hostName; + this.port = port; + + if (!OpenConnection()) + { + throw new System.Exception(); + } + } + + protected virtual void EventHandler() + { + try + { + Handshake(); + eventLabel = reader.ReadLine(); + while (eventLabel != null) + { + Dispatch(eventLabel); + Ack(); + eventLabel = reader.ReadLine(); + } + } + catch (System.Exception e) + { + Console.Error.WriteLine(e); + Console.Error.WriteLine(e.StackTrace); + } + finally + { + CloseConnection(); + } + } + + protected virtual bool OpenConnection() + { + bool success = false; + try + { + channel = new TcpClient(hostName, port); + channel.NoDelay = true; + writer = new StreamWriter(channel.GetStream(), Encoding.UTF8); + reader = new StreamReader(channel.GetStream(), Encoding.UTF8); + success = true; + } + catch (Exception e) + { + Console.Error.WriteLine(e); + } + return success; + } + + protected virtual void CloseConnection() + { + try + { + reader.Close(); reader = null; + writer.Close(); + writer = null; + channel.Close(); + channel = null; + } + catch (System.Exception e) + { + Console.Error.WriteLine(e); + Console.Error.WriteLine(e.StackTrace); + } + finally + { + if (reader != null) + { + try + { + reader.Close(); + } + catch (IOException ioe) + { + Console.Error.WriteLine(ioe); + } + } + if (writer != null) + { + writer.Close(); + } + if (channel != null) + { + try + { + channel.Close(); + } + catch (IOException ioe) + { + Console.Error.WriteLine(ioe); + } + } + } + } + + protected virtual void Handshake() + { + string antlrLine = reader.ReadLine(); + string[] antlrElements = GetEventElements(antlrLine); + version = antlrElements[1]; + string grammarLine = reader.ReadLine(); + string[] grammarElements = GetEventElements(grammarLine); + grammarFileName = grammarElements[1]; + Ack(); + listener.Commence(); // inform listener after handshake + } + + protected virtual void Ack() + { + writer.WriteLine("ack"); + writer.Flush(); + } + + protected virtual void Dispatch(string line) + { + string[] elements = GetEventElements(line); + if (elements == null || elements[0] == null) + { + Console.Error.WriteLine("unknown debug event: " + line); + return ; + } + if (elements[0].Equals("enterRule")) + { + listener.EnterRule(elements[1], elements[2]); + } + else if (elements[0].Equals("exitRule")) + { + listener.ExitRule(elements[1], elements[2]); + } + else if (elements[0].Equals("enterAlt")) + { + listener.EnterAlt(int.Parse(elements[1], CultureInfo.InvariantCulture)); + } + else if (elements[0].Equals("enterSubRule")) + { + listener.EnterSubRule(int.Parse(elements[1], CultureInfo.InvariantCulture)); + } + else if (elements[0].Equals("exitSubRule")) + { + listener.ExitSubRule(int.Parse(elements[1], CultureInfo.InvariantCulture)); + } + else if (elements[0].Equals("enterDecision")) + { + listener.EnterDecision(int.Parse(elements[1], CultureInfo.InvariantCulture)); + } + else if (elements[0].Equals("exitDecision")) + { + listener.ExitDecision(int.Parse(elements[1], CultureInfo.InvariantCulture)); + } + else if (elements[0].Equals("location")) + { + listener.Location(int.Parse(elements[1], CultureInfo.InvariantCulture), + int.Parse(elements[2], CultureInfo.InvariantCulture)); + } + else if (elements[0].Equals("consumeToken")) + { + ProxyToken t = DeserializeToken(elements, 1); + if (t.TokenIndex == previousTokenIndex) + { + tokenIndexesInvalid = true; + } + previousTokenIndex = t.TokenIndex; + listener.ConsumeToken(t); + } + else if (elements[0].Equals("consumeHiddenToken")) + { + ProxyToken t = DeserializeToken(elements, 1); + if (t.TokenIndex == previousTokenIndex) + { + tokenIndexesInvalid = true; + } + previousTokenIndex = t.TokenIndex; + listener.ConsumeHiddenToken(t); + } + else if (elements[0].Equals("LT")) + { + IToken t = DeserializeToken(elements, 2); + listener.LT(int.Parse(elements[1], CultureInfo.InvariantCulture), t); + } + else if (elements[0].Equals("mark")) + { + listener.Mark(int.Parse(elements[1], CultureInfo.InvariantCulture)); + } + else if (elements[0].Equals("rewind")) + { + if (elements[1] != null) + { + listener.Rewind(int.Parse(elements[1], CultureInfo.InvariantCulture)); + } + else + { + listener.Rewind(); + } + } + else if (elements[0].Equals("beginBacktrack")) + { + listener.BeginBacktrack(int.Parse(elements[1], CultureInfo.InvariantCulture)); + } + else if (elements[0].Equals("endBacktrack")) + { + int level = int.Parse(elements[1], CultureInfo.InvariantCulture); + int successI = int.Parse(elements[2], CultureInfo.InvariantCulture); + //listener.EndBacktrack(level, successI == (int)true); + listener.EndBacktrack(level, successI == 1 /*1=TRUE*/); + } + else if (elements[0].Equals("exception")) + { + string excName = elements[1]; + string indexS = elements[2]; + string lineS = elements[3]; + string posS = elements[4]; + Type excClass = null; + try + { + excClass = System.Type.GetType(excName); + RecognitionException e = (RecognitionException) System.Activator.CreateInstance(excClass); + e.Index = int.Parse(indexS, CultureInfo.InvariantCulture); + e.Line = int.Parse(lineS, CultureInfo.InvariantCulture); + e.CharPositionInLine = int.Parse(posS, CultureInfo.InvariantCulture); + listener.RecognitionException(e); + } + catch (System.UnauthorizedAccessException iae) + { + Console.Error.WriteLine("can't access class " + iae); + Console.Error.WriteLine(iae.StackTrace); + } + } + else if (elements[0].Equals("beginResync")) + { + listener.BeginResync(); + } + else if (elements[0].Equals("endResync")) + { + listener.EndResync(); + } + else if (elements[0].Equals("terminate")) + { + listener.Terminate(); + } + else if (elements[0].Equals("semanticPredicate")) + { + bool result = bool.Parse(elements[1]); + string predicateText = elements[2]; + predicateText = UnEscapeNewlines(predicateText); + listener.SemanticPredicate(result, predicateText); + } + else if (elements[0].Equals("consumeNode")) + { + ProxyTree node = DeserializeNode(elements, 1); + listener.ConsumeNode(node); + } + else if (elements[0].Equals("LN")) + { + int i = int.Parse(elements[1], CultureInfo.InvariantCulture); + ProxyTree node = DeserializeNode(elements, 2); + listener.LT(i, node); + } + else if (elements[0].Equals("createNodeFromTokenElements")) + { + int ID = int.Parse(elements[1], CultureInfo.InvariantCulture); + int type = int.Parse(elements[2], CultureInfo.InvariantCulture); + string text = elements[3]; + text = UnEscapeNewlines(text); + ProxyTree node = new ProxyTree(ID, type, -1, -1, -1, text); + listener.CreateNode(node); + } + else if (elements[0].Equals("createNode")) + { + int ID = int.Parse(elements[1], CultureInfo.InvariantCulture); + int tokenIndex = int.Parse(elements[2], CultureInfo.InvariantCulture); + // create dummy node/token filled with ID, tokenIndex + ProxyTree node = new ProxyTree(ID); + ProxyToken token = new ProxyToken(tokenIndex); + listener.CreateNode(node, token); + } + else if (elements[0].Equals("nilNode")) + { + int ID = int.Parse(elements[1], CultureInfo.InvariantCulture); + ProxyTree node = new ProxyTree(ID); + listener.GetNilNode(node); + } + else if ( elements[0].Equals("errorNode") ) { + // TODO: do we need a special tree here? + int ID = int.Parse(elements[1], CultureInfo.InvariantCulture); + int type = int.Parse(elements[2], CultureInfo.InvariantCulture); + String text = elements[3]; + text = UnEscapeNewlines(text); + ProxyTree node = new ProxyTree(ID, type, -1, -1, -1, text); + listener.ErrorNode(node); + } + else if (elements[0].Equals("becomeRoot")) + { + int newRootID = int.Parse(elements[1], CultureInfo.InvariantCulture); + int oldRootID = int.Parse(elements[2], CultureInfo.InvariantCulture); + ProxyTree newRoot = new ProxyTree(newRootID); + ProxyTree oldRoot = new ProxyTree(oldRootID); + listener.BecomeRoot(newRoot, oldRoot); + } + else if (elements[0].Equals("addChild")) + { + int rootID = int.Parse(elements[1], CultureInfo.InvariantCulture); + int childID = int.Parse(elements[2], CultureInfo.InvariantCulture); + ProxyTree root = new ProxyTree(rootID); + ProxyTree child = new ProxyTree(childID); + listener.AddChild(root, child); + } + else if (elements[0].Equals("setTokenBoundaries")) + { + int ID = int.Parse(elements[1], CultureInfo.InvariantCulture); + ProxyTree node = new ProxyTree(ID); + listener.SetTokenBoundaries(node, + int.Parse(elements[2], CultureInfo.InvariantCulture), + int.Parse(elements[3], CultureInfo.InvariantCulture)); + } + else + { + Console.Error.WriteLine("unknown debug event: " + line); + } + } + + protected internal ProxyTree DeserializeNode(string[] elements, int offset) + { + int ID = int.Parse(elements[offset + 0], CultureInfo.InvariantCulture); + int type = int.Parse(elements[offset + 1], CultureInfo.InvariantCulture); + int tokenLine = int.Parse(elements[offset + 2], CultureInfo.InvariantCulture); + int charPositionInLine = int.Parse(elements[offset + 3], CultureInfo.InvariantCulture); + int tokenIndex = int.Parse(elements[offset + 4], CultureInfo.InvariantCulture); + string text = elements[offset + 5]; + text = UnEscapeNewlines(text); + return new ProxyTree(ID, type, tokenLine, charPositionInLine, tokenIndex, text); + } + + protected internal virtual ProxyToken DeserializeToken(string[] elements, int offset) + { + string indexS = elements[offset + 0]; + string typeS = elements[offset + 1]; + string channelS = elements[offset + 2]; + string lineS = elements[offset + 3]; + string posS = elements[offset + 4]; + string text = elements[offset + 5]; + text = UnEscapeNewlines(text); + int index = int.Parse(indexS, CultureInfo.InvariantCulture); + ProxyToken t = new ProxyToken(index, int.Parse(typeS, CultureInfo.InvariantCulture), int.Parse(channelS, CultureInfo.InvariantCulture), int.Parse(lineS, CultureInfo.InvariantCulture), int.Parse(posS, CultureInfo.InvariantCulture), text); + return t; + } + + /// <summary>Create a thread to listen to the remote running recognizer </summary> + public virtual void start() + { + Thread t = new Thread(new ThreadStart(this.Run)); + t.Start(); + } + + public virtual void Run() + { + EventHandler(); + } + + // M i s c + + public virtual string[] GetEventElements(string eventLabel) + { + if (eventLabel == null) + return null; + + string[] elements = new string[MAX_EVENT_ELEMENTS]; + string str = null; // a string element if present (must be last) + try + { + int firstQuoteIndex = eventLabel.IndexOf('"'); + if (firstQuoteIndex >= 0) + { + // treat specially; has a string argument like "a comment\n + // Note that the string is terminated by \n not end quote. + // Easier to parse that way. + string eventWithoutString = eventLabel.Substring(0, (firstQuoteIndex) - (0)); + str = eventLabel.Substring(firstQuoteIndex + 1, (eventLabel.Length) - (firstQuoteIndex + 1)); + eventLabel = eventWithoutString; + } + + string[] strings = eventLabel.Split('\t'); + int i = 0; + for ( ; i < strings.Length; i++) + { + if (i >= MAX_EVENT_ELEMENTS) + { + return elements; + } + elements[i] = strings[i]; + } + + if (str != null) + { + elements[i] = str; + } + } + catch (System.Exception e) + { + Console.Error.WriteLine(e.StackTrace); + } + return elements; + } + + protected string UnEscapeNewlines(string txt) + { + // this unescape is slow but easy to understand + txt = txt.Replace("%0A", "\n"); // unescape \n + txt = txt.Replace("%0D", "\r"); // unescape \r + txt = txt.Replace("%25", "%"); // undo escaped escape chars + return txt; + } + + public bool TokenIndexesAreInvalid + { + get { return false; /*tokenIndexesInvalid;*/ } + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/TraceDebugEventListener.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/TraceDebugEventListener.cs new file mode 100644 index 0000000..dee88ff --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/TraceDebugEventListener.cs @@ -0,0 +1,126 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Debug +{ + using System; + using Antlr.Runtime; + using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; + using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream; + + + /// <summary> + /// Print out (most of) the events... Useful for debugging, testing... + /// </summary> + public class TraceDebugEventListener : BlankDebugEventListener + { + ITreeAdaptor adaptor; + + public TraceDebugEventListener(ITreeAdaptor adaptor) + { + this.adaptor = adaptor; + } + + override public void EnterRule(string grammarFileName, string ruleName) { + Console.Out.WriteLine("EnterRule " + grammarFileName + " " + ruleName); + } + override public void ExitRule(string grammarFileName, string ruleName) { + Console.Out.WriteLine("ExitRule " + grammarFileName + " " + ruleName); + } + override public void EnterSubRule(int decisionNumber) { Console.Out.WriteLine("EnterSubRule"); } + override public void ExitSubRule(int decisionNumber) { Console.Out.WriteLine("ExitSubRule"); } + override public void Location(int line, int pos) { Console.Out.WriteLine("Location " + line + ":" + pos); } + + #region Tree parsing stuff + + override public void ConsumeNode(object t) + { + int ID = adaptor.GetUniqueID(t); + string text = adaptor.GetNodeText(t); + int type = adaptor.GetNodeType(t); + Console.Out.WriteLine("ConsumeNode " + ID + " " + text + " " + type); + } + + override public void LT(int i, object t) + { + int ID = adaptor.GetUniqueID(t); + string text = adaptor.GetNodeText(t); + int type = adaptor.GetNodeType(t); + Console.Out.WriteLine("LT " + i + " " + ID + " " + text + " " + type); + } + + #endregion + + #region AST stuff + + override public void GetNilNode(object t) + { + Console.Out.WriteLine("GetNilNode " + adaptor.GetUniqueID(t)); + } + + override public void CreateNode(object t) + { + int ID = adaptor.GetUniqueID(t); + string text = adaptor.GetNodeText(t); + int type = adaptor.GetNodeType(t); + Console.Out.WriteLine("Create " + ID + ": " + text + ", " + type); + } + + override public void CreateNode(object t, IToken token) + { + int ID = adaptor.GetUniqueID(t); + //string text = adaptor.GetNodeText(t); + int tokenIndex = token.TokenIndex; + Console.Out.WriteLine("Create " + ID + ": " + tokenIndex); + } + + override public void BecomeRoot(object newRoot, object oldRoot) + { + Console.Out.WriteLine("BecomeRoot " + adaptor.GetUniqueID(newRoot) + ", " + adaptor.GetUniqueID(oldRoot)); + } + + override public void AddChild(object root, object child) + { + Console.Out.WriteLine("AddChild " + adaptor.GetUniqueID(root) + ", " + adaptor.GetUniqueID(child)); + } + + override public void SetTokenBoundaries(object t, int tokenStartIndex, int tokenStopIndex) + { + Console.Out.WriteLine("SetTokenBoundaries " + adaptor.GetUniqueID(t) + ", " + tokenStartIndex + ", " + tokenStopIndex); + } + + #endregion + } +} + diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/Tracer.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/Tracer.cs new file mode 100644 index 0000000..3458f58 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Debug/Tracer.cs @@ -0,0 +1,87 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Debug +{ + using System; + using Antlr.Runtime; + using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; + using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream; + + + /// <summary> + /// The default tracer mimics the traceParser behavior of ANTLR 2.x. + /// This listens for debugging events from the parser and implies + /// that you cannot debug and trace at the same time. + /// </summary> + public class Tracer : BlankDebugEventListener + { + public IIntStream input; + protected int level = 0; + + public Tracer(IIntStream input) + { + this.input = input; + } + + override public void EnterRule(string grammarFileName, string ruleName) + { + for (int i = 1; i <= level; i++) + { + Console.Out.Write(" "); + } + Console.Out.WriteLine("> " + grammarFileName + " " + ruleName + " lookahead(1)=" + GetInputSymbol(1)); + level++; + } + + override public void ExitRule(string grammarFileName, string ruleName) + { + level--; + for (int i = 1; i <= level; i++) + { + Console.Out.Write(" "); + } + Console.Out.WriteLine("< " + grammarFileName + " " + ruleName + " lookahead(1)=" + GetInputSymbol(1)); + } + + public virtual object GetInputSymbol(int k) + { + if (input is ITokenStream) + { + return ((ITokenStream) input).LT(k); + } + return (char) input.LA(k); + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Misc/ErrorManager.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Misc/ErrorManager.cs new file mode 100644 index 0000000..2c12838 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Misc/ErrorManager.cs @@ -0,0 +1,101 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Misc +{ + using System; + using StringBuilder = System.Text.StringBuilder; + using StackTrace = System.Diagnostics.StackTrace; + using StackFrame = System.Diagnostics.StackFrame; + using IList = System.Collections.IList; + + /// <summary>A minimal ANTLR3 error [message] manager with the ST bits</summary> + public class ErrorManager + { + public static void InternalError(object error, Exception e) + { + StackFrame location = GetLastNonErrorManagerCodeLocation(e); + string msg = "Exception " + e + "@" + location + ": " + error; + //Error(MSG_INTERNAL_ERROR, msg); + Error(msg); + } + + public static void InternalError(object error) + { + StackFrame location = GetLastNonErrorManagerCodeLocation(new Exception()); + string msg = location + ": " + error; + //Error(MSG_INTERNAL_ERROR, msg); + Error(msg); + } + + /// <summary> + /// Return first non ErrorManager code location for generating messages + /// </summary> + /// <param name="e">Current exception</param> + /// <returns></returns> + private static StackFrame GetLastNonErrorManagerCodeLocation(Exception e) + { + StackTrace stackTrace = new StackTrace(e); + int i = 0; + for (; i < stackTrace.FrameCount; i++) + { + StackFrame f = stackTrace.GetFrame(i); + if (f.ToString().IndexOf("ErrorManager") < 0) + { + break; + } + } + StackFrame location = stackTrace.GetFrame(i); + + return location; + } + + public static void Error(/*int msgID,*/ object arg) + { + //getErrorCount().errors++; + //getErrorListener().error(new ToolMessage(msgID, arg)); + + StringBuilder sb = new StringBuilder(); + //sb.AppendFormat("internal error: {0} {1}", arg); + sb.AppendFormat("internal error: {0} ", arg); + } + + /* + INTERNAL_ERROR(arg,arg2,exception,stackTrace) ::= << + internal error: <arg> <arg2><if(exception)>: <exception><endif> + <stackTrace; separator="\n"> + >> + */ + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Misc/Stats.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Misc/Stats.cs new file mode 100644 index 0000000..e2c475e --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Misc/Stats.cs @@ -0,0 +1,179 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Misc +{ + using System; + using FileInfo = System.IO.FileInfo; + using DirectoryInfo = System.IO.DirectoryInfo; + using StreamWriter = System.IO.StreamWriter; + using Path = System.IO.Path; + using IOException = System.IO.IOException; + + /// <summary>Stats routines needed by profiler etc...</summary> + /// <remarks> + /// Note that these routines return 0.0 if no values exist in X[] + /// which is not "correct" but, it is useful so I don't generate NaN + /// in my output + /// </remarks> + public class Stats + { + /// <summary>Compute the sample (unbiased estimator) standard deviation</summary> + /// <remarks> + /// The computation follows: + /// Computing Deviations: Standard Accuracy + /// Tony F. Chan and John Gregg Lewis + /// Stanford University + /// Communications of ACM September 1979 of Volume 22 the ACM Number 9 + /// + /// The "two-pass" method from the paper; supposed to have better + /// numerical properties than the textbook summation/sqrt. To me + /// this looks like the textbook method, but I ain't no numerical + /// methods guy. + /// </remarks> + public static double Stddev(int[] X) + { + int m = X.Length; + if (m <= 1) + { + return 0; + } + double xbar = Avg(X); + double s2 = 0.0; + for (int i = 0; i < m; i++) + { + s2 += (X[i] - xbar) * (X[i] - xbar); + } + s2 = s2 / (m - 1); + return Math.Sqrt(s2); + } + + /// <summary>Compute the sample mean</summary> + public static double Avg(int[] X) + { + double xbar = 0.0; + int m = X.Length; + if (m == 0) + { + return 0; + } + for (int i = 0; i < m; i++) + { + xbar += X[i]; + } + if (xbar >= 0.0) + { + return xbar / m; + } + return 0.0; + } + + public static int Min(int[] X) + { + int min = Int32.MaxValue; + int m = X.Length; + if (m == 0) + { + return 0; + } + for (int i = 0; i < m; i++) + { + if (X[i] < min) + { + min = X[i]; + } + } + return min; + } + + public static int Max(int[] X) + { + int max = Int32.MinValue; + int m = X.Length; + if (m == 0) + { + return 0; + } + for (int i = 0; i < m; i++) + { + if (X[i] > max) + { + max = X[i]; + } + } + return max; + } + + public static int Sum(int[] X) + { + int s = 0; + int m = X.Length; + if (m == 0) + { + return 0; + } + for (int i = 0; i < m; i++) + { + s += X[i]; + } + return s; + } + + public static void WriteReport(string filename, string data) + { + string absoluteFilename = GetAbsoluteFileName(filename); + FileInfo f = new FileInfo(absoluteFilename); + f.Directory.Create(); // ensure parent dir exists + // write file + try + { + StreamWriter w = new StreamWriter(f.FullName, true); // append + w.WriteLine(data); + w.Close(); + } + catch (IOException ioe) + { + ErrorManager.InternalError("can't write stats to " + absoluteFilename, + ioe); + } + } + + public static string GetAbsoluteFileName(string filename) + { + return Path.Combine( + Path.Combine(Environment.CurrentDirectory, Constants.ANTLRWORKS_DIR), + filename); + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/BaseTree.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/BaseTree.cs new file mode 100644 index 0000000..cec59f9 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/BaseTree.cs @@ -0,0 +1,455 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + using IList = System.Collections.IList; + using ArrayList = System.Collections.ArrayList; + using StringBuilder = System.Text.StringBuilder; + + /// <summary> + /// A generic tree implementation with no payload. You must subclass to + /// actually have any user data. ANTLR v3 uses a list of children approach + /// instead of the child-sibling approach in v2. A flat tree (a list) is + /// an empty node whose children represent the list. An empty, but + /// non-null node is called "nil". + /// </summary> + [Serializable] + public abstract class BaseTree : ITree + { + public BaseTree() + { + } + + /// <summary>Create a new node from an existing node does nothing for BaseTree + /// as there are no fields other than the children list, which cannot + /// be copied as the children are not considered part of this node. + /// </summary> + public BaseTree(ITree node) + { + } + + virtual public int ChildCount + { + get + { + if (children == null) + { + return 0; + } + return children.Count; + } + } + + virtual public bool IsNil + { + get { return false; } + } + + virtual public int Line + { + get { return 0; } + } + + virtual public int CharPositionInLine + { + get { return 0; } + } + + protected IList children; + + public virtual ITree GetChild(int i) + { + if (children == null || i >= children.Count) + { + return null; + } + return (ITree) children[i]; + } + + /// <summary> + /// Get the children internal list of children. Manipulating the list + /// directly is not a supported operation (i.e. you do so at your own risk) + /// </summary> + public IList Children + { + get { return children; } + } + + /// <summary> + /// Add t as child of this node. + /// </summary> + /// <remarks> + /// Warning: if t has no children, but child does and child isNil then + /// this routine moves children to t via t.children = child.children; + /// i.e., without copying the array. + /// </remarks> + /// <param name="t"></param> + public virtual void AddChild(ITree t) + { + if (t == null) + { + return; + } + + BaseTree childTree = (BaseTree)t; + if (childTree.IsNil) // t is an empty node possibly with children + { + if ((children != null) && (children == childTree.children)) + { + throw new InvalidOperationException("attempt to add child list to itself"); + } + // just add all of childTree's children to this + if (childTree.children != null) + { + if (children != null) // must copy, this has children already + { + int n = childTree.children.Count; + for (int i = 0; i < n; i++) + { + ITree c = (ITree)childTree.Children[i]; + children.Add(c); + // handle double-link stuff for each child of nil root + c.Parent = this; + c.ChildIndex = children.Count - 1; + } + } + else + { + // no children for this but t has children; just set pointer + // call general freshener routine + children = childTree.children; + FreshenParentAndChildIndexes(); + } + } + } + else + { + // child is not nil (don't care about children) + if (children == null) + { + children = CreateChildrenList(); // create children list on demand + } + children.Add(t); + childTree.Parent = this; + childTree.ChildIndex = children.Count - 1; + } + } + + /// <summary> + /// Add all elements of kids list as children of this node + /// </summary> + /// <param name="kids"></param> + public void AddChildren(IList kids) + { + for (int i = 0; i < kids.Count; i++) + { + ITree t = (ITree) kids[i]; + AddChild(t); + } + } + + public virtual void SetChild(int i, ITree t) + { + if (t == null) + { + return; + } + if (t.IsNil) + { + throw new ArgumentException("Can't set single child to a list"); + } + if (children == null) + { + children = CreateChildrenList(); + } + children[i] = t; + t.Parent = this; + t.ChildIndex = i; + } + + public virtual object DeleteChild(int i) + { + if (children == null) + { + return null; + } + ITree killed = (ITree)children[i]; + children.RemoveAt(i); + // walk rest and decrement their child indexes + FreshenParentAndChildIndexes(i); + return killed; + } + + /// <summary> + /// Delete children from start to stop and replace with t even if t is + /// a list (nil-root tree). + /// </summary> + /// <remarks> + /// Number of children can increase or decrease. + /// For huge child lists, inserting children can force walking rest of + /// children to set their childindex; could be slow. + /// </remarks> + public virtual void ReplaceChildren(int startChildIndex, int stopChildIndex, object t) + { + /* + Console.Out.WriteLine("replaceChildren "+startChildIndex+", "+stopChildIndex+ + " with "+((BaseTree)t).ToStringTree()); + Console.Out.WriteLine("in="+ToStringTree()); + */ + if (children == null) + { + throw new ArgumentException("indexes invalid; no children in list"); + } + int replacingHowMany = stopChildIndex - startChildIndex + 1; + int replacingWithHowMany; + BaseTree newTree = (BaseTree)t; + IList newChildren; + // normalize to a list of children to add: newChildren + if (newTree.IsNil) + { + newChildren = newTree.Children; + } + else + { + newChildren = new ArrayList(1); + newChildren.Add(newTree); + } + replacingWithHowMany = newChildren.Count; + int numNewChildren = newChildren.Count; + int delta = replacingHowMany - replacingWithHowMany; + // if same number of nodes, do direct replace + if (delta == 0) + { + int j = 0; // index into new children + for (int i = startChildIndex; i <= stopChildIndex; i++) + { + BaseTree child = (BaseTree)newChildren[j]; + children[i] = child; + child.Parent = this; + child.ChildIndex = i; + j++; + } + } + else if (delta > 0) + { // fewer new nodes than there were + // set children and then delete extra + for (int j = 0; j < numNewChildren; j++) + { + children[startChildIndex + j] = newChildren[j]; + } + int indexToDelete = startChildIndex + numNewChildren; + for (int c = indexToDelete; c <= stopChildIndex; c++) + { + // delete same index, shifting everybody down each time + children.RemoveAt(indexToDelete); + } + FreshenParentAndChildIndexes(startChildIndex); + } + else + { // more new nodes than were there before + // fill in as many children as we can (replacingHowMany) w/o moving data + int replacedSoFar; + for (replacedSoFar = 0; replacedSoFar < replacingHowMany; replacedSoFar++) + { + children[startChildIndex + replacedSoFar] = newChildren[replacedSoFar]; + } + // replacedSoFar has correct index for children to add + for ( ; replacedSoFar < replacingWithHowMany; replacedSoFar++) + { + children.Insert(startChildIndex + replacedSoFar, newChildren[replacedSoFar]); + } + FreshenParentAndChildIndexes(startChildIndex); + } + //Console.Out.WriteLine("out="+ToStringTree()); + } + + /// <summary>Override in a subclass to change the impl of children list </summary> + protected internal virtual IList CreateChildrenList() + { + return new ArrayList(); + } + + /// <summary>Set the parent and child index values for all child of t</summary> + public virtual void FreshenParentAndChildIndexes() + { + FreshenParentAndChildIndexes(0); + } + + public virtual void FreshenParentAndChildIndexes(int offset) + { + int n = ChildCount; + for (int c = offset; c < n; c++) + { + ITree child = (ITree)GetChild(c); + child.ChildIndex = c; + child.Parent = this; + } + } + + public virtual void SanityCheckParentAndChildIndexes() + { + SanityCheckParentAndChildIndexes(null, -1); + } + + public virtual void SanityCheckParentAndChildIndexes(ITree parent, int i) + { + if (parent != this.Parent) + { + throw new ArgumentException("parents don't match; expected " + parent + " found " + this.Parent); + } + if (i != this.ChildIndex) + { + throw new NotSupportedException("child indexes don't match; expected " + i + " found " + this.ChildIndex); + } + int n = this.ChildCount; + for (int c = 0; c < n; c++) + { + CommonTree child = (CommonTree)this.GetChild(c); + child.SanityCheckParentAndChildIndexes(this, c); + } + } + + /// <summary>BaseTree doesn't track child indexes.</summary> + public virtual int ChildIndex + { + get { return 0; } + set { } + } + + /// <summary>BaseTree doesn't track parent pointers.</summary> + public virtual ITree Parent + { + get { return null; } + set { } + } + + /// <summary> + /// Walk upwards looking for ancestor with this token type. + /// </summary> + public bool HasAncestor(int ttype) { return GetAncestor(ttype)!=null; } + + /// <summary> + /// Walk upwards and get first ancestor with this token type. + /// </summary> + public ITree GetAncestor(int ttype) { + ITree t = this; + t = t.Parent; + while ( t!=null ) { + if ( t.Type == ttype ) return t; + t = t.Parent; + } + return null; + } + + /// <summary> + /// Return a list of all ancestors of this node. The first node of + /// list is the root and the last is the parent of this node. + /// </summary> + public IList GetAncestors() { + if ( Parent==null ) return null; + IList ancestors = new ArrayList(); + ITree t = this; + t = t.Parent; + while ( t!=null ) { + ancestors.Insert(0, t); // insert at start + t = t.Parent; + } + return ancestors; + } + + /// <summary> + /// Print out a whole tree not just a node + /// </summary> + public virtual string ToStringTree() + { + if (children == null || children.Count == 0) + { + return this.ToString(); + } + StringBuilder buf = new StringBuilder(); + if (!IsNil) + { + buf.Append("("); + buf.Append(this.ToString()); + buf.Append(' '); + } + for (int i = 0; children != null && i < children.Count; i++) + { + ITree t = (ITree) children[i]; + if (i > 0) + { + buf.Append(' '); + } + buf.Append(t.ToStringTree()); + } + if (!IsNil) + { + buf.Append(")"); + } + return buf.ToString(); + } + + /// <summary> + /// Force base classes override and say how a node (not a tree) + /// should look as text + /// </summary> + public override abstract string ToString(); + + public abstract ITree DupNode(); + + public abstract int Type + { + get; + } + + public abstract int TokenStartIndex + { + get; + set; + } + + public abstract int TokenStopIndex + { + get; + set; + } + + public abstract string Text + { + get; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/BaseTreeAdaptor.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/BaseTreeAdaptor.cs new file mode 100644 index 0000000..459015c --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/BaseTreeAdaptor.cs @@ -0,0 +1,382 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + using IDictionary = System.Collections.IDictionary; + using Hashtable = System.Collections.Hashtable; + using IToken = Antlr.Runtime.IToken; + + /// <summary> + /// A TreeAdaptor that works with any Tree implementation + /// </summary> + public abstract class BaseTreeAdaptor : ITreeAdaptor + { + /// <summary>A map of tree node to unique IDs.</summary> + protected IDictionary treeToUniqueIDMap; + + /// <summary>Next available unique ID.</summary> + protected int uniqueNodeID = 1; + + public virtual object GetNilNode() + { + return Create(null); + } + + /// <summary> + /// Create tree node that holds the start and stop tokens associated + /// with an error. + /// </summary> + /// <remarks> + /// <para>If you specify your own kind of tree nodes, you will likely have to + /// override this method. CommonTree returns Token.INVALID_TOKEN_TYPE + /// if no token payload but you might have to set token type for diff + /// node type.</para> + /// + /// <para>You don't have to subclass CommonErrorNode; you will likely need to + /// subclass your own tree node class to avoid class cast exception.</para> + /// </remarks> + public virtual object ErrorNode(ITokenStream input, IToken start, IToken stop, + RecognitionException e) + { + CommonErrorNode t = new CommonErrorNode(input, start, stop, e); + //System.out.println("returning error node '"+t+"' @index="+input.index()); + return t; + } + + public virtual bool IsNil(object tree) + { + return ((ITree)tree).IsNil; + } + + public virtual object DupTree(object tree) + { + return DupTree(tree, null); + } + + /// <summary> + /// This is generic in the sense that it will work with any kind of + /// tree (not just the ITree interface). It invokes the adaptor routines + /// not the tree node routines to do the construction. + /// </summary> + public virtual object DupTree(object t, object parent) + { + if (t == null) + { + return null; + } + object newTree = DupNode(t); + // ensure new subtree root has parent/child index set + SetChildIndex(newTree, GetChildIndex(t)); // same index in new tree + SetParent(newTree, parent); + int n = GetChildCount(t); + for (int i = 0; i < n; i++) + { + object child = GetChild(t, i); + object newSubTree = DupTree(child, t); + AddChild(newTree, newSubTree); + } + return newTree; + } + + /// <summary> + /// Add a child to the tree t. If child is a flat tree (a list), make all + /// in list children of t. + /// </summary> + /// <remarks> + /// <para> + /// Warning: if t has no children, but child does and child isNil + /// then you can decide it is ok to move children to t via + /// t.children = child.children; i.e., without copying the array. + /// Just make sure that this is consistent with how the user will build + /// ASTs. + /// </para> + /// </remarks> + public virtual void AddChild(object t, object child) + { + if ((t != null) && (child != null)) + { + ((ITree) t).AddChild((ITree) child); + } + } + + /// <summary> + /// If oldRoot is a nil root, just copy or move the children to newRoot. + /// If not a nil root, make oldRoot a child of newRoot. + /// </summary> + /// <remarks> + /// + /// old=^(nil a b c), new=r yields ^(r a b c) + /// old=^(a b c), new=r yields ^(r ^(a b c)) + /// + /// If newRoot is a nil-rooted single child tree, use the single + /// child as the new root node. + /// + /// old=^(nil a b c), new=^(nil r) yields ^(r a b c) + /// old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) + /// + /// If oldRoot was null, it's ok, just return newRoot (even if isNil). + /// + /// old=null, new=r yields r + /// old=null, new=^(nil r) yields ^(nil r) + /// + /// Return newRoot. Throw an exception if newRoot is not a + /// simple node or nil root with a single child node--it must be a root + /// node. If newRoot is ^(nil x) return x as newRoot. + /// + /// Be advised that it's ok for newRoot to point at oldRoot's + /// children; i.e., you don't have to copy the list. We are + /// constructing these nodes so we should have this control for + /// efficiency. + /// </remarks> + public virtual object BecomeRoot(object newRoot, object oldRoot) + { + ITree newRootTree = (ITree) newRoot; + ITree oldRootTree = (ITree) oldRoot; + if (oldRoot == null) + { + return newRoot; + } + // handle ^(nil real-node) + if (newRootTree.IsNil) + { + int nc = newRootTree.ChildCount; + if ( nc==1 ) newRootTree = (ITree)newRootTree.GetChild(0); + else if ( nc >1 ) { + throw new SystemException("more than one node as root (TODO: make exception hierarchy)"); + } + } + // add oldRoot to newRoot; AddChild takes care of case where oldRoot + // is a flat list (i.e., nil-rooted tree). All children of oldRoot + // are added to newRoot. + newRootTree.AddChild(oldRootTree); + return newRootTree; + } + + /// <summary>Transform ^(nil x) to x and nil to null</summary> + public virtual object RulePostProcessing(object root) + { + ITree r = (ITree) root; + if (r != null && r.IsNil) + { + if (r.ChildCount == 0) + { + r = null; + } + else if (r.ChildCount == 1) + { + r = (ITree)r.GetChild(0); + // whoever invokes rule will set parent and child index + r.Parent = null; + r.ChildIndex = -1; + } + } + return r; + } + + public virtual object BecomeRoot(IToken newRoot, object oldRoot) + { + return BecomeRoot(Create(newRoot), oldRoot); + } + + public virtual object Create(int tokenType, IToken fromToken) + { + fromToken = CreateToken(fromToken); + fromToken.Type = tokenType; + ITree t = (ITree) Create(fromToken); + return t; + } + + public virtual object Create(int tokenType, IToken fromToken, string text) + { + fromToken = CreateToken(fromToken); + fromToken.Type = tokenType; + fromToken.Text = text; + ITree t = (ITree) Create(fromToken); + return t; + } + + public virtual object Create(int tokenType, string text) + { + IToken fromToken = CreateToken(tokenType, text); + ITree t = (ITree) Create(fromToken); + return t; + } + + public virtual int GetNodeType(object t) + { + return ((ITree) t).Type; + } + + public virtual void SetNodeType(object t, int type) + { + throw new NotImplementedException("don't know enough about Tree node"); + } + + public virtual string GetNodeText(object t) + { + return ((ITree)t).Text; + } + + public virtual void SetNodeText(object t, string text) + { + throw new NotImplementedException("don't know enough about Tree node"); + } + + public virtual object GetChild(object t, int i) + { + return ((ITree)t).GetChild(i); + } + + public virtual void SetChild(object t, int i, object child) + { + ((ITree)t).SetChild(i, (ITree)child); + } + + public virtual object DeleteChild(object t, int i) + { + return ((ITree)t).DeleteChild(i); + } + + public virtual int GetChildCount(object t) + { + return ((ITree)t).ChildCount; + + } + + public abstract object DupNode(object param1); + public abstract object Create(IToken param1); + public abstract void SetTokenBoundaries(object param1, IToken param2, IToken param3); + public abstract int GetTokenStartIndex(object t); + public abstract int GetTokenStopIndex(object t); + public abstract IToken GetToken(object treeNode); + + /// <summary> + /// For identifying trees. How to identify nodes so we can say "add node + /// to a prior node"? + /// </summary> + /// <remarks> + /// <para> + /// System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode() is + /// not available in .NET 1.0. It is "broken/buggy" in .NET 1.1 + /// (for multi-appdomain scenarios). + /// </para> + /// <para> + /// We are tracking uniqueness of IDs ourselves manually since ANTLR + /// v3.1 release using hashtables. We will be tracking . Even though + /// it is expensive, we will create a hashtable with all tree nodes + /// in it as this is only for debugging. + /// </para> + /// </remarks> + public int GetUniqueID(object node) + { + if (treeToUniqueIDMap == null) + { + treeToUniqueIDMap = new Hashtable(); + } + object prevIdObj = treeToUniqueIDMap[node]; + if (prevIdObj != null) + { + return (int) prevIdObj; + } + int ID = uniqueNodeID; + treeToUniqueIDMap[node] = ID; + uniqueNodeID++; + return ID; + + //return node.GetHashCode(); + //return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(node); + } + + /// <summary> + /// Tell me how to create a token for use with imaginary token nodes. + /// For example, there is probably no input symbol associated with imaginary + /// token DECL, but you need to create it as a payload or whatever for + /// the DECL node as in ^(DECL type ID). + /// + /// If you care what the token payload objects' type is, you should + /// override this method and any other createToken variant. + /// </summary> + public abstract IToken CreateToken(int tokenType, string text); + + /// <summary> + /// Tell me how to create a token for use with imaginary token nodes. + /// For example, there is probably no input symbol associated with imaginary + /// token DECL, but you need to create it as a payload or whatever for + /// the DECL node as in ^(DECL type ID). + /// + /// This is a variant of createToken where the new token is derived from + /// an actual real input token. Typically this is for converting '{' + /// tokens to BLOCK etc... You'll see + /// + /// r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ; + /// + /// If you care what the token payload objects' type is, you should + /// override this method and any other createToken variant. + /// </summary> + public abstract IToken CreateToken(IToken fromToken); + + /// <summary> + /// Who is the parent node of this node; if null, implies node is root. + /// </summary> + /// <remarks> + /// If your node type doesn't handle this, it's ok but the tree rewrites + /// in tree parsers need this functionality. + /// </remarks> + public abstract object GetParent(object t); + public abstract void SetParent(object t, object parent); + + /// <summary> + /// What index is this node in the child list? Range: 0..n-1 + /// </summary> + /// <remarks> + /// If your node type doesn't handle this, it's ok but the tree rewrites + /// in tree parsers need this functionality. + /// </remarks> + public abstract int GetChildIndex(object t); + public abstract void SetChildIndex(object t, int index); + + /// <summary> + /// Replace from start to stop child index of parent with t, which might + /// be a list. Number of children may be different after this call. + /// </summary> + /// <remarks> + /// If parent is null, don't do anything; must be at root of overall tree. + /// Can't replace whatever points to the parent externally. Do nothing. + /// </remarks> + public abstract void ReplaceChildren(object parent, int startChildIndex, int stopChildIndex, object t); + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/CommonErrorNode.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/CommonErrorNode.cs new file mode 100644 index 0000000..347d292 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/CommonErrorNode.cs @@ -0,0 +1,120 @@ +/* + [The "BSD licence"] + Copyright (c) 2007-2008 Johannes Luber + 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. +*/ + +using Antlr.Runtime.Tree; +using System; + +namespace Antlr.Runtime +{ + + /** A node representing erroneous token range in token stream */ + [Serializable] + public class CommonErrorNode : CommonTree { + public IIntStream input; + public IToken start; + public IToken stop; + [NonSerialized] + public RecognitionException trappedException; + + public CommonErrorNode(ITokenStream input, IToken start, IToken stop, + RecognitionException e) { + if (stop == null || + (stop.TokenIndex < start.TokenIndex && + stop.Type != Runtime.Token.EOF) ) { + // sometimes resync does not consume a token (when LT(1) is + // in follow set). So, stop will be 1 to left to start. adjust. + // Also handle case where start is the first token and no token + // is consumed during recovery; LT(-1) will return null. + stop = start; + } + this.input = input; + this.start = start; + this.stop = stop; + this.trappedException = e; + } + + override public bool IsNil + { + get { return false; } + } + + override public int Type + { + get + { + return Runtime.Token.INVALID_TOKEN_TYPE; + } + } + + override public string Text + { + get + { + string badText = null; + if (start is IToken) { + int i = ((IToken)start).TokenIndex; + int j = ((IToken)stop).TokenIndex; + if ( ((IToken)stop).Type == Runtime.Token.EOF ) { + j = ((ITokenStream)input).Count; + } + badText = ((ITokenStream)input).ToString(i, j); + } + else if (start is ITree) { + badText = ((ITreeNodeStream)input).ToString(start, stop); + } + else { + // people should subclass if they alter the tree type so this + // next one is for sure correct. + badText = "<unknown>"; + } + return badText; + } + } + + public override string ToString() { + if (trappedException is MissingTokenException) { + return "<missing type: " + + ((MissingTokenException)trappedException).MissingType + + ">"; + } + else if (trappedException is UnwantedTokenException) { + return "<extraneous: " + + ((UnwantedTokenException)trappedException).UnexpectedToken + + ", resync=" + Text + ">"; + } + else if (trappedException is MismatchedTokenException) { + return "<mismatched token: " + trappedException.Token + ", resync=" + Text + ">"; + } + else if (trappedException is NoViableAltException) { + return "<unexpected: " + trappedException.Token + + ", resync=" + Text + ">"; + } + return "<error: " + Text + ">"; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/CommonTree.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/CommonTree.cs new file mode 100644 index 0000000..8403be0 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/CommonTree.cs @@ -0,0 +1,233 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + using IToken = Antlr.Runtime.IToken; + + /// <summary>A tree node that is wrapper for a Token object. </summary> + /// <remarks> + /// After 3.0 release while building tree rewrite stuff, it became clear + /// that computing parent and child index is very difficult and cumbersome. + /// Better to spend the space in every tree node. If you don't want these + /// extra fields, it's easy to cut them out in your own BaseTree subclass. + /// </remarks> + [Serializable] + public class CommonTree : BaseTree + { + public CommonTree() + { + } + + public CommonTree(CommonTree node) + : base(node) + { + this.token = node.token; + this.startIndex = node.startIndex; + this.stopIndex = node.stopIndex; + } + + public CommonTree(IToken t) + { + this.token = t; + } + + virtual public IToken Token + { + get { return token; } + } + + override public bool IsNil + { + get { return token == null; } + } + + override public int Type + { + get + { + if (token == null) + { + return Runtime.Token.INVALID_TOKEN_TYPE; + } + return token.Type; + } + } + + override public string Text + { + get + { + if (token == null) + { + return null; + } + return token.Text; + } + } + + override public int Line + { + get + { + if (token == null || token.Line == 0) + { + if (ChildCount > 0) + { + return GetChild(0).Line; + } + return 0; + } + return token.Line; + } + } + + override public int CharPositionInLine + { + get + { + if (token == null || token.CharPositionInLine == - 1) + { + if (ChildCount > 0) + { + return GetChild(0).CharPositionInLine; + } + return 0; + } + return token.CharPositionInLine; + } + } + + override public int TokenStartIndex + { + get + { + if ( (startIndex == -1) && (token != null) ) + { + return token.TokenIndex; + } + return startIndex; + } + + set { startIndex = value; } + } + + override public int TokenStopIndex + { + get + { + if ( (stopIndex == -1) && (token != null) ) + { + return token.TokenIndex; + } + return stopIndex; + } + + set { stopIndex = value; } + } + + /// <summary> + /// For every node in this subtree, make sure it's start/stop token's + /// are set. Walk depth first, visit bottom up. Only updates nodes + /// with at least one token index < 0. + /// </summary> + public void SetUnknownTokenBoundaries() { + if ( children==null ) { + if ( startIndex<0 || stopIndex<0 ) { + startIndex = stopIndex = token.TokenIndex; + } + return; + } + for (int i=0; i<children.Count; i++) { + ((CommonTree)children[i]).SetUnknownTokenBoundaries(); + } + if ( startIndex>=0 && stopIndex>=0 ) return; // already set + if ( children.Count > 0 ) { + CommonTree firstChild = (CommonTree)children[0]; + CommonTree lastChild = (CommonTree)children[children.Count-1]; + startIndex = firstChild.TokenStartIndex; + stopIndex = lastChild.TokenStopIndex; + } + } + + override public int ChildIndex + { + get { return childIndex; } + set { childIndex = value; } + } + + override public ITree Parent + { + get { return parent; } + set { parent = (CommonTree)value; } + } + + /// <summary> + /// What token indexes bracket all tokens associated with this node + /// and below? + /// </summary> + public int startIndex = -1, stopIndex = -1; + + /// <summary>A single token is the payload </summary> + protected IToken token; + + /// <summary>Who is the parent node of this node; if null, implies node is root</summary> + public CommonTree parent; + + /// <summary>What index is this node in the child list? Range: 0..n-1</summary> + public int childIndex = -1; + + public override ITree DupNode() + { + return new CommonTree(this); + } + + public override string ToString() + { + if (IsNil) + { + return "nil"; + } + if ( Type == Runtime.Token.INVALID_TOKEN_TYPE ) { + return "<errornode>"; + } + if (token == null) { + return null; + } + return token.Text; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/CommonTreeAdaptor.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/CommonTreeAdaptor.cs new file mode 100644 index 0000000..ce6f98d --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/CommonTreeAdaptor.cs @@ -0,0 +1,239 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + using CommonToken = Antlr.Runtime.CommonToken; + using IToken = Antlr.Runtime.IToken; + + /// <summary> + /// A TreeAdaptor that works with any Tree implementation. It provides + /// really just factory methods; all the work is done by BaseTreeAdaptor. + /// If you would like to have different tokens created than ClassicToken + /// objects, you need to override this and then set the parser tree adaptor to + /// use your subclass. + /// + /// To get your parser to build nodes of a different type, override + /// Create(Token), ErrorNode(), and to be safe, YourTreeClass.DupNode(). + /// DupNode() is called to duplicate nodes during rewrite operations. + /// </summary> + public class CommonTreeAdaptor : BaseTreeAdaptor + { + /// <summary> + /// Duplicate a node. This is part of the factory; + /// override if you want another kind of node to be built. + /// + /// I could use reflection to prevent having to override this + /// but reflection is slow. + /// </summary> + public override object DupNode(object t) + { + if (t == null) + { + return null; + } + return ((ITree)t).DupNode(); + } + + public override object Create(IToken payload) + { + return new CommonTree(payload); + } + + /// <summary>Create an imaginary token from a type and text </summary> + /// <remarks> + /// Tell me how to create a token for use with imaginary token nodes. + /// For example, there is probably no input symbol associated with imaginary + /// token DECL, but you need to create it as a payload or whatever for + /// the DECL node as in ^(DECL type ID). + /// + /// If you care what the token payload objects' type is, you should + /// override this method and any other createToken variant. + /// </remarks> + public override IToken CreateToken(int tokenType, string text) + { + return new CommonToken(tokenType, text); + } + + /// <summary>Create an imaginary token, copying the contents of a previous token </summary> + /// <remarks> + /// Tell me how to create a token for use with imaginary token nodes. + /// For example, there is probably no input symbol associated with imaginary + /// token DECL, but you need to create it as a payload or whatever for + /// the DECL node as in ^(DECL type ID). + /// + /// This is a variant of createToken where the new token is derived from + /// an actual real input token. Typically this is for converting '{' + /// tokens to BLOCK etc... You'll see + /// + /// r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ; + /// + /// If you care what the token payload objects' type is, you should + /// override this method and any other createToken variant. + /// </remarks> + public override IToken CreateToken(IToken fromToken) + { + return new CommonToken(fromToken); + } + + /// <summary>track start/stop token for subtree root created for a rule </summary> + /// <remarks> + /// Track start/stop token for subtree root created for a rule. + /// Only works with Tree nodes. For rules that match nothing, + /// seems like this will yield start=i and stop=i-1 in a nil node. + /// Might be useful info so I'll not force to be i..i. + /// </remarks> + public override void SetTokenBoundaries(object t, IToken startToken, IToken stopToken) + { + if (t == null) + { + return ; + } + + int start = 0; + int stop = 0; + if (startToken != null) + { + start = startToken.TokenIndex; + } + if (stopToken != null) + { + stop = stopToken.TokenIndex; + } + ((ITree) t).TokenStartIndex = start; + ((ITree) t).TokenStopIndex = stop; + } + + override public int GetTokenStartIndex(object t) + { + if (t == null) + { + return -1; + } + return ((ITree)t).TokenStartIndex; + } + + override public int GetTokenStopIndex(object t) + { + if (t == null) + { + return -1; + } + return ((ITree)t).TokenStopIndex; + } + + override public string GetNodeText(object t) + { + if (t == null) + { + return null; + } + return ((ITree)t).Text; + } + + override public int GetNodeType(object t) + { + if (t == null) + { + return Token.INVALID_TOKEN_TYPE; + } + return ((ITree)t).Type; + } + + /// <summary> + /// What is the Token associated with this node? + /// </summary> + /// <remarks> + /// If you are not using CommonTree, then you must override this in your + /// own adaptor. + /// </remarks> + override public IToken GetToken(object treeNode) + { + if ( treeNode is CommonTree ) + { + return ((CommonTree)treeNode).Token; + } + return null; // no idea what to do + } + + override public object GetChild(object t, int i) + { + if (t == null) + { + return null; + } + return ((ITree)t).GetChild(i); + } + + override public int GetChildCount(object t) + { + if (t == null) + { + return 0; + } + return ((ITree)t).ChildCount; + } + + override public object GetParent(object t) + { + if ( t==null ) return null; + return ((ITree)t).Parent; + } + + override public void SetParent(object t, object parent) + { + if ( t==null ) ((ITree)t).Parent = (ITree)parent; + } + + override public int GetChildIndex(object t) + { + if ( t==null ) return 0; + return ((ITree)t).ChildIndex; + } + + override public void SetChildIndex(object t, int index) + { + if ( t==null ) ((ITree)t).ChildIndex = index; + } + + override public void ReplaceChildren(object parent, int startChildIndex, int stopChildIndex, object t) + { + if (parent != null) + { + ((ITree)parent).ReplaceChildren(startChildIndex, stopChildIndex, t); + } + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/CommonTreeNodeStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/CommonTreeNodeStream.cs new file mode 100644 index 0000000..a1ff3e3 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/CommonTreeNodeStream.cs @@ -0,0 +1,663 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + using IEnumerable = System.Collections.IEnumerable; + using IEnumerator = System.Collections.IEnumerator; + using IDictionary = System.Collections.IDictionary; + using IList = System.Collections.IList; + using ArrayList = System.Collections.ArrayList; + using Hashtable = System.Collections.Hashtable; + using StringBuilder = System.Text.StringBuilder; + using StackList = Antlr.Runtime.Collections.StackList; + using Token = Antlr.Runtime.Token; + using ITokenStream = Antlr.Runtime.ITokenStream; + + /// <summary> + /// A buffered stream of tree nodes. Nodes can be from a tree of ANY kind. + /// </summary> + /// <remarks> + /// This node stream sucks all nodes out of the tree specified in the + /// constructor during construction and makes pointers into the tree + /// using an array of Object pointers. The stream necessarily includes + /// pointers to DOWN and UP and EOF nodes. + /// + /// This stream knows how to mark/release for backtracking. + /// + /// This stream is most suitable for tree interpreters that need to + /// jump around a lot or for tree parsers requiring speed (at cost of memory). + /// There is some duplicated functionality here with UnBufferedTreeNodeStream + /// but just in bookkeeping, not tree walking etc... + /// + /// <see cref="UnBufferedTreeNodeStream"/> + /// + /// </remarks> + public class CommonTreeNodeStream : ITreeNodeStream, IEnumerable + { + public const int DEFAULT_INITIAL_BUFFER_SIZE = 100; + public const int INITIAL_CALL_STACK_SIZE = 10; + + #region Helper classes + protected sealed class CommonTreeNodeStreamEnumerator : IEnumerator + { + private CommonTreeNodeStream _nodeStream; + private int _index; + private object _currentItem; + + #region Constructors + + internal CommonTreeNodeStreamEnumerator() + { + } + + internal CommonTreeNodeStreamEnumerator(CommonTreeNodeStream nodeStream) + { + _nodeStream = nodeStream; + Reset(); + } + + #endregion + + #region IEnumerator Members + + public void Reset() + { +// if (_version != _hashList._version) +// { +// throw new InvalidOperationException("Collection was modified; enumeration operation may not execute."); +// } + _index = 0; + _currentItem = null; + } + + public object Current + { + get + { + if (_currentItem == null) + { + throw new InvalidOperationException("Enumeration has either not started or has already finished."); + } + return _currentItem; + } + } + + public bool MoveNext() + { +// if (_version != _hashList._version) +// { +// throw new InvalidOperationException("Collection was modified; enumeration operation may not execute."); +// } + + if ( _index >= _nodeStream.nodes.Count ) + { + int current = _index; + _index++; + if ( current < _nodeStream.nodes.Count ) + { + _currentItem = _nodeStream.nodes[current]; + } + _currentItem = _nodeStream.eof; + return true; + } + _currentItem = null; + return false; + } + + #endregion + } + + #endregion + + #region IEnumerable Members + + public IEnumerator GetEnumerator() + { + if ( p == -1 ) + { + FillBuffer(); + } + return new CommonTreeNodeStreamEnumerator(this); + } + + #endregion + + #region Data Members + // all these navigation nodes are shared and hence they + // cannot contain any line/column info + + protected object down; + protected object up; + protected object eof; + + /// <summary> + /// The complete mapping from stream index to tree node. This buffer + /// includes pointers to DOWN, UP, and EOF nodes. + /// + /// It is built upon ctor invocation. The elements are type Object + /// as we don't what the trees look like. Load upon first need of + /// the buffer so we can set token types of interest for reverseIndexing. + /// Slows us down a wee bit to do all of the if p==-1 testing everywhere though. + /// </summary> + protected IList nodes; + + /// <summary>Pull nodes from which tree? </summary> + protected internal object root; + + /// <summary>IF this tree (root) was created from a token stream, track it</summary> + protected ITokenStream tokens; + + /// <summary>What tree adaptor was used to build these trees</summary> + ITreeAdaptor adaptor; + + /// <summary> + /// Reuse same DOWN, UP navigation nodes unless this is true + /// </summary> + protected bool uniqueNavigationNodes = false; + + /// <summary> + /// The index into the nodes list of the current node (next node + /// to consume). If -1, nodes array not filled yet. + /// </summary> + protected int p = -1; + + /// <summary> + /// Track the last mark() call result value for use in rewind(). + /// </summary> + protected int lastMarker; + + /// <summary> + /// Stack of indexes used for push/pop calls + /// </summary> + protected StackList calls; + + #endregion + + #region Constructors + + public CommonTreeNodeStream(object tree) + : this(new CommonTreeAdaptor(), tree) + { + } + + public CommonTreeNodeStream(ITreeAdaptor adaptor, object tree) + : this(adaptor, tree, DEFAULT_INITIAL_BUFFER_SIZE) + { + } + + public CommonTreeNodeStream(ITreeAdaptor adaptor, object tree, int initialBufferSize) + { + this.root = tree; + this.adaptor = adaptor; + nodes = new ArrayList(initialBufferSize); + down = adaptor.Create(Token.DOWN, "DOWN"); + up = adaptor.Create(Token.UP, "UP"); + eof = adaptor.Create(Token.EOF, "EOF"); + } + + #endregion + + #region Public API + + /// <summary> + /// Walk tree with depth-first-search and fill nodes buffer. + /// Don't do DOWN, UP nodes if its a list (t is isNil). + /// </summary> + protected void FillBuffer() + { + FillBuffer(root); + p = 0; // buffer of nodes intialized now + } + + public void FillBuffer(object t) + { + bool nil = adaptor.IsNil(t); + if ( !nil ) + { + nodes.Add(t); // add this node + } + // add DOWN node if t has children + int n = adaptor.GetChildCount(t); + if ( !nil && (n > 0) ) + { + AddNavigationNode(Token.DOWN); + } + // and now add all its children + for (int c = 0; c < n; c++) + { + object child = adaptor.GetChild(t, c); + FillBuffer(child); + } + // add UP node if t has children + if ( !nil && (n > 0) ) + { + AddNavigationNode(Token.UP); + } + } + + /// <summary> + /// Returns the stream index for the spcified node in the range 0..n-1 or, + /// -1 if node not found. + /// </summary> + protected int GetNodeIndex(object node) + { + if ( p == -1 ) + { + FillBuffer(); + } + for (int i = 0; i < nodes.Count; i++) + { + object t = (object) nodes[i]; + if ( t == node ) + { + return i; + } + } + return -1; + } + + /// <summary> + /// As we flatten the tree, we use UP, DOWN nodes to represent + /// the tree structure. When debugging we need unique nodes + /// so instantiate new ones when uniqueNavigationNodes is true. + /// </summary> + protected void AddNavigationNode(int ttype) + { + object navNode = null; + if ( ttype == Token.DOWN ) + { + if ( HasUniqueNavigationNodes ) + { + navNode = adaptor.Create(Token.DOWN, "DOWN"); + } + else + { + navNode = down; + } + } + else + { + if ( HasUniqueNavigationNodes ) + { + navNode = adaptor.Create(Token.UP, "UP"); + } + else + { + navNode = up; + } + } + nodes.Add(navNode); + } + + public object Get(int i) + { + if ( p == -1 ) + { + FillBuffer(); + } + return nodes[i]; + } + + public object LT(int k) + { + if ( p == -1 ) + { + FillBuffer(); + } + if ( k == 0 ) + { + return null; + } + if ( k < 0 ) + { + return LB(-k); + } + if ( (p+k-1) >= nodes.Count ) + { + return eof; + } + return nodes[p+k-1]; + } + + public virtual object CurrentSymbol { + get { return LT(1); } + } + + /// <summary> + /// Look backwards k nodes + /// </summary> + protected object LB(int k) + { + if ( k == 0 ) + { + return null; + } + if ( (p-k) < 0 ) + { + return null; + } + return nodes[p-k]; + } + + /// <summary> + /// Where is this stream pulling nodes from? This is not the name, but + /// the object that provides node objects. + /// </summary> + virtual public object TreeSource + { + get { return root; } + } + + virtual public string SourceName { + get { return TokenStream.SourceName; } + } + + virtual public ITokenStream TokenStream + { + get { return tokens; } + set { this.tokens = value; } + } + + public ITreeAdaptor TreeAdaptor + { + get { return adaptor; } + set { adaptor = value; } + } + + public bool HasUniqueNavigationNodes + { + get { return uniqueNavigationNodes; } + set { uniqueNavigationNodes = value; } + } + + /// <summary> + /// Make stream jump to a new location, saving old location. + /// Switch back with pop(). + /// </summary> + public void Push(int index) + { + if ( calls == null ) + { + calls = new StackList(); + } + calls.Push(p); // save current index + Seek(index); + } + + /// <summary> + /// Seek back to previous index saved during last Push() call. + /// Return top of stack (return index). + /// </summary> + public int Pop() + { + int ret = (int)calls.Pop(); + Seek(ret); + return ret; + } + + public void Reset() + { + p = -1; + lastMarker = 0; + if (calls != null) + { + calls.Clear(); + } + } + + #endregion + + #region Tree Rewrite Interface + + public void ReplaceChildren(object parent, int startChildIndex, int stopChildIndex, object t) + { + if (parent != null) + { + adaptor.ReplaceChildren(parent, startChildIndex, stopChildIndex, t); + } + } + + #endregion + + #region Satisfy IntStream interface + + public virtual void Consume() + { + if (p == -1) + { + FillBuffer(); + } + p++; + } + + public virtual int LA(int i) + { + return adaptor.GetNodeType( LT(i) ); + } + + /// <summary> + /// Record the current state of the tree walk which includes + /// the current node and stack state. + /// </summary> + public virtual int Mark() + { + if ( p == -1 ) + { + FillBuffer(); + } + lastMarker = Index(); + return lastMarker; + } + + public virtual void Release(int marker) + { + // no resources to release + } + + /// <summary> + /// Rewind the current state of the tree walk to the state it + /// was in when Mark() was called and it returned marker. Also, + /// wipe out the lookahead which will force reloading a few nodes + /// but it is better than making a copy of the lookahead buffer + /// upon Mark(). + /// </summary> + public virtual void Rewind(int marker) + { + Seek(marker); + } + + public void Rewind() + { + Seek(lastMarker); + } + + /// <summary> + /// Consume() ahead until we hit index. Can't just jump ahead--must + /// spit out the navigation nodes. + /// </summary> + public virtual void Seek(int index) + { + if ( p == -1 ) + { + FillBuffer(); + } + p = index; + } + + public virtual int Index() + { + return p; + } + + /// <summary> + /// Expensive to compute so I won't bother doing the right thing. + /// This method only returns how much input has been seen so far. So + /// after parsing it returns true size. + /// </summary> + [Obsolete("Please use property Count instead.")] + public virtual int Size() + { + return Count; + } + + /// <summary> + /// Expensive to compute so I won't bother doing the right thing. + /// This method only returns how much input has been seen so far. So + /// after parsing it returns true size. + /// </summary> + public virtual int Count + { + get + { + if ( p == -1 ) + { + FillBuffer(); + } + return nodes.Count; + } + } + + #endregion + + /// <summary> + /// Used for testing, just return the token type stream + /// </summary> + public override string ToString() + { + if ( p == -1 ) + { + FillBuffer(); + } + StringBuilder buf = new StringBuilder(); + for (int i = 0; i < nodes.Count; i++) + { + object t = (object) nodes[i]; + buf.Append(" "); + buf.Append(adaptor.GetNodeType(t)); + } + return buf.ToString(); + } + + /** Debugging */ + public String ToTokenString(int start, int stop) { + if ( p==-1 ) { + FillBuffer(); + } + StringBuilder buf = new StringBuilder(); + for (int i = start; i < nodes.Count && i <= stop; i++) { + Object t = (Object) nodes[i]; + buf.Append(" "); + buf.Append(adaptor.GetToken(t)); + } + return buf.ToString(); + } + + public virtual string ToString(object start, object stop) + { + Console.Out.WriteLine("ToString"); + if ( (start == null) || (stop == null) ) + { + return null; + } + if ( p == -1 ) + { + FillBuffer(); + } + //Console.Out.WriteLine("stop: " + stop); + if ( start is CommonTree ) + Console.Out.Write("ToString: " + ((CommonTree)start).Token + ", "); + else + Console.Out.WriteLine(start); + if ( stop is CommonTree ) + Console.Out.WriteLine(((CommonTree)stop).Token); + else + Console.Out.WriteLine(stop); + // if we have the token stream, use that to dump text in order + if ( tokens != null ) + { + int beginTokenIndex = adaptor.GetTokenStartIndex(start); + int endTokenIndex = adaptor.GetTokenStopIndex(stop); + // if it's a tree, use start/stop index from start node + // else use token range from start/stop nodes + if ( adaptor.GetNodeType(stop) == Token.UP ) + { + endTokenIndex = adaptor.GetTokenStopIndex(start); + } + else if ( adaptor.GetNodeType(stop) == Token.EOF ) + { + endTokenIndex = Count-2; // don't use EOF + } + return tokens.ToString(beginTokenIndex, endTokenIndex); + } + // walk nodes looking for start + object t = null; + int i = 0; + for (; i < nodes.Count; i++) + { + t = nodes[i]; + if ( t == start ) + { + break; + } + } + // now walk until we see stop, filling string buffer with text + StringBuilder buf = new StringBuilder(); + t = nodes[i]; + string text; + while ( t != stop ) + { + text = adaptor.GetNodeText(t); + if ( text == null ) + { + text = " " + adaptor.GetNodeType(t); + } + buf.Append(text); + i++; + t = nodes[i]; + } + // include stop node too + text = adaptor.GetNodeText(stop); + if ( text == null ) + { + text = " " + adaptor.GetNodeType(stop); + } + buf.Append(text); + return buf.ToString(); + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/ITree.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/ITree.cs new file mode 100644 index 0000000..a49278f --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/ITree.cs @@ -0,0 +1,182 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + using System.Collections; + using Token = Antlr.Runtime.Token; + + /// <summary> + /// What does a tree look like? ANTLR has a number of support classes + /// such as CommonTreeNodeStream that work on these kinds of trees. You + /// don't have to make your trees implement this interface, but if you do, + /// you'll be able to use more support code. + /// + /// NOTE: When constructing trees, ANTLR can build any kind of tree; it can + /// even use Token objects as trees if you add a child list to your tokens. + /// + /// This is a tree node without any payload; just navigation and factory stuff. + /// </summary> + public interface ITree + { + int ChildCount + { + get; + + } + + // Tree tracks parent and child index now > 3.0 + + ITree Parent + { + get; + set; + } + + /// <summary> + /// Is there is a node above with token type ttype? + /// </summary> + bool HasAncestor(int ttype); + + /// <summary> + /// Walk upwards and get first ancestor with this token type. + /// </summary> + /// <param name="ttype"> + /// A <see cref="System.Int32"/> + /// </param> + /// <returns> + /// A <see cref="ITree"/> + /// </returns> + ITree GetAncestor(int ttype); + + /// <summary> + /// Return a list of all ancestors of this node. The first node of + /// list is the root and the last is the parent of this node. + /// </summary> + /// <returns> + /// A <see cref="IList"/> + /// </returns> + IList GetAncestors(); + + /// <summary>This node is what child index? 0..n-1</summary> + int ChildIndex + { + get; + set; + } + + /// <summary>Set (or reset) the parent and child index values for all children</summary> + void FreshenParentAndChildIndexes(); + + /// <summary> + /// Indicates the node is a nil node but may still have children, meaning + /// the tree is a flat list. + /// </summary> + bool IsNil + { + get; + + } + /// <summary>Return a token type; needed for tree parsing </summary> + int Type + { + get; + + } + + string Text + { + get; + + } + + /// <summary>In case we don't have a token payload, what is the line for errors? </summary> + int Line + { + get; + + } + int CharPositionInLine + { + get; + + } + + ITree GetChild(int i); + + /// <summary> + /// Add t as a child to this node. If t is null, do nothing. If t + /// is nil, add all children of t to this' children. + /// </summary> + /// <param name="t">Tree to add</param> + void AddChild(ITree t); + + /// <summary>Set ith child (0..n-1) to t; t must be non-null and non-nil node</summary> + void SetChild(int i, ITree t); + + object DeleteChild(int i); + + /// <summary> + /// Delete children from start to stop and replace with t even if t is + /// a list (nil-root tree). num of children can increase or decrease. + /// For huge child lists, inserting children can force walking rest of + /// children to set their childindex; could be slow. + /// </summary> + void ReplaceChildren(int startChildIndex, int stopChildIndex, object t); + + /// <summary> + /// What is the smallest token index (indexing from 0) for this node + /// and its children? + /// </summary> + int TokenStartIndex { get; set; } + + /// <summary> + /// What is the largest token index (indexing from 0) for this node + /// and its children? + /// </summary> + int TokenStopIndex { get; set; } + + ITree DupNode(); + + string ToStringTree(); + + string ToString(); + } + + public sealed class Tree + { + public readonly static ITree INVALID_NODE = new CommonTree(Token.INVALID_TOKEN); + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/ITreeAdaptor.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/ITreeAdaptor.cs new file mode 100644 index 0000000..4500b95 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/ITreeAdaptor.cs @@ -0,0 +1,323 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + using IToken = Antlr.Runtime.IToken; + + /// <summary> + /// How to create and navigate trees. Rather than have a separate factory + /// and adaptor, I've merged them. Makes sense to encapsulate. + /// + /// This takes the place of the tree construction code generated in the + /// generated code in 2.x and the ASTFactory. + /// + /// I do not need to know the type of a tree at all so they are all + /// generic Objects. This may increase the amount of typecasting needed. :( + /// </summary> + public interface ITreeAdaptor + { + // C o n s t r u c t i o n + + /// <summary> + /// Create a tree node from Token object; for CommonTree type trees, + /// then the token just becomes the payload. + /// </summary> + /// <remarks> + /// This is the most common create call. Override if you want another kind of node to be built. + /// </remarks> + object Create(IToken payload); + + /// <summary>Duplicate a single tree node </summary> + /// <remarks> Override if you want another kind of node to be built.</remarks> + object DupNode(object treeNode); + + /// <summary>Duplicate tree recursively, using DupNode() for each node </summary> + object DupTree(object tree); + + /// <summary> + /// Return a nil node (an empty but non-null node) that can hold + /// a list of element as the children. If you want a flat tree (a list) + /// use "t=adaptor.nil(); t.AddChild(x); t.AddChild(y);" + /// </summary> + object GetNilNode(); + + /// <summary> + /// Return a tree node representing an error. This node records the + /// tokens consumed during error recovery. The start token indicates the + /// input symbol at which the error was detected. The stop token indicates + /// the last symbol consumed during recovery. + /// </summary> + /// <remarks> + /// <para>You must specify the input stream so that the erroneous text can + /// be packaged up in the error node. The exception could be useful + /// to some applications; default implementation stores ptr to it in + /// the CommonErrorNode.</para> + /// + /// <para>This only makes sense during token parsing, not tree parsing. + /// Tree parsing should happen only when parsing and tree construction + /// succeed.</para> + /// </remarks> + object ErrorNode(ITokenStream input, IToken start, IToken stop, RecognitionException e); + + /// <summary> + /// Is tree considered a nil node used to make lists of child nodes? + /// </summary> + bool IsNil(object tree); + + /// <summary> + /// Add a child to the tree t. If child is a flat tree (a list), make all + /// in list children of t. + /// </summary> + /// <remarks> + /// <para> + /// Warning: if t has no children, but child does and child isNil then you + /// can decide it is ok to move children to t via t.children = child.children; + /// i.e., without copying the array. Just make sure that this is consistent + /// with have the user will build ASTs. Do nothing if t or child is null. + /// </para> + /// <para> + /// This is for construction and I'm not sure it's completely general for + /// a tree's addChild method to work this way. Make sure you differentiate + /// between your tree's addChild and this parser tree construction addChild + /// if it's not ok to move children to t with a simple assignment. + /// </para> + /// </remarks> + void AddChild(object t, object child); + + /// <summary> + /// If oldRoot is a nil root, just copy or move the children to newRoot. + /// If not a nil root, make oldRoot a child of newRoot. + /// </summary> + /// <remarks> + /// + /// old=^(nil a b c), new=r yields ^(r a b c) + /// old=^(a b c), new=r yields ^(r ^(a b c)) + /// + /// If newRoot is a nil-rooted single child tree, use the single + /// child as the new root node. + /// + /// old=^(nil a b c), new=^(nil r) yields ^(r a b c) + /// old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) + /// + /// If oldRoot was null, it's ok, just return newRoot (even if isNil). + /// + /// old=null, new=r yields r + /// old=null, new=^(nil r) yields ^(nil r) + /// + /// Return newRoot. Throw an exception if newRoot is not a + /// simple node or nil root with a single child node--it must be a root + /// node. If newRoot is ^(nil x) return x as newRoot. + /// + /// Be advised that it's ok for newRoot to point at oldRoot's + /// children; i.e., you don't have to copy the list. We are + /// constructing these nodes so we should have this control for + /// efficiency. + /// </remarks> + object BecomeRoot(object newRoot, object oldRoot); + + /// <summary> + /// Given the root of the subtree created for this rule, post process + /// it to do any simplifications or whatever you want. A required + /// behavior is to convert ^(nil singleSubtree) to singleSubtree + /// as the setting of start/stop indexes relies on a single non-nil root + /// for non-flat trees. + /// + /// Flat trees such as for lists like "idlist : ID+ ;" are left alone + /// unless there is only one ID. For a list, the start/stop indexes + /// are set in the nil node. + /// + /// This method is executed after all rule tree construction and right + /// before SetTokenBoundaries(). + /// </summary> + object RulePostProcessing(object root); + + /// <summary> + /// For identifying trees. How to identify nodes so we can say "add node + /// to a prior node"? + /// </summary> + /// <remarks> + /// Even BecomeRoot is an issue. Ok, we could: + /// <list type="number"> + /// <item>Number the nodes as they are created?</item> + /// <item> + /// Use the original framework assigned hashcode that's unique + /// across instances of a given type. + /// WARNING: This is usually implemented either as IL to make a + /// non-virt call to object.GetHashCode() or by via a call to + /// System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(). + /// Both have issues especially on .NET 1.x and Mono. + /// </item> + /// </list> + /// </remarks> + int GetUniqueID(object node); + + + #region R e w r i t e R u l e s + + /// <summary> + /// Create a node for newRoot make it the root of oldRoot. + /// If oldRoot is a nil root, just copy or move the children to newRoot. + /// If not a nil root, make oldRoot a child of newRoot. + /// + /// Return node created for newRoot. + /// </summary> + object BecomeRoot(IToken newRoot, object oldRoot); + + /// <summary>Create a new node derived from a token, with a new token type. + /// This is invoked from an imaginary node ref on right side of a + /// rewrite rule as IMAG[$tokenLabel]. + /// + /// This should invoke createToken(Token). + /// </summary> + object Create(int tokenType, IToken fromToken); + + /// <summary>Same as Create(tokenType,fromToken) except set the text too. + /// This is invoked from an imaginary node ref on right side of a + /// rewrite rule as IMAG[$tokenLabel, "IMAG"]. + /// + /// This should invoke createToken(Token). + /// </summary> + object Create(int tokenType, IToken fromToken, string text); + + /// <summary>Create a new node derived from a token, with a new token type. + /// This is invoked from an imaginary node ref on right side of a + /// rewrite rule as IMAG["IMAG"]. + /// + /// This should invoke createToken(int,String). + /// </summary> + object Create(int tokenType, string text); + + #endregion + + #region C o n t e n t + + /// <summary>For tree parsing, I need to know the token type of a node </summary> + int GetNodeType(object t); + + /// <summary>Node constructors can set the type of a node </summary> + void SetNodeType(object t, int type); + + string GetNodeText(object t); + + /// <summary>Node constructors can set the text of a node </summary> + void SetNodeText(object t, string text); + + + /// <summary> + /// Return the token object from which this node was created. + /// </summary> + /// <remarks> + /// Currently used only for printing an error message. The error + /// display routine in BaseRecognizer needs to display where the + /// input the error occurred. If your tree of limitation does not + /// store information that can lead you to the token, you can create + /// a token filled with the appropriate information and pass that back. + /// <see cref="BaseRecognizer.GetErrorMessage"/> + /// </remarks> + IToken GetToken(object treeNode); + + /// <summary> + /// Where are the bounds in the input token stream for this node and + /// all children? + /// </summary> + /// <remarks> + /// Each rule that creates AST nodes will call this + /// method right before returning. Flat trees (i.e., lists) will + /// still usually have a nil root node just to hold the children list. + /// That node would contain the start/stop indexes then. + /// </remarks> + void SetTokenBoundaries(object t, IToken startToken, IToken stopToken); + + /// <summary> + /// Get the token start index for this subtree; return -1 if no such index + /// </summary> + int GetTokenStartIndex(object t); + + /// <summary> + /// Get the token stop index for this subtree; return -1 if no such index + /// </summary> + int GetTokenStopIndex(object t); + + #endregion + + #region N a v i g a t i o n / T r e e P a r s i n g + + /// <summary>Get a child 0..n-1 node </summary> + object GetChild(object t, int i); + + /// <summary>Set ith child (0..n-1) to t; t must be non-null and non-nil node</summary> + void SetChild(object t, int i, object child); + + /// <summary>Remove ith child and shift children down from right.</summary> + object DeleteChild(object t, int i); + + /// <summary>How many children? If 0, then this is a leaf node </summary> + int GetChildCount(object t); + + /// <summary> + /// Who is the parent node of this node; if null, implies node is root. + /// </summary> + /// <remarks> + /// If your node type doesn't handle this, it's ok but the tree rewrites + /// in tree parsers need this functionality. + /// </remarks> + object GetParent(object t); + void SetParent(object t, object parent); + + /// <summary> + /// What index is this node in the child list? Range: 0..n-1 + /// </summary> + /// <remarks> + /// If your node type doesn't handle this, it's ok but the tree rewrites + /// in tree parsers need this functionality. + /// </remarks> + int GetChildIndex(object t); + void SetChildIndex(object t, int index); + + /// <summary> + /// Replace from start to stop child index of parent with t, which might + /// be a list. Number of children may be different after this call. + /// </summary> + /// <remarks> + /// If parent is null, don't do anything; must be at root of overall tree. + /// Can't replace whatever points to the parent externally. Do nothing. + /// </remarks> + void ReplaceChildren(object parent, int startChildIndex, int stopChildIndex, object t); + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/ITreeNodeStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/ITreeNodeStream.cs new file mode 100644 index 0000000..ca38e61 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/ITreeNodeStream.cs @@ -0,0 +1,138 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + using IIntStream = Antlr.Runtime.IIntStream; + using ITokenStream = Antlr.Runtime.ITokenStream; + + /// <summary>A stream of tree nodes, accessing nodes from a tree of some kind </summary> + public interface ITreeNodeStream : IIntStream + { + /// <summary>Get a tree node at an absolute index i; 0..n-1.</summary> + /// <remarks> + /// If you don't want to buffer up nodes, then this method makes no + /// sense for you. + /// </remarks> + object Get(int i); + + /// <summary> + /// Where is this stream pulling nodes from? This is not the name, but + /// the object that provides node objects. + /// + /// TODO: do we really need this? + /// </summary> + object TreeSource + { + get; + } + + /// <summary> + /// Get the ITokenStream from which this stream's Tree was created + /// (may be null) + /// </summary> + /// <remarks> + /// If the tree associated with this stream was created from a + /// TokenStream, you can specify it here. Used to do rule $text + /// attribute in tree parser. Optional unless you use tree parser + /// rule text attribute or output=template and rewrite=true options. + /// </remarks> + ITokenStream TokenStream + { + get; + } + + /// <summary> + /// What adaptor can tell me how to interpret/navigate nodes and trees. + /// E.g., get text of a node. + /// </summary> + ITreeAdaptor TreeAdaptor + { + get; + } + + /// <summary> + /// As we flatten the tree, we use UP, DOWN nodes to represent + /// the tree structure. When debugging we need unique nodes + /// so we have to instantiate new ones. When doing normal tree + /// parsing, it's slow and a waste of memory to create unique + /// navigation nodes. Default should be false; + /// </summary> + bool HasUniqueNavigationNodes + { + set; + } + + /// <summary> + /// Get tree node at current input pointer + i ahead where i=1 is next node. + /// i<0 indicates nodes in the past. So LT(-1) is previous node, but + /// implementations are not required to provide results for k < -1. + /// LT(0) is undefined. For i>=n, return null. + /// Return null for LT(0) and any index that results in an absolute address + /// that is negative. + /// + /// This is analogus to the LT() method of the TokenStream, but this + /// returns a tree node instead of a token. Makes code gen identical + /// for both parser and tree grammars. :) + /// </summary> + object LT(int k); + + /// <summary>Return the text of all nodes from start to stop, inclusive. + /// If the stream does not buffer all the nodes then it can still + /// walk recursively from start until stop. You can always return + /// null or "" too, but users should not access $ruleLabel.text in + /// an action of course in that case. + /// </summary> + string ToString(object start, object stop); + + #region REWRITING TREES (used by tree parser) + + /// <summary> + /// Replace from start to stop child index of parent with t, which might + /// be a list. Number of children may be different after this call. + /// </summary> + /// <remarks> + /// The stream is notified because it is walking the tree and might need + /// to know you are monkeying with the underlying tree. Also, it might be + /// able to modify the node stream to avoid restreaming for future phases. + /// + /// If parent is null, don't do anything; must be at root of overall tree. + /// Can't replace whatever points to the parent externally. Do nothing. + /// </remarks> + void ReplaceChildren(object parent, int startChildIndex, int stopChildIndex, object t); + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/ITreeVisitorAction.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/ITreeVisitorAction.cs new file mode 100644 index 0000000..382ef9c --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/ITreeVisitorAction.cs @@ -0,0 +1,22 @@ +namespace Antlr.Runtime.Tree +{ + /// <summary> + /// How to execute code for node t when a visitor visits node t. Execute + /// Pre() before visiting children and execute Post() after visiting children. + /// </summary> + public interface ITreeVisitorAction + { + /// <summary> + /// Execute an action before visiting children of t. Return t or + /// a rewritten t. Children of returned value will be visited. + /// </summary> + object Pre(object t); + + /// <summary> + /// Execute an action after visiting children of t. Return t or + /// a rewritten t. It is up to the visitor to decide what to do + /// with the return value. + /// </summary> + object Post(object t); + } +} diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/ParseTree.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/ParseTree.cs new file mode 100644 index 0000000..98a5034 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/ParseTree.cs @@ -0,0 +1,136 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +Copyright (c) 2007-2008 Johannes Luber +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + using System.Collections; + using System.Text; + using IToken = Antlr.Runtime.IToken; + + /// <summary> + /// A record of the rules used to Match a token sequence. The tokens + /// end up as the leaves of this tree and rule nodes are the interior nodes. + /// This really adds no functionality, it is just an alias for CommonTree + /// that is more meaningful (specific) and holds a String to display for a node. + /// </summary> + public class ParseTree : BaseTree + { + public object payload; + public IList hiddenTokens; + + public ParseTree(object label) + { + this.payload = label; + } + + override public int Type + { + get { return 0; } + } + + override public string Text + { + get { return ToString(); } + } + + override public int TokenStartIndex + { + get { return 0; } + set { ; } + } + + override public int TokenStopIndex + { + get { return 0; } + set { ; } + } + + public override ITree DupNode() + { + return null; + } + + public override string ToString() + { + if ( payload is IToken ) + { + IToken t = (IToken)payload; + if ( t.Type == Token.EOF ) + { + return "<EOF>"; + } + return t.Text; + } + return payload.ToString(); + } + + /** Emit a token and all hidden nodes before. EOF node holds all + * hidden tokens after last real token. + */ + public string ToStringWithHiddenTokens() { + StringBuilder buf = new StringBuilder(); + if ( hiddenTokens!=null ) { + for (int i = 0; i < hiddenTokens.Count; i++) { + IToken hidden = (IToken) hiddenTokens[i]; + buf.Append(hidden.Text); + } + } + String nodeText = this.ToString(); + if ( nodeText != "<EOF>" ) buf.Append(nodeText); + return buf.ToString(); + } + + /** Print out the leaves of this tree, which means printing original + * input back out. + */ + public string ToInputString() { + StringBuilder buf = new StringBuilder(); + _ToStringLeaves(buf); + return buf.ToString(); + } + + public void _ToStringLeaves(StringBuilder buf) { + if ( payload is IToken ) { // leaf node token? + buf.Append(this.ToStringWithHiddenTokens()); + return; + } + for (int i = 0; children!=null && i < children.Count; i++) { + ParseTree t = (ParseTree)children[i]; + t._ToStringLeaves(buf); + } + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteCardinalityException.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteCardinalityException.cs new file mode 100644 index 0000000..ab8f0ef --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteCardinalityException.cs @@ -0,0 +1,81 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + + /// <summary>Base class for all exceptions thrown during AST rewrite construction.</summary> + /// <remarks> + /// This signifies a case where the cardinality of two or more elements + /// in a subrule are different: (ID INT)+ where |ID|!=|INT| + /// </remarks> + [Serializable] + public class RewriteCardinalityException : Exception + { + #region Constructors + + public RewriteCardinalityException(string elementDescription) + { + this.elementDescription = elementDescription; + } + + #endregion + + #region Public API + + /// <summary> + /// Returns the line at which the error occurred (for lexers) + /// </summary> + override public string Message + { + get + { + if ( elementDescription != null ) + { + return elementDescription; + } + return null; + } + } + + #endregion + + #region Data Members + + public string elementDescription; + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteEarlyExitException.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteEarlyExitException.cs new file mode 100644 index 0000000..2c49468 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteEarlyExitException.cs @@ -0,0 +1,55 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + + /// <summary> + /// No elements within a (...)+ in a rewrite rule + /// </summary> + [Serializable] + public class RewriteEarlyExitException : RewriteCardinalityException + { + public RewriteEarlyExitException() + : base(null) + { + } + + public RewriteEarlyExitException(string elementDescription) + : base(elementDescription) + { + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteEmptyStreamException.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteEmptyStreamException.cs new file mode 100644 index 0000000..6dc2933 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteEmptyStreamException.cs @@ -0,0 +1,50 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + + /// <summary> + /// Ref to ID or expr but no tokens in ID stream or subtrees in expr stream + /// </summary> + [Serializable] + public class RewriteEmptyStreamException : RewriteCardinalityException + { + public RewriteEmptyStreamException(string elementDescription) + : base(elementDescription) + { + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteRuleElementStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteRuleElementStream.cs new file mode 100644 index 0000000..480381f --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteRuleElementStream.cs @@ -0,0 +1,466 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +Copyright (c) 2007 Johannes Luber +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +#if DOTNET1 +namespace Antlr.Runtime.Tree +{ + using System; + using IList = System.Collections.IList; + using ArrayList = System.Collections.ArrayList; + using IToken = Antlr.Runtime.IToken; + using CommonToken = Antlr.Runtime.CommonToken; + + /// <summary> + /// A generic list of elements tracked in an alternative to be used in + /// a -> rewrite rule. We need to subclass to fill in the next() method, + /// which returns either an AST node wrapped around a token payload or + /// an existing subtree. + /// + /// Once you start next()ing, do not try to add more elements. It will + /// break the cursor tracking I believe. + /// + /// <see cref="RewriteRuleSubtreeStream"/> + /// <see cref="RewriteRuleTokenStream"/> + /// + /// TODO: add mechanism to detect/puke on modification after reading from stream + /// </summary> + public abstract class RewriteRuleElementStream + { + /// <summary> + /// Cursor 0..n-1. If singleElement!=null, cursor is 0 until you next(), + /// which bumps it to 1 meaning no more elements. + /// </summary> + protected int cursor = 0; + + /// <summary> + /// Track single elements w/o creating a list. Upon 2nd add, alloc list + /// </summary> + protected object singleElement; + + /// <summary> + /// The list of tokens or subtrees we are tracking + /// </summary> + protected IList elements; + + /// <summary> + /// Tracks whether a node or subtree has been used in a stream + /// </summary> + /// <remarks> + /// Once a node or subtree has been used in a stream, it must be dup'd + /// from then on. Streams are reset after subrules so that the streams + /// can be reused in future subrules. So, reset must set a dirty bit. + /// If dirty, then next() always returns a dup. + /// </remarks> + protected bool dirty = false; + + /// <summary> + /// The element or stream description; usually has name of the token or + /// rule reference that this list tracks. Can include rulename too, but + /// the exception would track that info. + /// </summary> + protected string elementDescription; + protected ITreeAdaptor adaptor; + + public RewriteRuleElementStream(ITreeAdaptor adaptor, string elementDescription) + { + this.elementDescription = elementDescription; + this.adaptor = adaptor; + } + + /// <summary> + /// Create a stream with one element + /// </summary> + public RewriteRuleElementStream(ITreeAdaptor adaptor, string elementDescription, object oneElement) + : this(adaptor, elementDescription) + { + Add(oneElement); + } + + /// <summary> + /// Create a stream, but feed off an existing list + /// </summary> + public RewriteRuleElementStream(ITreeAdaptor adaptor, string elementDescription, IList elements) + : this(adaptor, elementDescription) + { + this.singleElement = null; + this.elements = elements; + } + + public void Add(object el) + { + if ( el == null ) + { + return; + } + if ( elements != null ) + { // if in list, just add + elements.Add(el); + return; + } + if ( singleElement == null ) + { // no elements yet, track w/o list + singleElement = el; + return; + } + // adding 2nd element, move to list + elements = new ArrayList(5); + elements.Add(singleElement); + singleElement = null; + elements.Add(el); + } + + /// <summary> + /// Reset the condition of this stream so that it appears we have + /// not consumed any of its elements. Elements themselves are untouched. + /// </summary> + /// <remarks> + /// Once we reset the stream, any future use will need duplicates. Set + /// the dirty bit. + /// </remarks> + public virtual void Reset() + { + cursor = 0; + dirty = true; + } + + public bool HasNext() + { + return ( ((singleElement != null) && (cursor < 1)) || + ((elements != null) && (cursor < elements.Count)) ); + } + + /// <summary> + /// Return the next element in the stream. + /// </summary> + /// <remarks> + /// If out of elements, throw an exception unless Count==1. + /// If Count is 1, then return elements[0]. + /// Return a duplicate node/subtree if stream is out of + /// elements and Count==1. + /// If we've already used the element, dup (dirty bit set). + /// </remarks> + public virtual object NextTree() + { + int size = Count; + if ( dirty || ((cursor >= size) && (size == 1)) ) + { + // if out of elements and size is 1, dup + return Dup(_Next()); + } + // test size above then fetch + return _Next(); + } + + /// <summary> + /// Do the work of getting the next element, making sure that + /// it's a tree node or subtree. + /// </summary> + /// <remarks> + /// Deal with the optimization of single-element list versus + /// list of size > 1. Throw an exception if the stream is + /// empty or we're out of elements and size>1. + /// </remarks> + protected object _Next() + { + int size = Count; + if ( size == 0 ) + { + throw new RewriteEmptyStreamException(elementDescription); + } + if ( cursor >= size ) + { // out of elements? + if ( size == 1 ) + { // if size is 1, it's ok; return and we'll dup + return ToTree(singleElement); + } + // out of elements and size was not 1, so we can't dup + throw new RewriteCardinalityException(elementDescription); + } + // we have elements + if ( singleElement != null ) + { + cursor++; // move cursor even for single element list + return ToTree(singleElement); + } + // must have more than one in list, pull from elements + return ToTree(elements[cursor++]); + } + + /// <summary> + /// When constructing trees, sometimes we need to dup a token or AST + /// subtree. Dup'ing a token means just creating another AST node + /// around it. For trees, you must call the adaptor.dupTree() + /// unless the element is for a tree root; then it must be a node dup + /// </summary> + protected abstract object Dup(object el); + + /// <summary> + /// Ensure stream emits trees; tokens must be converted to AST nodes. + /// AST nodes can be passed through unmolested. + /// </summary> + protected virtual object ToTree(object el) + { + return el; + } + +#warning Size() should be converted into a property named Count. + public int Size() + { + if (singleElement != null) { + return 1; + } + if (elements != null) { + return elements.Count; + } + return 0; + } + + public string Description + { + get { return elementDescription; } + } + } +} +#elif DOTNET2 +namespace Antlr.Runtime.Tree { + using System; + using System.Collections.Generic; + using IToken = Antlr.Runtime.IToken; + using CommonToken = Antlr.Runtime.CommonToken; + + /// <summary> + /// A generic list of elements tracked in an alternative to be used in + /// a -> rewrite rule. We need to subclass to fill in the next() method, + /// which returns either an AST node wrapped around a token payload or + /// an existing subtree. + /// + /// Once you start next()ing, do not try to add more elements. It will + /// break the cursor tracking I believe. + /// + /// <see cref="RewriteRuleSubtreeStream"/> + /// <see cref="RewriteRuleTokenStream"/> + /// + /// TODO: add mechanism to detect/puke on modification after reading from stream + /// </summary> + public abstract class RewriteRuleElementStream<T> { + /// <summary> + /// Cursor 0..n-1. If singleElement!=null, cursor is 0 until you next(), + /// which bumps it to 1 meaning no more elements. + /// </summary> + protected int cursor = 0; + + /// <summary> + /// Track single elements w/o creating a list. Upon 2nd add, alloc list + /// </summary> + protected T singleElement; + + /// <summary> + /// The list of tokens or subtrees we are tracking + /// </summary> + protected IList<T> elements; + + /// <summary> + /// Tracks whether a node or subtree has been used in a stream + /// </summary> + /// <remarks> + /// Once a node or subtree has been used in a stream, it must be dup'd + /// from then on. Streams are reset after subrules so that the streams + /// can be reused in future subrules. So, reset must set a dirty bit. + /// If dirty, then next() always returns a dup. + /// </remarks> + protected bool dirty = false; + + /// <summary> + /// The element or stream description; usually has name of the token or + /// rule reference that this list tracks. Can include rulename too, but + /// the exception would track that info. + /// </summary> + protected string elementDescription; + protected ITreeAdaptor adaptor; + + public RewriteRuleElementStream(ITreeAdaptor adaptor, string elementDescription) { + this.elementDescription = elementDescription; + this.adaptor = adaptor; + } + + /// <summary> + /// Create a stream with one element + /// </summary> + public RewriteRuleElementStream( + ITreeAdaptor adaptor, + string elementDescription, + T oneElement + ) + : this(adaptor, elementDescription) { + Add(oneElement); + } + + /// <summary> + /// Create a stream, but feed off an existing list + /// </summary> + public RewriteRuleElementStream( + ITreeAdaptor adaptor, + string elementDescription, + IList<T> elements + ) + : this(adaptor, elementDescription) { + this.singleElement = default(T); + this.elements = elements; + } + + /// <summary> + /// Create a stream, but feed off an existing list + /// </summary> + [Obsolete("This constructor is for internal use only and might be phased out soon. Use instead the one with IList<T>.")] + public RewriteRuleElementStream( + ITreeAdaptor adaptor, + string elementDescription, + System.Collections.IList elements + ) + : this(adaptor, elementDescription) { + this.singleElement = default(T); + this.elements = new List<T>(); + if (elements != null) { + foreach (T t in elements) { + this.elements.Add(t); + } + } + } + + public void Add(T el) { + if (el == null) { + return; + } + if (elements != null) { // if in list, just add + elements.Add(el); + return; + } + if (singleElement == null) { // no elements yet, track w/o list + singleElement = el; + return; + } + // adding 2nd element, move to list + elements = new List<T>(5); + elements.Add(singleElement); + singleElement = default(T); + elements.Add(el); + } + + /// <summary> + /// Reset the condition of this stream so that it appears we have + /// not consumed any of its elements. Elements themselves are untouched. + /// </summary> + /// <remarks> + /// Once we reset the stream, any future use will need duplicates. Set + /// the dirty bit. + /// </remarks> + public virtual void Reset() { + cursor = 0; + dirty = true; + } + + public bool HasNext() { + return (((singleElement != null) && (cursor < 1)) || + ((elements != null) && (cursor < elements.Count))); + } + + /// <summary> + /// Return the next element in the stream. + /// </summary> + public virtual object NextTree() { + return _Next(); + } + + /// <summary> + /// Do the work of getting the next element, making sure that + /// it's a tree node or subtree. + /// </summary> + /// <remarks> + /// Deal with the optimization of single-element list versus + /// list of size > 1. Throw an exception if the stream is + /// empty or we're out of elements and size>1. + /// </remarks> + protected object _Next() { + int size = Count; + if (size == 0) { + throw new RewriteEmptyStreamException(elementDescription); + } + if (cursor >= size) { // out of elements? + if (size == 1) { // if size is 1, it's ok; return and we'll dup + return ToTree(singleElement); + } + // out of elements and size was not 1, so we can't dup + throw new RewriteCardinalityException(elementDescription); + } + // we have elements + if (singleElement != null) { + cursor++; // move cursor even for single element list + return ToTree(singleElement); + } + // must have more than one in list, pull from elements + return ToTree(elements[cursor++]); + } + + /// <summary> + /// Ensure stream emits trees; tokens must be converted to AST nodes. + /// AST nodes can be passed through unmolested. + /// </summary> + protected virtual object ToTree(T el) { + return el; + } + + public int Count { + get { + if (singleElement != null) { + return 1; + } + if (elements != null) { + return elements.Count; + } + return 0; + } + } + + [Obsolete("Please use property Count instead.")] + public int Size() { + return Count; + } + + public string Description { + get { return elementDescription; } + } + } +} +#endif \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteRuleNodeStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteRuleNodeStream.cs new file mode 100644 index 0000000..33310c6 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteRuleNodeStream.cs @@ -0,0 +1,133 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +Copyright (c) 2007 Johannes Luber +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +#if DOTNET1 +namespace Antlr.Runtime.Tree +{ + using System; + using IList = System.Collections.IList; + + /// <summary> + /// Queues up nodes matched on left side of -> in a tree parser. This is + /// the analog of RewriteRuleTokenStream for normal parsers. + /// </summary> + public class RewriteRuleNodeStream : RewriteRuleElementStream + { + public RewriteRuleNodeStream(ITreeAdaptor adaptor, string elementDescription) + : base(adaptor, elementDescription) + { + } + + /// <summary>Create a stream with one element</summary> + public RewriteRuleNodeStream(ITreeAdaptor adaptor, string elementDescription, object oneElement) + : base(adaptor, elementDescription, oneElement) + { + } + + /// <summary>Create a stream, but feed off an existing list</summary> + public RewriteRuleNodeStream(ITreeAdaptor adaptor, string elementDescription, IList elements) + : base(adaptor, elementDescription, elements) + { + } + + public object NextNode() + { + return _Next(); + } + + override protected object ToTree(object el) + { + return adaptor.DupNode(el); + } + + override protected object Dup(object el) + { + // we dup every node, so don't have to worry about calling dup; + // short-circuited NextTree() so it doesn't call. + throw new NotSupportedException("dup can't be called for a node stream."); + } + } +} +#elif DOTNET2 +namespace Antlr.Runtime.Tree { + using System; + using System.Collections.Generic; + using SpecializingType = System.Object; + + /// <summary> + /// Queues up nodes matched on left side of -> in a tree parser. This is + /// the analog of RewriteRuleTokenStream for normal parsers. + /// </summary> +#warning Check, if RewriteRuleNodeStream can be changed to take advantage of something more specific than object. + public class RewriteRuleNodeStream : RewriteRuleElementStream<SpecializingType> { + public RewriteRuleNodeStream(ITreeAdaptor adaptor, string elementDescription) + : base(adaptor, elementDescription) { + } + + /// <summary>Create a stream with one element</summary> + public RewriteRuleNodeStream( + ITreeAdaptor adaptor, + string elementDescription, + SpecializingType oneElement + ) : base(adaptor, elementDescription, oneElement) { + } + + /// <summary>Create a stream, but feed off an existing list</summary> + public RewriteRuleNodeStream( + ITreeAdaptor adaptor, + string elementDescription, + IList<SpecializingType> elements + ) : base(adaptor, elementDescription, elements) { + } + + /// <summary>Create a stream, but feed off an existing list</summary> + [Obsolete("This constructor is for internal use only and might be phased out soon. Use instead the one with IList<T>.")] + public RewriteRuleNodeStream( + ITreeAdaptor adaptor, + string elementDescription, + System.Collections.IList elements + ) : base(adaptor, elementDescription, elements) { + } + + public object NextNode() { + return _Next(); + } + + override protected object ToTree(object el) { + return adaptor.DupNode(el); + } + } +} +#endif \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteRuleSubtreeStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteRuleSubtreeStream.cs new file mode 100644 index 0000000..ec7e158 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteRuleSubtreeStream.cs @@ -0,0 +1,227 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +Copyright (c) 2007 Johannes Luber +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +#if DOTNET1 +namespace Antlr.Runtime.Tree +{ + using System; + using IList = System.Collections.IList; + + public class RewriteRuleSubtreeStream : RewriteRuleElementStream + { + public RewriteRuleSubtreeStream(ITreeAdaptor adaptor, string elementDescription) + : base(adaptor, elementDescription) + { + } + + /// <summary> + /// Create a stream with one element + /// </summary> + public RewriteRuleSubtreeStream(ITreeAdaptor adaptor, string elementDescription, object oneElement) + : base(adaptor, elementDescription, oneElement) + { + } + + /// <summary> + /// Create a stream, but feed off an existing list + /// </summary> + public RewriteRuleSubtreeStream(ITreeAdaptor adaptor, string elementDescription, IList elements) + : base(adaptor, elementDescription, elements) + { + } + + /// <summary> + /// Treat next element as a single node even if it's a subtree. + /// </summary> + /// <remarks> + /// This is used instead of next() when the result has to be a + /// tree root node. Also prevents us from duplicating recently-added + /// children; e.g., ^(type ID)+ adds ID to type and then 2nd iteration + /// must dup the type node, but ID has been added. + /// + /// Referencing a rule result twice is ok; dup entire tree as + /// we can't be adding trees as root; e.g., expr expr. + /// + /// Hideous code duplication here with respect to base.NextTree() inherited from Java version. + /// Can't think of a proper way to refactor. This needs to always call dup node + /// and base.NextTree() doesn't know which to call: dup node or dup tree. + /// </remarks> + public object NextNode() + { + int size = Count; + if (dirty || ((cursor >= size) && (size == 1))) + { + // if out of elements and size is 1, dup (at most a single node + // since this is for making root nodes). + object el = _Next(); + return adaptor.DupNode(el); + } + // test size above then fetch + object elem = _Next(); + return elem; + } + + override protected object Dup(object el) + { + return adaptor.DupTree(el); + } + } +} +#elif DOTNET2 +namespace Antlr.Runtime.Tree { + using System; + using System.Collections.Generic; + using SpecializingType = System.Object; + +#warning Check, if RewriteRuleSubtreeStream can be changed to take advantage of something more specific than object. + /// <summary> + /// </summary> + /// <remarks></remarks> + /// <example></example> + public class RewriteRuleSubtreeStream : RewriteRuleElementStream<SpecializingType> { + + #region private delegate object ProcessHandler(object o) + /// <summary> + /// This delegate is used to allow the outfactoring of some common code. + /// </summary> + /// <param name="o">The to be processed object</param> + private delegate object ProcessHandler(object o); + #endregion + + public RewriteRuleSubtreeStream(ITreeAdaptor adaptor, string elementDescription) + : base(adaptor, elementDescription) { + } + + /// <summary> + /// Create a stream with one element + /// </summary> + public RewriteRuleSubtreeStream( + ITreeAdaptor adaptor, + string elementDescription, + SpecializingType oneElement + ) : base(adaptor, elementDescription, oneElement) { + } + + /// <summary>Create a stream, but feed off an existing list</summary> + public RewriteRuleSubtreeStream( + ITreeAdaptor adaptor, + string elementDescription, + IList<SpecializingType> elements + ) : base(adaptor, elementDescription, elements) { + } + + /// <summary>Create a stream, but feed off an existing list</summary> + [Obsolete("This constructor is for internal use only and might be phased out soon. Use instead the one with IList<T>.")] + public RewriteRuleSubtreeStream( + ITreeAdaptor adaptor, + string elementDescription, + System.Collections.IList elements + ) : base(adaptor, elementDescription, elements) { + } + + /// <summary> + /// Treat next element as a single node even if it's a subtree. + /// </summary> + /// <remarks> + /// This is used instead of next() when the result has to be a + /// tree root node. Also prevents us from duplicating recently-added + /// children; e.g., ^(type ID)+ adds ID to type and then 2nd iteration + /// must dup the type node, but ID has been added. + /// + /// Referencing a rule result twice is ok; dup entire tree as + /// we can't be adding trees as root; e.g., expr expr. + /// </remarks> + public object NextNode() { + // if necessary, dup (at most a single node since this is for making root nodes). + return FetchObject(delegate(object o) { return adaptor.DupNode(o); }); + } + + #region private object FetchObject(ProcessHandler ph) + /// <summary> + /// This method has the common code of two other methods, which differed in only one + /// function call. + /// </summary> + /// <param name="ph">The delegate, which has the chosen function</param> + /// <returns>The required object</returns> + private object FetchObject(ProcessHandler ph) { + if (RequiresDuplication()) { + // process the object + return ph(_Next()); + } + // test above then fetch + return _Next(); + } + #endregion + + /// <summary> + /// Tests, if the to be returned object requires duplication + /// </summary> + /// <returns><code>true</code>, if positive, <code>false</code>, if negative.</returns> + private bool RequiresDuplication() { + int size = Count; + // if dirty or if out of elements and size is 1 + return dirty || ((cursor >= size) && (size == 1)); + } + + #region public override object NextTree() + /// <summary> + /// Return the next element in the stream. + /// </summary> + /// <remarks> + /// If out of elements, throw an exception unless Count==1. + /// If Count is 1, then return elements[0]. + /// Return a duplicate node/subtree if stream is out of + /// elements and Count==1. + /// If we've already used the element, dup (dirty bit set). + /// </remarks> + public override object NextTree() { + // if out of elements and size is 1, dup + return FetchObject(delegate(object o) { return Dup(o); }); + } + #endregion + + + /// <summary> + /// When constructing trees, sometimes we need to dup a token or AST + /// subtree. Dup'ing a token means just creating another AST node + /// around it. For trees, you must call the adaptor.dupTree() + /// unless the element is for a tree root; then it must be a node dup + /// </summary> + private SpecializingType Dup(SpecializingType el) { + return adaptor.DupTree(el); + } + } +} +#endif \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteRuleTokenStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteRuleTokenStream.cs new file mode 100644 index 0000000..a409cc6 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/RewriteRuleTokenStream.cs @@ -0,0 +1,163 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +Copyright (c) 2007 Johannes Luber +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +#if DOTNET1 +namespace Antlr.Runtime.Tree +{ + using System; + using IList = System.Collections.IList; + using IToken = Antlr.Runtime.IToken; + + public class RewriteRuleTokenStream : RewriteRuleElementStream + { + public RewriteRuleTokenStream(ITreeAdaptor adaptor, string elementDescription) + : base(adaptor, elementDescription) + { + } + + /// <summary> + /// Create a stream with one element + /// </summary> + public RewriteRuleTokenStream( + ITreeAdaptor adaptor, + string elementDescription, + object oneElement + ) : base(adaptor, elementDescription, oneElement) { + } + + /// <summary> + /// Create a stream, but feed off an existing list. + /// </summary> + public RewriteRuleTokenStream( + ITreeAdaptor adaptor, + string elementDescription, + IList elements + ) : base(adaptor, elementDescription, elements) { + } + + /// <summary> + /// Get next token from stream and make a node for it. + /// </summary> + public object NextNode() + { + return adaptor.Create((IToken)_Next()); + } + + public IToken NextToken() { + return (IToken) _Next(); + } + + /// <summary> + /// Don't convert to a tree unless they explicitly call NextTree(). + /// This way we can do hetero tree nodes in rewrite. + /// </summary> + override protected object ToTree(object el) { + return el; + } + + override protected object Dup(object el) { + //return adaptor.Create((IToken)el); + throw new NotSupportedException("dup can't be called for a token stream."); + } + } +} + +#elif DOTNET2 +namespace Antlr.Runtime.Tree { + using System; + using System.Collections.Generic; + using SpecializingType = Antlr.Runtime.IToken; + + /// <summary> + /// </summary> + /// <remarks></remarks> + /// <example></example> + public class RewriteRuleTokenStream : RewriteRuleElementStream<SpecializingType> { + public RewriteRuleTokenStream(ITreeAdaptor adaptor, string elementDescription) + : base(adaptor, elementDescription) { + } + + /// <summary> + /// Create a stream with one element + /// </summary> + public RewriteRuleTokenStream( + ITreeAdaptor adaptor, + string elementDescription, + SpecializingType oneElement + ) : base(adaptor, elementDescription, oneElement) { + } + + /// <summary>Create a stream, but feed off an existing list</summary> + public RewriteRuleTokenStream( + ITreeAdaptor adaptor, + string elementDescription, + IList<SpecializingType> elements + ) : base(adaptor, elementDescription, elements) { + } + + /// <summary>Create a stream, but feed off an existing list</summary> + [Obsolete("This constructor is for internal use only and might be phased out soon. Use instead the one with IList<T>.")] + public RewriteRuleTokenStream( + ITreeAdaptor adaptor, + string elementDescription, + System.Collections.IList elements + ) : base(adaptor, elementDescription, elements) { + } + + /// <summary> + /// Get next token from stream and make a node for it. + /// </summary> + /// <remarks> + /// ITreeAdaptor.Create() returns an object, so no further restrictions possible. + /// </remarks> + public object NextNode() { + return adaptor.Create((SpecializingType) _Next()); + } + + public SpecializingType NextToken() { + return (SpecializingType) _Next(); + } + + /// <summary> + /// + /// Don't convert to a tree unless they explicitly call NextTree(). + /// This way we can do hetero tree nodes in rewrite. + /// </summary> + override protected object ToTree(SpecializingType el) { + return el; + } + } +} +#endif \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreeParser.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreeParser.cs new file mode 100644 index 0000000..8b5699c --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreeParser.cs @@ -0,0 +1,201 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + using System.Text.RegularExpressions; + using Antlr.Runtime; + + /// <summary> + /// A parser for a stream of tree nodes. "tree grammars" result in a subclass + /// of this. All the error reporting and recovery is shared with Parser via + /// the BaseRecognizer superclass. + /// </summary> + public class TreeParser : BaseRecognizer + { + public const int DOWN = Token.DOWN; + public const int UP = Token.UP; + + // precompiled regex used by InContext() + static readonly string dotdot = @".*[^.]\.\.[^.].*"; + static readonly string doubleEtc = @".*\.\.\.\s+\.\.\..*"; + static readonly string spaces = @"\s+"; + static readonly Regex dotdotPattern = new Regex(dotdot, RegexOptions.Compiled); + static readonly Regex doubleEtcPattern = new Regex(doubleEtc, RegexOptions.Compiled); + static readonly Regex spacesPattern = new Regex(spaces, RegexOptions.Compiled); + + public TreeParser(ITreeNodeStream input) + : base() // highlight that we go to super to set state object + { + TreeNodeStream = input; + } + + public TreeParser(ITreeNodeStream input, RecognizerSharedState state) + : base(state) // share the state object with another parser + { + TreeNodeStream = input; + } + + /// <summary>Set the input stream</summary> + virtual public ITreeNodeStream TreeNodeStream + { + get { return input; } + set { this.input = value; } + } + + override public string SourceName { + get { return input.SourceName; } + } + + protected override object GetCurrentInputSymbol(IIntStream input) { + return ((ITreeNodeStream)input).LT(1); + } + + protected override object GetMissingSymbol(IIntStream input, + RecognitionException e, + int expectedTokenType, + BitSet follow) { + string tokenText = "<missing " + TokenNames[expectedTokenType] + ">"; + return new CommonTree(new CommonToken(expectedTokenType, tokenText)); + } + + /// <summary>Reset the parser </summary> + override public void Reset() + { + base.Reset(); // reset all recognizer state variables + if ( input != null ) + { + input.Seek(0); // rewind the input + } + } + + /// <summary> + /// Match '.' in tree parser. + /// </summary> + /// <remarks> + /// Match '.' in tree parser has special meaning. Skip node or + /// entire tree if node has children. If children, scan until + /// corresponding UP node. + /// </remarks> + override public void MatchAny(IIntStream ignore /* ignore stream param, it's a copy of this.input */) + { + state.errorRecovery = false; + state.failed = false; + object look = input.LT(1); + if ( input.TreeAdaptor.GetChildCount(look) == 0 ) + { + input.Consume(); // not subtree, consume 1 node and return + return; + } + // current node is a subtree, skip to corresponding UP. + // must count nesting level to get right UP + int level = 0; + int tokenType = input.TreeAdaptor.GetNodeType(look); + while ( (tokenType != Token.EOF) && !( (tokenType == UP) && (level == 0) ) ) + { + input.Consume(); + look = input.LT(1); + tokenType = input.TreeAdaptor.GetNodeType(look); + if ( tokenType == DOWN ) + { + level++; + } + else if ( tokenType == UP ) + { + level--; + } + } + input.Consume(); // consume UP + } + + override public IIntStream Input + { + get { return input; } + } + + protected internal ITreeNodeStream input; + + /// <summary>We have DOWN/UP nodes in the stream that have no line info; override. + /// plus we want to alter the exception type. Don't try to recover + /// from tree parser errors inline... + /// </summary> + protected internal override object RecoverFromMismatchedToken(IIntStream input, int ttype, BitSet follow) { + throw new MismatchedTreeNodeException(ttype, (ITreeNodeStream)input); + } + + /// <summary> + /// Prefix error message with the grammar name because message is + /// always intended for the programmer because the parser built + /// the input tree not the user. + /// </summary> + override public string GetErrorHeader(RecognitionException e) + { + return GrammarFileName + ": node from " + + (e.approximateLineInfo ? "after " : "") + + "line " + e.Line + ":" + e.CharPositionInLine; + } + + /// <summary> + /// Tree parsers parse nodes they usually have a token object as + /// payload. Set the exception token and do the default behavior. + /// + /// </summary> + override public string GetErrorMessage(RecognitionException e, string[] tokenNames) + { + if ( this is TreeParser ) + { + ITreeAdaptor adaptor = ((ITreeNodeStream)e.Input).TreeAdaptor; + e.Token = adaptor.GetToken(e.Node); + if ( e.Token == null ) + { // could be an UP/DOWN node + e.Token = new CommonToken(adaptor.GetNodeType(e.Node), adaptor.GetNodeText(e.Node)); + } + } + return base.GetErrorMessage(e, tokenNames); + } + + public virtual void TraceIn(string ruleName, int ruleIndex) + { + base.TraceIn(ruleName, ruleIndex, input.LT(1)); + } + + public virtual void TraceOut(string ruleName, int ruleIndex) + { + base.TraceOut(ruleName, ruleIndex, input.LT(1)); + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreePatternLexer.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreePatternLexer.cs new file mode 100644 index 0000000..e0462b5 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreePatternLexer.cs @@ -0,0 +1,167 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + using StringBuilder = System.Text.StringBuilder; + + public class TreePatternLexer + { + public const int EOF = -1; + public const int BEGIN = 1; + public const int END = 2; + public const int ID = 3; + public const int ARG = 4; + public const int PERCENT = 5; + public const int COLON = 6; + public const int DOT = 7; + + /// <summary>The tree pattern to lex like "(A B C)"</summary> + protected string pattern; + + /// <summary>Index into input string</summary> + protected int p = -1; + + /// <summary>Current char</summary> + protected int c; + + /// <summary>How long is the pattern in char?</summary> + protected int n; + + /// <summary> + /// Set when token type is ID or ARG (name mimics Java's StreamTokenizer) + /// </summary> + public StringBuilder sval = new StringBuilder(); + + public bool error = false; + + public TreePatternLexer(string pattern) + { + this.pattern = pattern; + this.n = pattern.Length; + Consume(); + } + + public int NextToken() + { + sval.Length = 0; // reset, but reuse buffer + while (c != EOF) + { + if (c == ' ' || c == '\n' || c == '\r' || c == '\t') + { + Consume(); + continue; + } + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_') + { + sval.Append((char)c); + Consume(); + while ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9') || c == '_') + { + sval.Append((char)c); + Consume(); + } + return ID; + } + if (c == '(') + { + Consume(); + return BEGIN; + } + if (c == ')') + { + Consume(); + return END; + } + if (c == '%') + { + Consume(); + return PERCENT; + } + if (c == ':') + { + Consume(); + return COLON; + } + if (c == '.') + { + Consume(); + return DOT; + } + if (c == '[') + { // grab [x] as a string, returning x + Consume(); + while (c != ']') + { + if (c == '\\') + { + Consume(); + if (c != ']') + { + sval.Append('\\'); + } + sval.Append((char)c); + } + else + { + sval.Append((char)c); + } + Consume(); + } + Consume(); + return ARG; + } + Consume(); + error = true; + return EOF; + } + return EOF; + } + + protected void Consume() + { + p++; + if (p >= n) + { + c = EOF; + } + else + { + c = pattern[p]; + } + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreePatternParser.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreePatternParser.cs new file mode 100644 index 0000000..49ef88b --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreePatternParser.cs @@ -0,0 +1,189 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + using IToken = Antlr.Runtime.IToken; + using CommonToken = Antlr.Runtime.CommonToken; + + public class TreePatternParser + { + protected TreePatternLexer tokenizer; + protected int ttype; + protected TreeWizard wizard; + protected ITreeAdaptor adaptor; + + public TreePatternParser(TreePatternLexer tokenizer, TreeWizard wizard, ITreeAdaptor adaptor) + { + this.tokenizer = tokenizer; + this.wizard = wizard; + this.adaptor = adaptor; + ttype = tokenizer.NextToken(); // kickstart + } + + public object Pattern() + { + if (ttype == TreePatternLexer.BEGIN) + { + return ParseTree(); + } + else if (ttype == TreePatternLexer.ID) + { + object node = ParseNode(); + if (ttype == TreePatternLexer.EOF) + { + return node; + } + return null; // extra junk on end + } + return null; + } + + public object ParseTree() + { + if (ttype != TreePatternLexer.BEGIN) + { + Console.Out.WriteLine("no BEGIN"); + return null; + } + ttype = tokenizer.NextToken(); + object root = ParseNode(); + if (root == null) + { + return null; + } + while (ttype == TreePatternLexer.BEGIN || + ttype == TreePatternLexer.ID || + ttype == TreePatternLexer.PERCENT || + ttype == TreePatternLexer.DOT) + { + if (ttype == TreePatternLexer.BEGIN) + { + object subtree = ParseTree(); + adaptor.AddChild(root, subtree); + } + else + { + object child = ParseNode(); + if (child == null) + { + return null; + } + adaptor.AddChild(root, child); + } + } + if (ttype != TreePatternLexer.END) + { + Console.Out.WriteLine("no END"); + return null; + } + ttype = tokenizer.NextToken(); + return root; + } + + public object ParseNode() + { + // "%label:" prefix + string label = null; + if (ttype == TreePatternLexer.PERCENT) + { + ttype = tokenizer.NextToken(); + if (ttype != TreePatternLexer.ID) + { + return null; + } + label = tokenizer.sval.ToString(); + ttype = tokenizer.NextToken(); + if (ttype != TreePatternLexer.COLON) + { + return null; + } + ttype = tokenizer.NextToken(); // move to ID following colon + } + + // Wildcard? + if (ttype == TreePatternLexer.DOT) + { + ttype = tokenizer.NextToken(); + IToken wildcardPayload = new CommonToken(0, "."); + TreeWizard.TreePattern node = new TreeWizard.WildcardTreePattern(wildcardPayload); + if (label != null) + { + node.label = label; + } + return node; + } + + // "ID" or "ID[arg]" + if (ttype != TreePatternLexer.ID) + { + return null; + } + string tokenName = tokenizer.sval.ToString(); + ttype = tokenizer.NextToken(); + if (tokenName.Equals("nil")) + { + return adaptor.GetNilNode(); + } + string text = tokenName; + // check for arg + string arg = null; + if (ttype == TreePatternLexer.ARG) + { + arg = tokenizer.sval.ToString(); + text = arg; + ttype = tokenizer.NextToken(); + } + + // create node + int treeNodeType = wizard.GetTokenType(tokenName); + if (treeNodeType == Token.INVALID_TOKEN_TYPE) + { + return null; + } + object nodeObj; + nodeObj = adaptor.Create(treeNodeType, text); + if ((label != null) && (nodeObj.GetType() == typeof(TreeWizard.TreePattern))) + { + ((TreeWizard.TreePattern)nodeObj).label = label; + } + if ((arg != null) && (nodeObj.GetType() == typeof(TreeWizard.TreePattern))) + { + ((TreeWizard.TreePattern)nodeObj).hasTextArg = true; + } + return nodeObj; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreeRuleReturnScope.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreeRuleReturnScope.cs new file mode 100644 index 0000000..7b220f1 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreeRuleReturnScope.cs @@ -0,0 +1,55 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2006 Kunle Odutola +Copyright (c) 2005 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. + */ + + +namespace Antlr.Runtime.Tree +{ + using System; + using RuleReturnScope = Antlr.Runtime.RuleReturnScope; + + /// <summary> + /// This is identical to the ParserRuleReturnScope except that + /// the start property is a tree node and not a Token object + /// when you are parsing trees. To be generic the tree node types + /// have to be Object :( + /// </summary> + public class TreeRuleReturnScope : RuleReturnScope + { + /// <summary>First node or root node of tree matched for this rule.</summary> + private object start; + + /// <summary>Return the start token or tree </summary> + override public object Start + { + get { return start; } + set { start = value; } + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreeVisitor.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreeVisitor.cs new file mode 100644 index 0000000..2fe0784 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreeVisitor.cs @@ -0,0 +1,46 @@ + +namespace Antlr.Runtime.Tree { + + /// <summary> + /// Do a depth first walk of a tree, applying pre() and post() actions + /// as we discover and finish nodes. + /// </summary> + public class TreeVisitor { + protected ITreeAdaptor adaptor; + + public TreeVisitor(ITreeAdaptor adaptor) { + this.adaptor = adaptor; + } + + public TreeVisitor() : this(new CommonTreeAdaptor()) { } + + /// <summary> + /// Visit every node in tree t and trigger an action for each node + /// before/after having visited all of its children. + /// Execute both actions even if t has no children. + /// If a child visit yields a new child, it can update its + /// parent's child list or just return the new child. The + /// child update code works even if the child visit alters its parent + /// and returns the new tree. + /// + /// Return result of applying post action to this node. + /// </summary> + public object Visit(object t, ITreeVisitorAction action) { + bool isNil = adaptor.IsNil(t); + if ( action!=null && !isNil ) { + t = action.Pre(t); // if rewritten, walk children of new t + } + int n = adaptor.GetChildCount(t); + for (int i=0; i<n; i++) { + object child = adaptor.GetChild(t, i); + object visitResult = Visit(child, action); + object childAfterVisit = adaptor.GetChild(t, i); + if ( visitResult != childAfterVisit ) { // result & child differ? + adaptor.SetChild(t, i, visitResult); + } + } + if ( action!=null && !isNil ) t = action.Post(t); + return t; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreeWizard.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreeWizard.cs new file mode 100644 index 0000000..1a93ee3 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/TreeWizard.cs @@ -0,0 +1,542 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +Copyright (c) 2007 Johannes Luber +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + using IList = System.Collections.IList; + using IDictionary = System.Collections.IDictionary; + using Hashtable = System.Collections.Hashtable; + using ArrayList = System.Collections.ArrayList; + using IToken = Antlr.Runtime.IToken; + using Token = Antlr.Runtime.Token; + + /// <summary> + /// Build and navigate trees with this object. Must know about the names + /// of tokens so you have to pass in a map or array of token names (from which + /// this class can build the map). I.e., Token DECL means nothing unless the + /// class can translate it to a token type. + /// </summary> + /// <remarks> + /// In order to create nodes and navigate, this class needs a TreeAdaptor. + /// + /// This class can build a token type -> node index for repeated use or for + /// iterating over the various nodes with a particular type. + /// + /// This class works in conjunction with the TreeAdaptor rather than moving + /// all this functionality into the adaptor. An adaptor helps build and + /// navigate trees using methods. This class helps you do it with string + /// patterns like "(A B C)". You can create a tree from that pattern or + /// match subtrees against it. + /// </remarks> + public class TreeWizard + { + protected ITreeAdaptor adaptor; + protected IDictionary tokenNameToTypeMap; + + public interface ContextVisitor + { + void Visit(object t, object parent, int childIndex, IDictionary labels); + } + + public abstract class Visitor : ContextVisitor + { + public void Visit(object t, object parent, int childIndex, IDictionary labels) + { + Visit(t); + } + public abstract void Visit(object t); + } + + private sealed class RecordAllElementsVisitor : Visitor + { + private IList list; + + public RecordAllElementsVisitor(IList list) + { + this.list = list; + } + + override public void Visit(object t) + { + list.Add(t); + } + } + + private sealed class PatternMatchingContextVisitor : ContextVisitor + { + private TreeWizard owner; + private TreePattern pattern; + private IList list; + + public PatternMatchingContextVisitor(TreeWizard owner, TreePattern pattern, IList list) + { + this.owner = owner; + this.pattern = pattern; + this.list = list; + } + + public void Visit(object t, object parent, int childIndex, IDictionary labels) + { + if (owner._Parse(t, pattern, null)) + { + list.Add(t); + } + } + } + + private sealed class InvokeVisitorOnPatternMatchContextVisitor : ContextVisitor + { + private TreeWizard owner; + private TreePattern pattern; + private ContextVisitor visitor; + private Hashtable labels = new Hashtable(); + + public InvokeVisitorOnPatternMatchContextVisitor(TreeWizard owner, TreePattern pattern, ContextVisitor visitor) + { + this.owner = owner; + this.pattern = pattern; + this.visitor = visitor; + } + + public void Visit(object t, object parent, int childIndex, IDictionary unusedlabels) + { + // the unusedlabels arg is null as visit on token type doesn't set. + labels.Clear(); + if (owner._Parse(t, pattern, labels)) + { + visitor.Visit(t, parent, childIndex, labels); + } + } + } + + /// <summary> + /// When using %label:TOKENNAME in a tree for parse(), we must track the label. + /// </summary> + public class TreePattern : CommonTree + { + public string label; + public bool hasTextArg; + public TreePattern(IToken payload) + : base(payload) + { + } + + override public string ToString() + { + if (label != null) + { + return "%" + label + ":" + base.ToString(); + } + else + { + return base.ToString(); + } + } + } + + public class WildcardTreePattern : TreePattern + { + public WildcardTreePattern(IToken payload) + : base(payload) + { + } + } + + /// <summary> + /// This adaptor creates TreePattern objects for use during scan() + /// </summary> + public class TreePatternTreeAdaptor : CommonTreeAdaptor + { + override public object Create(IToken payload) + { + return new TreePattern(payload); + } + } + + public TreeWizard(ITreeAdaptor adaptor) + { + this.adaptor = adaptor; + } + + public TreeWizard(ITreeAdaptor adaptor, IDictionary tokenNameToTypeMap) + { + this.adaptor = adaptor; + this.tokenNameToTypeMap = tokenNameToTypeMap; + } + + public TreeWizard(ITreeAdaptor adaptor, string[] tokenNames) + { + this.adaptor = adaptor; + this.tokenNameToTypeMap = ComputeTokenTypes(tokenNames); + } + + public TreeWizard(string[] tokenNames) + : this(null, tokenNames) + { + } + + /// <summary> + /// Compute a Map<String, Integer> that is an inverted index of + /// tokenNames (which maps int token types to names). + /// </summary> + public IDictionary ComputeTokenTypes(string[] tokenNames) + { + IDictionary m = new Hashtable(); + if (tokenNames == null) { + return m; + } + for (int ttype = Token.MIN_TOKEN_TYPE; ttype < tokenNames.Length; ttype++) + { + string name = tokenNames[ttype]; + m.Add(name, ttype); + } + return m; + } + + /// <summary> + /// Using the map of token names to token types, return the type. + /// </summary> + public int GetTokenType(string tokenName) + { + if (tokenNameToTypeMap == null) + { + return Token.INVALID_TOKEN_TYPE; + } + object ttypeI = tokenNameToTypeMap[tokenName]; + if (ttypeI != null) + { + return (int)ttypeI; + } + return Token.INVALID_TOKEN_TYPE; + } + + /// <summary> + /// Walk the entire tree and make a node name to nodes mapping. + /// </summary> + /// <remarks> + /// For now, use recursion but later nonrecursive version may be + /// more efficient. Returns Map<Integer, List> where the List is + /// of your AST node type. The Integer is the token type of the node. + /// + /// TODO: save this index so that find and visit are faster + /// </remarks> + public IDictionary Index(object t) + { + IDictionary m = new Hashtable(); + _Index(t, m); + return m; + } + + /// <summary>Do the work for index</summary> + protected void _Index(object t, IDictionary m) + { + if (t == null) + { + return; + } + int ttype = adaptor.GetNodeType(t); + IList elements = m[ttype] as IList; + if (elements == null) + { + elements = new ArrayList(); + m[ttype] = elements; + } + elements.Add(t); + int n = adaptor.GetChildCount(t); + for (int i = 0; i < n; i++) + { + object child = adaptor.GetChild(t, i); + _Index(child, m); + } + } + + /// <summary>Return a List of tree nodes with token type ttype</summary> + public IList Find(object t, int ttype) + { + IList nodes = new ArrayList(); + Visit(t, ttype, new TreeWizard.RecordAllElementsVisitor(nodes)); + return nodes; + } + + /// <summary>Return a List of subtrees matching pattern</summary> + public IList Find(object t, string pattern) + { + IList subtrees = new ArrayList(); + // Create a TreePattern from the pattern + TreePatternLexer tokenizer = new TreePatternLexer(pattern); + TreePatternParser parser = new TreePatternParser(tokenizer, this, new TreePatternTreeAdaptor()); + TreePattern tpattern = (TreePattern)parser.Pattern(); + // don't allow invalid patterns + if ((tpattern == null) || tpattern.IsNil || (tpattern.GetType() == typeof(WildcardTreePattern))) + { + return null; + } + int rootTokenType = tpattern.Type; + Visit(t, rootTokenType, new TreeWizard.PatternMatchingContextVisitor(this, tpattern, subtrees)); + return subtrees; + } + + public object FindFirst(object t, int ttype) + { + return null; + } + + public object FindFirst(object t, string pattern) + { + return null; + } + + /// <summary> + /// Visit every ttype node in t, invoking the visitor. + /// </summary> + /// <remarks> + /// This is a quicker + /// version of the general visit(t, pattern) method. The labels arg + /// of the visitor action method is never set (it's null) since using + /// a token type rather than a pattern doesn't let us set a label. + /// </remarks> + public void Visit(object t, int ttype, ContextVisitor visitor) + { + _Visit(t, null, 0, ttype, visitor); + } + + /// <summary>Do the recursive work for visit</summary> + protected void _Visit(object t, object parent, int childIndex, int ttype, ContextVisitor visitor) + { + if (t == null) + { + return; + } + if (adaptor.GetNodeType(t) == ttype) + { + visitor.Visit(t, parent, childIndex, null); + } + int n = adaptor.GetChildCount(t); + for (int i = 0; i < n; i++) + { + object child = adaptor.GetChild(t, i); + _Visit(child, t, i, ttype, visitor); + } + } + + /// <summary> + /// For all subtrees that match the pattern, execute the visit action. + /// </summary> + /// <remarks> + /// The implementation uses the root node of the pattern in combination + /// with visit(t, ttype, visitor) so nil-rooted patterns are not allowed. + /// Patterns with wildcard roots are also not allowed. + /// </remarks> + public void Visit(object t, string pattern, ContextVisitor visitor) + { + // Create a TreePattern from the pattern + TreePatternLexer tokenizer = new TreePatternLexer(pattern); + TreePatternParser parser = new TreePatternParser(tokenizer, this, new TreePatternTreeAdaptor()); + TreePattern tpattern = (TreePattern)parser.Pattern(); + // don't allow invalid patterns + if ((tpattern == null) || tpattern.IsNil || (tpattern.GetType() == typeof(WildcardTreePattern))) + { + return; + } + //IDictionary labels = new Hashtable(); // reused for each _parse + int rootTokenType = tpattern.Type; + Visit(t, rootTokenType, + new TreeWizard.InvokeVisitorOnPatternMatchContextVisitor(this, tpattern, visitor)); + } + + /// <summary> + /// Given a pattern like (ASSIGN %lhs:ID %rhs:.) with optional labels + /// on the various nodes and '.' (dot) as the node/subtree wildcard, + /// return true if the pattern matches and fill the labels Map with + /// the labels pointing at the appropriate nodes. Return false if + /// the pattern is malformed or the tree does not match. + /// </summary> + /// <remarks> + /// If a node specifies a text arg in pattern, then that must match + /// for that node in t. + /// + /// TODO: what's a better way to indicate bad pattern? Exceptions are a hassle + /// </remarks> + public bool Parse(object t, string pattern, IDictionary labels) + { + TreePatternLexer tokenizer = new TreePatternLexer(pattern); + TreePatternParser parser = new TreePatternParser(tokenizer, this, new TreePatternTreeAdaptor()); + TreePattern tpattern = (TreePattern)parser.Pattern(); + + bool matched = _Parse(t, tpattern, labels); + return matched; + } + + public bool Parse(object t, string pattern) + { + return Parse(t, pattern, null); + } + + /// <summary> + /// Do the work for Parse(). Check to see if the t2 pattern fits the + /// structure and token types in t1. Check text if the pattern has + /// text arguments on nodes. Fill labels map with pointers to nodes + /// in tree matched against nodes in pattern with labels. + /// </summary> + protected bool _Parse(object t1, TreePattern t2, IDictionary labels) + { + // make sure both are non-null + if (t1 == null || t2 == null) + { + return false; + } + // check roots (wildcard matches anything) + if (t2.GetType() != typeof(WildcardTreePattern)) + { + if (adaptor.GetNodeType(t1) != t2.Type) + { + return false; + } + if (t2.hasTextArg && !adaptor.GetNodeText(t1).Equals(t2.Text)) + { + return false; + } + } + if (t2.label != null && labels != null) + { + // map label in pattern to node in t1 + labels[t2.label] = t1; + } + // check children + int n1 = adaptor.GetChildCount(t1); + int n2 = t2.ChildCount; + if (n1 != n2) + { + return false; + } + for (int i = 0; i < n1; i++) + { + object child1 = adaptor.GetChild(t1, i); + TreePattern child2 = (TreePattern)t2.GetChild(i); + if (!_Parse(child1, child2, labels)) + { + return false; + } + } + return true; + } + + /// <summary> + /// Create a tree or node from the indicated tree pattern that closely + /// follows ANTLR tree grammar tree element syntax: + /// + /// (root child1 ... child2). + /// + /// </summary> + /// <remarks> + /// You can also just pass in a node: ID + /// + /// Any node can have a text argument: ID[foo] + /// (notice there are no quotes around foo--it's clear it's a string). + /// + /// nil is a special name meaning "give me a nil node". Useful for + /// making lists: (nil A B C) is a list of A B C. + /// </remarks> + public object Create(string pattern) + { + TreePatternLexer tokenizer = new TreePatternLexer(pattern); + TreePatternParser parser = new TreePatternParser(tokenizer, this, adaptor); + object t = parser.Pattern(); + return t; + } + + /// <summary> + /// Compare t1 and t2; return true if token types/text, structure match exactly. + /// The trees are examined in their entirety so that (A B) does not match + /// (A B C) nor (A (B C)). + /// </summary> + /// <remarks> + /// TODO: allow them to pass in a comparator + /// TODO: have a version that is nonstatic so it can use instance adaptor + /// + /// I cannot rely on the tree node's equals() implementation as I make + /// no constraints at all on the node types nor interface etc... + /// </remarks> + public static bool Equals(object t1, object t2, ITreeAdaptor adaptor) + { + return _Equals(t1, t2, adaptor); + } + + /// <summary> + /// Compare type, structure, and text of two trees, assuming adaptor in + /// this instance of a TreeWizard. + /// </summary> + public new bool Equals(object t1, object t2) + { + return _Equals(t1, t2, adaptor); + } + + protected static bool _Equals(object t1, object t2, ITreeAdaptor adaptor) + { + // make sure both are non-null + if (t1 == null || t2 == null) + { + return false; + } + // check roots + if (adaptor.GetNodeType(t1) != adaptor.GetNodeType(t2)) + { + return false; + } + if (!adaptor.GetNodeText(t1).Equals(adaptor.GetNodeText(t2))) + { + return false; + } + // check children + int n1 = adaptor.GetChildCount(t1); + int n2 = adaptor.GetChildCount(t2); + if (n1 != n2) + { + return false; + } + for (int i = 0; i < n1; i++) + { + object child1 = adaptor.GetChild(t1, i); + object child2 = adaptor.GetChild(t2, i); + if (!_Equals(child1, child2, adaptor)) + { + return false; + } + } + return true; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/UnBufferedTreeNodeStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/UnBufferedTreeNodeStream.cs new file mode 100644 index 0000000..6ea83c8 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime.Tree/UnBufferedTreeNodeStream.cs @@ -0,0 +1,695 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime.Tree +{ + using System; + using StringBuilder = System.Text.StringBuilder; + using IList = System.Collections.IList; + using ArrayList = System.Collections.ArrayList; + using StackList = Antlr.Runtime.Collections.StackList; + using IToken = Antlr.Runtime.IToken; + using ITokenStream = Antlr.Runtime.ITokenStream; + + /// <summary> + /// A stream of tree nodes, accessing nodes from a tree of ANY kind. + /// </summary> + /// <remarks> + /// No new nodes should be created in tree during the walk. A small buffer + /// of tokens is kept to efficiently and easily handle LT(i) calls, though + /// the lookahead mechanism is fairly complicated. + /// + /// For tree rewriting during tree parsing, this must also be able + /// to replace a set of children without "losing its place". + /// That part is not yet implemented. Will permit a rule to return + /// a different tree and have it stitched into the output tree probably. + /// + /// <see cref="CommonTreeNodeStream"/> + /// + /// </remarks> + public class UnBufferedTreeNodeStream : ITreeNodeStream + { + /// <summary> + /// Where is this stream pulling nodes from? This is not the name, but + /// the object that provides node objects. + /// </summary> + virtual public object TreeSource + { + get { return root; } + } + + #region IEnumerator Members (implementation) + + private ITree currentEnumerationNode; + + virtual public void Reset() + { + currentNode = root; + previousNode = null; + currentChildIndex = - 1; + absoluteNodeIndex = -1; + head = tail = 0; + } + + /// <summary> + /// Navigates to the next node found during a depth-first walk of root. + /// Also, adds these nodes and DOWN/UP imaginary nodes into the lokoahead + /// buffer as a side-effect. Normally side-effects are bad, but because + /// we can Emit many tokens for every MoveNext() call, it's pretty hard to + /// use a single return value for that. We must add these tokens to + /// the lookahead buffer. + /// + /// This routine does *not* cause the 'Current' property to ever return the + /// DOWN/UP nodes; those are only returned by the LT() method. + /// + /// Ugh. This mechanism is much more complicated than a recursive + /// solution, but it's the only way to provide nodes on-demand instead + /// of walking once completely through and buffering up the nodes. :( + /// </summary> + public virtual bool MoveNext() + { + // already walked entire tree; nothing to return + if (currentNode == null) + { + AddLookahead(eof); + currentEnumerationNode = null; + // this is infinite stream returning EOF at end forever + // so don't throw NoSuchElementException + return false; + } + + // initial condition (first time method is called) + if (currentChildIndex == - 1) + { + currentEnumerationNode = (ITree)handleRootNode(); + return true; + } + + // index is in the child list? + if (currentChildIndex < adaptor.GetChildCount(currentNode)) + { + currentEnumerationNode = (ITree)VisitChild(currentChildIndex); + return true; + } + + // hit end of child list, return to parent node or its parent ... + WalkBackToMostRecentNodeWithUnvisitedChildren(); + if (currentNode != null) + { + currentEnumerationNode = (ITree)VisitChild(currentChildIndex); + return true; + } + return false; + } + + public virtual object Current + { + get { return currentEnumerationNode; } + } + + #endregion + + public const int INITIAL_LOOKAHEAD_BUFFER_SIZE = 5; + + /// <summary>Reuse same DOWN, UP navigation nodes unless this is true</summary> + protected bool uniqueNavigationNodes = false; + + /// <summary>Pull nodes from which tree? </summary> + protected internal object root; + + /// <summary>IF this tree (root) was created from a token stream, track it.</summary> + protected ITokenStream tokens; + + /// <summary>What tree adaptor was used to build these trees</summary> + ITreeAdaptor adaptor; + + /// <summary> + /// As we walk down the nodes, we must track parent nodes so we know + /// where to go after walking the last child of a node. When visiting + /// a child, push current node and current index. + /// </summary> + protected internal StackList nodeStack = new StackList(); + + /// <summary> + /// Track which child index you are visiting for each node we push. + /// TODO: pretty inefficient...use int[] when you have time + /// </summary> + protected internal StackList indexStack = new StackList(); + + /// <summary>Which node are we currently visiting? </summary> + protected internal object currentNode; + + /// <summary>Which node did we visit last? Used for LT(-1) calls. </summary> + protected internal object previousNode; + + /// <summary> + /// Which child are we currently visiting? If -1 we have not visited + /// this node yet; next Consume() request will set currentIndex to 0. + /// </summary> + protected internal int currentChildIndex; + + /// <summary> + /// What node index did we just consume? i=0..n-1 for n node trees. + /// IntStream.next is hence 1 + this value. Size will be same. + /// </summary> + protected int absoluteNodeIndex; + + /// <summary> + /// Buffer tree node stream for use with LT(i). This list grows + /// to fit new lookahead depths, but Consume() wraps like a circular + /// buffer. + /// </summary> + protected internal object[] lookahead = new object[INITIAL_LOOKAHEAD_BUFFER_SIZE]; + + /// <summary>lookahead[head] is the first symbol of lookahead, LT(1). </summary> + protected internal int head; + + /// <summary> + /// Add new lookahead at lookahead[tail]. tail wraps around at the + /// end of the lookahead buffer so tail could be less than head. + /// </summary> + protected internal int tail; + + /// <summary> + /// When walking ahead with cyclic DFA or for syntactic predicates, + /// we need to record the state of the tree node stream. This + /// class wraps up the current state of the UnBufferedTreeNodeStream. + /// Calling Mark() will push another of these on the markers stack. + /// </summary> + protected class TreeWalkState + { + protected internal int currentChildIndex; + protected internal int absoluteNodeIndex; + protected internal object currentNode; + protected internal object previousNode; + ///<summary>Record state of the nodeStack</summary> + protected internal int nodeStackSize; + ///<summary>Record state of the indexStack</summary> + protected internal int indexStackSize; + protected internal object[] lookahead; + } + + /// <summary> + /// Calls to Mark() may be nested so we have to track a stack of them. + /// The marker is an index into this stack. This is a List<TreeWalkState>. + /// Indexed from 1..markDepth. A null is kept at index 0. It is created + /// upon first call to Mark(). + /// </summary> + protected IList markers; + + ///<summary> + /// tracks how deep Mark() calls are nested + /// </summary> + protected int markDepth = 0; + + ///<summary> + /// Track the last Mark() call result value for use in Rewind(). + /// </summary> + protected int lastMarker; + + // navigation nodes + + protected object down; + protected object up; + protected object eof; + + public UnBufferedTreeNodeStream(object tree) + : this(new CommonTreeAdaptor(), tree) + { + } + + public UnBufferedTreeNodeStream(ITreeAdaptor adaptor, object tree) + { + this.root = tree; + this.adaptor = adaptor; + Reset(); + down = adaptor.Create(Token.DOWN, "DOWN"); + up = adaptor.Create(Token.UP, "UP"); + eof = adaptor.Create(Token.EOF, "EOF"); + } + + + public virtual object Get(int i) + { + throw new NotSupportedException("stream is unbuffered"); + } + + /// <summary> + /// Get tree node at current input pointer + i ahead where i=1 is next node. + /// i < 0 indicates nodes in the past. So -1 is previous node and -2 is + /// two nodes ago. LT(0) is undefined. For i>=n, return null. + /// Return null for LT(0) and any index that results in an absolute address + /// that is negative. + /// + /// This is analogus to the LT() method of the TokenStream, but this + /// returns a tree node instead of a token. Makes code gen identical + /// for both parser and tree grammars. :) + /// </summary> + public virtual object LT(int k) + { + if (k == - 1) + { + return previousNode; + } + if (k < 0) + { + throw new ArgumentNullException("tree node streams cannot look backwards more than 1 node", "k"); + } + if (k == 0) + { + return Tree.INVALID_NODE; + } + fill(k); + return lookahead[(head + k - 1) % lookahead.Length]; + } + + /// <summary>Make sure we have at least k symbols in lookahead buffer </summary> + protected internal virtual void fill(int k) + { + int n = LookaheadSize; + for (int i = 1; i <= k - n; i++) + { + MoveNext(); // get at least k-depth lookahead nodes + } + } + + /// <summary> + /// Add a node to the lookahead buffer. Add at lookahead[tail]. + /// If you tail+1 == head, then we must create a bigger buffer + /// and copy all the nodes over plus reset head, tail. After + /// this method, LT(1) will be lookahead[0]. + /// </summary> + protected internal virtual void AddLookahead(object node) + { + lookahead[tail] = node; + tail = (tail + 1) % lookahead.Length; + if (tail == head) + { + // buffer overflow: tail caught up with head + // allocate a buffer 2x as big + object[] bigger = new object[2 * lookahead.Length]; + // copy head to end of buffer to beginning of bigger buffer + int remainderHeadToEnd = lookahead.Length - head; + Array.Copy(lookahead, head, bigger, 0, remainderHeadToEnd); + // copy 0..tail to after that + Array.Copy(lookahead, 0, bigger, remainderHeadToEnd, tail); + lookahead = bigger; // reset to bigger buffer + head = 0; + tail += remainderHeadToEnd; + } + } + + // Satisfy IntStream interface + + public virtual void Consume() + { + // make sure there is something in lookahead buf, which might call next() + fill(1); + absoluteNodeIndex++; + previousNode = lookahead[head]; // track previous node before moving on + head = (head + 1) % lookahead.Length; + } + + public virtual int LA(int i) + { + object t = (ITree) LT(i); + if (t == null) + { + return Token.INVALID_TOKEN_TYPE; + } + return adaptor.GetNodeType(t); + } + + /// <summary> + /// Record the current state of the tree walk which includes + /// the current node and stack state as well as the lookahead + /// buffer. + /// </summary> + public virtual int Mark() + { + if (markers == null) + { + markers = new ArrayList(); + markers.Add(null); // depth 0 means no backtracking, leave blank + } + markDepth++; + TreeWalkState state = null; + if ( markDepth >= markers.Count ) + { + state = new TreeWalkState(); + markers.Add(state); + } + else + { + state = (TreeWalkState)markers[markDepth]; + + } + state.absoluteNodeIndex = absoluteNodeIndex; + state.currentChildIndex = currentChildIndex; + state.currentNode = currentNode; + state.previousNode = previousNode; + state.nodeStackSize = nodeStack.Count; + state.indexStackSize = indexStack.Count; + // take snapshot of lookahead buffer + int n = LookaheadSize; + int i = 0; + state.lookahead = new object[n]; + for (int k = 1; k <= n; k++, i++) + { + state.lookahead[i] = LT(k); + } + lastMarker = markDepth; + return markDepth; + } + + public virtual void Release(int marker) + { + // unwind any other markers made after marker and release marker + markDepth = marker; + // release this marker + markDepth--; + } + + /// <summary> + /// Rewind the current state of the tree walk to the state it + /// was in when Mark() was called and it returned marker. Also, + /// wipe out the lookahead which will force reloading a few nodes + /// but it is better than making a copy of the lookahead buffer + /// upon Mark(). + /// </summary> + public virtual void Rewind(int marker) + { + if ( markers == null ) + { + return; + } + TreeWalkState state = (TreeWalkState)markers[marker]; + absoluteNodeIndex = state.absoluteNodeIndex; + currentChildIndex = state.currentChildIndex; + currentNode = state.currentNode; + previousNode = state.previousNode; + // drop node and index stacks back to old size + nodeStack.Capacity = state.nodeStackSize; + indexStack.Capacity = state.indexStackSize; + head = tail = 0; // wack lookahead buffer and then refill + for (; tail < state.lookahead.Length; tail++) + { + lookahead[tail] = state.lookahead[tail]; + } + Release(marker); + } + + public void Rewind() + { + Rewind(lastMarker); + } + + /// <summary> + /// Consume() ahead until we hit index. Can't just jump ahead--must + /// spit out the navigation nodes. + /// </summary> + public virtual void Seek(int index) + { + if (index < this.Index()) + { + throw new ArgumentOutOfRangeException("can't seek backwards in node stream", "index"); + } + // seek forward, consume until we hit index + while (this.Index() < index) + { + Consume(); + } + } + + public virtual int Index() + { + return absoluteNodeIndex + 1; + } + + /// <summary> + /// Expensive to compute; recursively walk tree to find size; + /// include navigation nodes and EOF. Reuse functionality + /// in CommonTreeNodeStream as we only really use this + /// for testing. + /// </summary> + [Obsolete("Please use property Count instead.")] + public virtual int Size() + { + return Count; + + } + + /// <summary> + /// Expensive to compute; recursively walk tree to find size; + /// include navigation nodes and EOF. Reuse functionality + /// in CommonTreeNodeStream as we only really use this + /// for testing. + /// </summary> + public virtual int Count + { + get + { + CommonTreeNodeStream s = new CommonTreeNodeStream(root); + return s.Count; + } + } + + // Satisfy Java's Iterator interface + + protected internal virtual object handleRootNode() + { + object node; + node = currentNode; + // point to first child in prep for subsequent next() + currentChildIndex = 0; + if ( adaptor.IsNil(node) ) + { + // don't count this root nil node + node = VisitChild(currentChildIndex); + } + else + { + AddLookahead(node); + if ( adaptor.GetChildCount(currentNode) == 0 ) + { + // single node case + currentNode = null; // say we're done + } + } + return node; + } + + protected internal virtual object VisitChild(int child) + { + object node = null; + // save state + nodeStack.Push(currentNode); + indexStack.Push(child); + if (child == 0 && !adaptor.IsNil(currentNode)) + { + AddNavigationNode(Token.DOWN); + } + // visit child + currentNode = adaptor.GetChild(currentNode, child); + currentChildIndex = 0; + node = currentNode; // record node to return + AddLookahead(node); + WalkBackToMostRecentNodeWithUnvisitedChildren(); + return node; + } + + /// <summary> + /// As we flatten the tree, we use UP, DOWN nodes to represent + /// the tree structure. When debugging we need unique nodes + /// so instantiate new ones when uniqueNavigationNodes is true. + /// </summary> + protected internal virtual void AddNavigationNode(int ttype) + { + object navNode = null; + if (ttype == Token.DOWN) + { + if (HasUniqueNavigationNodes) + navNode = adaptor.Create(Token.DOWN, "DOWN"); + else + navNode = down; + } + else + { + if ( HasUniqueNavigationNodes ) + { + navNode = adaptor.Create(Token.UP, "UP"); + } + else + { + navNode = up; + } + } + AddLookahead(navNode); + } + + /// <summary> + /// Walk upwards looking for a node with more children to walk. + /// </summary> + protected internal virtual void WalkBackToMostRecentNodeWithUnvisitedChildren() + { + while ( (currentNode != null) && (currentChildIndex >= adaptor.GetChildCount(currentNode)) ) + { + currentNode = nodeStack.Pop(); + if ( currentNode == null ) + { // hit the root? + return; + } + currentChildIndex = ((int) indexStack.Pop()); + currentChildIndex++; // move to next child + if (currentChildIndex >= adaptor.GetChildCount(currentNode)) + { + if ( !adaptor.IsNil(currentNode) ) + { + AddNavigationNode(Token.UP); + } + if (currentNode == root) + { + // we done yet? + currentNode = null; + } + } + } + } + + public ITreeAdaptor TreeAdaptor + { + get { return adaptor; } + } + + public string SourceName { + get { return TokenStream.SourceName; } + } + + public ITokenStream TokenStream + { + get { return tokens; } + set { tokens = value; } + } + + public bool HasUniqueNavigationNodes + { + get { return uniqueNavigationNodes; } + set { uniqueNavigationNodes = value; } + } + + public void ReplaceChildren(object parent, int startChildIndex, int stopChildIndex, object t) + { + throw new NotSupportedException("can't do stream rewrites yet"); + } + + /// <summary> + /// Print out the entire tree including DOWN/UP nodes. Uses + /// a recursive walk. Mostly useful for testing as it yields + /// the token types not text. + /// </summary> + public override string ToString() + { + return ToString(root, null); + } + + protected int LookaheadSize + { + get { return tail < head ? (lookahead.Length - head + tail) : (tail - head); } + } + + /// <summary>TODO: not sure this is what we want for trees. </summary> + public virtual string ToString(object start, object stop) + { + if ( start == null ) + { + return null; + } + // if we have the token stream, use that to dump text in order + if ( tokens != null ) + { + // don't trust stop node as it's often an UP node etc... + // walk backwards until you find a non-UP, non-DOWN node + // and ask for it's token index. + int beginTokenIndex = adaptor.GetTokenStartIndex(start); + int endTokenIndex = adaptor.GetTokenStopIndex(stop); + if ( (stop != null) && (adaptor.GetNodeType(stop) == Token.UP) ) + { + endTokenIndex = adaptor.GetTokenStopIndex(start); + } + else + { + endTokenIndex = Count-1; + } + return tokens.ToString(beginTokenIndex, endTokenIndex); + } + StringBuilder buf = new StringBuilder(); + ToStringWork(start, stop, buf); + return buf.ToString(); + } + + protected internal virtual void ToStringWork(object p, object stop, StringBuilder buf) + { + if ( !adaptor.IsNil(p) ) + { + string text = adaptor.GetNodeText(p); + if (text == null) + { + text = " " + adaptor.GetNodeType(p); + } + buf.Append(text); // ask the node to go to string + } + if (p == stop) + { + return; + } + int n = adaptor.GetChildCount(p); + if ( (n > 0) && !adaptor.IsNil(p) ) + { + buf.Append(" "); + buf.Append(Token.DOWN); + } + for (int c = 0; c < n; c++) + { + object child = adaptor.GetChild(p, c); + ToStringWork(child, stop, buf); + } + if ( (n > 0) && !adaptor.IsNil(p) ) + { + buf.Append(" "); + buf.Append(Token.UP); + } + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ANTLRFileStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ANTLRFileStream.cs new file mode 100644 index 0000000..f66bbfc --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ANTLRFileStream.cs @@ -0,0 +1,150 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + using StreamReader = System.IO.StreamReader; + using FileInfo = System.IO.FileInfo; + using Encoding = System.Text.Encoding; + + /// <summary> + /// A character stream - an <see cref="ICharStream"/> - that loads + /// and caches the contents of it's underlying file fully during + /// object construction + /// </summary> + /// <remarks> + /// This looks very much like an ANTLReaderStream or an ANTLRInputStream + /// but, it is a special case. Since we know the exact size of the file to + /// load, we can avoid lots of data copying and buffer resizing. + /// </remarks> + public class ANTLRFileStream : ANTLRStringStream + { + #region Constructors + + /// <summary> + /// Initializes a new instance of the ANTLRFileStream class + /// </summary> + protected ANTLRFileStream() + { + } + + /// <summary> + /// Initializes a new instance of the ANTLRFileStream class for the + /// specified file name + /// </summary> + public ANTLRFileStream(string fileName) : + this(fileName, Encoding.Default) + { + } + + /// <summary> + /// Initializes a new instance of the ANTLRFileStream class for the + /// specified file name and encoding + /// </summary> + public ANTLRFileStream(string fileName, Encoding encoding) + { + this.fileName = fileName; + Load(fileName, encoding); + } + + /// <summary> + /// Gets the file name of this ANTLRFileStream underlying file + /// </summary> + override public string SourceName + { + get { return fileName; } + } + + #endregion + + + #region Public API + + /// <summary> + /// Loads and buffers the specified file to be used as this + /// ANTLRFileStream's source + /// </summary> + /// <param name="fileName">File to load</param> + /// <param name="encoding">Encoding to apply to file</param> + public virtual void Load(string fileName, Encoding encoding) + { + if (fileName == null) + return; + + StreamReader fr = null; + try + { + FileInfo f = new FileInfo(fileName); + int filelen = (int)GetFileLength(f); + data = new char[filelen]; + if (encoding != null) + fr = new StreamReader(fileName, encoding); + else + fr = new StreamReader(fileName, Encoding.Default); + n = fr.Read((Char[])data, 0, data.Length); + } + finally + { + if (fr != null) + { + fr.Close(); + } + } + } + + #endregion + + + #region Data Members + + /// <summary>Fully qualified name of the stream's underlying file</summary> + protected string fileName; + + #endregion + + + #region Private Members + + private long GetFileLength(FileInfo file) + { + if (file.Exists) + return file.Length; + else + return 0; + } + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ANTLRInputStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ANTLRInputStream.cs new file mode 100644 index 0000000..14c6891 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ANTLRInputStream.cs @@ -0,0 +1,120 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + using Encoding = System.Text.Encoding; + using Stream = System.IO.Stream; + using StreamReader = System.IO.StreamReader; + + /// <summary> + /// A character stream - an <see cref="ICharStream"/> - that loads + /// and caches the contents of it's underlying + /// <see cref="System.IO.Stream"/> fully during object construction + /// </summary> + /// <remarks> + /// Useful for reading from stdin and, for specifying file encodings etc... + /// </remarks> + public class ANTLRInputStream : ANTLRReaderStream + { + #region Constructors + + /// <summary> + /// Initializes a new instance of the ANTLRInputStream class + /// </summary> + protected ANTLRInputStream() + { + } + + /// <summary> + /// Initializes a new instance of the ANTLRInputStream class for the + /// specified stream + /// </summary> + public ANTLRInputStream(Stream istream) + : this(istream, null) + { + } + + /// <summary> + /// Initializes a new instance of the ANTLRInputStream class for the + /// specified stream and encoding + /// </summary> + public ANTLRInputStream(Stream istream, Encoding encoding) + : this(istream, INITIAL_BUFFER_SIZE, encoding) + { + + } + + /// <summary> + /// Initializes a new instance of the ANTLRInputStream class for the + /// specified stream and initial data buffer size + /// </summary> + public ANTLRInputStream(Stream istream, int size) + : this(istream, size, null) + { + } + + /// <summary> + /// Initializes a new instance of the ANTLRInputStream class for the + /// specified stream, encoding and initial data buffer size + /// </summary> + public ANTLRInputStream(Stream istream, int size, Encoding encoding) + : this(istream, size, READ_BUFFER_SIZE, encoding) + { + } + + /// <summary> + /// Initializes a new instance of the ANTLRInputStream class for the + /// specified stream, encoding, initial data buffer size and, using + /// a read buffer of the specified size + /// </summary> + public ANTLRInputStream(Stream istream, int size, int readBufferSize, Encoding encoding) + { + StreamReader reader; + if (encoding != null) + { + reader = new StreamReader(istream, encoding); + } + else + { + reader = new StreamReader(istream); + } + Load(reader, size, readBufferSize); + } + + #endregion + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ANTLRReaderStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ANTLRReaderStream.cs new file mode 100644 index 0000000..9cb9b9c --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ANTLRReaderStream.cs @@ -0,0 +1,149 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + using TextReader = System.IO.TextReader; + + /// <summary> + /// An ANTLRStringStream that caches all the input from a TextReader. It + /// behaves just like a plain ANTLRStringStream + /// </summary> + /// <remarks> + /// Manages the buffer manually to avoid unnecessary data copying. + /// If you need encoding, use ANTLRInputStream. + /// </remarks> + public class ANTLRReaderStream : ANTLRStringStream + { + /// <summary>Default size (in characters) of the buffer used for IO reads</summary> + public static readonly int READ_BUFFER_SIZE = 1024; + + /// <summary>Initial size (in characters) of the data cache</summary> + public static readonly int INITIAL_BUFFER_SIZE = 1024; + + #region Constructors + + /// <summary> + /// Initializes a new instance of the ANTLRReaderStream class + /// </summary> + protected ANTLRReaderStream() + { + } + + /// <summary> + /// Initializes a new instance of the ANTLRReaderStream class for the + /// specified TextReader + /// </summary> + public ANTLRReaderStream(TextReader reader) + : this(reader, INITIAL_BUFFER_SIZE, READ_BUFFER_SIZE) + { + + } + + /// <summary> + /// Initializes a new instance of the ANTLRReaderStream class for the + /// specified TextReader and initial data buffer size + /// </summary> + public ANTLRReaderStream(TextReader reader, int size) + : this(reader, size, READ_BUFFER_SIZE) + { + } + + /// <summary> + /// Initializes a new instance of the ANTLRReaderStream class for the + /// specified TextReader, initial data buffer size and, using + /// a read buffer of the specified size + /// </summary> + public ANTLRReaderStream(TextReader reader, int size, int readChunkSize) + { + Load(reader, size, readChunkSize); + } + + #endregion + + + #region Public API + + /// <summary> + /// Loads and buffers the contents of the specified reader to be + /// used as this ANTLRReaderStream's source + /// </summary> + public virtual void Load(TextReader reader, int size, int readChunkSize) + { + if (reader == null) + { + return; + } + if (size <= 0) + { + size = INITIAL_BUFFER_SIZE; + } + if (readChunkSize <= 0) + { + readChunkSize = READ_BUFFER_SIZE; + } + + try + { + // alloc initial buffer size. + data = new char[size]; + // read all the data in chunks of readChunkSize + int numRead = 0; + int p = 0; + do + { + if (p + readChunkSize > data.Length) + { // overflow? + char[] newdata = new char[data.Length * 2]; // resize + Array.Copy(data, 0, newdata, 0, data.Length); + data = newdata; + } + numRead = reader.Read(data, p, readChunkSize); + p += numRead; + } while (numRead != 0); // while not EOF + // set the actual size of the data available; + // EOF subtracted one above in p+=numRead; add one back + //n = p + 1; + n = p; + } + finally + { + reader.Close(); + } + } + + #endregion + } +} diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ANTLRStringStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ANTLRStringStream.cs new file mode 100644 index 0000000..4e66b0d --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ANTLRStringStream.cs @@ -0,0 +1,319 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + using IList = System.Collections.IList; + using ArrayList = System.Collections.ArrayList; + + /// <summary> + /// A pretty quick <see cref="ICharStream"/> that uses a character array + /// directly as it's underlying source. + /// </summary> + public class ANTLRStringStream : ICharStream + { + #region Constructors + + /// <summary> + /// Initializes a new instance of the ANTLRStringStream class + /// </summary> + protected ANTLRStringStream() + { + } + + /// <summary> + /// Initializes a new instance of the ANTLRStringStream class for the + /// specified string. This copies data from the string to a local + /// character array + /// </summary> + public ANTLRStringStream(string input) + { + this.data = input.ToCharArray(); + this.n = input.Length; + } + + /// <summary> + /// Initializes a new instance of the ANTLRStringStream class for the + /// specified character array. This is the preferred constructor as + /// no data is copied + /// </summary> + public ANTLRStringStream(char[] data, int numberOfActualCharsInArray) + { + this.data = data; + this.n = numberOfActualCharsInArray; + } + + #endregion + + #region Public API + + /// <summary> + /// Current line position in stream. + /// </summary> + virtual public int Line + { + get { return line; } + set { this.line = value; } + + } + + /// <summary> + /// Current character position on the current line stream + /// (i.e. columnn position) + /// </summary> + virtual public int CharPositionInLine + { + get { return charPositionInLine; } + set { this.charPositionInLine = value; } + + } + + /// <summary> + /// Resets the stream so that it is in the same state it was + /// when the object was created *except* the data array is not + /// touched. + /// </summary> + public virtual void Reset() + { + p = 0; + line = 1; + charPositionInLine = 0; + markDepth = 0; + } + + /// <summary> + /// Advances the read position of the stream. Updates line and column state + /// </summary> + public virtual void Consume() + { + if (p < n) + { + charPositionInLine++; + if (data[p] == '\n') + { + line++; + charPositionInLine = 0; + } + p++; + } + } + + /// <summary> + /// Return lookahead characters at the specified offset from the current read position. + /// The lookahead offset can be negative. + /// </summary> + public virtual int LA(int i) + { + if (i == 0) + { + return 0; // undefined + } + if (i < 0) + { + i++; // e.g., translate LA(-1) to use offset i=0; then data[p+0-1] + if ( (p + i - 1) < 0 ) + { + return (int)CharStreamConstants.EOF; // invalid; no char before first char + } + } + + if ((p + i - 1) >= n) + { + return (int)CharStreamConstants.EOF; + } + return data[p + i - 1]; + } + + public virtual int LT(int i) + { + return LA(i); + } + + /// <summary> + /// Return the current input symbol index 0..n where n indicates the + /// last symbol has been read. The index is the index of char to + /// be returned from LA(1). + /// </summary> + public virtual int Index() + { + return p; + } + + /// <summary> + /// Returns the size of the stream + /// </summary> + [Obsolete("Please use property Count instead.")] + public virtual int Size() + { + return Count; + } + + /// <summary> + /// Returns the size of the stream + /// </summary> + public virtual int Count + { + get { return n; } + } + + public virtual int Mark() + { + if (markers == null) + { + markers = new ArrayList(); + markers.Add(null); // depth 0 means no backtracking, leave blank + } + markDepth++; + CharStreamState state = null; + if (markDepth >= markers.Count) + { + state = new CharStreamState(); + markers.Add(state); + } + else + { + state = (CharStreamState)markers[markDepth]; + } + state.p = p; + state.line = line; + state.charPositionInLine = charPositionInLine; + lastMarker = markDepth; + return markDepth; + } + + public virtual void Rewind(int m) + { + CharStreamState state = (CharStreamState)markers[m]; + // restore stream state + Seek(state.p); + line = state.line; + charPositionInLine = state.charPositionInLine; + Release(m); + } + + public virtual void Rewind() + { + Rewind(lastMarker); + } + + public virtual void Release(int marker) + { + // unwind any other markers made after m and release m + markDepth = marker; + // release this marker + markDepth--; + } + + /// <summary>Seeks to the specified position.</summary> + /// <remarks> + /// Consume ahead until p==index; can't just set p=index as we must + /// update line and charPositionInLine. + /// </remarks> + public virtual void Seek(int index) + { + if (index <= p) + { + p = index; // just jump; don't update stream state (line, ...) + return; + } + // seek forward, consume until p hits index + while (p < index) + { + Consume(); + } + } + + public virtual string Substring(int start, int stop) + { + return new string(data, start, stop - start + 1); + } + + public virtual string SourceName { + get { return name; } + set { name = value; } + } + + #endregion + + + #region Data Members + + /// <summary>The data for the stream</summary> + protected internal char[] data; + + /// <summary>How many characters are actually in the buffer?</summary> + protected int n; + + + /// <summary>Index in our array for the next char (0..n-1)</summary> + protected internal int p = 0; + + /// <summary>Current line number within the input (1..n )</summary> + protected internal int line = 1; + + /// <summary> + /// The index of the character relative to the beginning of the + /// line (0..n-1) + /// </summary> + protected internal int charPositionInLine = 0; + + /// <summary> + /// Tracks the depth of nested <see cref="IIntStream.Mark"/> calls + /// </summary> + protected internal int markDepth = 0; + + /// <summary> + /// A list of CharStreamState objects that tracks the stream state + /// (i.e. line, charPositionInLine, and p) that can change as you + /// move through the input stream. Indexed from 1..markDepth. + /// A null is kept @ index 0. Create upon first call to Mark(). + /// </summary> + protected internal IList markers; + + /// <summary> + /// Track the last Mark() call result value for use in Rewind(). + /// </summary> + protected int lastMarker; + + /// <summary> + /// What is name or source of this char stream? + /// </summary> + protected string name; + + #endregion + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/BaseRecognizer.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/BaseRecognizer.cs new file mode 100644 index 0000000..3d28fb3 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/BaseRecognizer.cs @@ -0,0 +1,1008 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + using IList = System.Collections.IList; + using ArrayList = System.Collections.ArrayList; + using IDictionary = System.Collections.IDictionary; + using Hashtable = System.Collections.Hashtable; + using StackTrace = System.Diagnostics.StackTrace; + using StackFrame = System.Diagnostics.StackFrame; + using CollectionUtils = Antlr.Runtime.Collections.CollectionUtils; + + public delegate void SynPredPointer(); + + /// <summary> + /// A generic recognizer that can handle recognizers generated from + /// lexer, parser, and tree grammars. This is all the parsing + /// support code essentially; most of it is error recovery stuff and + /// backtracking. + /// </summary> + public abstract class BaseRecognizer + { + #region Constructors + + public BaseRecognizer() + { + state = new RecognizerSharedState(); + } + + public BaseRecognizer(RecognizerSharedState state) + { + if ( state==null ) { + state = new RecognizerSharedState(); + } + this.state = state; + } + + #endregion + + + #region Public API + + public virtual void BeginBacktrack(int level) + { + } + + public virtual void EndBacktrack(int level, bool successful) + { + } + + abstract public IIntStream Input { get; } + + public int BacktrackingLevel + { + get { return state.backtracking; } + set { state.backtracking = value; } + } + + /** Return whether or not a backtracking attempt failed. */ + public bool Failed() { return state.failed; } + + /// <summary>Reset the parser's state. Subclasses must rewind the input stream.</summary> + public virtual void Reset() + { + // wack everything related to error recovery + if (state == null) { + return; // no shared state work to do + } + state.followingStackPointer = -1; + state.errorRecovery = false; + state.lastErrorIndex = -1; + state.failed = false; + state.syntaxErrors = 0; + // wack everything related to backtracking and memoization + state.backtracking = 0; + for (int i = 0; (state.ruleMemo != null) && (i < state.ruleMemo.Length); i++) + { + // wipe cache + state.ruleMemo[i] = null; + } + } + + /// <summary> + /// Match current input symbol against ttype. Attempt + /// single token insertion or deletion error recovery. If + /// that fails, throw MismatchedTokenException. + /// </summary> + /// <remarks> + /// To turn off single token insertion or deletion error + /// recovery, override RecoverFromMismatchedToken() and have it call + /// pthrow an exception. See TreeParser.RecoverFromMismatchedToken(). + /// This way any error in a rule will cause an exception and + /// immediate exit from rule. Rule would recover by resynchronizing + /// to the set of symbols that can follow rule ref. + /// </remarks> + public virtual object Match(IIntStream input, int ttype, BitSet follow) { + object matchedSymbol = GetCurrentInputSymbol(input); + if (input.LA(1) == ttype) { + input.Consume(); + state.errorRecovery = false; + state.failed = false; + return matchedSymbol; + } + if (state.backtracking > 0) { + state.failed = true; + return matchedSymbol; + } + matchedSymbol = RecoverFromMismatchedToken(input, ttype, follow); + return matchedSymbol; + } + + /// <summary> Match the wildcard: in a symbol</summary> + public virtual void MatchAny(IIntStream input) + { + state.errorRecovery = false; + state.failed = false; + input.Consume(); + } + + public bool MismatchIsUnwantedToken(IIntStream input, int ttype) { + return input.LA(2) == ttype; + } + + public bool MismatchIsMissingToken(IIntStream input, BitSet follow) { + if (follow == null) { + // we have no information about the follow; we can only consume + // a single token and hope for the best + return false; + } + + // compute what can follow this grammar element reference + if (follow.Member(Token.EOR_TOKEN_TYPE)) { + BitSet viableTokensFollowingThisRule = ComputeContextSensitiveRuleFOLLOW(); + follow = follow.Or(viableTokensFollowingThisRule); + if (state.followingStackPointer >= 0) { // remove EOR if we're not the start symbol + follow.Remove(Token.EOR_TOKEN_TYPE); + } + } + + // if current token is consistent with what could come after set + // then we know we're missing a token; error recovery is free to + // "insert" the missing token + + // BitSet cannot handle negative numbers like -1 (EOF) so I leave EOR + // in follow set to indicate that the fall of the start symbol is + // in the set (EOF can follow). + if ( follow.Member(input.LA(1)) || follow.Member(Token.EOR_TOKEN_TYPE) ) { + return true; + } + return false; + } + + /// <summary> + /// Report a recognition problem. + /// </summary> + /// <remarks> + /// This method sets errorRecovery to indicate the parser is recovering + /// not parsing. Once in recovery mode, no errors are generated. + /// To get out of recovery mode, the parser must successfully Match + /// a token (after a resync). So it will go: + /// + /// 1. error occurs + /// 2. enter recovery mode, report error + /// 3. consume until token found in resynch set + /// 4. try to resume parsing + /// 5. next Match() will reset errorRecovery mode + /// + /// If you override, make sure to update syntaxErrors if you care about that. + /// </remarks> + public virtual void ReportError(RecognitionException e) + { + // if we've already reported an error and have not matched a token + // yet successfully, don't report any errors. + if (state.errorRecovery) + { + return; + } + state.syntaxErrors++; // don't count spurious + state.errorRecovery = true; + + DisplayRecognitionError(this.TokenNames, e); + } + + public virtual void DisplayRecognitionError(string[] tokenNames, RecognitionException e) + { + string hdr = GetErrorHeader(e); + string msg = GetErrorMessage(e, tokenNames); + EmitErrorMessage(hdr + " " + msg); + } + + /// <summary> + /// What error message should be generated for the various exception types? + /// + /// Not very object-oriented code, but I like having all error message generation + /// within one method rather than spread among all of the exception classes. This + /// also makes it much easier for the exception handling because the exception + /// classes do not have to have pointers back to this object to access utility + /// routines and so on. Also, changing the message for an exception type would be + /// difficult because you would have to subclassing exception, but then somehow get + /// ANTLR to make those kinds of exception objects instead of the default. + /// + /// This looks weird, but trust me--it makes the most sense in terms of flexibility. + /// + /// For grammar debugging, you will want to override this to add more information + /// such as the stack frame with GetRuleInvocationStack(e, this.GetType().Fullname) + /// and, for no viable alts, the decision description and state etc... + /// + /// Override this to change the message generated for one or more exception types. + /// </summary> + public virtual string GetErrorMessage(RecognitionException e, string[] tokenNames) + { + string msg = e.Message; + if (e is UnwantedTokenException) { + UnwantedTokenException ute = (UnwantedTokenException)e; + string tokenName="<unknown>"; + if ( ute.Expecting== Token.EOF ) { + tokenName = "EOF"; + } + else { + tokenName = tokenNames[ute.Expecting]; + } + msg = "extraneous input " + GetTokenErrorDisplay(ute.UnexpectedToken) + + " expecting " + tokenName; + } + else if (e is MissingTokenException) { + MissingTokenException mte = (MissingTokenException)e; + string tokenName="<unknown>"; + if (mte.Expecting == Token.EOF) { + tokenName = "EOF"; + } + else { + tokenName = tokenNames[mte.Expecting]; + } + msg = "missing " + tokenName + " at " + GetTokenErrorDisplay(e.Token); + } + else if (e is MismatchedTokenException) + { + MismatchedTokenException mte = (MismatchedTokenException)e; + string tokenName = "<unknown>"; + if (mte.Expecting == Token.EOF) + { + tokenName = "EOF"; + } + else + { + tokenName = tokenNames[mte.Expecting]; + } + msg = "mismatched input " + GetTokenErrorDisplay(e.Token) + " expecting " + tokenName; + + } + else if (e is MismatchedTreeNodeException) + { + MismatchedTreeNodeException mtne = (MismatchedTreeNodeException)e; + string tokenName = "<unknown>"; + if (mtne.expecting == Token.EOF) + { + tokenName = "EOF"; + } + else + { + tokenName = tokenNames[mtne.expecting]; + } + // The ternary operator is only necessary because of a bug in the .NET framework + msg = "mismatched tree node: " + ((mtne.Node!=null && mtne.Node.ToString()!=null) ? + mtne.Node : string.Empty) + " expecting " + tokenName; + } + else if (e is NoViableAltException) + { + //NoViableAltException nvae = (NoViableAltException)e; + // for development, can add "decision=<<"+nvae.grammarDecisionDescription+">>" + // and "(decision="+nvae.decisionNumber+") and + // "state "+nvae.stateNumber + msg = "no viable alternative at input " + GetTokenErrorDisplay(e.Token); + } + else if (e is EarlyExitException) + { + //EarlyExitException eee = (EarlyExitException)e; + // for development, can add "(decision="+eee.decisionNumber+")" + msg = "required (...)+ loop did not match anything at input " + GetTokenErrorDisplay(e.Token); + } + else if (e is MismatchedSetException) + { + MismatchedSetException mse = (MismatchedSetException)e; + msg = "mismatched input " + GetTokenErrorDisplay(e.Token) + " expecting set " + mse.expecting; + } + else if (e is MismatchedNotSetException) + { + MismatchedNotSetException mse = (MismatchedNotSetException)e; + msg = "mismatched input " + GetTokenErrorDisplay(e.Token) + " expecting set " + mse.expecting; + } + else if (e is FailedPredicateException) + { + FailedPredicateException fpe = (FailedPredicateException)e; + msg = "rule " + fpe.ruleName + " failed predicate: {" + fpe.predicateText + "}?"; + } + return msg; + } + + /// <summary> + /// Get number of recognition errors (lexer, parser, tree parser). Each + /// recognizer tracks its own number. So parser and lexer each have + /// separate count. Does not count the spurious errors found between + /// an error and next valid token match + /// + /// See also ReportError() + /// </summary> + public int NumberOfSyntaxErrors { + get { return state.syntaxErrors; } + } + + /// <summary> + /// What is the error header, normally line/character position information? + /// </summary> + public virtual string GetErrorHeader(RecognitionException e) + { + return "line " + e.Line + ":" + e.CharPositionInLine; + } + + /// <summary> + /// How should a token be displayed in an error message? The default + /// is to display just the text, but during development you might + /// want to have a lot of information spit out. Override in that case + /// to use t.ToString() (which, for CommonToken, dumps everything about + /// the token). This is better than forcing you to override a method in + /// your token objects because you don't have to go modify your lexer + /// so that it creates a new type. + /// </summary> + public virtual string GetTokenErrorDisplay(IToken t) + { + string s = t.Text; + if (s == null) + { + if (t.Type == Token.EOF) + { + s = "<EOF>"; + } + else + { + s = "<" + t.Type + ">"; + } + } + s = s.Replace("\n", "\\\\n"); + s = s.Replace("\r", "\\\\r"); + s = s.Replace("\t", "\\\\t"); + return "'" + s + "'"; + } + + /// <summary> + /// Override this method to change where error messages go + /// </summary> + public virtual void EmitErrorMessage(string msg) + { + Console.Error.WriteLine(msg); + } + + /// <summary> + /// Recover from an error found on the input stream. This is + /// for NoViableAlt and mismatched symbol exceptions. If you enable + /// single token insertion and deletion, this will usually not + /// handle mismatched symbol exceptions but there could be a mismatched + /// token that the Match() routine could not recover from. + /// </summary> + public virtual void Recover(IIntStream input, RecognitionException re) + { + if (state.lastErrorIndex == input.Index()) + { + // uh oh, another error at same token index; must be a case + // where LT(1) is in the recovery token set so nothing is + // consumed; consume a single token so at least to prevent + // an infinite loop; this is a failsafe. + input.Consume(); + } + state.lastErrorIndex = input.Index(); + BitSet followSet = ComputeErrorRecoverySet(); + BeginResync(); + ConsumeUntil(input, followSet); + EndResync(); + } + + /// <summary>A hook to listen in on the token consumption during error recovery. + /// The DebugParser subclasses this to fire events to the listenter. + /// </summary> + public virtual void BeginResync() + { + } + + public virtual void EndResync() + { + } + + /// <summary> + /// Attempt to Recover from a single missing or extra token. + /// </summary> + /// <remarks> + /// EXTRA TOKEN + /// + /// LA(1) is not what we are looking for. If LA(2) has the right token, + /// however, then assume LA(1) is some extra spurious token. Delete it + /// and LA(2) as if we were doing a normal Match(), which advances the + /// input. + /// + /// MISSING TOKEN + /// + /// If current token is consistent with what could come after + /// ttype then it is ok to "insert" the missing token, else throw + /// exception For example, Input "i=(3;" is clearly missing the + /// ')'. When the parser returns from the nested call to expr, it + /// will have call chain: + /// + /// stat -> expr -> atom + /// + /// and it will be trying to Match the ')' at this point in the + /// derivation: + /// + /// => ID '=' '(' INT ')' ('+' atom)* ';' + /// ^ + /// Match() will see that ';' doesn't Match ')' and report a + /// mismatched token error. To Recover, it sees that LA(1)==';' + /// is in the set of tokens that can follow the ')' token + /// reference in rule atom. It can assume that you forgot the ')'. + /// </remarks> + protected internal virtual Object RecoverFromMismatchedToken(IIntStream input, int ttype, BitSet follow) { + RecognitionException e = null; + // if next token is what we are looking for then "delete" this token + if (MismatchIsUnwantedToken(input, ttype)) { + e = new UnwantedTokenException(ttype, input); + BeginResync(); + input.Consume(); // simply delete extra token + EndResync(); + ReportError(e); // report after consuming so AW sees the token in the exception + // we want to return the token we're actually matching + object matchedSymbol = GetCurrentInputSymbol(input); + input.Consume(); // move past ttype token as if all were ok + return matchedSymbol; + } + // can't recover with single token deletion, try insertion + if (MismatchIsMissingToken(input, follow)) { + object inserted = GetMissingSymbol(input, e, ttype, follow); + e = new MissingTokenException(ttype, input, inserted); + ReportError(e); // report after inserting so AW sees the token in the exception + return inserted; + } + // even that didn't work; must throw the exception + e = new MismatchedTokenException(ttype, input); + throw e; + } + + /** Not currently used */ + public virtual object RecoverFromMismatchedSet(IIntStream input, RecognitionException e, BitSet follow) { + if (MismatchIsMissingToken(input, follow)) { + ReportError(e); + // we don't know how to conjure up a token for sets yet + return GetMissingSymbol(input, e, Token.INVALID_TOKEN_TYPE, follow); + } + // TODO do single token deletion like above for Token mismatch + throw e; + } + + public virtual void ConsumeUntil(IIntStream input, int tokenType) + { + int ttype = input.LA(1); + while (ttype != Token.EOF && ttype != tokenType) + { + input.Consume(); + ttype = input.LA(1); + } + } + + /// <summary>Consume tokens until one matches the given token set </summary> + public virtual void ConsumeUntil(IIntStream input, BitSet set) + { + int ttype = input.LA(1); + while (ttype != Token.EOF && !set.Member(ttype)) + { + input.Consume(); + ttype = input.LA(1); + } + } + + /// <summary> + /// Returns List <String> of the rules in your parser instance + /// leading up to a call to this method. You could override if + /// you want more details such as the file/line info of where + /// in the parser source code a rule is invoked. + /// </summary> + /// <remarks> + /// This is very useful for error messages and for context-sensitive + /// error recovery. + /// </remarks> + public virtual IList GetRuleInvocationStack() + { + string parserClassName = GetType().FullName; + return GetRuleInvocationStack(new System.Exception(), parserClassName); + } + + /// <summary> + /// A more general version of GetRuleInvocationStack where you can + /// pass in, for example, a RecognitionException to get it's rule + /// stack trace. This routine is shared with all recognizers, hence, + /// static. + /// + /// TODO: move to a utility class or something; weird having lexer call this + /// </summary> + public static IList GetRuleInvocationStack(Exception e, string recognizerClassName) + { + IList rules = new ArrayList(); + StackTrace st = new StackTrace(e); + int i = 0; + for (i = st.FrameCount - 1; i >= 0; i--) + { + StackFrame sf = st.GetFrame(i); + if (sf.GetMethod().DeclaringType.FullName.StartsWith("Antlr.Runtime.")) + { + continue; // skip support code such as this method + } + if (sf.GetMethod().Name.Equals(NEXT_TOKEN_RULE_NAME)) + { + continue; + } + if (!sf.GetMethod().DeclaringType.FullName.Equals(recognizerClassName)) + { + continue; // must not be part of this parser + } + //kunle: rules.Add(sf.GetMethod().DeclaringType.FullName + "." + sf.GetMethod().Name); + rules.Add(sf.GetMethod().Name); + } + return rules; + } + + /// <summary> + /// For debugging and other purposes, might want the grammar name. + /// Have ANTLR generate an implementation for this property. + /// </summary> + /// <returns></returns> + public virtual string GrammarFileName + { + get { return null; } + } + + /// <summary> + /// For debugging and other purposes, might want the source name. + /// Have ANTLR provide a hook for this property. + /// </summary> + /// <returns>The source name</returns> + public abstract string SourceName { + get; + } + + /// <summary>A convenience method for use most often with template rewrites. + /// Convert a List<Token> to List<String> + /// </summary> + public virtual IList ToStrings(IList tokens) + { + if (tokens == null) + return null; + IList strings = new ArrayList(tokens.Count); + for (int i = 0; i < tokens.Count; i++) + { + strings.Add(((IToken)tokens[i]).Text); + } + return strings; + } + + /// <summary> + /// Given a rule number and a start token index number, return + /// MEMO_RULE_UNKNOWN if the rule has not parsed input starting from + /// start index. If this rule has parsed input starting from the + /// start index before, then return where the rule stopped parsing. + /// It returns the index of the last token matched by the rule. + /// </summary> + /// <remarks> + /// For now we use a hashtable and just the slow Object-based one. + /// Later, we can make a special one for ints and also one that + /// tosses out data after we commit past input position i. + /// </remarks> + public virtual int GetRuleMemoization(int ruleIndex, int ruleStartIndex) + { + if (state.ruleMemo[ruleIndex] == null) + { + state.ruleMemo[ruleIndex] = new Hashtable(); + } + object stopIndexI = state.ruleMemo[ruleIndex][(int)ruleStartIndex]; + if (stopIndexI == null) + { + return MEMO_RULE_UNKNOWN; + } + return (int)stopIndexI; + } + + /// <summary> + /// Has this rule already parsed input at the current index in the + /// input stream? Return the stop token index or MEMO_RULE_UNKNOWN. + /// If we attempted but failed to parse properly before, return + /// MEMO_RULE_FAILED. + /// + /// This method has a side-effect: if we have seen this input for + /// this rule and successfully parsed before, then seek ahead to + /// 1 past the stop token matched for this rule last time. + /// </summary> + public virtual bool AlreadyParsedRule(IIntStream input, int ruleIndex) + { + int stopIndex = GetRuleMemoization(ruleIndex, input.Index()); + if (stopIndex == MEMO_RULE_UNKNOWN) + { + return false; + } + if (stopIndex == MEMO_RULE_FAILED) + { + state.failed = true; + } + else + { + input.Seek(stopIndex + 1); // jump to one past stop token + } + return true; + } + + /// <summary> + /// Record whether or not this rule parsed the input at this position + /// successfully. Use a standard hashtable for now. + /// </summary> + public virtual void Memoize(IIntStream input, int ruleIndex, int ruleStartIndex) + { + int stopTokenIndex = state.failed ? MEMO_RULE_FAILED : input.Index() - 1; + if (state.ruleMemo[ruleIndex] != null) + { + state.ruleMemo[ruleIndex][(int)ruleStartIndex] = (int)stopTokenIndex; + } + } + + /// <summary> + /// Return how many rule/input-index pairs there are in total. + /// TODO: this includes synpreds. :( + /// </summary> + /// <returns></returns> + public int GetRuleMemoizationCacheSize() + { + int n = 0; + for (int i = 0; state.ruleMemo != null && i < state.ruleMemo.Length; i++) + { + IDictionary ruleMap = state.ruleMemo[i]; + if (ruleMap != null) + { + n += ruleMap.Count; // how many input indexes are recorded? + } + } + return n; + } + + public virtual void TraceIn(string ruleName, int ruleIndex, object inputSymbol) + { + Console.Out.Write("enter " + ruleName + " " + inputSymbol); + if (state.backtracking > 0) + { + Console.Out.Write(" backtracking=" + state.backtracking); + } + Console.Out.WriteLine(); + } + + public virtual void TraceOut(string ruleName, int ruleIndex, object inputSymbol) { + Console.Out.Write("exit " + ruleName + " " + inputSymbol); + if (state.backtracking > 0) { + Console.Out.Write(" backtracking=" + state.backtracking); + if (state.failed) { + Console.Out.WriteLine(" failed" + state.failed); + } else { + Console.Out.WriteLine(" succeeded" + state.failed); + } + } + Console.Out.WriteLine(); + } + + + /// <summary> + /// Used to print out token names like ID during debugging and + /// error reporting. The generated parsers implement a method + /// that overrides this to point to their string[] tokenNames. + /// </summary> + virtual public string[] TokenNames + { + get { return null; } + } + + #endregion + + + #region Helper Methods + + /// <summary> + /// Factor out what to do upon token mismatch so tree parsers can behave + /// differently. Override and call RecoverFromMismatchedToken() + /// to get single token insertion and deletion. Use this to turn off + /// single token insertion and deletion. Override mismatchRecover + /// to call this instead. + /// + /// TODO: fix this comment, mismatchRecover doesn't exist, for example + /// </summary> + /* + protected internal virtual void Mismatch(IIntStream input, int ttype, BitSet follow) + { + if ( MismatchIsUnwantedToken(input, ttype) ) { + throw new UnwantedTokenException(ttype, input); + } + else if ( MismatchIsMissingToken(input, follow) ) { + throw new MissingTokenException(ttype, input, null); + } + throw new MismatchedTokenException(ttype, input); + }*/ + + /* Compute the error recovery set for the current rule. During + * rule invocation, the parser pushes the set of tokens that can + * follow that rule reference on the stack; this amounts to + * computing FIRST of what follows the rule reference in the + * enclosing rule. This local follow set only includes tokens + * from within the rule; i.e., the FIRST computation done by + * ANTLR stops at the end of a rule. + * + * EXAMPLE + * + * When you find a "no viable alt exception", the input is not + * consistent with any of the alternatives for rule r. The best + * thing to do is to consume tokens until you see something that + * can legally follow a call to r *or* any rule that called r. + * You don't want the exact set of viable next tokens because the + * input might just be missing a token--you might consume the + * rest of the input looking for one of the missing tokens. + * + * Consider grammar: + * + * a : '[' b ']' + * | '(' b ')' + * ; + * b : c '^' INT ; + * c : ID + * | INT + * ; + * + * At each rule invocation, the set of tokens that could follow + * that rule is pushed on a stack. Here are the various "local" + * follow sets: + * + * FOLLOW(b1_in_a) = FIRST(']') = ']' + * FOLLOW(b2_in_a) = FIRST(')') = ')' + * FOLLOW(c_in_b) = FIRST('^') = '^' + * + * Upon erroneous input "[]", the call chain is + * + * a -> b -> c + * + * and, hence, the follow context stack is: + * + * depth local follow set after call to rule + * 0 <EOF> a (from main()) + * 1 ']' b + * 3 '^' c + * + * Notice that ')' is not included, because b would have to have + * been called from a different context in rule a for ')' to be + * included. + * + * For error recovery, we cannot consider FOLLOW(c) + * (context-sensitive or otherwise). We need the combined set of + * all context-sensitive FOLLOW sets--the set of all tokens that + * could follow any reference in the call chain. We need to + * resync to one of those tokens. Note that FOLLOW(c)='^' and if + * we resync'd to that token, we'd consume until EOF. We need to + * sync to context-sensitive FOLLOWs for a, b, and c: {']','^'}. + * In this case, for input "[]", LA(1) is in this set so we would + * not consume anything and after printing an error rule c would + * return normally. It would not find the required '^' though. + * At this point, it gets a mismatched token error and throws an + * exception (since LA(1) is not in the viable following token + * set). The rule exception handler tries to Recover, but finds + * the same recovery set and doesn't consume anything. Rule b + * exits normally returning to rule a. Now it finds the ']' (and + * with the successful Match exits errorRecovery mode). + * + * So, you cna see that the parser walks up call chain looking + * for the token that was a member of the recovery set. + * + * Errors are not generated in errorRecovery mode. + * + * ANTLR's error recovery mechanism is based upon original ideas: + * + * "Algorithms + Data Structures = Programs" by Niklaus Wirth + * + * and + * + * "A note on error recovery in recursive descent parsers": + * http://portal.acm.org/citation.cfm?id=947902.947905 + * + * Later, Josef Grosch had some good ideas: + * + * "Efficient and Comfortable Error Recovery in Recursive Descent + * Parsers": + * ftp://www.cocolab.com/products/cocktail/doca4.ps/ell.ps.zip + * + * Like Grosch I implemented local FOLLOW sets that are combined + * at run-time upon error to avoid overhead during parsing. + */ + protected internal virtual BitSet ComputeErrorRecoverySet() + { + return CombineFollows(false); + } + + /// <summary>Compute the context-sensitive FOLLOW set for current rule. + /// This is set of token types that can follow a specific rule + /// reference given a specific call chain. You get the set of + /// viable tokens that can possibly come next (lookahead depth 1) + /// given the current call chain. Contrast this with the + /// definition of plain FOLLOW for rule r: + /// + /// FOLLOW(r)={x | S=>*alpha r beta in G and x in FIRST(beta)} + /// + /// where x in T* and alpha, beta in V*; T is set of terminals and + /// V is the set of terminals and nonterminals. In other words, + /// FOLLOW(r) is the set of all tokens that can possibly follow + /// references to r in *any* sentential form (context). At + /// runtime, however, we know precisely which context applies as + /// we have the call chain. We may compute the exact (rather + /// than covering superset) set of following tokens. + /// + /// For example, consider grammar: + /// + /// stat : ID '=' expr ';' // FOLLOW(stat)=={EOF} + /// | "return" expr '.' + /// ; + /// expr : atom ('+' atom)* ; // FOLLOW(expr)=={';','.',')'} + /// atom : INT // FOLLOW(atom)=={'+',')',';','.'} + /// | '(' expr ')' + /// ; + /// + /// The FOLLOW sets are all inclusive whereas context-sensitive + /// FOLLOW sets are precisely what could follow a rule reference. + /// For input input "i=(3);", here is the derivation: + /// + /// stat => ID '=' expr ';' + /// => ID '=' atom ('+' atom)* ';' + /// => ID '=' '(' expr ')' ('+' atom)* ';' + /// => ID '=' '(' atom ')' ('+' atom)* ';' + /// => ID '=' '(' INT ')' ('+' atom)* ';' + /// => ID '=' '(' INT ')' ';' + /// + /// At the "3" token, you'd have a call chain of + /// + /// stat -> expr -> atom -> expr -> atom + /// + /// What can follow that specific nested ref to atom? Exactly ')' + /// as you can see by looking at the derivation of this specific + /// input. Contrast this with the FOLLOW(atom)={'+',')',';','.'}. + /// + /// You want the exact viable token set when recovering from a + /// token mismatch. Upon token mismatch, if LA(1) is member of + /// the viable next token set, then you know there is most likely + /// a missing token in the input stream. "Insert" one by just not + /// throwing an exception. + /// </summary> + protected internal virtual BitSet ComputeContextSensitiveRuleFOLLOW() + { + return CombineFollows(true); + } + + protected internal virtual BitSet CombineFollows(bool exact) + { + int top = state.followingStackPointer; + BitSet followSet = new BitSet(); + for (int i = top; i >= 0; i--) + { + BitSet localFollowSet = (BitSet)state.following[i]; + followSet.OrInPlace(localFollowSet); + if (exact) { + // can we see end of rule? + if (localFollowSet.Member(Token.EOR_TOKEN_TYPE)) { + // Only leave EOR in set if at top (start rule); this lets + // us know if have to include follow(start rule); i.e., EOF + if (i > 0) { + followSet.Remove(Token.EOR_TOKEN_TYPE); + } + } + else { // can't see end of rule, quit + break; + } + } + } + return followSet; + } + + /// <summary> + /// Match needs to return the current input symbol, which gets put + /// into the label for the associated token ref; e.g., x=ID. Token + /// and tree parsers need to return different objects. Rather than test + /// for input stream type or change the IntStream interface, I use + /// a simple method to ask the recognizer to tell me what the current + /// input symbol is. + /// </summary> + /// <remarks>This is ignored for lexers.</remarks> + protected virtual object GetCurrentInputSymbol(IIntStream input) { + return null; + } + + /// <summary> + /// Conjure up a missing token during error recovery. + /// </summary> + /// <remarks> + /// The recognizer attempts to recover from single missing + /// symbols. But, actions might refer to that missing symbol. + /// For example, x=ID {f($x);}. The action clearly assumes + /// that there has been an identifier matched previously and that + /// $x points at that token. If that token is missing, but + /// the next token in the stream is what we want we assume that + /// this token is missing and we keep going. Because we + /// have to return some token to replace the missing token, + /// we have to conjure one up. This method gives the user control + /// over the tokens returned for missing tokens. Mostly, + /// you will want to create something special for identifier + /// tokens. For literals such as '{' and ',', the default + /// action in the parser or tree parser works. It simply creates + /// a CommonToken of the appropriate type. The text will be the token. + /// If you change what tokens must be created by the lexer, + /// override this method to create the appropriate tokens. + /// </remarks> + protected virtual object GetMissingSymbol(IIntStream input, + RecognitionException e, + int expectedTokenType, + BitSet follow) { + return null; + } + + /// <summary> + /// Push a rule's follow set using our own hardcoded stack + /// </summary> + /// <param name="fset"></param> + protected void PushFollow(BitSet fset) + { + if ((state.followingStackPointer + 1) >= state.following.Length) + { + BitSet[] f = new BitSet[state.following.Length * 2]; + Array.Copy(state.following, 0, f, 0, state.following.Length); + state.following = f; + } + state.following[++state.followingStackPointer] = fset; + } + + #endregion + + + #region Data Members + + public const int MEMO_RULE_FAILED = -2; + public const int MEMO_RULE_UNKNOWN = -1; + public const int INITIAL_FOLLOW_STACK_SIZE = 100; + + // copies from Token object for convenience in actions + public const int DEFAULT_TOKEN_CHANNEL = Token.DEFAULT_CHANNEL; + public const int HIDDEN = Token.HIDDEN_CHANNEL; + + public static readonly string NEXT_TOKEN_RULE_NAME = "nextToken"; + + /// <summary> + /// An externalized representation of the - shareable - internal state of + /// this lexer, parser or tree parser. + /// </summary> + /// <remarks> + /// The state of a lexer, parser, or tree parser are collected into + /// external state objects so that the state can be shared. This sharing + /// is needed to have one grammar import others and share same error + /// variables and other state variables. It's a kind of explicit multiple + /// inheritance via delegation of methods and shared state. + /// </remarks> + protected internal RecognizerSharedState state; + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/BitSet.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/BitSet.cs new file mode 100644 index 0000000..6c88d09 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/BitSet.cs @@ -0,0 +1,413 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + using IList = System.Collections.IList; + using StringBuilder = System.Text.StringBuilder; + + /// <summary> + /// A stripped-down version of org.antlr.misc.BitSet that is just + /// good enough to handle runtime requirements such as FOLLOW sets + /// for automatic error recovery. + /// </summary> + public class BitSet : ICloneable + { + #region Constructors + + /// <summary>Construct a bitset of size one word (64 bits) </summary> + public BitSet() + : this(BITS) + { + } + + /// <summary>Construction from a static array of ulongs </summary> + public BitSet(ulong[] bits_) + { + bits = bits_; + } + + /// <summary>Construction from a list of integers </summary> + public BitSet(IList items) + : this(BITS) + { + for (int i = 0; i < items.Count; i++) + { + int v = (int)items[i]; + Add(v); + } + } + + /// <summary>Construct a bitset given the size</summary> + /// <param name="nbits">The size of the bitset in bits</param> + public BitSet(int nbits) + { + bits = new ulong[((nbits - 1) >> LOG_BITS) + 1]; + } + + #endregion + + #region Public API + + public static BitSet Of(int el) + { + BitSet s = new BitSet(el + 1); + s.Add(el); + return s; + } + + public static BitSet Of(int a, int b) + { + BitSet s = new BitSet(Math.Max(a, b) + 1); + s.Add(a); + s.Add(b); + return s; + } + + public static BitSet Of(int a, int b, int c) + { + BitSet s = new BitSet(); + s.Add(a); + s.Add(b); + s.Add(c); + return s; + } + + public static BitSet Of(int a, int b, int c, int d) + { + BitSet s = new BitSet(); + s.Add(a); + s.Add(b); + s.Add(c); + s.Add(d); + return s; + } + + /// <summary>return "this | a" in a new set </summary> + public virtual BitSet Or(BitSet a) + { + if (a == null) + { + return this; + } + BitSet s = (BitSet)this.Clone(); + s.OrInPlace(a); + return s; + } + + /// <summary>Or this element into this set (grow as necessary to accommodate)</summary> + public virtual void Add(int el) + { + int n = WordNumber(el); + if (n >= bits.Length) + { + GrowToInclude(el); + } + bits[n] |= BitMask(el); + } + + /// <summary> Grows the set to a larger number of bits.</summary> + /// <param name="bit">element that must fit in set + /// </param> + public virtual void GrowToInclude(int bit) + { + int newSize = Math.Max(bits.Length << 1, NumWordsToHold(bit)); + ulong[] newbits = new ulong[newSize]; + Array.Copy(bits, 0, newbits, 0, bits.Length); + bits = newbits; + } + + public virtual void OrInPlace(BitSet a) + { + if (a == null) + { + return; + } + // If this is smaller than a, grow this first + if (a.bits.Length > bits.Length) + { + SetSize(a.bits.Length); + } + int min = Math.Min(bits.Length, a.bits.Length); + for (int i = min - 1; i >= 0; i--) + { + bits[i] |= a.bits[i]; + } + } + + virtual public bool Nil + { + get + { + for (int i = bits.Length - 1; i >= 0; i--) + { + if (bits[i] != 0) + return false; + } + return true; + } + + } + + public virtual object Clone() + { + BitSet s; + try + { + s = (BitSet)MemberwiseClone(); + s.bits = new ulong[bits.Length]; + Array.Copy(bits, 0, s.bits, 0, bits.Length); + } + catch (System.Exception e) + { + throw new InvalidOperationException("Unable to clone BitSet", e); + } + return s; + } + + public virtual int Count + { + get + { + int deg = 0; + for (int i = bits.Length - 1; i >= 0; i--) + { + ulong word = bits[i]; + if (word != 0L) + { + for (int bit = BITS - 1; bit >= 0; bit--) + { + if ((word & (1UL << bit)) != 0) + { + deg++; + } + } + } + } + return deg; + } + } + + public virtual bool Member(int el) + { + if (el < 0) + { + return false; + } + int n = WordNumber(el); + if (n >= bits.Length) + return false; + return (bits[n] & BitMask(el)) != 0; + } + + // remove this element from this set + public virtual void Remove(int el) + { + int n = WordNumber(el); + if (n < bits.Length) + { + bits[n] &= ~BitMask(el); + } + } + + public virtual int NumBits() + { + return bits.Length << LOG_BITS; // num words * bits per word + } + + /// <summary>return how much space is being used by the bits array not + /// how many actually have member bits on. + /// </summary> + public virtual int LengthInLongWords() + { + return bits.Length; + } + + public virtual int[] ToArray() + { + int[] elems = new int[Count]; + int en = 0; + for (int i = 0; i < (bits.Length << LOG_BITS); i++) + { + if (Member(i)) + { + elems[en++] = i; + } + } + return elems; + } + + public virtual ulong[] ToPackedArray() + { + return bits; + } + + private static int WordNumber(int bit) + { + return bit >> LOG_BITS; // bit / BITS + } + + public override string ToString() + { + return ToString(null); + } + + public virtual string ToString(string[] tokenNames) + { + StringBuilder buf = new StringBuilder(); + string separator = ","; + bool havePrintedAnElement = false; + + buf.Append('{'); + + for (int i = 0; i < (bits.Length << LOG_BITS); i++) + { + if (Member(i)) + { + if (i > 0 && havePrintedAnElement) + { + buf.Append(separator); + } + if (tokenNames != null) + { + buf.Append(tokenNames[i]); + } + else + { + buf.Append(i); + } + havePrintedAnElement = true; + } + } + buf.Append('}'); + return buf.ToString(); + } + + public override bool Equals(object other) + { + if (other == null || !(other is BitSet)) + { + return false; + } + + BitSet otherSet = (BitSet)other; + + int n = Math.Min(this.bits.Length, otherSet.bits.Length); + + // for any bits in common, compare + for (int i = 0; i < n; i++) + { + if (this.bits[i] != otherSet.bits[i]) + { + return false; + } + } + + // make sure any extra bits are off + + if (this.bits.Length > n) + { + for (int i = n + 1; i < this.bits.Length; i++) + { + if (this.bits[i] != 0) + { + return false; + } + } + } + else if (otherSet.bits.Length > n) + { + for (int i = n + 1; i < otherSet.bits.Length; i++) + { + if (otherSet.bits[i] != 0) + { + return false; + } + } + } + return true; + } + + public override int GetHashCode() + { + return base.GetHashCode(); + } + + #endregion + + #region Data Members + + protected internal const int BITS = 64; // number of bits / ulong + protected internal const int LOG_BITS = 6; // 2^6 == 64 + + ///<summary> We will often need to do a mod operator (i mod nbits). + /// Its turns out that, for powers of two, this mod operation is + /// same as <![CDATA[(i & (nbits-1))]]>. Since mod is slow, we use a precomputed + /// mod mask to do the mod instead. + /// </summary> + protected internal static readonly int MOD_MASK = BITS - 1; + + /// <summary>The actual data bits </summary> + protected internal ulong[] bits; + + #endregion + + #region Private API + + private static ulong BitMask(int bitNumber) + { + int bitPosition = bitNumber & MOD_MASK; // bitNumber mod BITS + return 1UL << bitPosition; + } + + /// <summary> Sets the size of a set.</summary> + /// <param name="nwords">how many words the new set should be + /// </param> + private void SetSize(int nwords) + { + ulong[] newbits = new ulong[nwords]; + int n = Math.Min(nwords, bits.Length); + Array.Copy(bits, 0, newbits, 0, n); + bits = newbits; + } + + private int NumWordsToHold(int el) + { + return (el >> LOG_BITS) + 1; + } + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/CharStreamState.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/CharStreamState.cs new file mode 100644 index 0000000..778facd --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/CharStreamState.cs @@ -0,0 +1,57 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + /// <summary> + /// This is the complete state of a stream. + /// + /// When walking ahead with cyclic DFA for syntactic predicates, we + /// need to record the state of the input stream (char index, line, + /// etc...) so that we can rewind the state after scanning ahead. + /// </summary> + public class CharStreamState + { + /// <summary>Index into the char stream of next lookahead char </summary> + internal int p; + + /// <summary>What line number is the scanner at before processing buffer[p]? </summary> + internal int line; + + /// <summary>What char position 0..n-1 in line is scanner before processing buffer[p]? </summary> + internal int charPositionInLine; + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ClassicToken.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ClassicToken.cs new file mode 100644 index 0000000..4fc73b5 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ClassicToken.cs @@ -0,0 +1,161 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + /// <summary> + /// A Token object like we'd use in ANTLR 2.x; has an actual string created + /// and associated with this object. These objects are needed for imaginary + /// tree nodes that have payload objects. We need to create a Token object + /// that has a string; the tree node will point at this token. CommonToken + /// has indexes into a char stream and hence cannot be used to introduce + /// new strings. + /// </summary> + [Serializable] + public class ClassicToken : IToken + { + #region Constructors + + public ClassicToken(int type) + { + this.type = type; + } + + public ClassicToken(IToken oldToken) + { + text = oldToken.Text; + type = oldToken.Type; + line = oldToken.Line; + charPositionInLine = oldToken.CharPositionInLine; + channel = oldToken.Channel; + } + + public ClassicToken(int type, string text) + { + this.type = type; + this.text = text; + } + + public ClassicToken(int type, string text, int channel) + { + this.type = type; + this.text = text; + this.channel = channel; + } + + #endregion + + #region Public API + + virtual public int Type + { + get { return type; } + set { this.type = value; } + } + + virtual public int Line + { + get { return line; } + set { this.line = value; } + } + + virtual public int CharPositionInLine + { + get { return charPositionInLine; } + set { this.charPositionInLine = value; } + } + + virtual public int Channel + { + get { return channel; } + set { this.channel = value; } + } + + virtual public int TokenIndex + { + get { return index; } + set { this.index = value; } + } + + virtual public string Text + { + get { return text; } + set { text = value; } + } + + virtual public ICharStream InputStream + { + get { return null; } + set { } + } + + override public string ToString() + { + string channelStr = ""; + if (channel > 0) + { + channelStr = ",channel=" + channel; + } + string txt = Text; + if (txt != null) + { + txt = txt.Replace("\n", "\\\\n"); + txt = txt.Replace("\r", "\\\\r"); + txt = txt.Replace("\t", "\\\\t"); + } + else + { + txt = "<no text>"; + } + return "[@" + TokenIndex + ",'" + txt + "',<" + type + ">" + channelStr + "," + line + ":" + CharPositionInLine + "]"; + } + + #endregion + + #region Data Members + + protected internal string text; + protected internal int type; + protected internal int line; + protected internal int charPositionInLine; + protected internal int channel = Token.DEFAULT_CHANNEL; + + /// <summary>What token number is this from 0..n-1 tokens </summary> + protected internal int index; + + #endregion + } +} diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/CommonToken.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/CommonToken.cs new file mode 100644 index 0000000..473ff63 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/CommonToken.cs @@ -0,0 +1,207 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + [Serializable] + public class CommonToken : IToken + { + #region Constructors + + public CommonToken(int type) + { + this.type = type; + } + + public CommonToken(ICharStream input, int type, int channel, int start, int stop) + { + this.input = input; + this.type = type; + this.channel = channel; + this.start = start; + this.stop = stop; + } + + public CommonToken(int type, string text) + { + this.type = type; + this.channel = Token.DEFAULT_CHANNEL; + this.text = text; + } + + public CommonToken(IToken oldToken) + { + text = oldToken.Text; + type = oldToken.Type; + line = oldToken.Line; + index = oldToken.TokenIndex; + charPositionInLine = oldToken.CharPositionInLine; + channel = oldToken.Channel; + if (oldToken is CommonToken) { + start = ((CommonToken)oldToken).start; + stop = ((CommonToken)oldToken).stop; + } + } + + #endregion + + #region Public API + + virtual public int Type + { + get { return type; } + set { this.type = value; } + } + + virtual public int Line + { + get { return line; } + set { this.line = value; } + } + + virtual public int CharPositionInLine + { + get { return charPositionInLine; } + set { this.charPositionInLine = value; } + } + + virtual public int Channel + { + get { return channel; } + set { this.channel = value; } + } + + virtual public int StartIndex + { + get { return start; } + set { this.start = value; } + } + + virtual public int StopIndex + { + get { return stop; } + set { this.stop = value; } + } + + virtual public int TokenIndex + { + get { return index; } + set { this.index = value; } + } + + virtual public ICharStream InputStream + { + get { return input; } + set { this.input = value; } + } + + virtual public string Text + { + get + { + if (text != null) + { + return text; + } + if (input == null) + { + return null; + } + text = input.Substring(start, stop); + return text; + } + set + { + /* Override the text for this token. The property getter + * will return this text rather than pulling from the buffer. + * Note that this does not mean that start/stop indexes are + * not valid. It means that the input was converted to a new + * string in the token object. + */ + this.text = value; + } + } + + public override string ToString() + { + string channelStr = ""; + if (channel > 0) + { + channelStr = ",channel=" + channel; + } + string txt = Text; + if (txt != null) + { + txt = txt.Replace("\n", "\\\\n"); + txt = txt.Replace("\r", "\\\\r"); + txt = txt.Replace("\t", "\\\\t"); + } + else + { + txt = "<no text>"; + } + return "[@" + TokenIndex + "," + start + ":" + stop + "='" + txt + "',<" + type + ">" + channelStr + "," + line + ":" + CharPositionInLine + "]"; + } + + #endregion + + #region Data Members + + protected internal int type; + protected internal int line; + protected internal int charPositionInLine = -1; // set to invalid position + protected internal int channel = Token.DEFAULT_CHANNEL; + [NonSerialized] protected internal ICharStream input; + + /// <summary>We need to be able to change the text once in a while. If + /// this is non-null, then getText should return this. Note that + /// start/stop are not affected by changing this. + /// </summary> + protected internal string text; + + /// <summary>What token number is this from 0..n-1 tokens; < 0 implies invalid index </summary> + protected internal int index = -1; + + /// <summary>The char position into the input buffer where this token starts </summary> + protected internal int start; + + /// <summary>The char position into the input buffer where this token stops </summary> + protected internal int stop; + + #endregion + } +} diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/CommonTokenStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/CommonTokenStream.cs new file mode 100644 index 0000000..20a5709 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/CommonTokenStream.cs @@ -0,0 +1,488 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2006 Kunle Odutola +Copyright (c) 2005 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. + */ + + +namespace Antlr.Runtime +{ + using System; + using IList = System.Collections.IList; + using IDictionary = System.Collections.IDictionary; + using ArrayList = System.Collections.ArrayList; + using Hashtable = System.Collections.Hashtable; + using HashList = Antlr.Runtime.Collections.HashList; + + /// <summary> + /// The most common stream of tokens is one where every token is buffered up + /// and tokens are prefiltered for a certain channel (the parser will only + /// see these tokens and cannot change the filter channel number during the + /// parse). + /// + /// TODO: how to access the full token stream? How to track all tokens matched per rule? + /// </summary> + public class CommonTokenStream : ITokenStream + { + protected ITokenSource tokenSource; + + /// <summary>Record every single token pulled from the source so we can reproduce + /// chunks of it later. + /// </summary> + protected IList tokens; + + /// <summary><![CDATA[Map<tokentype, channel>]]> to override some Tokens' channel numbers </summary> + protected IDictionary channelOverrideMap; + + /// <summary><![CDATA[Set<tokentype>;]]> discard any tokens with this type </summary> + protected HashList discardSet; + + /// <summary>Skip tokens on any channel but this one; this is how we skip whitespace... </summary> + protected int channel; + + /// <summary>By default, track all incoming tokens </summary> + protected bool discardOffChannelTokens = false; + + /// <summary>Track the last Mark() call result value for use in Rewind().</summary> + protected int lastMarker; + + /// <summary> + /// The index into the tokens list of the current token (next token + /// to consume). p==-1 indicates that the tokens list is empty + /// </summary> + protected int p = -1; + + #region Constructors + + public CommonTokenStream() + { + channel = Token.DEFAULT_CHANNEL; + tokens = new ArrayList(500); + } + + public CommonTokenStream(ITokenSource tokenSource) : this() + { + this.tokenSource = tokenSource; + } + + public CommonTokenStream(ITokenSource tokenSource, int channel) : this(tokenSource) + { + this.channel = channel; + } + + #endregion + + #region ITokenStream Members + + /// <summary>Get the ith token from the current position 1..n where k=1 is the + /// first symbol of lookahead. + /// </summary> + public virtual IToken LT(int k) + { + if (p == -1) + { + FillBuffer(); + } + if (k == 0) + { + return null; + } + if (k < 0) + { + return LB(-k); + } + //System.out.print("LT(p="+p+","+k+")="); + if ((p + k - 1) >= tokens.Count) + { + return Token.EOF_TOKEN; + } + //System.out.println(tokens.get(p+k-1)); + int i = p; + int n = 1; + // find k good tokens + while (n < k) + { + // skip off-channel tokens + i = SkipOffTokenChannels(i + 1); // leave p on valid token + n++; + } + if (i >= tokens.Count) + { + return Token.EOF_TOKEN; + } + return (IToken)tokens[i]; + } + + /// <summary>Return absolute token i; ignore which channel the tokens are on; + /// that is, count all tokens not just on-channel tokens. + /// </summary> + public virtual IToken Get(int i) + { + return (IToken)tokens[i]; + } + + /// <summary> + /// Gets or sets the token source for this stream (i.e. the source + /// that supplies the stream with Token objects). + /// </summary> + /// + /// <remarks> + /// Setting the token source resets the stream. + /// </remarks> + public virtual ITokenSource TokenSource + { + get { return tokenSource; } + set + { + this.tokenSource = value; + tokens.Clear(); + p = -1; + channel = Token.DEFAULT_CHANNEL; + } + } + + public virtual string SourceName { + get { return TokenSource.SourceName; } + } + + public virtual string ToString(int start, int stop) + { + if ((start < 0) || (stop < 0)) + { + return null; + } + if (p == -1) + { + FillBuffer(); + } + if (stop >= tokens.Count) + { + stop = tokens.Count - 1; + } + System.Text.StringBuilder buf = new System.Text.StringBuilder(); + for (int i = start; i <= stop; i++) + { + IToken t = (IToken)tokens[i]; + buf.Append(t.Text); + } + return buf.ToString(); + } + + public virtual string ToString(IToken start, IToken stop) + { + if (start != null && stop != null) + { + return ToString(start.TokenIndex, stop.TokenIndex); + } + return null; + } + + #endregion + + #region IIntStream Members + + /// <summary>Move the input pointer to the next incoming token. The stream + /// must become active with LT(1) available. Consume() simply + /// moves the input pointer so that LT(1) points at the next + /// input symbol. Consume at least one token. + /// + /// Walk past any token not on the channel the parser is listening to. + /// </summary> + public virtual void Consume() + { + if (p < tokens.Count) + { + p++; + p = SkipOffTokenChannels(p); // leave p on valid token + } + } + + public virtual int LA(int i) + { + return LT(i).Type; + } + + public virtual int Mark() + { + if (p == -1) + { + FillBuffer(); + } + lastMarker = Index(); + return lastMarker; + } + + public virtual int Index() + { + return p; + } + + public virtual void Rewind(int marker) + { + Seek(marker); + } + + public virtual void Rewind() + { + Seek(lastMarker); + } + + public virtual void Reset() + { + p = 0; + lastMarker = 0; + } + + public virtual void Release(int marker) + { + // no resources to release + } + + public virtual void Seek(int index) + { + p = index; + } + + [Obsolete("Please use the property Count instead.")] + public virtual int Size() + { + return Count; + } + + public virtual int Count + { + get { return tokens.Count; } + } + + #endregion + + /// <summary>Load all tokens from the token source and put in tokens. + /// This is done upon first LT request because you might want to + /// set some token type / channel overrides before filling buffer. + /// </summary> + protected virtual void FillBuffer() + { + int index = 0; + IToken t = tokenSource.NextToken(); + while ((t != null) && (t.Type != (int)Antlr.Runtime.CharStreamConstants.EOF)) + { + bool discard = false; + // is there a channel override for token type? + if (channelOverrideMap != null) + { + object channelI = channelOverrideMap[(int) t.Type]; + if (channelI != null) + { + t.Channel = (int)channelI; + } + } + if (discardSet != null && discardSet.Contains(t.Type.ToString())) + { + discard = true; + } + else if (discardOffChannelTokens && t.Channel != this.channel) + { + discard = true; + } + if (!discard) + { + t.TokenIndex = index; + tokens.Add(t); + index++; + } + t = tokenSource.NextToken(); + } + // leave p pointing at first token on channel + p = 0; + p = SkipOffTokenChannels(p); + } + + /// <summary>Given a starting index, return the index of the first on-channel + /// token. + /// </summary> + protected virtual int SkipOffTokenChannels(int i) + { + int n = tokens.Count; + while (i < n && ((IToken) tokens[i]).Channel != channel) + { + i++; + } + return i; + } + + protected virtual int SkipOffTokenChannelsReverse(int i) + { + while (i >= 0 && ((IToken) tokens[i]).Channel != channel) + { + i--; + } + return i; + } + + /// <summary> + /// A simple filter mechanism whereby you can tell this token stream + /// to force all tokens of type ttype to be on channel. + /// </summary> + /// + /// <remarks> + /// For example, + /// when interpreting, we cannot exec actions so we need to tell + /// the stream to force all WS and NEWLINE to be a different, ignored + /// channel. + /// </remarks> + public virtual void SetTokenTypeChannel(int ttype, int channel) + { + if (channelOverrideMap == null) + { + channelOverrideMap = new Hashtable(); + } + channelOverrideMap[(int) ttype] = (int) channel; + } + + public virtual void DiscardTokenType(int ttype) + { + if (discardSet == null) + { + discardSet = new HashList(); + } + discardSet.Add(ttype.ToString(), ttype); + } + + public virtual void DiscardOffChannelTokens(bool discardOffChannelTokens) + { + this.discardOffChannelTokens = discardOffChannelTokens; + } + + public virtual IList GetTokens() + { + if (p == - 1) + { + FillBuffer(); + } + return tokens; + } + + public virtual IList GetTokens(int start, int stop) + { + return GetTokens(start, stop, (BitSet) null); + } + + /// <summary>Given a start and stop index, return a List of all tokens in + /// the token type BitSet. Return null if no tokens were found. This + /// method looks at both on and off channel tokens. + /// </summary> + public virtual IList GetTokens(int start, int stop, BitSet types) + { + if (p == - 1) + { + FillBuffer(); + } + if (stop >= tokens.Count) + { + stop = tokens.Count-1; + } + if (start < 0) + { + start = 0; + } + if (start > stop) + { + return null; + } + + // list = tokens[start:stop]:{Token t, t.getType() in types} + IList filteredTokens = new ArrayList(); + for (int i = start; i <= stop; i++) + { + IToken t = (IToken) tokens[i]; + if (types == null || types.Member(t.Type)) + { + filteredTokens.Add(t); + } + } + if (filteredTokens.Count == 0) + { + filteredTokens = null; + } + return filteredTokens; + } + + public virtual IList GetTokens(int start, int stop, IList types) + { + return GetTokens(start, stop, new BitSet(types)); + } + + public virtual IList GetTokens(int start, int stop, int ttype) + { + return GetTokens(start, stop, BitSet.Of(ttype)); + } + + /// <summary>Look backwards k tokens on-channel tokens </summary> + protected virtual IToken LB(int k) + { + //System.out.print("LB(p="+p+","+k+") "); + if (p == - 1) + { + FillBuffer(); + } + if (k == 0) + { + return null; + } + if ((p - k) < 0) + { + return null; + } + + int i = p; + int n = 1; + // find k good tokens looking backwards + while (n <= k) + { + // skip off-channel tokens + i = SkipOffTokenChannelsReverse(i - 1); // leave p on valid token + n++; + } + if (i < 0) + { + return null; + } + return (IToken) tokens[i]; + } + + public override string ToString() + { + if (p == - 1) + { + FillBuffer(); + } + return ToString(0, tokens.Count - 1); + } + + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/Constants.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/Constants.cs new file mode 100644 index 0000000..85a443a --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/Constants.cs @@ -0,0 +1,51 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + /// <summary> + /// Global constants + /// </summary> + internal sealed class Constants + { + public static readonly string VERSION = "3.1b1"; + + // Moved to version 2 for v3.1: added grammar name to enter/exit Rule + public static readonly string DEBUG_PROTOCOL_VERSION = "2"; + + public static readonly string ANTLRWORKS_DIR = "antlrworks"; + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/DFA.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/DFA.cs new file mode 100644 index 0000000..bbe62c2 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/DFA.cs @@ -0,0 +1,272 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + /// <summary> + /// A DFA implemented as a set of transition tables. + /// </summary> + /// <remarks> + /// <para> + /// Any state that has a semantic predicate edge is special; those states are + /// generated with if-then-else structures in a SpecialStateTransition() + /// which is generated by cyclicDFA template. + /// </para> + /// <para> + /// There are at most 32767 states (16-bit signed short). Could get away with byte + /// sometimes but would have to generate different types and the simulation code too. + /// </para> + /// <para> + /// As a point of reference, the Tokens rule DFA for the lexer in the Java grammar + /// sample has approximately 326 states. + /// </para> + /// </remarks> + abstract public class DFA + { + public delegate int SpecialStateTransitionHandler(DFA dfa, int s, IIntStream input); + + protected short[] eot; + protected short[] eof; + protected char[] min; + protected char[] max; + protected short[] accept; + protected short[] special; + protected short[][] transition; + + protected int decisionNumber; + + // HACK: Has to be public to allow generated recognizer to set. + public SpecialStateTransitionHandler specialStateTransitionHandler; + + /// <summary> + /// Which recognizer encloses this DFA? Needed to check backtracking + /// </summary> + protected BaseRecognizer recognizer; + + public const bool debug = false; + + /// <summary> + /// From the input stream, predict what alternative will succeed using this + /// DFA (representing the covering regular approximation to the underlying CFL). + /// </summary> + /// <param name="input">Input stream</param> + /// <returns>Return an alternative number 1..n. Throw an exception upon error.</returns> + public int Predict(IIntStream input) + { + if ( debug ) { + Console.Error.WriteLine("Enter DFA.predict for decision "+decisionNumber); + } + int mark = input.Mark(); // remember where decision started in input + int s = 0; // we always start at s0 + try + { + while (true) + { + if (debug) + Console.Error.WriteLine("DFA " + decisionNumber + " state " + s + " LA(1)=" + (char)input.LA(1) + "(" + input.LA(1) + "), index="+input.Index()); + int specialState = special[s]; + if (specialState >= 0) + { + if (debug) Console.Error.WriteLine("DFA " + decisionNumber + " state " + s + " is special state " + specialState); + s = specialStateTransitionHandler(this, specialState, input); + if ( debug ) { + Console.Error.WriteLine("DFA "+decisionNumber+ + " returns from special state "+specialState+" to "+s); + } + if ( s==-1 ) { + NoViableAlt(s,input); + return 0; + } + input.Consume(); + continue; + } + if (accept[s] >= 1) + { + if (debug) Console.Error.WriteLine("accept; predict " + accept[s] + " from state " + s); + return accept[s]; + } + // look for a normal char transition + char c = (char)input.LA(1); // -1 == \uFFFF, all tokens fit in 65000 space + if ((c >= min[s]) && (c <= max[s])) + { + int snext = transition[s][c - min[s]]; // move to next state + if (snext < 0) + { + // was in range but not a normal transition + // must check EOT, which is like the else clause. + // eot[s]>=0 indicates that an EOT edge goes to another + // state. + if (eot[s] >= 0) // EOT Transition to accept state? + { + if (debug) + Console.Error.WriteLine("EOT transition"); + s = eot[s]; + input.Consume(); + // TODO: I had this as return accept[eot[s]] + // which assumed here that the EOT edge always + // went to an accept...faster to do this, but + // what about predicated edges coming from EOT + // target? + continue; + } + NoViableAlt(s, input); + return 0; + } + s = snext; + input.Consume(); + continue; + } + if (eot[s] >= 0) + { // EOT Transition? + if (debug) + Console.Error.WriteLine("EOT transition"); + s = eot[s]; + input.Consume(); + continue; + } + if ((c == (char)Token.EOF) && (eof[s] >= 0)) + { // EOF Transition to accept state? + if (debug) + Console.Error.WriteLine("accept via EOF; predict " + accept[eof[s]] + " from " + eof[s]); + return accept[eof[s]]; + } + // not in range and not EOF/EOT, must be invalid symbol + if (debug) + { + Console.Error.WriteLine("min[" + s + "]=" + min[s]); + Console.Error.WriteLine("max[" + s + "]=" + max[s]); + Console.Error.WriteLine("eot[" + s + "]=" + eot[s]); + Console.Error.WriteLine("eof[" + s + "]=" + eof[s]); + for (int p = 0; p < transition[s].Length; p++) + { + Console.Error.Write(transition[s][p] + " "); + } + Console.Error.WriteLine(); + } + NoViableAlt(s, input); + return 0; + } + } + finally + { + input.Rewind(mark); + } + } + + protected void NoViableAlt(int s, IIntStream input) + { + if (recognizer.state.backtracking > 0) + { + recognizer.state.failed = true; + return; + } + NoViableAltException nvae = + new NoViableAltException(Description, + decisionNumber, + s, + input); + Error(nvae); + throw nvae; + } + + /// <summary> + /// A hook for debugging interface + /// </summary> + /// <param name="nvae"></param> + public virtual void Error(NoViableAltException nvae) { ; } + + public virtual int SpecialStateTransition(int s, IIntStream input) + { + return -1; + } + + public virtual string Description + { + get { return "n/a"; } + } + + public static short[] UnpackEncodedString( string encodedString ) + { + int size = 0; + for ( int i = 0; i < encodedString.Length; i += 2 ) + size += (int)encodedString[i]; + + short[] data = new short[size]; + int di = 0; + for ( int i = 0; i < encodedString.Length; i += 2 ) + { + char n = encodedString[i]; + char v = encodedString[i+1]; + // add v n times to data + for ( int j = 1; j <= n; j++ ) + data[di++] = (short)v; + } + + return data; + } + public static short[][] UnpackEncodedStringArray( string[] encodedStrings ) + { + short[][] result = new short[encodedStrings.Length][]; + for ( int i = 0; i < encodedStrings.Length; i++ ) + result[i] = UnpackEncodedString( encodedStrings[i] ); + return result; + } + public static char[] UnpackEncodedStringToUnsignedChars( string encodedString ) + { + int size = 0; + for ( int i = 0; i < encodedString.Length; i += 2 ) + size += (int)encodedString[i]; + + char[] data = new char[size]; + int di = 0; + for ( int i = 0; i < encodedString.Length; i += 2 ) + { + char n = encodedString[i]; + char v = encodedString[i+1]; + // add v n times to data + for ( int j = 1; j <= n; j++ ) + data[di++] = v; + } + + return data; + } + + public int SpecialTransition(int state, int symbol) + { + return 0; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/EarlyExitException.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/EarlyExitException.cs new file mode 100644 index 0000000..f3c8874 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/EarlyExitException.cs @@ -0,0 +1,59 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + /// <summary> + /// The recognizer did not match anything for a (..)+ loop. + /// </summary> + [Serializable] + public class EarlyExitException : RecognitionException + { + public int decisionNumber; + + /// <summary>Used for remote debugger deserialization </summary> + public EarlyExitException() + { + ; + } + + public EarlyExitException(int decisionNumber, IIntStream input) + : base(input) + { + this.decisionNumber = decisionNumber; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/FailedPredicateException.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/FailedPredicateException.cs new file mode 100644 index 0000000..ed9586e --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/FailedPredicateException.cs @@ -0,0 +1,68 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + /// <summary> + /// A semantic predicate failed during validation. Validation of predicates + /// occurs when normally parsing the alternative just like matching a token. + /// Disambiguating predicate evaluation occurs when we hoist a predicate into + /// a prediction decision. + /// </summary> + [Serializable] + public class FailedPredicateException : RecognitionException + { + public string ruleName; + public string predicateText; + + /// <summary>Used for remote debugger deserialization </summary> + public FailedPredicateException() + { + } + + public FailedPredicateException(IIntStream input, string ruleName, string predicateText) + : base(input) + { + this.ruleName = ruleName; + this.predicateText = predicateText; + } + + public override string ToString() + { + return "FailedPredicateException(" + ruleName + ",{" + predicateText + "}?)"; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ICharStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ICharStream.cs new file mode 100644 index 0000000..8531b23 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ICharStream.cs @@ -0,0 +1,85 @@ +/* +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + public enum CharStreamConstants + { + EOF = - 1, + } + + /// <summary>A source of characters for an ANTLR lexer </summary> + public interface ICharStream : IIntStream + { + /// <summary> + /// The current line in the character stream (ANTLR tracks the + /// line information automatically. To support rewinding character + /// streams, we are able to [re-]set the line. + /// </summary> + int Line + { + get; + set; + } + + /// <summary> + /// The index of the character relative to the beginning of the + /// line (0..n-1). To support rewinding character streams, we are + /// able to [re-]set the character position. + /// </summary> + int CharPositionInLine + { + get; + set; + } + + /// <summary> + /// Get the ith character of lookahead. This is usually the same as + /// LA(i). This will be used for labels in the generated lexer code. + /// I'd prefer to return a char here type-wise, but it's probably + /// better to be 32-bit clean and be consistent with LA. + /// </summary> + int LT(int i); + + /// <summary> + /// This primarily a useful interface for action code (just make sure + /// actions don't use this on streams that don't support it). + /// For infinite streams, you don't need this. + /// </summary> + string Substring(int start, int stop); + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/IIntStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/IIntStream.cs new file mode 100644 index 0000000..f7788cd --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/IIntStream.cs @@ -0,0 +1,169 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + /// <summary> + /// A simple stream of integers. This is useful when all we care about is the char + /// or token type sequence (such as for interpretation). + /// </summary> + public interface IIntStream + { + void Consume(); + + /// <summary> + /// Get int at current input pointer + i ahead (where i=1 is next int) + /// Negative indexes are allowed. LA(-1) is previous token (token just matched). + /// LA(-i) where i is before first token should yield -1, invalid char or EOF. + /// </summary> + int LA(int i); + + /// <summary>Tell the stream to start buffering if it hasn't already.</summary> + /// <remarks> + /// Executing Rewind(Mark()) on a stream should not affect the input position. + /// The Lexer tracks line/col info as well as input index so its markers are + /// not pure input indexes. Same for tree node streams. */ + /// </remarks> + /// <returns>Return a marker that can be passed to + /// <see cref="IIntStream.Rewind(int)"/> to return to the current position. + /// This could be the current input position, a value return from + /// <see cref="IIntStream.Index"/>, or some other marker.</returns> + int Mark(); + + /// <summary> + /// Return the current input symbol index 0..n where n indicates the + /// last symbol has been read. The index is the symbol about to be + /// read not the most recently read symbol. + /// </summary> + int Index(); + + /// <summary> + /// Resets the stream so that the next call to + /// <see cref="IIntStream.Index"/> would return marker. + /// </summary> + /// <remarks> + /// The marker will usually be <see cref="IIntStream.Index"/> but + /// it doesn't have to be. It's just a marker to indicate what + /// state the stream was in. This is essentially calling + /// <see cref="IIntStream.Release"/> and <see cref="IIntStream.Seek"/>. + /// If there are other markers created after the specified marker, + /// this routine must unroll them like a stack. Assumes the state the + /// stream was in when this marker was created. + /// </remarks> + void Rewind(int marker); + + /// <summary> + /// Rewind to the input position of the last marker. + /// </summary> + /// <remarks> + /// Used currently only after a cyclic DFA and just before starting + /// a sem/syn predicate to get the input position back to the start + /// of the decision. Do not "pop" the marker off the state. Mark(i) + /// and Rewind(i) should balance still. It is like invoking + /// Rewind(last marker) but it should not "pop" the marker off. + /// It's like Seek(last marker's input position). + /// </remarks> + void Rewind(); + + /// <summary> + /// You may want to commit to a backtrack but don't want to force the + /// stream to keep bookkeeping objects around for a marker that is + /// no longer necessary. This will have the same behavior as + /// <see cref="IIntStream.Rewind(int)"/> except it releases resources without + /// the backward seek. + /// </summary> + /// <remarks> + /// This must throw away resources for all markers back to the marker + /// argument. So if you're nested 5 levels of Mark(), and then Release(2) + /// you have to release resources for depths 2..5. + /// </remarks> + void Release(int marker); + + /// <summary> + /// Set the input cursor to the position indicated by index. This is + /// normally used to seek ahead in the input stream. + /// </summary> + /// <remarks> + /// No buffering is required to do this unless you know your stream + /// will use seek to move backwards such as when backtracking. + /// + /// This is different from rewind in its multi-directional requirement + /// and in that its argument is strictly an input cursor (index). + /// + /// For char streams, seeking forward must update the stream state such + /// as line number. For seeking backwards, you will be presumably + /// backtracking using the + /// <see cref="IIntStream.Mark"/>/<see cref="IIntStream.Rewind(int)"/> + /// mechanism that restores state and so this method does not need to + /// update state when seeking backwards. + /// + /// Currently, this method is only used for efficient backtracking using + /// memoization, but in the future it may be used for incremental parsing. + /// + /// The index is 0..n-1. A seek to position i means that LA(1) will return + /// the ith symbol. So, seeking to 0 means LA(1) will return the first + /// element in the stream. + /// </remarks> + void Seek(int index); + + /// <summary>Returns the size of the entire stream.</summary> + /// <remarks> + /// Only makes sense for streams that buffer everything up probably, + /// but might be useful to display the entire stream or for testing. + /// This value includes a single EOF. + /// </remarks> + [Obsolete("Please use property Count instead.")] + int Size(); + + /// <summary>Returns the size of the entire stream.</summary> + /// <remarks> + /// Only makes sense for streams that buffer everything up probably, + /// but might be useful to display the entire stream or for testing. + /// This value includes a single EOF. + /// </remarks> + int Count { get; } + + /// <summary> + /// Where are you getting symbols from? Normally, implementations will + /// pass the buck all the way to the lexer who can ask its input stream + /// for the file name or whatever. + /// </summary> + string SourceName { + get; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/IToken.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/IToken.cs new file mode 100644 index 0000000..9857fce --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/IToken.cs @@ -0,0 +1,92 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + public interface IToken + { + int Type + { + get; + set; + } + + /// <summary>The line number on which this token was matched; line=1..n</summary> + int Line + { + get; + set; + } + + /// <summary> + /// The index of the first character relative to the beginning of the line 0..n-1 + /// </summary> + int CharPositionInLine + { + get; + set; + } + + int Channel + { + get; + set; + } + + /// <summary> + /// An index from 0..n-1 of the token object in the input stream + /// </summary> + /// <remarks> + /// This must be valid in order to use the ANTLRWorks debugger. + /// </remarks> + int TokenIndex + { + get; + set; + } + + /// <summary>The text of the token</summary> + /// <remarks> + /// When setting the text, it might be a NOP such as for the CommonToken, + /// which doesn't have string pointers, just indexes into a char buffer. + /// </remarks> + string Text + { + get; + set; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ITokenSource.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ITokenSource.cs new file mode 100644 index 0000000..19ee66b --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ITokenSource.cs @@ -0,0 +1,72 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + /// <summary> + /// A source of tokens must provide a sequence of tokens via NextToken() + /// and also must reveal it's source of characters; CommonToken's text is + /// computed from a CharStream; it only store indices into the char stream. + /// + /// Errors from the lexer are never passed to the parser. Either you want + /// to keep going or you do not upon token recognition error. If you do not + /// want to continue lexing then you do not want to continue parsing. Just + /// throw an exception not under RecognitionException and Java will naturally + /// toss you all the way out of the recognizers. If you want to continue + /// lexing then you should not throw an exception to the parser--it has already + /// requested a token. Keep lexing until you get a valid one. Just report + /// errors and keep going, looking for a valid token. + /// </summary> + public interface ITokenSource + { + /// <summary> + /// Returns a Token object from the input stream (usually a CharStream). + /// Does not fail/return upon lexing error; just keeps chewing on the + /// characters until it gets a good one; errors are not passed through + /// to the parser. + /// </summary> + IToken NextToken(); + + /// <summary> + /// Where are you getting tokens from? normally the implication will simply + /// ask lexers input stream. + /// </summary> + string SourceName { + get; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ITokenStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ITokenStream.cs new file mode 100644 index 0000000..b57e80d --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ITokenStream.cs @@ -0,0 +1,82 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + /// <summary>A stream of tokens accessing tokens from a TokenSource </summary> + public interface ITokenStream : IIntStream + { + /// <summary> + /// Get Token at current input pointer + i ahead (where i=1 is next + /// Token). + /// i < 0 indicates tokens in the past. So -1 is previous token and -2 is + /// two tokens ago. LT(0) is undefined. For i>=n, return Token.EOFToken. + /// Return null for LT(0) and any index that results in an absolute address + /// that is negative. + /// </summary> + IToken LT(int k); + + /// <summary> + /// Get a token at an absolute index i; 0..n-1. This is really only + /// needed for profiling and debugging and token stream rewriting. + /// If you don't want to buffer up tokens, then this method makes no + /// sense for you. Naturally you can't use the rewrite stream feature. + /// I believe DebugTokenStream can easily be altered to not use + /// this method, removing the dependency. + /// </summary> + IToken Get(int i); + + /// <summary>Where is this stream pulling tokens from? This is not the name, but + /// the object that provides Token objects. + /// </summary> + ITokenSource TokenSource { get; } + + /// <summary>Return the text of all tokens from start to stop, inclusive. + /// If the stream does not buffer all the tokens then it can just + /// return "" or null; Users should not access $ruleLabel.text in + /// an action of course in that case. + /// </summary> + string ToString(int start, int stop); + + /// <summary>Because the user is not required to use a token with an index stored + /// in it, we must provide a means for two token objects themselves to + /// indicate the start/end location. Most often this will just delegate + /// to the other toString(int,int). This is also parallel with + /// the TreeNodeStream.toString(Object,Object). + /// </summary> + string ToString(IToken start, IToken stop); + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/Lexer.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/Lexer.cs new file mode 100644 index 0000000..ab511c7 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/Lexer.cs @@ -0,0 +1,410 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + using CollectionUtils = Antlr.Runtime.Collections.CollectionUtils; + + /// <summary> + /// A lexer is recognizer that draws input symbols from a character stream. + /// lexer grammars result in a subclass of this object. A Lexer object + /// uses simplified Match() and error recovery mechanisms in the interest + /// of speed. + /// </summary> + public abstract class Lexer : BaseRecognizer, ITokenSource + { + const int TOKEN_dot_EOF = (int)CharStreamConstants.EOF; + + #region Constructors + + public Lexer() + { + } + + public Lexer(ICharStream input) + { + this.input = input; + } + + public Lexer(ICharStream input, RecognizerSharedState state) + : base(state) { + this.input = input; + } + + #endregion + + #region Public API + + /// <summary>Set the char stream and reset the lexer </summary> + virtual public ICharStream CharStream + { + get { return this.input; } + set + { + this.input = null; + Reset(); + this.input = value; + } + } + + override public string SourceName { + get { return input.SourceName; } + } + + + override public IIntStream Input + { + get { return input; } + } + + virtual public int Line + { + get { return input.Line; } + } + + virtual public int CharPositionInLine + { + get { return input.CharPositionInLine; } + } + + /// <summary>What is the index of the current character of lookahead? </summary> + virtual public int CharIndex + { + get { return input.Index(); } + } + + /// <summary> + /// Gets or sets the 'lexeme' for the current token. + /// </summary> + /// <remarks> + /// <para> + /// The getter returns the text matched so far for the current token or any + /// text override. + /// </para> + /// <para> + /// The setter sets the complete text of this token. It overrides/wipes any + /// previous changes to the text. + /// </para> + /// </remarks> + virtual public string Text + { + get + { + if (state.text != null) + { + return state.text; + } + return input.Substring(state.tokenStartCharIndex, CharIndex - 1); + } + set + { + state.text = value; + } + } + + override public void Reset() + { + base.Reset(); // reset all recognizer state variables + // wack Lexer state variables + if (input != null) { + input.Seek(0); // rewind the input + } + if (state == null) { + return; // no shared state work to do + } + state.token = null; + state.type = Token.INVALID_TOKEN_TYPE; + state.channel = Token.DEFAULT_CHANNEL; + state.tokenStartCharIndex = -1; + state.tokenStartCharPositionInLine = -1; + state.tokenStartLine = -1; + state.text = null; + } + + /// <summary> + /// Return a token from this source; i.e., Match a token on the char stream. + /// </summary> + public virtual IToken NextToken() + { + while (true) + { + state.token = null; + state.channel = Token.DEFAULT_CHANNEL; + state.tokenStartCharIndex = input.Index(); + state.tokenStartCharPositionInLine = input.CharPositionInLine; + state.tokenStartLine = input.Line; + state.text = null; + if (input.LA(1) == (int)CharStreamConstants.EOF) + { + return Token.EOF_TOKEN; + } + try + { + mTokens(); + if (state.token == null) + { + Emit(); + } + else if (state.token == Token.SKIP_TOKEN) + { + continue; + } + return state.token; + } + catch (NoViableAltException nva) { + ReportError(nva); + Recover(nva); // throw out current char and try again + } + catch (RecognitionException re) { + ReportError(re); + // Match() routine has already called Recover() + } + } + } + + /// <summary> + /// Instruct the lexer to skip creating a token for current lexer rule and + /// look for another token. NextToken() knows to keep looking when a lexer + /// rule finishes with token set to SKIP_TOKEN. Recall that if token==null + /// at end of any token rule, it creates one for you and emits it. + /// </summary> + public void Skip() + { + state.token = Token.SKIP_TOKEN; + } + + /// <summary>This is the lexer entry point that sets instance var 'token' </summary> + public abstract void mTokens(); + + /// <summary> + /// Currently does not support multiple emits per nextToken invocation + /// for efficiency reasons. Subclass and override this method and + /// nextToken (to push tokens into a list and pull from that list rather + /// than a single variable as this implementation does). + /// </summary> + public virtual void Emit(IToken token) + { + state.token = token; + } + + /// <summary> + /// The standard method called to automatically emit a token at the + /// outermost lexical rule. The token object should point into the + /// char buffer start..stop. If there is a text override in 'text', + /// use that to set the token's text. + /// </summary> + /// <remarks><para>Override this method to emit custom Token objects.</para> + /// <para>If you are building trees, then you should also override + /// Parser or TreeParser.getMissingSymbol().</para> + ///</remarks> + public virtual IToken Emit() + { + IToken t = new CommonToken(input, state.type, state.channel, state.tokenStartCharIndex, CharIndex - 1); + t.Line = state.tokenStartLine; + t.Text = state.text; + t.CharPositionInLine = state.tokenStartCharPositionInLine; + Emit(t); + return t; + } + + public virtual void Match(string s) + { + int i = 0; + while (i < s.Length) + { + if (input.LA(1) != s[i]) + { + if (state.backtracking > 0) + { + state.failed = true; + return; + } + MismatchedTokenException mte = new MismatchedTokenException(s[i], input); + Recover(mte); // don't really recover; just consume in lexer + throw mte; + } + i++; + input.Consume(); + state.failed = false; + } + } + + public virtual void MatchAny() + { + input.Consume(); + } + + public virtual void Match(int c) + { + if (input.LA(1) != c) + { + if (state.backtracking > 0) + { + state.failed = true; + return; + } + MismatchedTokenException mte = new MismatchedTokenException(c, input); + Recover(mte); + throw mte; + } + input.Consume(); + state.failed = false; + } + + public virtual void MatchRange(int a, int b) + { + if (input.LA(1) < a || input.LA(1) > b) + { + if (state.backtracking > 0) + { + state.failed = true; + return; + } + MismatchedRangeException mre = new MismatchedRangeException(a, b, input); + Recover(mre); + throw mre; + } + input.Consume(); + state.failed = false; + } + + /// <summary> + /// Lexers can normally Match any char in it's vocabulary after matching + /// a token, so do the easy thing and just kill a character and hope + /// it all works out. You can instead use the rule invocation stack + /// to do sophisticated error recovery if you are in a Fragment rule. + /// </summary> + public virtual void Recover(RecognitionException re) + { + input.Consume(); + } + + public override void ReportError(RecognitionException e) + { + DisplayRecognitionError(this.TokenNames, e); + } + + override public string GetErrorMessage(RecognitionException e, string[] tokenNames) + { + string msg = null; + + if (e is MismatchedTokenException) + { + MismatchedTokenException mte = (MismatchedTokenException)e; + msg = "mismatched character " + GetCharErrorDisplay(e.Char) + " expecting " + GetCharErrorDisplay(mte.Expecting); + } + else if (e is NoViableAltException) + { + NoViableAltException nvae = (NoViableAltException)e; + // for development, can add "decision=<<"+nvae.grammarDecisionDescription+">>" + // and "(decision="+nvae.decisionNumber+") and + // "state "+nvae.stateNumber + msg = "no viable alternative at character " + GetCharErrorDisplay(nvae.Char); + } + else if (e is EarlyExitException) + { + EarlyExitException eee = (EarlyExitException)e; + // for development, can add "(decision="+eee.decisionNumber+")" + msg = "required (...)+ loop did not match anything at character " + GetCharErrorDisplay(eee.Char); + } + else if (e is MismatchedNotSetException) + { + MismatchedSetException mse = (MismatchedSetException)e; + msg = "mismatched character " + GetCharErrorDisplay(mse.Char) + " expecting set " + mse.expecting; + } + else if (e is MismatchedSetException) + { + MismatchedSetException mse = (MismatchedSetException)e; + msg = "mismatched character " + GetCharErrorDisplay(mse.Char) + " expecting set " + mse.expecting; + } + else if (e is MismatchedRangeException) + { + MismatchedRangeException mre = (MismatchedRangeException)e; + msg = "mismatched character " + GetCharErrorDisplay(mre.Char) + " expecting set " + GetCharErrorDisplay(mre.A) + ".." + GetCharErrorDisplay(mre.B); + } + else + { + msg = base.GetErrorMessage(e, tokenNames); + } + return msg; + } + + public string GetCharErrorDisplay(int c) + { + string s; + switch ( c ) + { + //case Token.EOF : + case TOKEN_dot_EOF : + s = "<EOF>"; + break; + case '\n' : + s = "\\n"; + break; + case '\t' : + s = "\\t"; + break; + case '\r' : + s = "\\r"; + break; + default: + s = Convert.ToString((char)c); + break; + } + return "'" + s + "'"; + } + + public virtual void TraceIn(string ruleName, int ruleIndex) + { + string inputSymbol = ((char)input.LT(1)) + " line=" + Line + ":" + CharPositionInLine; + base.TraceIn(ruleName, ruleIndex, inputSymbol); + } + + public virtual void TraceOut(string ruleName, int ruleIndex) + { + string inputSymbol = ((char)input.LT(1)) + " line=" + Line + ":" + CharPositionInLine; + base.TraceOut(ruleName, ruleIndex, inputSymbol); + } + #endregion + + #region Data Members + + /// <summary>Where is the lexer drawing characters from? </summary> + protected internal ICharStream input; + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MismatchedNotSetException.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MismatchedNotSetException.cs new file mode 100644 index 0000000..23c754d --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MismatchedNotSetException.cs @@ -0,0 +1,59 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + [Serializable] + public class MismatchedNotSetException : MismatchedSetException + { + + /// <summary>Used for remote debugger deserialization </summary> + public MismatchedNotSetException() + { + ; + } + + public MismatchedNotSetException(BitSet expecting, IIntStream input) + : base(expecting, input) + { + } + + public override string ToString() + { + return "MismatchedNotSetException(" + UnexpectedType + "!=" + expecting + ")"; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MismatchedRangeException.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MismatchedRangeException.cs new file mode 100644 index 0000000..bc4eb12 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MismatchedRangeException.cs @@ -0,0 +1,70 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + [Serializable] + public class MismatchedRangeException : RecognitionException { + private int a, b; + + public int A { + get { return a; } + set { a = value; } + } + + public int B { + get { return b; } + set { b = value; } + } + + /// <summary> + /// Used for remote debugger deserialization + /// </summary> + public MismatchedRangeException() { + } + + public MismatchedRangeException(int a, int b, IIntStream input) + : base(input) { + this.a = a; + this.b = b; + } + + public override string ToString() { + return "MismatchedNotSetException(" + UnexpectedType + " not in [" + a + "," + b + "])"; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MismatchedSetException.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MismatchedSetException.cs new file mode 100644 index 0000000..1ccc33c --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MismatchedSetException.cs @@ -0,0 +1,61 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + [Serializable] + public class MismatchedSetException : RecognitionException + { + public BitSet expecting; + + /// <summary>Used for remote debugger deserialization </summary> + public MismatchedSetException() + { + ; + } + + public MismatchedSetException(BitSet expecting, IIntStream input) + : base(input) + { + this.expecting = expecting; + } + + public override string ToString() + { + return "MismatchedSetException(" + UnexpectedType + "!=" + expecting + ")"; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MismatchedTokenException.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MismatchedTokenException.cs new file mode 100644 index 0000000..2e110a3 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MismatchedTokenException.cs @@ -0,0 +1,71 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + /// <summary> + /// A mismatched char or Token or tree node. + /// </summary> + [Serializable] + public class MismatchedTokenException : RecognitionException + { + private int expecting = Runtime.Token.INVALID_TOKEN_TYPE; + + public int Expecting { + get { return expecting; } + set { expecting = value; } + } + + /// <summary> + /// Used for remote debugger deserialization + /// </summary> + public MismatchedTokenException() + { + } + + public MismatchedTokenException(int expecting, IIntStream input) + : base(input) + { + this.expecting = expecting; + } + + public override string ToString() + { + return "MismatchedTokenException(" + UnexpectedType + "!=" + expecting + ")"; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MismatchedTreeNodeException.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MismatchedTreeNodeException.cs new file mode 100644 index 0000000..6acdccf --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MismatchedTreeNodeException.cs @@ -0,0 +1,61 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + using TreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream; + + + [Serializable] + public class MismatchedTreeNodeException : RecognitionException + { + public int expecting; + + public MismatchedTreeNodeException() + { + } + + public MismatchedTreeNodeException(int expecting, TreeNodeStream input) + : base(input) + { + this.expecting = expecting; + } + + public override string ToString() + { + return "MismatchedTreeNodeException(" + UnexpectedType + "!=" + expecting + ")"; + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MissingTokenException.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MissingTokenException.cs new file mode 100644 index 0000000..1722e87 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/MissingTokenException.cs @@ -0,0 +1,73 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +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. +*/ + +using System; + +namespace Antlr.Runtime +{ + + /// <summary> + /// We were expecting a token but it's not found. The current token + /// is actually what we wanted next. Used for tree node errors too. + /// </summary> + [Serializable] + public class MissingTokenException : MismatchedTokenException { + private object inserted; + + /// <summary> + /// Used for remote debugger deserialization + /// </summary> + public MissingTokenException() { + } + + public MissingTokenException(int expecting, IIntStream input, object inserted) + : base(expecting, input) { + this.inserted = inserted; + } + + public int MissingType { + get { return Expecting; } + } + + public object Inserted { + get { return inserted; } + set { inserted = value; } + } + + public override String ToString() { + if (inserted != null && token != null) { + return "MissingTokenException(inserted " + inserted + " at " + token.Text + ")"; + } + if (token != null) { + return "MissingTokenException(at " + token.Text + ")"; + } + return "MissingTokenException"; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/NoViableAltException.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/NoViableAltException.cs new file mode 100644 index 0000000..dd8951f --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/NoViableAltException.cs @@ -0,0 +1,71 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + [Serializable] + public class NoViableAltException : RecognitionException + { + public string grammarDecisionDescription; + public int decisionNumber; + public int stateNumber; + + /// <summary>Used for remote debugger deserialization</summary> + public NoViableAltException() + { + ; + } + + public NoViableAltException(string grammarDecisionDescription, int decisionNumber, int stateNumber, IIntStream input) + : base(input) + { + this.grammarDecisionDescription = grammarDecisionDescription; + this.decisionNumber = decisionNumber; + this.stateNumber = stateNumber; + } + + public override string ToString() + { + if (input is ICharStream) { + return "NoViableAltException('" + (char)UnexpectedType + "'@[" + grammarDecisionDescription + "])"; + } + else { + return "NoViableAltException(" + UnexpectedType + "@[" + grammarDecisionDescription + "])"; + } + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/Parser.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/Parser.cs new file mode 100644 index 0000000..91a3aff --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/Parser.cs @@ -0,0 +1,125 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + /// <summary>A parser for TokenStreams. Parser grammars result in a subclass + /// of this. + /// </summary> + public class Parser : BaseRecognizer + { + public Parser(ITokenStream input) + : base() // highlight that we go to base class to set state object + { + TokenStream = input; + } + + public Parser(ITokenStream input, RecognizerSharedState state) + : base(state) // share the state object with another parser + { + TokenStream = input; + } + + override public void Reset() + { + base.Reset(); // reset all recognizer state variables + if ( input != null ) + { + input.Seek(0); // rewind the input + } + } + + protected override object GetCurrentInputSymbol(IIntStream input) { + return ((ITokenStream)input).LT(1); + } + + protected override object GetMissingSymbol(IIntStream input, + RecognitionException e, + int expectedTokenType, + BitSet follow) + { + String tokenText = null; + if ( expectedTokenType==Token.EOF ) tokenText = "<missing EOF>"; + else tokenText = "<missing " + TokenNames[expectedTokenType] + ">"; + CommonToken t = new CommonToken(expectedTokenType, tokenText); + IToken current = ((ITokenStream)input).LT(1); + if (current.Type == Token.EOF) { + current = ((ITokenStream)input).LT(-1); + } + t.line = current.Line; + t.CharPositionInLine = current.CharPositionInLine; + t.Channel = DEFAULT_TOKEN_CHANNEL; + return t; + } + + /// <summary>Set the token stream and reset the parser </summary> + virtual public ITokenStream TokenStream + { + get { return input; } + + set + { + this.input = null; + Reset(); + this.input = value; + } + + } + + public override string SourceName { + get { return input.SourceName; } + } + + protected internal ITokenStream input; + + override public IIntStream Input + { + get { return input; } + } + + public virtual void TraceIn(string ruleName, int ruleIndex) + { + base.TraceIn(ruleName, ruleIndex, input.LT(1)); + } + + public virtual void TraceOut(string ruleName, int ruleIndex) + { + base.TraceOut(ruleName, ruleIndex, input.LT(1)); + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ParserRuleReturnScope.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ParserRuleReturnScope.cs new file mode 100644 index 0000000..3f4c9fa --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/ParserRuleReturnScope.cs @@ -0,0 +1,76 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + /// <summary> + /// Rules that return more than a single value must return an object + /// containing all the values. Besides the properties defined in + /// RuleLabelScope.PredefinedRulePropertiesScope there may be user-defined + /// return values. This class simply defines the minimum properties that + /// are always defined and methods to access the others that might be + /// available depending on output option such as template and tree. + /// + /// Note text is not an actual property of the return value, it is computed + /// from start and stop using the input stream's ToString() method. I + /// could add a ctor to this so that we can pass in and store the input + /// stream, but I'm not sure we want to do that. It would seem to be undefined + /// to get the .text property anyway if the rule matches tokens from multiple + /// input streams. + /// + /// I do not use getters for fields of objects that are used simply to + /// group values such as this aggregate. + /// </summary> + public class ParserRuleReturnScope : RuleReturnScope + { + private IToken start, stop; + + /// <summary>Return the start token or tree </summary> + override public object Start + { + get { return start; } + set { start = (IToken) value; } + } + + /// <summary>Return the stop token or tree </summary> + override public object Stop + { + get { return stop; } + set { stop = (IToken) value; } + } + + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/RecognitionException.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/RecognitionException.cs new file mode 100644 index 0000000..0f82e28 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/RecognitionException.cs @@ -0,0 +1,324 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + using ITree = Antlr.Runtime.Tree.ITree; + using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; + using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream; + using CommonTreeNodeStream = Antlr.Runtime.Tree.CommonTreeNodeStream; + using CommonTree = Antlr.Runtime.Tree.CommonTree; + + /// <summary>The root of the ANTLR exception hierarchy.</summary> + /// <remarks> + /// To avoid English-only error messages and to generally make things + /// as flexible as possible, these exceptions are not created with strings, + /// but rather the information necessary to generate an error. Then + /// the various reporting methods in Parser and Lexer can be overridden + /// to generate a localized error message. For example, MismatchedToken + /// exceptions are built with the expected token type. + /// So, don't expect getMessage() to return anything. + /// + /// You can access the stack trace, which means that you can compute the + /// complete trace of rules from the start symbol. This gives you considerable + /// context information with which to generate useful error messages. + /// + /// ANTLR generates code that throws exceptions upon recognition error and + /// also generates code to catch these exceptions in each rule. If you + /// want to quit upon first error, you can turn off the automatic error + /// handling mechanism using rulecatch action, but you still need to + /// override methods mismatch and recoverFromMismatchSet. + /// + /// In general, the recognition exceptions can track where in a grammar a + /// problem occurred and/or what was the expected input. While the parser + /// knows its state (such as current input symbol and line info) that + /// state can change before the exception is reported so current token index + /// is computed and stored at exception time. From this info, you can + /// perhaps print an entire line of input not just a single token, for example. + /// Better to just say the recognizer had a problem and then let the parser + /// figure out a fancy report. + /// </remarks> + [Serializable] + public class RecognitionException : Exception + { + #region Constructors + + /// <summary>Used for remote debugger deserialization </summary> + public RecognitionException() + : this(null, null, null) + { + } + + public RecognitionException(string message) + : this(message, null, null) + { + } + + public RecognitionException(string message, Exception inner) + : this(message, inner, null) + { + } + + public RecognitionException(IIntStream input) + : this(null, null, input) + { + } + + public RecognitionException(string message, IIntStream input) + : this(message, null, input) + { + } + + public RecognitionException(string message, Exception inner, IIntStream input) + : base(message, inner) + { + this.input = input; + this.index = input.Index(); + if (input is ITokenStream) + { + this.token = ((ITokenStream)input).LT(1); + this.line = token.Line; + this.charPositionInLine = token.CharPositionInLine; + } + if (input is ITreeNodeStream) + { + ExtractInformationFromTreeNodeStream(input); + } + else if (input is ICharStream) + { + this.c = input.LA(1); + this.line = ((ICharStream)input).Line; + this.charPositionInLine = ((ICharStream)input).CharPositionInLine; + } + else + { + this.c = input.LA(1); + } + } + + #endregion + + #region Public API + + /// <summary>Returns the input stream in which the error occurred</summary> + public IIntStream Input + { + get { return input; } + set { input = value; } + } + + /// <summary> + /// Returns the token/char index in the stream when the error occurred + /// </summary> + public int Index + { + get { return index; } + set { index = value; } + } + + /// <summary> + /// Returns the current Token when the error occurred (for parsers + /// although a tree parser might also set the token) + /// </summary> + public IToken Token + { + get { return token; } + set { token = value; } + } + + /// <summary> + /// Returns the [tree parser] node where the error occured (for tree parsers). + /// </summary> + public object Node + { + get { return node; } + set { node = value; } + } + + /// <summary> + /// Returns the current char when the error occurred (for lexers) + /// </summary> + public int Char + { + get { return c; } + set { c = value; } + } + + /// <summary> + /// Returns the character position in the line when the error + /// occurred (for lexers) + /// </summary> + public int CharPositionInLine + { + get { return charPositionInLine; } + set { charPositionInLine = value; } + } + + /// <summary> + /// Returns the line at which the error occurred (for lexers) + /// </summary> + public int Line + { + get { return line; } + set { line = value; } + } + + /// <summary> + /// Returns the token type or char of the unexpected input element + /// </summary> + virtual public int UnexpectedType + { + get + { + if (input is ITokenStream) + { + return token.Type; + } + else if (input is ITreeNodeStream) + { + ITreeNodeStream nodes = (ITreeNodeStream)input; + ITreeAdaptor adaptor = nodes.TreeAdaptor; + return adaptor.GetNodeType(node); + } + else + { + return c; + } + } + + } + + #endregion + + #region Non-Public API + + protected void ExtractInformationFromTreeNodeStream(IIntStream input) + { + ITreeNodeStream nodes = (ITreeNodeStream)input; + this.node = nodes.LT(1); + ITreeAdaptor adaptor = nodes.TreeAdaptor; + IToken payload = adaptor.GetToken(node); + if ( payload != null ) + { + this.token = payload; + if ( payload.Line <= 0 ) + { + // imaginary node; no line/pos info; scan backwards + int i = -1; + object priorNode = nodes.LT(i); + while ( priorNode != null ) + { + IToken priorPayload = adaptor.GetToken(priorNode); + if ( (priorPayload != null) && (priorPayload.Line > 0) ) + { + // we found the most recent real line / pos info + this.line = priorPayload.Line; + this.charPositionInLine = priorPayload.CharPositionInLine; + this.approximateLineInfo = true; + break; + } + --i; + priorNode = nodes.LT(i); + } + } + else + { + // node created from real token + this.line = payload.Line; + this.charPositionInLine = payload.CharPositionInLine; + } + } + else if (this.node is ITree) + { + this.line = ((ITree)this.node).Line; + this.charPositionInLine = ((ITree)this.node).CharPositionInLine; + if (this.node is CommonTree) + { + this.token = ((CommonTree)this.node).Token; + } + } + else + { + int type = adaptor.GetNodeType(this.node); + string text = adaptor.GetNodeText(this.node); + this.token = new CommonToken(type, text); + } + } + + #endregion + + #region Data Members + + + /// <summary>What input stream did the error occur in? </summary> + [NonSerialized] + protected IIntStream input; + + /// <summary> + /// What is index of token/char were we looking at when the error occurred? + /// </summary> + protected int index; + + /// <summary> + /// The current Token when an error occurred. Since not all streams + /// can retrieve the ith Token, we have to track the Token object. + /// </summary> + protected IToken token; + + /// <summary>[Tree parser] Node with the problem.</summary> + protected object node; + + /// <summary>The current char when an error occurred. For lexers. </summary> + protected int c; + + /// <summary>Track the line at which the error occurred in case this is + /// generated from a lexer. We need to track this since the + /// unexpected char doesn't carry the line info. + /// </summary> + protected int line; + + protected int charPositionInLine; + + /// <summary> + /// If you are parsing a tree node stream, you will encounter some + /// imaginary nodes w/o line/col info. We now search backwards looking + /// for most recent token with line/col info, but notify getErrorHeader() + /// that info is approximate. + /// </summary> + public bool approximateLineInfo; + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/RecognizerSharedState.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/RecognizerSharedState.cs new file mode 100644 index 0000000..6a27686 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/RecognizerSharedState.cs @@ -0,0 +1,160 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + using IDictionary = System.Collections.IDictionary; + + /// <summary> + /// The set of fields needed by an abstract recognizer to recognize input + /// and recover from errors + /// </summary> + /// <remarks> + /// As a separate state object, it can be shared among multiple grammars; + /// e.g., when one grammar imports another. + /// These fields are publicly visible but the actual state pointer per + /// parser is protected. + /// </remarks> + public class RecognizerSharedState + { + /// <summary> + /// Tracks the set of token types that can follow any rule invocation. + /// Stack grows upwards. When it hits the max, it grows 2x in size + /// and keeps going. + /// </summary> + public BitSet[] following = new BitSet[BaseRecognizer.INITIAL_FOLLOW_STACK_SIZE]; + public int followingStackPointer = -1; + + /// <summary> + /// This is true when we see an error and before having successfully + /// matched a token. Prevents generation of more than one error message + /// per error. + /// </summary> + public bool errorRecovery = false; + + /// <summary> + /// The index into the input stream where the last error occurred. + /// </summary> + /// <remarks> + /// This is used to prevent infinite loops where an error is found + /// but no token is consumed during recovery...another error is found, + /// ad naseum. This is a failsafe mechanism to guarantee that at least + /// one token/tree node is consumed for two errors. + /// </remarks> + public int lastErrorIndex = -1; + + /// <summary> + /// In lieu of a return value, this indicates that a rule or token + /// has failed to match. Reset to false upon valid token match. + /// </summary> + public bool failed = false; + + /// <summary> + /// Did the recognizer encounter a syntax error? Track how many. + /// </summary> + public int syntaxErrors = 0; + + /// <summary> + /// If 0, no backtracking is going on. Safe to exec actions etc... + /// If >0 then it's the level of backtracking. + /// </summary> + public int backtracking = 0; + + /// <summary> + /// An array[size num rules] of Map<Integer,Integer> that tracks + /// the stop token index for each rule. + /// </summary> + /// <remarks> + /// ruleMemo[ruleIndex] is the memoization table for ruleIndex. + /// For key ruleStartIndex, you get back the stop token for + /// associated rule or MEMO_RULE_FAILED. + /// + /// This is only used if rule memoization is on (which it is by default). + /// </remarks> + public IDictionary[] ruleMemo; + + + #region Lexer Specific Members + // LEXER FIELDS (must be in same state object to avoid casting + // constantly in generated code and Lexer object) :( + + + /// <summary> + /// Token object normally returned by NextToken() after matching lexer rules. + /// </summary> + /// <remarks> + /// The goal of all lexer rules/methods is to create a token object. + /// This is an instance variable as multiple rules may collaborate to + /// create a single token. nextToken will return this object after + /// matching lexer rule(s). If you subclass to allow multiple token + /// emissions, then set this to the last token to be matched or + /// something nonnull so that the auto token emit mechanism will not + /// emit another token. + /// </remarks> + public IToken token; + + /// <summary> + /// What character index in the stream did the current token start at? + /// </summary> + /// <remarks> + /// Needed, for example, to get the text for current token. Set at + /// the start of nextToken. + /// </remarks> + public int tokenStartCharIndex = -1; + + /// <summary> + /// The line on which the first character of the token resides + /// </summary> + public int tokenStartLine; + + /// <summary>The character position of first character within the line</summary> + public int tokenStartCharPositionInLine; + + /// <summary>The channel number for the current token</summary> + public int channel; + + /// <summary>The token type for the current token</summary> + public int type; + + /// <summary> + /// You can set the text for the current token to override what is in + /// the input char buffer. Use setText() or can set this instance var. + /// </summary> + public string text; + + #endregion + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/RuleReturnScope.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/RuleReturnScope.cs new file mode 100644 index 0000000..e78abfd --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/RuleReturnScope.cs @@ -0,0 +1,74 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + + /// <summary> + /// Rules can return start/stop info as well as possible trees and templates + /// </summary> + public class RuleReturnScope + { + /// <summary>Return the start token or tree </summary> + virtual public object Start + { + get { return null; } + set { throw new NotSupportedException("Setter has not been defined for this property."); } + } + + /// <summary>Return the stop token or tree </summary> + virtual public object Stop + { + get { return null; } + set { throw new NotSupportedException("Setter has not been defined for this property."); } + } + + /// <summary>Has a value potentially if output=AST; </summary> + virtual public object Tree + { + get { return null; } + set { throw new NotSupportedException("Setter has not been defined for this property."); } + } + /// <summary> + /// Has a value potentially if output=template; + /// Don't use StringTemplate type to avoid dependency on ST assembly + /// </summary> + virtual public object Template + { + get { return null; } + + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/Token.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/Token.cs new file mode 100644 index 0000000..7965cdc --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/Token.cs @@ -0,0 +1,84 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + +#if DOTNET1 + public sealed class Token { + /// <summary> + /// Instead of a static modifier for Token. + /// </summary> + private Token() { + } +#else + public static class Token { +#endif + + public const int EOR_TOKEN_TYPE = 1; + + /// <summary>imaginary tree navigation type; traverse "get child" link </summary> + public const int DOWN = 2; + /// <summary>imaginary tree navigation type; finish with a child list </summary> + public const int UP = 3; + + public static readonly int MIN_TOKEN_TYPE = UP + 1; + + public static readonly int EOF = (int)CharStreamConstants.EOF; + public static readonly IToken EOF_TOKEN = new CommonToken(EOF); + + public const int INVALID_TOKEN_TYPE = 0; + public static readonly IToken INVALID_TOKEN = new CommonToken(INVALID_TOKEN_TYPE); + + /// <summary> + /// In an action, a lexer rule can set token to this SKIP_TOKEN and ANTLR + /// will avoid creating a token for this symbol and try to fetch another. + /// </summary> + public static readonly IToken SKIP_TOKEN = new CommonToken(INVALID_TOKEN_TYPE); + + /// <summary> + /// All tokens go to the parser (unless skip() is called in that rule) + /// on a particular "channel". The parser tunes to a particular channel + /// so that whitespace etc... can go to the parser on a "hidden" channel. + /// </summary> + public const int DEFAULT_CHANNEL = 0; + + /// <summary> + /// Anything on different channel than DEFAULT_CHANNEL is not parsed by parser. + /// </summary> + public const int HIDDEN_CHANNEL = 99; + } +} diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/TokenRewriteStream.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/TokenRewriteStream.cs new file mode 100644 index 0000000..c2d84f4 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/TokenRewriteStream.cs @@ -0,0 +1,664 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +Copyright (c) 2007-2008 Johannes Luber +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Runtime +{ + using System; + using StringBuilder = System.Text.StringBuilder; + using IList = System.Collections.IList; + using ArrayList = System.Collections.ArrayList; + using IDictionary = System.Collections.IDictionary; + using Hashtable = System.Collections.Hashtable; + using IComparer = System.Collections.IComparer; + using IEnumerator = System.Collections.IEnumerator; + + /// <summary>Useful for dumping out the input stream after doing some + /// augmentation or other manipulations. + /// </summary> + /// + /// <remarks> + /// You can insert stuff, Replace, and delete chunks. Note that the + /// operations are done lazily--only if you convert the buffer to a + /// String. This is very efficient because you are not moving data around + /// all the time. As the buffer of tokens is converted to strings, the + /// ToString() method(s) check to see if there is an operation at the + /// current index. If so, the operation is done and then normal String + /// rendering continues on the buffer. This is like having multiple Turing + /// machine instruction streams (programs) operating on a single input tape. :) + /// + /// Since the operations are done lazily at ToString-time, operations do not + /// screw up the token index values. That is, an insert operation at token + /// index i does not change the index values for tokens i+1..n-1. + /// + /// Because operations never actually alter the buffer, you may always get + /// the original token stream back without undoing anything. Since + /// the instructions are queued up, you can easily simulate transactions and + /// roll back any changes if there is an error just by removing instructions. + /// For example, + /// + /// CharStream input = new ANTLRFileStream("input"); + /// TLexer lex = new TLexer(input); + /// TokenRewriteStream tokens = new TokenRewriteStream(lex); + /// T parser = new T(tokens); + /// parser.startRule(); + /// + /// Then in the rules, you can execute + /// IToken t,u; + /// ... + /// input.InsertAfter(t, "text to put after t");} + /// input.InsertAfter(u, "text after u");} + /// System.out.println(tokens.ToString()); + /// + /// Actually, you have to cast the 'input' to a TokenRewriteStream. :( + /// + /// You can also have multiple "instruction streams" and get multiple + /// rewrites from a single pass over the input. Just name the instruction + /// streams and use that name again when printing the buffer. This could be + /// useful for generating a C file and also its header file--all from the + /// same buffer: + /// + /// tokens.InsertAfter("pass1", t, "text to put after t");} + /// tokens.InsertAfter("pass2", u, "text after u");} + /// System.out.println(tokens.ToString("pass1")); + /// System.out.println(tokens.ToString("pass2")); + /// + /// If you don't use named rewrite streams, a "default" stream is used as + /// the first example shows. + /// </remarks> + public class TokenRewriteStream : CommonTokenStream + { + public const string DEFAULT_PROGRAM_NAME = "default"; + public const int PROGRAM_INIT_SIZE = 100; + public const int MIN_TOKEN_INDEX = 0; + + private class RewriteOpComparer : IComparer + { + public virtual int Compare(object o1, object o2) + { + RewriteOperation r1 = (RewriteOperation) o1; + RewriteOperation r2 = (RewriteOperation) o2; + if (r1.index < r2.index) + return - 1; + if (r1.index > r2.index) + return 1; + return 0; + } + } + + // Define the rewrite operation hierarchy + + protected internal class RewriteOperation + { + /** What index into rewrites List are we? */ + protected internal int instructionIndex; + /** Token buffer index. */ + protected internal int index; + protected internal object text; + protected internal TokenRewriteStream parent; + + protected internal RewriteOperation(int index, object text, TokenRewriteStream parent) + { + this.index = index; + this.text = text; + this.parent = parent; + } + /// <summary>Execute the rewrite operation by possibly adding to the buffer. + /// Return the index of the next token to operate on. + /// </summary> + public virtual int Execute(StringBuilder buf) + { + return index; + } + public override string ToString() + { + string opName = GetType().FullName; + int dollarIndex = opName.IndexOf('$'); + opName = opName.Substring(dollarIndex + 1, (opName.Length) - (dollarIndex + 1)); + return "<" + opName + "@" + index + ":\"" + text + "\">"; + } + } + + protected internal class InsertBeforeOp : RewriteOperation + { + public InsertBeforeOp(int index, object text, TokenRewriteStream parent) + : base(index, text, parent) + { + } + public override int Execute(StringBuilder buf) + { + buf.Append(text); + buf.Append(parent.Get(index).Text); + return index+1; + } + } + + /// <summary>I'm going to try replacing range from x..y with (y-x)+1 ReplaceOp + /// instructions. + /// </summary> + protected internal class ReplaceOp : RewriteOperation + { + protected internal int lastIndex; + public ReplaceOp(int from, int to, object text, TokenRewriteStream parent) + : base(from, text, parent) + { + lastIndex = to; + } + + public override int Execute(StringBuilder buf) + { + if (text != null) + { + buf.Append(text); + } + return lastIndex + 1; + } + + public override string ToString() { + return "<ReplaceOp@" + index + ".." + lastIndex + ":\"" + text + "\">"; + } + } + + protected internal class DeleteOp : ReplaceOp + { + public DeleteOp(int from, int to, TokenRewriteStream parent) + : base(from, to, null, parent) + { + } + + public override string ToString() { + return "<DeleteOp@" + index + ".." + lastIndex + ">"; + } + } + + /// <summary>You may have multiple, named streams of rewrite operations. + /// I'm calling these things "programs." + /// Maps String (name) -> rewrite (IList) + /// </summary> + protected IDictionary programs = null; + + /// <summary>Map String (program name) -> Integer index </summary> + protected IDictionary lastRewriteTokenIndexes = null; + + public TokenRewriteStream() + { + Init(); + } + + public TokenRewriteStream(ITokenSource tokenSource) + : base(tokenSource) + { + Init(); + } + + public TokenRewriteStream(ITokenSource tokenSource, int channel) + : base(tokenSource, channel) + { + Init(); + } + + protected internal virtual void Init() + { + programs = new Hashtable(); + programs[DEFAULT_PROGRAM_NAME] = new ArrayList(PROGRAM_INIT_SIZE); + lastRewriteTokenIndexes = new Hashtable(); + } + + public virtual void Rollback(int instructionIndex) + { + Rollback(DEFAULT_PROGRAM_NAME, instructionIndex); + } + + /// <summary>Rollback the instruction stream for a program so that + /// the indicated instruction (via instructionIndex) is no + /// longer in the stream. UNTESTED! + /// </summary> + public virtual void Rollback(string programName, int instructionIndex) + { + IList instructionStream = (IList) programs[programName]; + if (instructionStream != null) + { + programs[programName] = (IList) ((ArrayList) instructionStream).GetRange(MIN_TOKEN_INDEX, instructionIndex - MIN_TOKEN_INDEX); + } + } + + public virtual void DeleteProgram() + { + DeleteProgram(DEFAULT_PROGRAM_NAME); + } + + /// <summary>Reset the program so that no instructions exist </summary> + public virtual void DeleteProgram(string programName) + { + Rollback(programName, MIN_TOKEN_INDEX); + } + + public virtual void InsertAfter(IToken t, object text) + { + InsertAfter(DEFAULT_PROGRAM_NAME, t, text); + } + + public virtual void InsertAfter(int index, object text) + { + InsertAfter(DEFAULT_PROGRAM_NAME, index, text); + } + + public virtual void InsertAfter(string programName, IToken t, object text) + { + InsertAfter(programName, t.TokenIndex, text); + } + + public virtual void InsertAfter(string programName, int index, object text) + { + // to insert after, just insert before next index (even if past end) + InsertBefore(programName, index + 1, text); + //AddToSortedRewriteList(programName, new InsertAfterOp(index,text)); + } + + public virtual void InsertBefore(IToken t, object text) + { + InsertBefore(DEFAULT_PROGRAM_NAME, t, text); + } + + public virtual void InsertBefore(int index, object text) + { + InsertBefore(DEFAULT_PROGRAM_NAME, index, text); + } + + public virtual void InsertBefore(string programName, IToken t, object text) + { + InsertBefore(programName, t.TokenIndex, text); + } + + public virtual void InsertBefore(string programName, int index, object text) + { + RewriteOperation op = new InsertBeforeOp(index, text, this); + IList rewrites = GetProgram(programName); + rewrites.Add(op); + } + + public virtual void Replace(int index, object text) + { + Replace(DEFAULT_PROGRAM_NAME, index, index, text); + } + + public virtual void Replace(int from, int to, object text) + { + Replace(DEFAULT_PROGRAM_NAME, from, to, text); + } + + public virtual void Replace(IToken indexT, object text) + { + Replace(DEFAULT_PROGRAM_NAME, indexT, indexT, text); + } + + public virtual void Replace(IToken from, IToken to, object text) + { + Replace(DEFAULT_PROGRAM_NAME, from, to, text); + } + + public virtual void Replace(string programName, int from, int to, object text) + { + if ( from > to || from<0 || to<0 || to >= tokens.Count ) { + throw new ArgumentOutOfRangeException("replace: range invalid: " + from + ".." + + to + "(size=" + tokens.Count + ")"); + } + RewriteOperation op = new ReplaceOp(from, to, text, this); + IList rewrites = GetProgram(programName); + op.instructionIndex = rewrites.Count; + rewrites.Add(op); + } + + public virtual void Replace(string programName, IToken from, IToken to, object text) + { + Replace(programName, from.TokenIndex, to.TokenIndex, text); + } + + public virtual void Delete(int index) + { + Delete(DEFAULT_PROGRAM_NAME, index, index); + } + + public virtual void Delete(int from, int to) + { + Delete(DEFAULT_PROGRAM_NAME, from, to); + } + + public virtual void Delete(IToken indexT) + { + Delete(DEFAULT_PROGRAM_NAME, indexT, indexT); + } + + public virtual void Delete(IToken from, IToken to) + { + Delete(DEFAULT_PROGRAM_NAME, from, to); + } + + public virtual void Delete(string programName, int from, int to) + { + Replace(programName, from, to, null); + } + + public virtual void Delete(string programName, IToken from, IToken to) + { + Replace(programName, from, to, null); + } + + public virtual int GetLastRewriteTokenIndex() + { + return GetLastRewriteTokenIndex(DEFAULT_PROGRAM_NAME); + } + + protected virtual int GetLastRewriteTokenIndex(string programName) + { + object I = lastRewriteTokenIndexes[programName]; + if (I == null) + { + return -1; + } + return (int)I; + } + + protected virtual void SetLastRewriteTokenIndex(string programName, int i) + { + lastRewriteTokenIndexes[programName] = i; + } + + protected virtual IList GetProgram(string name) + { + IList instructionStream = (IList) programs[name]; + if (instructionStream == null) + { + instructionStream = InitializeProgram(name); + } + return instructionStream; + } + + private IList InitializeProgram(string name) + { + IList instructionStream = new ArrayList(PROGRAM_INIT_SIZE); + programs[name] = instructionStream; + return instructionStream; + } + + public virtual string ToOriginalString() + { + return ToOriginalString(MIN_TOKEN_INDEX, Count - 1); + } + + public virtual string ToOriginalString(int start, int end) + { + StringBuilder buf = new StringBuilder(); + for (int i = start; (i >= MIN_TOKEN_INDEX) && (i <= end) && (i < tokens.Count); i++) + { + buf.Append(Get(i).Text); + } + return buf.ToString(); + } + + public override string ToString() + { + return ToString(MIN_TOKEN_INDEX, Count-1); + } + + public virtual string ToString(string programName) + { + return ToString(programName, MIN_TOKEN_INDEX, Count-1); + } + + public override string ToString(int start, int end) + { + return ToString(DEFAULT_PROGRAM_NAME, start, end); + } + + public virtual string ToString(string programName, int start, int end) + { + IList rewrites = (IList) programs[programName]; + + // ensure start/end are in range + if ( end>tokens.Count-1 ) end = tokens.Count-1; + if ( start<0 ) start = 0; + + if ( (rewrites == null) || (rewrites.Count == 0) ) + { + return ToOriginalString(start, end); // no instructions to execute + } + StringBuilder buf = new StringBuilder(); + + // First, optimize instruction stream + IDictionary indexToOp = ReduceToSingleOperationPerIndex(rewrites); + + // Walk buffer, executing instructions and emitting tokens + int i = start; + while ( i <= end && i < tokens.Count ) { + RewriteOperation op = (RewriteOperation)indexToOp[i]; + indexToOp.Remove(i); // remove so any left have index size-1 + IToken t = (IToken) tokens[i]; + if ( op==null ) { + // no operation at that index, just dump token + buf.Append(t.Text); + i++; // move to next token + } + else { + i = op.Execute(buf); // execute operation and skip + } + } + + // include stuff after end if it's last index in buffer + // So, if they did an insertAfter(lastValidIndex, "foo"), include + // foo if end==lastValidIndex. + if ( end==tokens.Count-1 ) { + // Scan any remaining operations after last token + // should be included (they will be inserts). + IEnumerator iter = indexToOp.Values.GetEnumerator(); + while (iter.MoveNext()) { + InsertBeforeOp iop = (InsertBeforeOp)iter.Current; + if ( iop.index >= tokens.Count-1 ) buf.Append(iop.text); + } + } + return buf.ToString(); + } + + /// <summary> + /// Return a map from token index to operation. + /// </summary> + /// <remarks>We need to combine operations and report invalid operations (like + /// overlapping replaces that are not completed nested). Inserts to + /// same index need to be combined etc... Here are the cases: + /// + /// I.i.u I.j.v leave alone, nonoverlapping + /// I.i.u I.i.v combine: Iivu + /// + /// R.i-j.u R.x-y.v | i-j in x-y delete first R + /// R.i-j.u R.i-j.v delete first R + /// R.i-j.u R.x-y.v | x-y in i-j ERROR + /// R.i-j.u R.x-y.v | boundaries overlap ERROR + /// + /// I.i.u R.x-y.v | i in x-y delete I + /// I.i.u R.x-y.v | i not in x-y leave alone, nonoverlapping + /// R.x-y.v I.i.u | i in x-y ERROR + /// R.x-y.v I.x.u R.x-y.uv (combine, delete I) + /// R.x-y.v I.i.u | i not in x-y leave alone, nonoverlapping + /// + /// I.i.u = insert u before op @ index i + /// R.x-y.u = replace x-y indexed tokens with u + /// + /// First we need to examine replaces. For any replace op: + /// + /// 1. wipe out any insertions before op within that range. + /// 2. Drop any replace op before that is contained completely within + /// that range. + /// 3. Throw exception upon boundary overlap with any previous replace. + /// + /// Then we can deal with inserts: + /// + /// 1. for any inserts to same index, combine even if not adjacent. + /// 2. for any prior replace with same left boundary, combine this + /// insert with replace and delete this replace. + /// 3. throw exception if index in same range as previous replace + /// + /// Don't actually delete; make op null in list. Easier to walk list. + /// Later we can throw as we add to index -> op map. + /// + /// Note that I.2 R.2-2 will wipe out I.2 even though, technically, the + /// inserted stuff would be before the replace range. But, if you + /// add tokens in front of a method body '{' and then delete the method + /// body, I think the stuff before the '{' you added should disappear too. + /// </remarks> + protected IDictionary ReduceToSingleOperationPerIndex(IList rewrites) { + + // WALK REPLACES + for (int i = 0; i < rewrites.Count; i++) { + RewriteOperation op = (RewriteOperation)rewrites[i]; + if ( op==null ) continue; + if ( !(op is ReplaceOp) ) continue; + ReplaceOp rop = (ReplaceOp)rewrites[i]; + // Wipe prior inserts within range + IList inserts = GetKindOfOps(rewrites, typeof(InsertBeforeOp), i); + for (int j = 0; j < inserts.Count; j++) { + InsertBeforeOp iop = (InsertBeforeOp) inserts[j]; + if ( iop.index >= rop.index && iop.index <= rop.lastIndex ) { + // delete insert as it's a no-op. + rewrites[iop.instructionIndex] = null; + } + } + // Drop any prior replaces contained within + IList prevReplaces = GetKindOfOps(rewrites, typeof(ReplaceOp), i); + for (int j = 0; j < prevReplaces.Count; j++) { + ReplaceOp prevRop = (ReplaceOp) prevReplaces[j]; + if ( prevRop.index>=rop.index && prevRop.lastIndex <= rop.lastIndex ) { + // delete replace as it's a no-op. + rewrites[prevRop.instructionIndex] = null; + continue; + } + // throw exception unless disjoint or identical + bool disjoint = + prevRop.lastIndex<rop.index || prevRop.index > rop.lastIndex; + bool same = + prevRop.index==rop.index && prevRop.lastIndex==rop.lastIndex; + if ( !disjoint && !same ) { + throw new ArgumentOutOfRangeException("replace op boundaries of "+rop+ + " overlap with previous "+prevRop); + } + } + } + + // WALK INSERTS + for (int i = 0; i < rewrites.Count; i++) { + RewriteOperation op = (RewriteOperation)rewrites[i]; + if ( op==null ) continue; + if ( !(op is InsertBeforeOp) ) continue; + InsertBeforeOp iop = (InsertBeforeOp)rewrites[i]; + // combine current insert with prior if any at same index + IList prevInserts = GetKindOfOps(rewrites, typeof(InsertBeforeOp), i); + for (int j = 0; j < prevInserts.Count; j++) { + InsertBeforeOp prevIop = (InsertBeforeOp) prevInserts[j]; + if ( prevIop.index == iop.index ) { // combine objects + // convert to strings...we're in process of toString'ing + // whole token buffer so no lazy eval issue with any templates + iop.text = CatOpText(iop.text,prevIop.text); + // delete redundant prior insert + rewrites[prevIop.instructionIndex] = null; + } + } + // look for replaces where iop.index is in range; error + IList prevReplaces = GetKindOfOps(rewrites, typeof(ReplaceOp), i); + for (int j = 0; j < prevReplaces.Count; j++) { + ReplaceOp rop = (ReplaceOp) prevReplaces[j]; + if ( iop.index == rop.index ) { + rop.text = CatOpText(iop.text,rop.text); + rewrites[i] = null; // delete current insert + continue; + } + if ( iop.index >= rop.index && iop.index <= rop.lastIndex ) { + throw new ArgumentOutOfRangeException("insert op "+iop+ + " within boundaries of previous "+rop); + } + } + } + // System.out.println("rewrites after="+rewrites); + IDictionary m = new Hashtable(); + for (int i = 0; i < rewrites.Count; i++) { + RewriteOperation op = (RewriteOperation)rewrites[i]; + if ( op==null ) continue; // ignore deleted ops + if ( m[op.index]!=null ) { + throw new Exception("should only be one op per index"); + } + m[op.index] = op; + } + //System.out.println("index to op: "+m); + return m; + } + + protected String CatOpText(Object a, Object b) { + String x = ""; + String y = ""; + if ( a!=null ) x = a.ToString(); + if ( b!=null ) y = b.ToString(); + return x+y; + } + + protected IList GetKindOfOps(IList rewrites, Type kind) { + return GetKindOfOps(rewrites, kind, rewrites.Count); + } + + /// <summary> + /// Get all operations before an index of a particular kind + /// </summary> + protected IList GetKindOfOps(IList rewrites, Type kind, int before) { + IList ops = new ArrayList(); + for (int i=0; i<before && i<rewrites.Count; i++) { + RewriteOperation op = (RewriteOperation)rewrites[i]; + if ( op==null ) continue; // ignore deleted + if ( op.GetType() == kind ) ops.Add(op); + } + return ops; + } + + public virtual string ToDebugString() + { + return ToDebugString(MIN_TOKEN_INDEX, Count-1); + } + + public virtual string ToDebugString(int start, int end) + { + StringBuilder buf = new StringBuilder(); + for (int i = start; i >= MIN_TOKEN_INDEX && i <= end && i < tokens.Count; i++) + { + buf.Append(Get(i)); + } + return buf.ToString(); + } + } +} \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/UnwantedTokenException.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/UnwantedTokenException.cs new file mode 100644 index 0000000..2ade9fa --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr.Runtime/UnwantedTokenException.cs @@ -0,0 +1,64 @@ +/* +[The "BSD licence"] +Copyright (c) 2007-2008 Johannes Luber +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. +*/ + +using System; + +namespace Antlr.Runtime +{ + + /// <summary> + /// An extra token while parsing a TokenStream. + /// </summary> + public class UnwantedTokenException : MismatchedTokenException { + /// <summary> + /// Used for remote debugger deserialization + /// </summary> + public UnwantedTokenException() { + } + + public UnwantedTokenException(int expecting, IIntStream input) + : base(expecting, input) { + } + + public IToken UnexpectedToken { + get { return token; } + } + + public override string ToString() { + string exp = ", expected " + Expecting; + if (Expecting == Runtime.Token.INVALID_TOKEN_TYPE) { + exp = ""; + } + if (token==null) { + return "UnwantedTokenException(found=" + null + exp + ")"; + } + return "UnwantedTokenException(found=" + token.Text + exp + ")"; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr3.Runtime (VS2003).csproj b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr3.Runtime (VS2003).csproj new file mode 100644 index 0000000..573d1fc --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr3.Runtime (VS2003).csproj @@ -0,0 +1,464 @@ +<VisualStudioProject> + <CSHARP + ProjectType = "Local" + ProductVersion = "7.10.3077" + SchemaVersion = "2.0" + ProjectGuid = "{38F943D7-937F-4911-9882-24955F6AD0CC}" + > + <Build> + <Settings + ApplicationIcon = "" + AssemblyKeyContainerName = "" + AssemblyName = "Antlr3.Runtime" + AssemblyOriginatorKeyFile = "" + DefaultClientScript = "JScript" + DefaultHTMLPageLayout = "Grid" + DefaultTargetSchema = "IE50" + DelaySign = "false" + OutputType = "Library" + PreBuildEvent = "" + PostBuildEvent = "" + RootNamespace = "Antlr.Runtime" + RunPostBuildEvent = "OnBuildSuccess" + StartupObject = "" + > + <Config + Name = "Debug" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "DEBUG;STRONG_NAME;DOTNET1" + DocumentationFile = "bin\Debug\net-1.1\Antlr3.Runtime.xml" + DebugSymbols = "true" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "false" + OutputPath = "bin\Debug\net-1.1\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + <Config + Name = "Release" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "STRONG_NAME;DOTNET1" + DocumentationFile = "bin\Release\net-1.1\Antlr3.Runtime.xml" + DebugSymbols = "false" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "false" + OutputPath = "bin\Release\net-1.1\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + </Settings> + <References> + <Reference + Name = "system" + AssemblyName = "System" + HintPath = "..\..\..\..\..\..\WINDOWS\Microsoft.NET\Framework\v1.1.4322\system.dll" + /> + </References> + </Build> + <Files> + <Include> + <File + RelPath = "AssemblyInfo.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Messages.resx" + BuildAction = "EmbeddedResource" + /> + <File + RelPath = "Antlr.Runtime\ANTLRFileStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\ANTLRInputStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\ANTLRReaderStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\ANTLRStringStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\BaseRecognizer.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\BitSet.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\CharStreamState.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\ClassicToken.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\CommonToken.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\CommonTokenStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\Constants.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\DFA.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\EarlyExitException.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\FailedPredicateException.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\ICharStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\IIntStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\IToken.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\ITokenSource.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\ITokenStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\Lexer.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\MismatchedNotSetException.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\MismatchedRangeException.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\MismatchedSetException.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\MismatchedTokenException.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\MismatchedTreeNodeException.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\NoViableAltException.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\Parser.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\ParserRuleReturnScope.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\RecognitionException.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\RecognizerSharedState.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\RuleReturnScope.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\Token.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime\TokenRewriteStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Collections\CollectionUtils.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Collections\HashList.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Collections\StackList.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.debug\BlankDebugEventListener.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.debug\DebugEventHub.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.debug\DebugEventRepeater.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.debug\DebugEventSocketProxy.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.debug\DebugParser.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.debug\DebugTokenStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.debug\DebugTreeAdaptor.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.debug\DebugTreeNodeStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.debug\DebugTreeParser.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.debug\IDebugEventListener.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.debug\ParseTreeBuilder.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.debug\Profiler.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.debug\RemoteDebugEventSocketListener.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.debug\TraceDebugEventListener.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.debug\Tracer.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Misc\ErrorManager.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Misc\Stats.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\BaseTree.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\BaseTreeAdaptor.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\CommonTree.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\CommonTreeAdaptor.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\CommonTreeNodeStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\ITree.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\ITreeAdaptor.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\ITreeNodeStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\ParseTree.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\RewriteCardinalityException.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\RewriteEarlyExitException.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\RewriteEmptyStreamException.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\RewriteRuleElementStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\RewriteRuleNodeStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\RewriteRuleSubtreeStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\RewriteRuleTokenStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\TreeParser.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\TreePatternLexer.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\TreePatternParser.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\TreeRuleReturnScope.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\TreeWizard.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Runtime.Tree\UnBufferedTreeNodeStream.cs" + SubType = "Code" + BuildAction = "Compile" + /> + </Include> + </Files> + </CSHARP> +</VisualStudioProject> + diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr3.Runtime (VS2005).csproj b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr3.Runtime (VS2005).csproj new file mode 100644 index 0000000..19ec15e --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Antlr3.Runtime (VS2005).csproj @@ -0,0 +1,174 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <ProjectType>Local</ProjectType> + <ProductVersion>8.0.50727</ProductVersion> + <SchemaVersion>2.0</SchemaVersion> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ApplicationIcon> + </ApplicationIcon> + <AssemblyKeyContainerName> + </AssemblyKeyContainerName> + <AssemblyName>Antlr3.Runtime</AssemblyName> + <AssemblyOriginatorKeyFile> + </AssemblyOriginatorKeyFile> + <DefaultClientScript>JScript</DefaultClientScript> + <DefaultHTMLPageLayout>Grid</DefaultHTMLPageLayout> + <DefaultTargetSchema>IE50</DefaultTargetSchema> + <DelaySign>false</DelaySign> + <OutputType>Library</OutputType> + <RootNamespace>Antlr.Runtime</RootNamespace> + <NoStandardLibraries>false</NoStandardLibraries> + <RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent> + <StartupObject> + </StartupObject> + <FileUpgradeFlags> + </FileUpgradeFlags> + <ProjectGuid>{CF15D0D5-BE72-4F98-B70F-229ABA1DF0E8}</ProjectGuid> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <OutputPath>bin\Debug\net-2.0\</OutputPath> + <BaseAddress>285212672</BaseAddress> + <ConfigurationOverrideFile> + </ConfigurationOverrideFile> + <DefineConstants>TRACE;DEBUG;STRONG_NAME;DOTNET2</DefineConstants> + <DocumentationFile> + </DocumentationFile> + <DebugSymbols>true</DebugSymbols> + <FileAlignment>4096</FileAlignment> + <RegisterForComInterop>false</RegisterForComInterop> + <RemoveIntegerChecks>false</RemoveIntegerChecks> + <WarningLevel>4</WarningLevel> + <DebugType>full</DebugType> + <ErrorReport>prompt</ErrorReport> + <OutputType>Library</OutputType> + <RootNamespace>Antlr.Runtime</RootNamespace> + <Optimize>true</Optimize> + <StartupObject /> + <GenerateDocumentation>true</GenerateDocumentation> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <OutputPath>bin\Release\net-2.0\</OutputPath> + <BaseAddress>285212672</BaseAddress> + <ConfigurationOverrideFile> + </ConfigurationOverrideFile> + <DefineConstants>STRONG_NAME;DOTNET2</DefineConstants> + <DocumentationFile>bin\Release\net-2.0\Antlr3.Runtime.XML</DocumentationFile> + <DebugSymbols>true</DebugSymbols> + <FileAlignment>4096</FileAlignment> + <RegisterForComInterop>false</RegisterForComInterop> + <RemoveIntegerChecks>false</RemoveIntegerChecks> + <WarningLevel>4</WarningLevel> + <DebugType>full</DebugType> + <ErrorReport>prompt</ErrorReport> + <OutputType>Library</OutputType> + <RootNamespace>Antlr.Runtime</RootNamespace> + <Optimize>true</Optimize> + <StartupObject /> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Antlr.Runtime\ANTLRFileStream.cs" /> + <Compile Include="Antlr.Runtime\ANTLRStringStream.cs" /> + <Compile Include="AssemblyInfo.cs" /> + <Compile Include="Antlr.Runtime\BitSet.cs" /> + <Compile Include="Antlr.Runtime\ICharStream.cs" /> + <Compile Include="Antlr.Runtime\CharStreamState.cs" /> + <Compile Include="Antlr.Runtime\ClassicToken.cs" /> + <Compile Include="Antlr.Runtime\CommonToken.cs" /> + <Compile Include="Antlr.Runtime\DFA.cs" /> + <Compile Include="Antlr.Runtime\EarlyExitException.cs" /> + <Compile Include="Antlr.Runtime\FailedPredicateException.cs" /> + <Compile Include="Antlr.Runtime\IIntStream.cs" /> + <Compile Include="Antlr.Runtime\MismatchedNotSetException.cs" /> + <Compile Include="Antlr.Runtime\MismatchedRangeException.cs" /> + <Compile Include="Antlr.Runtime\MismatchedSetException.cs" /> + <Compile Include="Antlr.Runtime\MismatchedTokenException.cs" /> + <Compile Include="Antlr.Runtime\MismatchedTreeNodeException.cs" /> + <Compile Include="Antlr.Runtime\NoViableAltException.cs" /> + <Compile Include="Antlr.Runtime\Parser.cs" /> + <Compile Include="Antlr.Runtime\ParserRuleReturnScope.cs" /> + <Compile Include="Antlr.Runtime\RecognitionException.cs" /> + <Compile Include="Antlr.Runtime\RuleReturnScope.cs" /> + <Compile Include="Antlr.Runtime\Token.cs" /> + <Compile Include="Antlr.Runtime\ITokenSource.cs" /> + <Compile Include="Antlr.Runtime\MissingTokenException.cs" /> + <Compile Include="Antlr.Runtime.Tree\CommonErrorNode.cs" /> + <Compile Include="Antlr.Runtime\UnwantedTokenException.cs" /> + <Compile Include="Antlr.Runtime.Collections\CollectionUtils.cs" /> + <Compile Include="Antlr.Runtime.Collections\HashList.cs" /> + <Compile Include="Antlr.Runtime.Collections\StackList.cs" /> + <Compile Include="Antlr.Runtime.Tree\BaseTree.cs" /> + <Compile Include="Antlr.Runtime.Tree\BaseTreeAdaptor.cs" /> + <Compile Include="Antlr.Runtime.Tree\CommonTree.cs" /> + <Compile Include="Antlr.Runtime.Tree\CommonTreeAdaptor.cs" /> + <Compile Include="Antlr.Runtime.Tree\CommonTreeNodeStream.cs" /> + <Compile Include="Antlr.Runtime.Tree\ITree.cs" /> + <Compile Include="Antlr.Runtime.Tree\ITreeAdaptor.cs" /> + <Compile Include="Antlr.Runtime.Tree\ITreeNodeStream.cs" /> + <Compile Include="Antlr.Runtime.Tree\ParseTree.cs" /> + <Compile Include="Antlr.Runtime.Tree\TreeParser.cs" /> + <Compile Include="Antlr.Runtime.Tree\TreeRuleReturnScope.cs" /> + <Compile Include="Antlr.Runtime.Debug\DebugEventSocketProxy.cs" /> + <Compile Include="Antlr.Runtime.Debug\DebugParser.cs" /> + <Compile Include="Antlr.Runtime.Debug\DebugTokenStream.cs" /> + <Compile Include="Antlr.Runtime.Debug\IDebugEventListener.cs" /> + <Compile Include="Antlr.Runtime.Debug\DebugTreeAdaptor.cs" /> + <Compile Include="Antlr.Runtime\Constants.cs" /> + <Compile Include="Messages.Designer.cs"> + <DependentUpon>Messages.resx</DependentUpon> + </Compile> + <Compile Include="Antlr.Runtime.Debug\DebugTreeNodeStream.cs" /> + <Compile Include="Antlr.Runtime.Debug\BlankDebugEventListener.cs" /> + <Compile Include="Antlr.Runtime.Debug\RemoteDebugEventSocketListener.cs" /> + <Compile Include="Antlr.Runtime.Debug\TraceDebugEventListener.cs" /> + <Compile Include="Antlr.Runtime.Debug\DebugEventHub.cs" /> + <Compile Include="Antlr.Runtime.Debug\DebugEventRepeater.cs" /> + <Compile Include="Antlr.Runtime.Debug\DebugTreeParser.cs" /> + <Compile Include="Antlr.Runtime.Debug\ParseTreeBuilder.cs" /> + <Compile Include="Antlr.Runtime.Debug\Profiler.cs" /> + <Compile Include="Antlr.Runtime.Debug\Tracer.cs" /> + <Compile Include="Antlr.Runtime.Misc\Stats.cs" /> + <Compile Include="Antlr.Runtime.Misc\ErrorManager.cs" /> + <Compile Include="Antlr.Runtime.Tree\TreeWizard.cs" /> + <Compile Include="Antlr.Runtime.Tree\TreePatternLexer.cs" /> + <Compile Include="Antlr.Runtime.Tree\RewriteRuleNodeStream.cs" /> + <Compile Include="Antlr.Runtime.Tree\TreePatternParser.cs" /> + <Compile Include="Antlr.Runtime.Tree\RewriteCardinalityException.cs" /> + <Compile Include="Antlr.Runtime.Tree\RewriteEarlyExitException.cs" /> + <Compile Include="Antlr.Runtime.Tree\RewriteEmptyStreamException.cs" /> + <Compile Include="Antlr.Runtime.Tree\RewriteRuleElementStream.cs" /> + <Compile Include="Antlr.Runtime.Tree\RewriteRuleSubtreeStream.cs" /> + <Compile Include="Antlr.Runtime.Tree\RewriteRuleTokenStream.cs" /> + <Compile Include="Antlr.Runtime.Tree\UnBufferedTreeNodeStream.cs" /> + <Compile Include="Antlr.Runtime\ANTLRInputStream.cs" /> + <Compile Include="Antlr.Runtime\ANTLRReaderStream.cs" /> + <Compile Include="Antlr.Runtime\BaseRecognizer.cs" /> + <Compile Include="Antlr.Runtime\CommonTokenStream.cs" /> + <Compile Include="Antlr.Runtime\RecognizerSharedState.cs" /> + <Compile Include="Antlr.Runtime\IToken.cs" /> + <Compile Include="Antlr.Runtime\ITokenStream.cs" /> + <Compile Include="Antlr.Runtime\Lexer.cs" /> + <Compile Include="Antlr.Runtime\TokenRewriteStream.cs" /> + <Compile Include="Antlr.Runtime.Tree\ITreeVisitorAction.cs" /> + <Compile Include="Antlr.Runtime.Tree\TreeVisitor.cs" /> + </ItemGroup> + <ItemGroup> + <EmbeddedResource Include="Messages.resx"> + <SubType>Designer</SubType> + <Generator>ResXFileCodeGenerator</Generator> + </EmbeddedResource> + </ItemGroup> + <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> + <PropertyGroup> + <PreBuildEvent> + </PreBuildEvent> + <PostBuildEvent> + </PostBuildEvent> + <ProjectGuid>{CF15D0D5-BE72-4F98-B70F-229ABA1DF0E8}</ProjectGuid> + <RootNamespace>Antlr.Runtime</RootNamespace> + </PropertyGroup> +</Project> \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/AssemblyInfo.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/AssemblyInfo.cs new file mode 100644 index 0000000..02be7d5 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/AssemblyInfo.cs @@ -0,0 +1,111 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +using System.Reflection; +using System.Runtime.CompilerServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. + +// TODO: Review the values of the assembly attributes + +[assembly: AssemblyTitle("ANTLR3.Runtime.NET")] +[assembly: AssemblyDescription("ANTLR3 C# Runtime for .NET")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("Copyright (c) 2005-2007 Kunle Odutola, 2007-2009 Johannes Luber")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Allow the use of the assembly in medium trust for websites +[assembly: System.Security.AllowPartiallyTrustedCallers] + + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Revision +// Build Number +// +// You can specify all the values or you can default the Revision and Build Numbers +// by using the '*' as shown below: + +[assembly: AssemblyVersion("3.1.3.*")] + +// +// In order to sign your assembly you must specify a key to use. Refer to the +// Microsoft .NET Framework documentation for more information on assembly signing. +// +// Use the attributes below to control which key is used for signing. +// +// Notes: +// (*) If no key is specified, the assembly is not signed. +// (*) KeyName refers to a key that has been installed in the Crypto Service +// Provider (CSP) on your machine. KeyFile refers to a file which contains +// a key. +// (*) If the KeyFile and the KeyName values are both specified, the +// following processing occurs: +// (1) If the KeyName can be found in the CSP, that key is used. +// (2) If the KeyName does not exist and the KeyFile does exist, the key +// in the KeyFile is installed into the CSP and used. +// (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility. +// When specifying the KeyFile, the location of the KeyFile should be +// relative to the project output directory which is +// %Project Directory%\obj\<configuration>. For example, if your KeyFile is +// located in the project directory, you would specify the AssemblyKeyFile +// attribute as [assembly: AssemblyKeyFile("..\..\mykey.snk")] +// (*) Delay Signing is an advanced option - see the Microsoft .NET Framework +// documentation for more information on this. +// + +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyKeyName("")] + +#if STRONG_NAME +// This strongly suggests that the build is a VS.NET build. +[assembly: AssemblyDelaySign(false)] +[assembly: AssemblyKeyFile("../../../Antlr3_KeyPair.snk")] +#elif NANT_STRONGNAME +// This strongly suggests that the build is a NANT build. +[assembly: AssemblyDelaySign(false)] +[assembly: AssemblyKeyFile("../Antlr3_KeyPair.snk")] +#else +// This should never happen as the assembly should always be strong named. +[assembly: AssemblyDelaySign(false)] +[assembly: AssemblyKeyFile("")] +#endif + + diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Messages.Designer.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Messages.Designer.cs new file mode 100644 index 0000000..85c5c1e --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Messages.Designer.cs @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// <auto-generated> +// This code was generated by a tool. +// Runtime Version:2.0.50727.42 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// </auto-generated> +//------------------------------------------------------------------------------ + +namespace Antlr.Runtime { + using System; + + + /// <summary> + /// A strongly-typed resource class, for looking up localized strings, etc. + /// </summary> + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "2.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Messages { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Messages() { + } + + /// <summary> + /// Returns the cached ResourceManager instance used by this class. + /// </summary> + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Antlr.Runtime.Messages", typeof(Messages).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// <summary> + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// </summary> + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Messages.resx b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Messages.resx new file mode 100644 index 0000000..4fdb1b6 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/Messages.resx @@ -0,0 +1,101 @@ +<?xml version="1.0" encoding="utf-8"?> +<root> + <!-- + Microsoft ResX Schema + + Version 1.3 + + The primary goals of this format is to allow a simple XML format + that is mostly human readable. The generation and parsing of the + various data types are done through the TypeConverter classes + associated with the data types. + + Example: + + ... ado.net/XML headers & schema ... + <resheader name="resmimetype">text/microsoft-resx</resheader> + <resheader name="version">1.3</resheader> + <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader> + <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader> + <data name="Name1">this is my long string</data> + <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data> + <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64"> + [base64 mime encoded serialized .NET Framework object] + </data> + <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> + [base64 mime encoded string representing a byte array form of the .NET Framework object] + </data> + + There are any number of "resheader" rows that contain simple + name/value pairs. + + Each data row contains a name, and value. The row also contains a + type or mimetype. Type corresponds to a .NET class that support + text/value conversion through the TypeConverter architecture. + Classes that don't support this are serialized and stored with the + mimetype set. + + The mimetype is used for serialized objects, and tells the + ResXResourceReader how to depersist the object. This is currently not + extensible. For a given mimetype the value must be set accordingly: + + Note - application/x-microsoft.net.object.binary.base64 is the format + that the ResXResourceWriter will generate, however the reader can + read any of the formats listed below. + + mimetype: application/x-microsoft.net.object.binary.base64 + value : The object must be serialized with + : System.Serialization.Formatters.Binary.BinaryFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.soap.base64 + value : The object must be serialized with + : System.Runtime.Serialization.Formatters.Soap.SoapFormatter + : and then encoded with base64 encoding. + + mimetype: application/x-microsoft.net.object.bytearray.base64 + value : The object must be serialized into a byte array + : using a System.ComponentModel.TypeConverter + : and then encoded with base64 encoding. + --> + + <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> + <xsd:element name="root" msdata:IsDataSet="true"> + <xsd:complexType> + <xsd:choice maxOccurs="unbounded"> + <xsd:element name="data"> + <xsd:complexType> + <xsd:sequence> + <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> + <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" /> + </xsd:sequence> + <xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" /> + <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" /> + <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" /> + </xsd:complexType> + </xsd:element> + <xsd:element name="resheader"> + <xsd:complexType> + <xsd:sequence> + <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> + </xsd:sequence> + <xsd:attribute name="name" type="xsd:string" use="required" /> + </xsd:complexType> + </xsd:element> + </xsd:choice> + </xsd:complexType> + </xsd:element> + </xsd:schema> + <resheader name="resmimetype"> + <value>text/microsoft-resx</value> + </resheader> + <resheader name="version"> + <value>1.3</value> + </resheader> + <resheader name="reader"> + <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + <resheader name="writer"> + <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> +</root> \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/default.build b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/default.build new file mode 100644 index 0000000..4b818e9 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Runtime/default.build @@ -0,0 +1,132 @@ +<?xml version="1.0"?> +<project name="ANTLR C# Runtime Library" default="build"> + <tstamp/> + <property name="base.dir" value="${path::get-full-path( project::get-base-directory() )}" /> + + <include buildfile="../../antlr3.runtime.net.common.inc" /> + + <property name="antlr3.runtime.test" value="true" unless="${property::exists('antlr3.runtime.test')}" /> + <property name="enabletest" value="ALLOWTEST" unless="${property::exists('enabletest')}" /> + + <property name="name" value="Antlr3.Runtime" /> + <property name="test.name" value="${name}.Tests" /> + + <property name="assembly.name" value="${name}.dll" /> + <property name="test.assembly.name" value="${test.name}.exe" /> + + <property name="src.dir" value="${base.dir}/" /> + <property name="test.src.dir" value="${base.dir}/../Antlr3.Runtime.Tests" /> + + <property name="debug" value="true" unless="${property::exists('debug')}" /> + + <echo message="Building project: '${name}' version ${version} ==> '${assembly.name}'"/> + + <target name="build" depends="init, compile, test" description="compiles the source code"> + </target> + + <target name="init" depends="clean, copy_build_dependencies"> + <mkdir dir="${build.working.dir}/tests" /> + </target> + + <target name="copy_build_dependencies"> + <!-- Copy Antlr3.Runtime.Tests dependencies --> + <copy todir="${build.working.dir}" overwrite="true"> + <fileset basedir="${sharedlibrary.dir}/MbUnit"> + <include name="MbUnit.Framework.dll" /> + <include name="QuickGraph.Algorithms.dll" /> + <include name="QuickGraph.dll" /> + </fileset> + </copy> + <copy todir="${build.working.dir}" overwrite="true"> + <fileset basedir="${sharedlibrary.dir}/StringTemplate.NET/${target.clr}"> + <include name="StringTemplate.dll" /> + <include name="antlr.runtime.dll" /> + </fileset> + </copy> + </target> + + <target name="clean"> + </target> + + <target name="clean.vsnet" description="cleans up VS.NET build artifacts and output"> + <!-- delete VS.NET project artifacts directory --> + <echo message="Deleting VS.NET artifacts directory '${base.dir}/bin'" /> + <delete dir="${base.dir}/bin" failonerror="false" /> + <echo message="Deleting VS.NET artifacts directory '${base.dir}/obj'" /> + <delete dir="${base.dir}/obj" failonerror="false" /> + + <!-- delete VS.NET project artifacts directory for Tests projects--> + <echo message="Deleting VS.NET artifacts directory '${base.dir}/../Antlr3.Runtime.Tests/bin'" /> + <delete dir="${base.dir}/../Antlr3.Runtime.Tests/bin" failonerror="false" /> + <echo message="Deleting VS.NET artifacts directory '${base.dir}/../Antlr3.Runtime.Tests/obj'" /> + <delete dir="${base.dir}/../Antlr3.Runtime.Tests/obj" failonerror="false" /> + </target> + + <target name="test" depends="tests.run" if="${antlr3.runtime.test}"> + </target> + + <target name="tests.run" depends="tests.compile"> + <loadtasks assembly="${sharedlibrary.dir}/MbUnit/MbUnit.Tasks.dll" /> + <mbunit + report-types="Html" + report-filename-format="antlr3-runtime-report-{0}-{1}" + report-output-directory="${build.working.dir}" + halt-on-failure="true" + > + <assemblies> + <include name="${build.working.dir}/${test.assembly.name}" /> + </assemblies> + </mbunit> + </target> + + <target name="compile" depends="init"> + <csc + define="${strong_name};${dotnet_define}" + target="library" + debug="${debug}" + optimize="${optimize}" + output="${build.working.dir}/${assembly.name}" + doc="${build.working.dir}/${name}.xml"> + + <nowarn> + <warning number="1591" /> + <warning number="1572" /> + </nowarn> + + <sources basedir="${src.dir}" defaultexcludes="true"> + <include name="**/*.cs" /> + <exclude name="**/*.Designer.cs" + if="${framework::get-target-framework()=='net-1.1' or framework::get-target-framework()=='mono-1.0'}" + /> + </sources> + + <references> + <!-- <include name="${build.working.dir}/antlr.runtime.dll" /> --> + </references> + </csc> + </target> + + <target name="tests.compile" depends="init"> + <csc + define="${dotnet_define}" + target="exe" + debug="${debug}" + output="${build.working.dir}/${test.assembly.name}"> + + <sources basedir="${test.src.dir}" defaultexcludes="true"> + <include name="**/*.cs" /> + <exclude name="**/*.Designer.cs" + if="${framework::get-target-framework()=='net-1.1' or framework::get-target-framework()=='mono-1.0'}" + /> + </sources> + + <references> + <include name="${build.working.dir}/${assembly.name}" /> + <include name="${build.working.dir}/MbUnit.Framework.dll" /> + <include name="${build.working.dir}/QuickGraph.Algorithms.dll" /> + <include name="${build.working.dir}/QuickGraph.dll" /> + </references> + </csc> + </target> + +</project> diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Utility/Antlr.Utility.Tree/DOTTreeGenerator.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Utility/Antlr.Utility.Tree/DOTTreeGenerator.cs new file mode 100644 index 0000000..b816238 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Utility/Antlr.Utility.Tree/DOTTreeGenerator.cs @@ -0,0 +1,226 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +namespace Antlr.Utility.Tree +{ + using System; + using IDictionary = System.Collections.IDictionary; + using Hashtable = System.Collections.Hashtable; + using StringTemplate = Antlr.StringTemplate.StringTemplate; + using ITree = Antlr.Runtime.Tree.ITree; + using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; + using CommonTreeAdaptor = Antlr.Runtime.Tree.CommonTreeAdaptor; + + /// <summary> + /// A utility class to generate DOT diagrams (graphviz) from + /// arbitrary trees. You can pass in your own templates and + /// can pass in any kind of tree or use Tree interface method. + /// I wanted this separator so that you don't have to include + /// ST just to use the org.antlr.runtime.tree.* package. + /// This is a set of non-static methods so you can subclass + /// to override. For example, here is an invocation: + /// + /// CharStream input = new ANTLRInputStream(Console.In); + /// TLexer lex = new TLexer(input); + /// CommonTokenStream tokens = new CommonTokenStream(lex); + /// TParser parser = new TParser(tokens); + /// TParser.e_return r = parser.e(); + /// Tree t = (Tree)r.tree; + /// Console.Out.WriteLine(t.ToStringTree()); + /// DOTTreeGenerator gen = new DOTTreeGenerator(); + /// StringTemplate st = gen.ToDOT(t); + /// Console.Out.WriteLine(st); + /// + /// </summary> + public class DOTTreeGenerator + { + public static StringTemplate _treeST = new StringTemplate( + "digraph {\n" + + " ordering=out;\n" + + " ranksep=.4;\n" + + " node [shape=plaintext, fixedsize=true, fontsize=11, fontname=\"Courier\",\n" + + " width=.25, height=.25];\n" + + " edge [arrowsize=.5]\n" + + " $nodes$\n" + + " $edges$\n" + + "}\n" + ); + + public static StringTemplate _nodeST = new StringTemplate("$name$ [label=\"$text$\"];\n"); + + public static StringTemplate _edgeST = new StringTemplate("$parent$ -> $child$ // \"$parentText$\" -> \"$childText$\"\n"); + + /// <summary> + /// Track node to number mapping so we can get proper node name back + /// </summary> + IDictionary nodeToNumberMap = new Hashtable(); + + /// <summary> + /// Track node number so we can get unique node names + /// </summary> + int nodeNumber = 0; + + public StringTemplate ToDOT(object tree, ITreeAdaptor adaptor, StringTemplate _treeST, StringTemplate _edgeST) + { + StringTemplate treeST = _treeST.GetInstanceOf(); + nodeNumber = 0; + ToDOTDefineNodes(tree, adaptor, treeST); + nodeNumber = 0; + ToDOTDefineEdges(tree, adaptor, treeST); + /* + if ( adaptor.GetChildCount(tree)==0 ) { + // single node, don't do edge. + treeST.SetAttribute("nodes", adaptor.GetNodeText(tree)); + } + */ + return treeST; + } + + public StringTemplate ToDOT(object tree, ITreeAdaptor adaptor) + { + return ToDOT(tree, adaptor, _treeST, _edgeST); + } + + /// <summary> + /// Generate DOT (graphviz) for a whole tree not just a node. + /// For example, 3+4*5 should generate: + /// + /// digraph { + /// node [shape=plaintext, fixedsize=true, fontsize=11, fontname="Courier", + /// width=.4, height=.2]; + /// edge [arrowsize=.7] + /// "+"->3 + /// "+"->"*" + /// "*"->4 + /// "*"->5 + /// } + /// + /// Return the ST not a string in case people want to alter. + /// + /// Takes a Tree interface object. + /// </summary> + public StringTemplate ToDOT(ITree tree) + { + return ToDOT(tree, new CommonTreeAdaptor()); + } + + protected void ToDOTDefineNodes(object tree, ITreeAdaptor adaptor, StringTemplate treeST) + { + if ( tree == null ) + { + return; + } + int n = adaptor.GetChildCount(tree); + if ( n == 0 ) + { + // must have already dumped as child from previous + // invocation; do nothing + return; + } + + // define parent node + StringTemplate parentNodeST = GetNodeST(adaptor, tree); + treeST.SetAttribute("nodes", parentNodeST); + + // for each child, do a "<unique-name> [label=text]" node def + for (int i = 0; i < n; i++) + { + object child = adaptor.GetChild(tree, i); + StringTemplate nodeST = GetNodeST(adaptor, child); + treeST.SetAttribute("nodes", nodeST); + ToDOTDefineNodes(child, adaptor, treeST); + } + } + + protected void ToDOTDefineEdges(object tree, ITreeAdaptor adaptor, StringTemplate treeST) + { + if ( tree == null ) + { + return; + } + int n = adaptor.GetChildCount(tree); + if ( n == 0 ) + { + // must have already dumped as child from previous + // invocation; do nothing + return; + } + + string parentName = "n" + GetNodeNumber(tree); + + // for each child, do a parent -> child edge using unique node names + string parentText = adaptor.GetNodeText(tree); + for (int i = 0; i < n; i++) + { + object child = adaptor.GetChild(tree, i); + string childText = adaptor.GetNodeText(child); + string childName = "n" + GetNodeNumber(child); + StringTemplate edgeST = _edgeST.GetInstanceOf(); + edgeST.SetAttribute("parent", parentName); + edgeST.SetAttribute("child", childName); + edgeST.SetAttribute("parentText", parentText); + edgeST.SetAttribute("childText", childText); + treeST.SetAttribute("edges", edgeST); + ToDOTDefineEdges(child, adaptor, treeST); + } + } + + protected StringTemplate GetNodeST(ITreeAdaptor adaptor, object t) + { + string text = adaptor.GetNodeText(t); + StringTemplate nodeST = _nodeST.GetInstanceOf(); + string uniqueName = "n" + GetNodeNumber(t); + nodeST.SetAttribute("name", uniqueName); + if (text != null) + text = text.Replace("\"", "\\\\\""); + nodeST.SetAttribute("text", text); + return nodeST; + } + + protected int GetNodeNumber(object t) + { + object boxedInt = nodeToNumberMap[t]; + if ( boxedInt != null ) + { + return (int)boxedInt; + } + else + { + nodeToNumberMap[t] = nodeNumber; + nodeNumber++; + return nodeNumber-1; + } + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Utility/Antlr3.Utility (VS2003).csproj b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Utility/Antlr3.Utility (VS2003).csproj new file mode 100644 index 0000000..d1f3282 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Utility/Antlr3.Utility (VS2003).csproj @@ -0,0 +1,100 @@ +<VisualStudioProject> + <CSHARP + ProjectType = "Local" + ProductVersion = "7.10.3077" + SchemaVersion = "2.0" + ProjectGuid = "{B8845D7E-8201-4904-87E7-85508F58B2F0}" + > + <Build> + <Settings + ApplicationIcon = "" + AssemblyKeyContainerName = "" + AssemblyName = "Antlr3.Utility" + AssemblyOriginatorKeyFile = "" + DefaultClientScript = "JScript" + DefaultHTMLPageLayout = "Grid" + DefaultTargetSchema = "IE50" + DelaySign = "false" + OutputType = "Library" + PreBuildEvent = "" + PostBuildEvent = "" + RootNamespace = "Antlr3.Utility" + RunPostBuildEvent = "OnBuildSuccess" + StartupObject = "" + > + <Config + Name = "Debug" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "DEBUG;TRACE;STRONG_NAME;DOTNET1" + DocumentationFile = "" + DebugSymbols = "true" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "false" + OutputPath = "bin\Debug\net-1.1\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + <Config + Name = "Release" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "TRACE;STRONG_NAME;DOTNET1" + DocumentationFile = "" + DebugSymbols = "false" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "true" + OutputPath = "bin\Release\net-1.1\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + </Settings> + <References> + <Reference + Name = "System" + AssemblyName = "System" + HintPath = "..\..\..\..\..\..\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.dll" + /> + <Reference + Name = "StringTemplate" + AssemblyName = "StringTemplate" + HintPath = "..\..\Libraries\StringTemplate.NET\net-1.1\StringTemplate.dll" + /> + <Reference + Name = "Antlr3.Runtime (VS2003)" + Project = "{38F943D7-937F-4911-9882-24955F6AD0CC}" + Package = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}" + /> + </References> + </Build> + <Files> + <Include> + <File + RelPath = "AssemblyInfo.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Antlr.Utility.Tree\DOTTreeGenerator.cs" + SubType = "Code" + BuildAction = "Compile" + /> + </Include> + </Files> + </CSHARP> +</VisualStudioProject> + diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Utility/Antlr3.Utility (VS2005).csproj b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Utility/Antlr3.Utility (VS2005).csproj new file mode 100644 index 0000000..9f75322 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Utility/Antlr3.Utility (VS2005).csproj @@ -0,0 +1,70 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProductVersion>8.0.50727</ProductVersion> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{BEB27DCC-ABFB-4951-B618-2B639EC65A46}</ProjectGuid> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>Antlr.Utility</RootNamespace> + <AssemblyName>Antlr3.Utility</AssemblyName> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\net-2.0\</OutputPath> + <DefineConstants>TRACE;DEBUG;STRONG_NAME;DOTNET2</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <OutputType>Library</OutputType> + <RootNamespace>Antlr.Utility</RootNamespace> + <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow> + <EnvironmentVariables> + <EnvironmentVariables /> + </EnvironmentVariables> + <GenerateDocumentation>true</GenerateDocumentation> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\net-2.0\</OutputPath> + <DefineConstants>TRACE;STRONG_NAME;DOTNET2</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <DocumentationFile>bin\Release\net-2.0\Antlr3.Utility.XML</DocumentationFile> + <OutputType>Library</OutputType> + <DebugSymbols>true</DebugSymbols> + <RootNamespace>Antlr.Utility</RootNamespace> + <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow> + </PropertyGroup> + <ItemGroup> + <Compile Include="Antlr.Utility.Tree\DOTTreeGenerator.cs" /> + <Compile Include="AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Antlr3.Runtime\Antlr3.Runtime (VS2005).csproj"> + <Project>{CF15D0D5-BE72-4F98-B70F-229ABA1DF0E8}</Project> + <Name>Antlr3.Runtime (VS2005)</Name> + </ProjectReference> + </ItemGroup> + <ItemGroup> + <Reference Include="StringTemplate, Version=3.0.1.6846, Culture=neutral, PublicKeyToken=beee492b52c41e85"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\..\Libraries\StringTemplate.NET\net-2.0\StringTemplate.dll</HintPath> + </Reference> + </ItemGroup> + <ItemGroup> + <Folder Include="Properties/" /> + </ItemGroup> + <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Utility/AssemblyInfo.cs b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Utility/AssemblyInfo.cs new file mode 100644 index 0000000..eb59831 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Utility/AssemblyInfo.cs @@ -0,0 +1,108 @@ +/* +[The "BSD licence"] +Copyright (c) 2005-2007 Kunle Odutola +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. +4. Unless explicitly state otherwise, any contribution intentionally + submitted for inclusion in this work to the copyright owner or licensor + shall be under the terms and conditions of this license, without any + additional terms or conditions. + +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. +*/ + + +using System.Reflection; +using System.Runtime.CompilerServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. + +// TODO: Review the values of the assembly attributes + +[assembly: AssemblyTitle("ANTLR3 Utilities for .NET")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("Copyright (c) 2005-2007 Kunle Odutola")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Revision +// Build Number +// +// You can specify all the values or you can default the Revision and Build Numbers +// by using the '*' as shown below: + +[assembly: AssemblyVersion("0.1.0.*")] + +// +// In order to sign your assembly you must specify a key to use. Refer to the +// Microsoft .NET Framework documentation for more information on assembly signing. +// +// Use the attributes below to control which key is used for signing. +// +// Notes: +// (*) If no key is specified, the assembly is not signed. +// (*) KeyName refers to a key that has been installed in the Crypto Service +// Provider (CSP) on your machine. KeyFile refers to a file which contains +// a key. +// (*) If the KeyFile and the KeyName values are both specified, the +// following processing occurs: +// (1) If the KeyName can be found in the CSP, that key is used. +// (2) If the KeyName does not exist and the KeyFile does exist, the key +// in the KeyFile is installed into the CSP and used. +// (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility. +// When specifying the KeyFile, the location of the KeyFile should be +// relative to the project output directory which is +// %Project Directory%\obj\<configuration>. For example, if your KeyFile is +// located in the project directory, you would specify the AssemblyKeyFile +// attribute as [assembly: AssemblyKeyFile("..\..\mykey.snk")] +// (*) Delay Signing is an advanced option - see the Microsoft .NET Framework +// documentation for more information on this. +// + +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyKeyName("")] + +#if STRONG_NAME +// This strongly suggests that the build is a VS.NET build. +[assembly: AssemblyDelaySign(false)] +[assembly: AssemblyKeyFile("../../../Antlr3_KeyPair.snk")] +#elif NANT_STRONGNAME +// This strongly suggests that the build is a NANT build. +[assembly: AssemblyDelaySign(false)] +[assembly: AssemblyKeyFile("../Antlr3_KeyPair.snk")] +#else +// This should never happen as the assembly should always be strong named. +[assembly: AssemblyDelaySign(false)] +[assembly: AssemblyKeyFile("")] +#endif + + diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Utility/default.build b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Utility/default.build new file mode 100644 index 0000000..537900a --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3.Utility/default.build @@ -0,0 +1,78 @@ +<?xml version="1.0"?> +<project name="ANTLR C# Runtime Utility Library" default="build"> + <tstamp/> + <property name="base.dir" value="${path::get-full-path( project::get-base-directory() )}" /> + + <include buildfile="../../antlr3.runtime.net.common.inc" /> + + <property name="version" value="0.1" /> + + <property name="name" value="Antlr3.Utility" /> + <property name="assembly.name" value="${name}.dll" /> + <property name="src.dir" value="${base.dir}/" /> + + <property name="debug" value="true" unless="${property::exists('debug')}" /> + + <echo message="Building project: '${name}' version ${version} ==> '${assembly.name}'"/> + + <target name="release" depends="clean" description="build non-debug version"> + <!-- build a clean release distribution for release --> + <property name="debug" value="false"/> + <echo message="Debug = ${debug}"/> + <call target="build"/> + </target> + + <target name="build" depends="init, compile" description="compiles the source code"> + </target> + + <target name="init" depends="clean"> + </target> + + <target name="clean"> + </target> + + <target name="clean.vsnet" description="cleans up VS.NET build artifacts and output"> + <!-- delete VS.NET project artifacts directory --> + <echo message="Deleting VS.NET artifacts directory '${base.dir}/bin'" /> + <delete dir="${base.dir}/bin" failonerror="false" /> + <echo message="Deleting VS.NET artifacts directory '${base.dir}/obj'" /> + <delete dir="${base.dir}/obj" failonerror="false" /> + </target> + + <target name="compile" depends="init"> + <csc + define="${strong_name};${dotnet_define}" + target="library" + debug="${debug}" + optimize="${optimize}" + output="${build.working.dir}/${assembly.name}" + doc="${build.working.dir}/${name}.xml"> + + <nowarn> + <warning number="1591" /> + <warning number="1572" /> + </nowarn> + + <resources prefix="Antlr.Utility" dynamicprefix="true"> + <include name="**/*.resx" /> + </resources> + + <sources basedir="${src.dir}" defaultexcludes="true"> + <include name="**/*.cs" /> + </sources> + + <references> + <include name="${build.working.dir}/Antlr3.Runtime.dll" /> + <include name="${build.working.dir}/StringTemplate.dll" /> + <include name="${build.working.dir}/antlr.runtime.dll" /> + </references> + </csc> + </target> + + <target name="docs"> + </target> + + <target name="dist" depends="docs"> + </target> + +</project> diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3_KeyPair.snk b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3_KeyPair.snk new file mode 100644 index 0000000..52a67cf Binary files /dev/null and b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3_KeyPair.snk differ diff --git a/antlr-3.1.3/runtime/CSharp/Sources/Antlr3_PublicKey.snk b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3_PublicKey.snk new file mode 100644 index 0000000..dee0c49 Binary files /dev/null and b/antlr-3.1.3/runtime/CSharp/Sources/Antlr3_PublicKey.snk differ diff --git a/antlr-3.1.3/runtime/CSharp/all.antlr3.runtime.net.build b/antlr-3.1.3/runtime/CSharp/all.antlr3.runtime.net.build new file mode 100644 index 0000000..67720b8 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/all.antlr3.runtime.net.build @@ -0,0 +1,329 @@ +<?xml version="1.0" encoding='iso-8859-1' ?> +<project name="all" default="usage"> + + <property name="base.dir" value="${path::get-full-path( project::get-base-directory() )}" /> + <property name="sharedlibrary.dir" value="${base.dir}/Libraries" /> + <property name="tools.dir" value="${base.dir}/Tools" /> + <property name="tempdir" value="tempdir" /> + + <property name="version" value="3.1b1" /> + + <include buildfile="antlr3.runtime.net.common.inc" /> + + <!-- =================================================================== --> + <!-- Help on usage --> + <!-- =================================================================== --> + + <target name="usage"> + <echo message="ANTLR v3 C# Runtime - Nant Build file"/> + <echo message="-------------------------------------------------------------"/> + <echo message=""/> + <echo message="available targets are:"/> + <echo message=""/> + <echo message=" build - build debug or release version in '${base.dir}/build/${framework::get-target-framework()}'"/> + <echo message=" (use -Ddebug=true or -Ddebug=false )"/> + <echo message=""/> + <echo message=" release - build release version in '${base.dir}/build/${framework::get-target-framework()}'"/> + <echo message=""/> + <echo message=" dist - build src/bin/docs distro in '${base.dir}'"/> + <echo message=" (uses **pre-built** binaries.)"/> + <echo message=""/> + <echo message=" cleanall - delete all the Nant and VS.NET build artifacts"/> + <echo message=" clean - delete all the Nant build artifacts only"/> + <echo message=""/> + <echo message=" usage - show this message (default)"/> + <echo message=""/> + <echo message="-------------------------------------------------------------"/> + </target> + + <target name="build" + depends="clean, init, build.v3.runtime, build.v3.utility, copytobuilddir" + description="Build debug or release version ( -Ddebug=true|false )" + /> + + <target name="release" depends="clean" description="Builds release (i.e. non-debug) version"> + <!-- build a clean release distribution for release --> + <property name="debug" value="false"/> + <call target="build"/> + </target> + + <target name="cleanall" depends="clean" description="Cleans all the Nant and VS.NET build artifacts"> + <nant buildfile="Sources/Antlr3.Runtime/default.build" target="clean.vsnet" inheritall="true" /> + <nant buildfile="Sources/Antlr3.Utility/default.build" target="clean.vsnet" inheritall="true" /> + </target> + + <target name="dist" + depends="releaseDocs, zipsource, zip11, zip20, zipdoc" + description="Creates src/bin/docs distros (uses pre-built binaries)" + /> + + + <target name="clean" description="Cleans Nant build artifacts"> + <delete dir="${build.working.dir}" failonerror="false" /> + <delete dir="${build.dest.dir}" failonerror="false" /> + <!-- <delete dir="${doc.dir}" failonerror="false" /> --> + </target> + + <target name="init"> + <mkdir dir="${build.working.dir}" /> + <mkdir dir="${build.dest.dir}" /> + <call target="copy_build_dependencies" /> + </target> + + <target name="copy_build_dependencies"> + <copy todir="${build.dest.dir}"> + <fileset basedir="${sharedlibrary.dir}/MbUnit"> + <include name="MbUnit.Framework.dll" /> + <include name="QuickGraph.Algorithms.dll" /> + <include name="QuickGraph.dll" /> + </fileset> + </copy> + <copy todir="${build.dest.dir}"> + <fileset basedir="${sharedlibrary.dir}/StringTemplate.NET/${target.clr}"> + <include name="StringTemplate.dll" /> + <include name="antlr.runtime.dll" /> + </fileset> + </copy> + </target> + + <target name="build.v3.runtime" depends="init"> + <nant buildfile="Sources/Antlr3.Runtime/default.build" target="build" inheritall="true" /> + </target> + + <target name="build.v3.utility" depends="init"> + <nant buildfile="Sources/Antlr3.Utility/default.build" target="build" inheritall="true" /> + </target> + + <target name="copytobuilddir"> + <copy todir="${build.dest.dir}"> + <fileset basedir="${build.working.dir}"> + <include name="Antlr3.Runtime.dll" /> + <include name="Antlr3.Runtime.xml" /> + <include name="Antlr3.Utility.dll" /> + <include name="Antlr3.Utility.xml" /> + <include name="StringTemplate.dll" /> + <include name="antlr.runtime.dll" /> + + <include name="Antlr3.Runtime.pdb" /> + <include name="Antlr3.Utility.pdb" /> + <include name="StringTemplate.pdb" /> + <include name="antlr.runtime.pdb" /> + </fileset> + </copy> + </target> + + <target name="zip11"> + <zip zipfile="${base.dir}/Antlr3.C#.Runtime.v${version}.net-1.1.zip"> + <fileset basedir="${base.dir}/build"> + <include name="net-1.1/Antlr3.Runtime.dll" /> + <include name="net-1.1/Antlr3.Utility.dll" /> + <include name="net-1.1/StringTemplate.dll" /> + <include name="net-1.1/antlr.runtime.dll" /> + </fileset> + <fileset basedir="${base.dir}"> + <include name="README.TXT" /> + <include name="LICENSE.TXT" /> + <include name="NOTICE.TXT" /> + <include name="CHANGES.TXT" /> + </fileset> + <fileset basedir="${base.dir}/docs"> + <include name="Antlr3.Runtime.chm" /> + <include name="Antlr3.Utility.chm" /> + </fileset> + </zip> + </target> + + <target name="zip20"> + <zip zipfile="${base.dir}/Antlr3.C#.Runtime.v${version}.net-2.0.zip"> + <fileset basedir="${base.dir}/build"> + <include name="net-2.0/Antlr3.Runtime.dll" /> + <include name="net-2.0/Antlr3.Utility.dll" /> + <include name="net-2.0/StringTemplate.dll" /> + <include name="net-2.0/antlr.runtime.dll" /> + </fileset> + <fileset basedir="${base.dir}"> + <include name="README.TXT" /> + <include name="LICENSE.TXT" /> + <include name="NOTICE.TXT" /> + <include name="CHANGES.TXT" /> + </fileset> + <fileset basedir="${base.dir}/docs"> + <include name="Antlr3.Runtime.chm" /> + <include name="Antlr3.Utility.chm" /> + </fileset> + </zip> + </target> + + <target name="zipdoc"> + <zip zipfile="${base.dir}/Antlr3.C#.Runtime.v${version}.api-doc.zip"> + <fileset basedir="${base.dir}/docs"> + <include name="Antlr3.Runtime.chm" /> + <include name="Antlr3.Utility.chm" /> + </fileset> + </zip> + </target> + + <target name="zipsource"> + <zip zipfile="${base.dir}/Antlr3.C#.Runtime.v${version}.src.zip"> + <fileset basedir="${base.dir}"> + <include name="**/*.build" /> + <include name="**/*.xml" /> + <include name="**/*.inc" /> + <include name="**/*.sln" /> + <include name="**/*.cs" /> + <include name="**/*.csproj" /> + <include name="**/*.resx" /> + <include name="**/*.ico" /> + <include name="**/*.html" /> + <include name="**/*.jpg" /> + <include name="**/*.pdf" /> + + <include name="${sharedlibrary.dir}/**/*.dll" /> + + <include name="${tools.dir}/**/*.dll" /> + <include name="${tools.dir}/**/*.exe" /> + + <include name="README.TXT" /> + <include name="LICENSE.TXT" /> + <include name="NOTICE.TXT" /> + <include name="CHANGES.TXT" /> + + <exclude name="*.snk" /> + <exclude name="**/obj/Debug/*.*" /> + <exclude name="**/obj/Debug/net-1.1/*.*" /> + <exclude name="**/obj/Debug/net-2.0/*.*" /> + <exclude name="**/bin/Debug/*.*" /> + <exclude name="**/bin/Debug/net-1.1/*.*" /> + <exclude name="**/bin/Debug/net-2.0/*.*" /> + <exclude name="**/bin/*.*" /> + <exclude name="bin/**/*.*" /> + <exclude name="build/**/*.*" /> + <exclude name="**/*.user" /> + <exclude name="**/*.resharperoptions" /> + <exclude name="**/*.obj" /> + <exclude name="**/*.pch" /> + <exclude name="**/*.pdb" /> + <exclude name="**/*.idb" /> + <exclude name="**/*.log*" /> + <exclude name="**/*.suo" /> + <exclude name="**/*.bak" /> + <exclude name="**/*.new" /> + <exclude name="**/*.original" /> + </fileset> + </zip> + </target> + + <!-- Documentation generation. --> + <property name="base.dir" value="." /> + <property name="doc.dir" value="${base.dir}/docs" /> + <property name="tempBin.dir" value="${doc.dir}/bin"/> + <property name="outputDocs.file" value="Antlr3.Runtime"/> + + <!-- Docs generation properties. --> + <property name="ShowMissing" value="false"/> + <property name="ShowPrivate" value="false"/> + <property name="OutputTarget" value="HTMLHelp"/> + +<!-- + description: + Generates documentation with default properties. +--> + <target name="releaseDocs" depends="clean.docs"> + <call target="copyFilesToDocument"/> + <call target="generateReleaseDocs"/> + </target> + +<!-- + description: + Generates documentation for developers, with all missing information indicated and private members visible. +--> + <target name="devDocs" depends="clean.docs"> + <property name="ShowMissing" value="true"/> + <property name="ShowPrivate" value="true"/> + <property name="outputDocs.file" value="Antlr3.Runtime.Dev"/> + <call target="copyFilesToDocument"/> + <call target="generateReleaseDocs"/> + </target> + +<!-- + description: + Generates documentation for release. +--> + <target name="generateReleaseDocs"> + <ndoc failonerror="true"> + <assemblies basedir="${tempBin.dir}"> + <include name="Antlr3.Runtime.dll"/> + <include name="Antlr3.Utility.dll"/> + </assemblies> + <referencepaths> + <include name="Libraries/StringTemplate.NET/${framework::get-target-framework()}"/> + </referencepaths> + <documenters> + <documenter name="MSDN"> + <property name="OutputTarget" value="${OutputTarget}" /> + <property name="Preliminary" value="true" /> + <property name="BinaryTOC" value="true" /> + <property name="CleanIntermediates" value="true" /> + <property name="SdkLinksOnWeb" value="true" /> + + <property name="OutputDirectory" value="${doc.dir}" /> + <property name="HtmlHelpName" value="${outputDocs.file}" /> + <property name="IncludeFavorites" value="True" /> + <property name="SplitTOCs" value="False" /> + + <property name="Title" value="ANTLR v3 C# Runtime Library API documentation" /> + <property name="DefaulTOC" value="Antlr.Runtime" /> + + <property name="ShowVisualBasic" value="True" /> + <property name="ShowMissingSummaries" value="${ShowMissing}" /> + <property name="ShowMissingRemarks" value="${ShowMissing}" /> + <property name="ShowMissingParams" value="${ShowMissing}" /> + <property name="ShowMissingReturns" value="${ShowMissing}" /> + <property name="ShowMissingValues" value="${ShowMissing}" /> + <property name="DocumentEmptyNamespaces" value="${ShowMissing}" /> + + <property name="AutoPropertyBackerSummaries" value="true" /> + <property name="AutoDocumentConstructors" value="true" /> + <property name="DocumentProtected" value="True" /> + <property name="DocumentInternals" value="${ShowPrivate}" /> + <property name="DocumentPrivates" value="${ShowPrivate}" /> + + <property name="IncludeAssemblyVersion" value="True" /> + <property name="CopyrightText" value="Copyright 2005-2007 - Kunle Odutola" /> + <property name="CopyrightHref" value="http://www.antlr.org" /> + </documenter> + </documenters> + </ndoc> + </target> + +<!-- + description: + Copies all files to be documented to a single location to speed up the documentation process. +--> + <target name="copyFilesToDocument"> + <copy todir="${tempBin.dir}" flatten="true"> + <fileset basedir="build/${target.clr}"> + <include name="Antlr3.Runtime.dll"/> + <include name="Antlr3.Runtime.xml"/> + <include name="Antlr3.Utility.dll"/> + <include name="Antlr3.Utility.xml"/> + </fileset> + </copy> + </target> + +<!-- + description: + Cleans up prior to documentation generation. +--> + <target name="clean.docs"> + <delete> + <fileset> +<!-- + <include name="${doc.dir}/**"/> +--> + <exclude name="**/*.chm"/> + </fileset> + </delete> + </target> + +</project> diff --git a/antlr-3.1.3/runtime/CSharp/antlr3.runtime.net.common.inc b/antlr-3.1.3/runtime/CSharp/antlr3.runtime.net.common.inc new file mode 100644 index 0000000..fc718ac --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/antlr3.runtime.net.common.inc @@ -0,0 +1,62 @@ +<?xml version="1.0"?> +<project name="Common Build Properties"> + + <!-- Common properties that allow overrides --> + + <property name="rootbuild.dir" value="${directory::get-current-directory()}" /> + + <property name="build.dest.dir" value="${rootbuild.dir}/build/${framework::get-target-framework()}" /> + <property name="build.working.dir" value="${rootbuild.dir}/bin" /> + + <property name="strong_name" value="NANT_STRONGNAME" unless="${property::exists('strong_name')}" /> + <property name="debug" value="false" unless="${property::exists('debug')}" /> + <property name="optimize" value="true" unless="${property::exists('optimize')}" /> + <property name="doc.dir" value="${base.dir}/docs" /> + <property name="nunit.formatter" value="Plain" unless="${property::exists('nunit.formatter')}" /> + <property name="os" value="${operating-system::to-string(environment::get-operating-system())}" /> + <property name="isWindows" value="${string::starts-with(os, 'Microsoft Windows')}" /> + <property name="isMono" value="${string::starts-with(framework::get-target-framework(),'mono')}" /> + <property name="target.clr" value="${framework::get-target-framework()}" /> + + <if test="${framework::get-target-framework()=='netcf-1.0'}"> + <fail message=".NET-CF is not supported. Please use the .NET Framework or Mono." /> + </if> + + <if test="${framework::get-target-framework()=='net-1.1' or framework::get-target-framework()=='mono-1.0'}"> + <property name="dotnet_define" value="DOTNET1" /> + </if> + <if test="${not (framework::get-target-framework()=='net-1.1') and not (framework::get-target-framework()=='mono-1.0')}"> + <property name="dotnet_define" value="DOTNET2" /> + </if> + + + <!-- find out where nunit.framework.dll is --> + + <property name="lib.dir" + value="${path::combine(nant::get-base-directory(), 'lib')}" + dynamic="true" /> + + <property name="lib.family.dir" + value="${path::combine(lib.dir,framework::get-family(framework::get-target-framework()))}" + dynamic="true" /> + + <!-- for nant 0.85 rc2 or higher --> + + <property name="lib.framework.dir" + value="${path::combine(lib.family.dir, version::to-string(framework::get-version(framework::get-target-framework())))}" + dynamic="true" /> + + <!-- Uncomment this block for nant backward compatibility + <property name="lib.framework.dir" + value="${path::combine(lib.family.dir, framework::get-version(framework::get-target-framework()))}" + dynamic="true" /> + --> + + <property name="nunit.framework.dll" + value="${path::combine(lib.framework.dir, 'nunit.framework.dll')}" /> + + <property name="nant.tasks.nunit2" + value="False" + unless="${property::exists('nant.tasks.nunit2')}" /> + +</project> diff --git a/antlr-3.1.3/runtime/CSharp/dist/DOT-NET-runtime-3.1.3.zip b/antlr-3.1.3/runtime/CSharp/dist/DOT-NET-runtime-3.1.3.zip new file mode 100644 index 0000000..200991c Binary files /dev/null and b/antlr-3.1.3/runtime/CSharp/dist/DOT-NET-runtime-3.1.3.zip differ diff --git a/antlr-3.1.3/runtime/CSharp/doxyfile b/antlr-3.1.3/runtime/CSharp/doxyfile new file mode 100644 index 0000000..680316f --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp/doxyfile @@ -0,0 +1,264 @@ +# Doxyfile 1.5.2 + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- +DOXYFILE_ENCODING = UTF-8 +PROJECT_NAME = "ANTLR API" +PROJECT_NUMBER = 3.1.2 +OUTPUT_DIRECTORY = api +CREATE_SUBDIRS = NO +OUTPUT_LANGUAGE = English +BRIEF_MEMBER_DESC = YES +REPEAT_BRIEF = YES +ABBREVIATE_BRIEF = "The $name class" \ + "The $name widget" \ + "The $name file" \ + is \ + provides \ + specifies \ + contains \ + represents \ + a \ + an \ + the +ALWAYS_DETAILED_SEC = YES +INLINE_INHERITED_MEMB = NO +FULL_PATH_NAMES = YES +STRIP_FROM_PATH = /Applications/ +STRIP_FROM_INC_PATH = +SHORT_NAMES = NO +JAVADOC_AUTOBRIEF = NO +MULTILINE_CPP_IS_BRIEF = NO +DETAILS_AT_TOP = NO +INHERIT_DOCS = YES +SEPARATE_MEMBER_PAGES = NO +TAB_SIZE = 8 +ALIASES = +OPTIMIZE_OUTPUT_FOR_C = NO +OPTIMIZE_OUTPUT_JAVA = YES +BUILTIN_STL_SUPPORT = NO +CPP_CLI_SUPPORT = NO +DISTRIBUTE_GROUP_DOC = NO +SUBGROUPING = YES +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- +EXTRACT_ALL = YES +EXTRACT_PRIVATE = YES +EXTRACT_STATIC = YES +EXTRACT_LOCAL_CLASSES = YES +EXTRACT_LOCAL_METHODS = NO +HIDE_UNDOC_MEMBERS = NO +HIDE_UNDOC_CLASSES = NO +HIDE_FRIEND_COMPOUNDS = NO +HIDE_IN_BODY_DOCS = NO +INTERNAL_DOCS = NO +CASE_SENSE_NAMES = NO +HIDE_SCOPE_NAMES = NO +SHOW_INCLUDE_FILES = YES +INLINE_INFO = YES +SORT_MEMBER_DOCS = YES +SORT_BRIEF_DOCS = NO +SORT_BY_SCOPE_NAME = NO +GENERATE_TODOLIST = YES +GENERATE_TESTLIST = NO +GENERATE_BUGLIST = NO +GENERATE_DEPRECATEDLIST= NO +ENABLED_SECTIONS = +MAX_INITIALIZER_LINES = 30 +SHOW_USED_FILES = YES +SHOW_DIRECTORIES = NO +FILE_VERSION_FILTER = +#--------------------------------------------------------------------------- +# configuration options related to warning and progress messages +#--------------------------------------------------------------------------- +QUIET = NO +WARNINGS = YES +WARN_IF_UNDOCUMENTED = YES +WARN_IF_DOC_ERROR = YES +WARN_NO_PARAMDOC = NO +WARN_FORMAT = "$file:$line: $text" +WARN_LOGFILE = +#--------------------------------------------------------------------------- +# configuration options related to the input files +#--------------------------------------------------------------------------- +INPUT = /Users/parrt/antlr/code/antlr/main/runtime/CSharp/Sources +INPUT_ENCODING = UTF-8 +FILE_PATTERNS = *.c \ + *.cc \ + *.cxx \ + *.cpp \ + *.c++ \ + *.d \ + *.java \ + *.ii \ + *.ixx \ + *.ipp \ + *.i++ \ + *.inl \ + *.h \ + *.hh \ + *.hxx \ + *.hpp \ + *.h++ \ + *.idl \ + *.odl \ + *.cs \ + *.php \ + *.php3 \ + *.inc \ + *.m \ + *.mm \ + *.dox \ + *.py +RECURSIVE = YES +EXCLUDE = +EXCLUDE_SYMLINKS = NO +EXCLUDE_PATTERNS = +EXCLUDE_SYMBOLS = java::util \ + java::io +EXAMPLE_PATH = +EXAMPLE_PATTERNS = * +EXAMPLE_RECURSIVE = NO +IMAGE_PATH = +INPUT_FILTER = +FILTER_PATTERNS = +FILTER_SOURCE_FILES = NO +#--------------------------------------------------------------------------- +# configuration options related to source browsing +#--------------------------------------------------------------------------- +SOURCE_BROWSER = YES +INLINE_SOURCES = NO +STRIP_CODE_COMMENTS = YES +REFERENCED_BY_RELATION = NO +REFERENCES_RELATION = NO +REFERENCES_LINK_SOURCE = YES +USE_HTAGS = NO +VERBATIM_HEADERS = YES +#--------------------------------------------------------------------------- +# configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- +ALPHABETICAL_INDEX = NO +COLS_IN_ALPHA_INDEX = 5 +IGNORE_PREFIX = +#--------------------------------------------------------------------------- +# configuration options related to the HTML output +#--------------------------------------------------------------------------- +GENERATE_HTML = YES +HTML_OUTPUT = . +HTML_FILE_EXTENSION = .html +HTML_HEADER = +HTML_FOOTER = +HTML_STYLESHEET = +HTML_ALIGN_MEMBERS = YES +GENERATE_HTMLHELP = NO +CHM_FILE = +HHC_LOCATION = +GENERATE_CHI = NO +BINARY_TOC = NO +TOC_EXPAND = NO +DISABLE_INDEX = NO +ENUM_VALUES_PER_LINE = 4 +GENERATE_TREEVIEW = NO +TREEVIEW_WIDTH = 250 +#--------------------------------------------------------------------------- +# configuration options related to the LaTeX output +#--------------------------------------------------------------------------- +GENERATE_LATEX = NO +LATEX_OUTPUT = latex +LATEX_CMD_NAME = latex +MAKEINDEX_CMD_NAME = makeindex +COMPACT_LATEX = NO +PAPER_TYPE = a4wide +EXTRA_PACKAGES = +LATEX_HEADER = +PDF_HYPERLINKS = NO +USE_PDFLATEX = YES +LATEX_BATCHMODE = NO +LATEX_HIDE_INDICES = NO +#--------------------------------------------------------------------------- +# configuration options related to the RTF output +#--------------------------------------------------------------------------- +GENERATE_RTF = NO +RTF_OUTPUT = rtf +COMPACT_RTF = NO +RTF_HYPERLINKS = NO +RTF_STYLESHEET_FILE = +RTF_EXTENSIONS_FILE = +#--------------------------------------------------------------------------- +# configuration options related to the man page output +#--------------------------------------------------------------------------- +GENERATE_MAN = NO +MAN_OUTPUT = man +MAN_EXTENSION = .3 +MAN_LINKS = NO +#--------------------------------------------------------------------------- +# configuration options related to the XML output +#--------------------------------------------------------------------------- +GENERATE_XML = NO +XML_OUTPUT = xml +XML_SCHEMA = +XML_DTD = +XML_PROGRAMLISTING = YES +#--------------------------------------------------------------------------- +# configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- +GENERATE_AUTOGEN_DEF = NO +#--------------------------------------------------------------------------- +# configuration options related to the Perl module output +#--------------------------------------------------------------------------- +GENERATE_PERLMOD = NO +PERLMOD_LATEX = NO +PERLMOD_PRETTY = YES +PERLMOD_MAKEVAR_PREFIX = +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- +ENABLE_PREPROCESSING = YES +MACRO_EXPANSION = NO +EXPAND_ONLY_PREDEF = NO +SEARCH_INCLUDES = YES +INCLUDE_PATH = +INCLUDE_FILE_PATTERNS = +PREDEFINED = +EXPAND_AS_DEFINED = +SKIP_FUNCTION_MACROS = YES +#--------------------------------------------------------------------------- +# Configuration::additions related to external references +#--------------------------------------------------------------------------- +TAGFILES = +GENERATE_TAGFILE = +ALLEXTERNALS = NO +EXTERNAL_GROUPS = YES +PERL_PATH = /usr/bin/perl +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- +CLASS_DIAGRAMS = NO +MSCGEN_PATH = /Applications/Doxygen.app/Contents/Resources/ +HIDE_UNDOC_RELATIONS = YES +HAVE_DOT = YES +CLASS_GRAPH = YES +COLLABORATION_GRAPH = YES +GROUP_GRAPHS = YES +UML_LOOK = NO +TEMPLATE_RELATIONS = NO +INCLUDE_GRAPH = YES +INCLUDED_BY_GRAPH = YES +CALL_GRAPH = NO +CALLER_GRAPH = NO +GRAPHICAL_HIERARCHY = YES +DIRECTORY_GRAPH = YES +DOT_IMAGE_FORMAT = png +DOT_PATH = /Applications/Doxygen.app/Contents/Resources/ +DOTFILE_DIRS = +DOT_GRAPH_MAX_NODES = 50 +DOT_TRANSPARENT = NO +DOT_MULTI_TARGETS = NO +GENERATE_LEGEND = YES +DOT_CLEANUP = YES +#--------------------------------------------------------------------------- +# Configuration::additions related to the search engine +#--------------------------------------------------------------------------- +SEARCHENGINE = NO diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Antlr3.Runtime.Debug.csproj b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Antlr3.Runtime.Debug.csproj new file mode 100644 index 0000000..8bfee73 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Antlr3.Runtime.Debug.csproj @@ -0,0 +1,97 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProductVersion>9.0.30729</ProductVersion> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{5EE27A90-B023-42C9-AAF1-52B0424C5D0B}</ProjectGuid> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>Antlr.Runtime.Debug</RootNamespace> + <AssemblyName>Antlr3.Runtime.Debug</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + <SccProjectName>SAK</SccProjectName> + <SccLocalPath>SAK</SccLocalPath> + <SccAuxPath>SAK</SccAuxPath> + <SccProvider>SAK</SccProvider> + <SignAssembly>true</SignAssembly> + <AssemblyOriginatorKeyFile>Key.snk</AssemblyOriginatorKeyFile> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + <Reference Include="System.Core"> + <RequiredTargetFramework>3.5</RequiredTargetFramework> + </Reference> + </ItemGroup> + <ItemGroup> + <Compile Include="BlankDebugEventListener.cs" /> + <Compile Include="DebugEventHub.cs" /> + <Compile Include="DebugEventRepeater.cs" /> + <Compile Include="DebugEventSocketProxy.cs" /> + <Compile Include="DebugParser.cs" /> + <Compile Include="DebugTokenStream.cs" /> + <Compile Include="DebugTreeAdaptor.cs" /> + <Compile Include="DebugTreeNodeStream.cs" /> + <Compile Include="DebugTreeParser.cs" /> + <Compile Include="JavaExtensions\DictionaryExtensions.cs" /> + <Compile Include="JavaExtensions\EnumeratorExtensions.cs" /> + <Compile Include="JavaExtensions\ExceptionExtensions.cs" /> + <Compile Include="JavaExtensions\IOExtensions.cs" /> + <Compile Include="JavaExtensions\Iterator.cs" /> + <Compile Include="JavaExtensions\JSystem.cs" /> + <Compile Include="JavaExtensions\ListExtensions.cs" /> + <Compile Include="JavaExtensions\ObjectExtensions.cs" /> + <Compile Include="JavaExtensions\SetExtensions.cs" /> + <Compile Include="JavaExtensions\StackExtensions.cs" /> + <Compile Include="JavaExtensions\StringBuilderExtensions.cs" /> + <Compile Include="JavaExtensions\StringExtensions.cs" /> + <Compile Include="JavaExtensions\StringTokenizer.cs" /> + <Compile Include="JavaExtensions\SubList.cs" /> + <Compile Include="JavaExtensions\TreeExtensions.cs" /> + <Compile Include="JavaExtensions\TypeExtensions.cs" /> + <Compile Include="Misc\Stats.cs" /> + <None Include="ParserDebugger.cs" /> + <Compile Include="ParseTreeBuilder.cs" /> + <Compile Include="Profiler.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="RemoteDebugEventSocketListener.cs" /> + <Compile Include="TraceDebugEventListener.cs" /> + <Compile Include="Tracer.cs" /> + </ItemGroup> + <ItemGroup> + <None Include="Key.snk" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Antlr3.Runtime\Antlr3.Runtime.csproj"> + <Project>{8FDC0A87-9005-4D5A-AB75-E55CEB575559}</Project> + <Name>Antlr3.Runtime</Name> + </ProjectReference> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Antlr3.Runtime.Debug.csproj.vspscc b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Antlr3.Runtime.Debug.csproj.vspscc new file mode 100644 index 0000000..b6d3289 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Antlr3.Runtime.Debug.csproj.vspscc @@ -0,0 +1,10 @@ +"" +{ +"FILE_VERSION" = "9237" +"ENLISTMENT_CHOICE" = "NEVER" +"PROJECT_FILE_RELATIVE_PATH" = "" +"NUMBER_OF_EXCLUDED_FILES" = "0" +"ORIGINAL_PROJECT_FILE_PATH" = "" +"NUMBER_OF_NESTED_PROJECTS" = "0" +"SOURCE_CONTROL_SETTINGS_PROVIDER" = "PROVIDER" +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/BlankDebugEventListener.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/BlankDebugEventListener.cs new file mode 100644 index 0000000..b018386 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/BlankDebugEventListener.cs @@ -0,0 +1,166 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + + /** <summary> + * A blank listener that does nothing; useful for real classes so + * they don't have to have lots of blank methods and are less + * sensitive to updates to debug interface. + * </summary> + */ + public class BlankDebugEventListener : IDebugEventListener + { + public int RuleLevel + { + get; + protected set; + } + + public virtual void Initialize() + { + } + + public virtual void EnterRule( string grammarFileName, string ruleName ) + { + if ( RuleLevel == 0 ) + Commence(); + RuleLevel++; + } + public virtual void ExitRule( string grammarFileName, string ruleName ) + { + RuleLevel--; + if ( RuleLevel == 0 ) + Terminate(); + } + public virtual void EnterAlt( int alt ) + { + } + public virtual void EnterSubRule( int decisionNumber ) + { + } + public virtual void ExitSubRule( int decisionNumber ) + { + } + public virtual void EnterDecision( int decisionNumber ) + { + } + public virtual void ExitDecision( int decisionNumber ) + { + } + public virtual void Location( int line, int pos ) + { + } + public virtual void ConsumeToken( IToken token ) + { + } + public virtual void ConsumeHiddenToken( IToken token ) + { + } + public virtual void LT( int i, IToken t ) + { + } + public virtual void Mark( int i ) + { + } + public virtual void Rewind( int i ) + { + } + public virtual void Rewind() + { + } + public virtual void BeginBacktrack( int level ) + { + } + public virtual void EndBacktrack( int level, bool successful ) + { + } + public virtual void RecognitionException( RecognitionException e ) + { + } + public virtual void BeginResync() + { + } + public virtual void EndResync() + { + } + public virtual void SemanticPredicate( bool result, string predicate ) + { + } + public virtual void Commence() + { + } + public virtual void Terminate() + { + } + + #region Tree parsing stuff + + public virtual void ConsumeNode( object t ) + { + } + public virtual void LT( int i, object t ) + { + } + + #endregion + + + #region AST Stuff + + public virtual void NilNode( object t ) + { + } + public virtual void ErrorNode( object t ) + { + } + public virtual void CreateNode( object t ) + { + } + public virtual void CreateNode( object node, IToken token ) + { + } + public virtual void BecomeRoot( object newRoot, object oldRoot ) + { + } + public virtual void AddChild( object root, object child ) + { + } + public virtual void SetTokenBoundaries( object t, int tokenStartIndex, int tokenStopIndex ) + { + } + + #endregion + } +} + diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugEventHub.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugEventHub.cs new file mode 100644 index 0000000..f8649c3 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugEventHub.cs @@ -0,0 +1,366 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + using System.Collections.Generic; + + /** <summary> + * Broadcast debug events to multiple listeners. Lets you debug and still + * use the event mechanism to build parse trees etc... Not thread-safe. + * Don't add events in one thread while parser fires events in another. + * </summary> + * + * <seealso cref="DebugEventRepeater"/> + */ + public class DebugEventHub : IDebugEventListener + { + protected IList<IDebugEventListener> _listeners = new List<IDebugEventListener>(); + + public DebugEventHub( params IDebugEventListener[] listeners ) + { + _listeners = new List<IDebugEventListener>( listeners ); + } + + public virtual void Initialize() + { + } + + /** <summary> + * Add another listener to broadcast events too. Not thread-safe. + * Don't add events in one thread while parser fires events in another. + * </summary> + */ + public virtual void AddListener( IDebugEventListener listener ) + { + _listeners.Add( listener ); + } + + /* To avoid a mess like this: + public void enterRule(final String ruleName) { + broadcast(new Code(){ + public void exec(DebugEventListener listener) {listener.enterRule(ruleName);}} + ); + } + I am dup'ing the for-loop in each. Where are Java closures!? blech! + */ + + public virtual void EnterRule( string grammarFileName, string ruleName ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.EnterRule( grammarFileName, ruleName ); + } + } + + public virtual void ExitRule( string grammarFileName, string ruleName ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.ExitRule( grammarFileName, ruleName ); + } + } + + public virtual void EnterAlt( int alt ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.EnterAlt( alt ); + } + } + + public virtual void EnterSubRule( int decisionNumber ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.EnterSubRule( decisionNumber ); + } + } + + public virtual void ExitSubRule( int decisionNumber ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.ExitSubRule( decisionNumber ); + } + } + + public virtual void EnterDecision( int decisionNumber ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.EnterDecision( decisionNumber ); + } + } + + public virtual void ExitDecision( int decisionNumber ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.ExitDecision( decisionNumber ); + } + } + + public virtual void Location( int line, int pos ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.Location( line, pos ); + } + } + + public virtual void ConsumeToken( IToken token ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.ConsumeToken( token ); + } + } + + public virtual void ConsumeHiddenToken( IToken token ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.ConsumeHiddenToken( token ); + } + } + + public virtual void LT( int index, IToken t ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.LT( index, t ); + } + } + + public virtual void Mark( int index ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.Mark( index ); + } + } + + public virtual void Rewind( int index ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.Rewind( index ); + } + } + + public virtual void Rewind() + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.Rewind(); + } + } + + public virtual void BeginBacktrack( int level ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.BeginBacktrack( level ); + } + } + + public virtual void EndBacktrack( int level, bool successful ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.EndBacktrack( level, successful ); + } + } + + public virtual void RecognitionException( RecognitionException e ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.RecognitionException( e ); + } + } + + public virtual void BeginResync() + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.BeginResync(); + } + } + + public virtual void EndResync() + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.EndResync(); + } + } + + public virtual void SemanticPredicate( bool result, string predicate ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.SemanticPredicate( result, predicate ); + } + } + + public virtual void Commence() + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.Commence(); + } + } + + public virtual void Terminate() + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.Terminate(); + } + } + + + #region Tree parsing stuff + + public virtual void ConsumeNode( object t ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.ConsumeNode( t ); + } + } + + public virtual void LT( int index, object t ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.LT( index, t ); + } + } + + #endregion + + + #region AST Stuff + + public virtual void NilNode( object t ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.NilNode( t ); + } + } + + public virtual void ErrorNode( object t ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.ErrorNode( t ); + } + } + + public virtual void CreateNode( object t ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.CreateNode( t ); + } + } + + public virtual void CreateNode( object node, IToken token ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.CreateNode( node, token ); + } + } + + public virtual void BecomeRoot( object newRoot, object oldRoot ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.BecomeRoot( newRoot, oldRoot ); + } + } + + public virtual void AddChild( object root, object child ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.AddChild( root, child ); + } + } + + public virtual void SetTokenBoundaries( object t, int tokenStartIndex, int tokenStopIndex ) + { + for ( int i = 0; i < _listeners.Count; i++ ) + { + IDebugEventListener listener = _listeners[i]; + listener.SetTokenBoundaries( t, tokenStartIndex, tokenStopIndex ); + } + } + + #endregion + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugEventRepeater.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugEventRepeater.cs new file mode 100644 index 0000000..2b9430e --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugEventRepeater.cs @@ -0,0 +1,196 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + + /** <summary> + * A simple event repeater (proxy) that delegates all functionality to the + * listener sent into the ctor. Useful if you want to listen in on a few + * debug events w/o interrupting the debugger. Just subclass the repeater + * and override the methods you want to listen in on. Remember to call + * the method in this class so the event will continue on to the original + * recipient. + * </summary> + * + * <seealso cref="DebugEventHub"/> + */ + public class DebugEventRepeater : IDebugEventListener + { + protected IDebugEventListener _listener; + + public DebugEventRepeater( IDebugEventListener listener ) + { + _listener = listener; + } + + public virtual void Initialize() + { + } + + public virtual void EnterRule( string grammarFileName, string ruleName ) + { + _listener.EnterRule( grammarFileName, ruleName ); + } + public virtual void ExitRule( string grammarFileName, string ruleName ) + { + _listener.ExitRule( grammarFileName, ruleName ); + } + public virtual void EnterAlt( int alt ) + { + _listener.EnterAlt( alt ); + } + public virtual void EnterSubRule( int decisionNumber ) + { + _listener.EnterSubRule( decisionNumber ); + } + public virtual void ExitSubRule( int decisionNumber ) + { + _listener.ExitSubRule( decisionNumber ); + } + public virtual void EnterDecision( int decisionNumber ) + { + _listener.EnterDecision( decisionNumber ); + } + public virtual void ExitDecision( int decisionNumber ) + { + _listener.ExitDecision( decisionNumber ); + } + public virtual void Location( int line, int pos ) + { + _listener.Location( line, pos ); + } + public virtual void ConsumeToken( IToken token ) + { + _listener.ConsumeToken( token ); + } + public virtual void ConsumeHiddenToken( IToken token ) + { + _listener.ConsumeHiddenToken( token ); + } + public virtual void LT( int i, IToken t ) + { + _listener.LT( i, t ); + } + public virtual void Mark( int i ) + { + _listener.Mark( i ); + } + public virtual void Rewind( int i ) + { + _listener.Rewind( i ); + } + public virtual void Rewind() + { + _listener.Rewind(); + } + public virtual void BeginBacktrack( int level ) + { + _listener.BeginBacktrack( level ); + } + public virtual void EndBacktrack( int level, bool successful ) + { + _listener.EndBacktrack( level, successful ); + } + public virtual void RecognitionException( RecognitionException e ) + { + _listener.RecognitionException( e ); + } + public virtual void BeginResync() + { + _listener.BeginResync(); + } + public virtual void EndResync() + { + _listener.EndResync(); + } + public virtual void SemanticPredicate( bool result, string predicate ) + { + _listener.SemanticPredicate( result, predicate ); + } + public virtual void Commence() + { + _listener.Commence(); + } + public virtual void Terminate() + { + _listener.Terminate(); + } + + #region Tree parsing stuff + + public virtual void ConsumeNode( object t ) + { + _listener.ConsumeNode( t ); + } + public virtual void LT( int i, object t ) + { + _listener.LT( i, t ); + } + + #endregion + + + #region AST Stuff + + public virtual void NilNode( object t ) + { + _listener.NilNode( t ); + } + public virtual void ErrorNode( object t ) + { + _listener.ErrorNode( t ); + } + public virtual void CreateNode( object t ) + { + _listener.CreateNode( t ); + } + public virtual void CreateNode( object node, IToken token ) + { + _listener.CreateNode( node, token ); + } + public virtual void BecomeRoot( object newRoot, object oldRoot ) + { + _listener.BecomeRoot( newRoot, oldRoot ); + } + public virtual void AddChild( object root, object child ) + { + _listener.AddChild( root, child ); + } + public virtual void SetTokenBoundaries( object t, int tokenStartIndex, int tokenStopIndex ) + { + _listener.SetTokenBoundaries( t, tokenStartIndex, tokenStopIndex ); + } + + #endregion + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugEventSocketProxy.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugEventSocketProxy.cs new file mode 100644 index 0000000..5ac1199 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugEventSocketProxy.cs @@ -0,0 +1,445 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + using System; + using Antlr.Runtime.JavaExtensions; + + using IOException = System.IO.IOException; + using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; + using Socket = System.Net.Sockets.Socket; + using StringBuilder = System.Text.StringBuilder; + using TcpListener = System.Net.Sockets.TcpListener; + + /** <summary> + * A proxy debug event listener that forwards events over a socket to + * a debugger (or any other listener) using a simple text-based protocol; + * one event per line. ANTLRWorks listens on server socket with a + * RemoteDebugEventSocketListener instance. These two objects must therefore + * be kept in sync. New events must be handled on both sides of socket. + * </summary> + */ + public class DebugEventSocketProxy : BlankDebugEventListener + { + public const int DEFAULT_DEBUGGER_PORT = 49100; + protected int port = DEFAULT_DEBUGGER_PORT; + protected TcpListener serverSocket; + protected Socket socket; + protected string grammarFileName; + //protected PrintWriter @out; + //protected BufferedReader @in; + + /** <summary>Who am i debugging?</summary> */ + protected BaseRecognizer recognizer; + + /** <summary> + * Almost certainly the recognizer will have adaptor set, but + * we don't know how to cast it (Parser or TreeParser) to get + * the adaptor field. Must be set with a constructor. :( + * </summary> + */ + protected ITreeAdaptor adaptor; + + public DebugEventSocketProxy( BaseRecognizer recognizer, ITreeAdaptor adaptor ) : + this( recognizer, DEFAULT_DEBUGGER_PORT, adaptor ) + { + } + + public DebugEventSocketProxy( BaseRecognizer recognizer, int port, ITreeAdaptor adaptor ) + { + this.grammarFileName = recognizer.GrammarFileName; + this.adaptor = adaptor; + this.port = port; + } + + #region Properties + public virtual ITreeAdaptor TreeAdaptor + { + get + { + return adaptor; + } + set + { + adaptor = value; + } + } + #endregion + + public virtual void Handshake() + { + if ( serverSocket == null ) + { + System.Net.IPHostEntry hostInfo = System.Net.Dns.GetHostEntry( "localhost" ); + System.Net.IPAddress ipAddress = hostInfo.AddressList[0]; + serverSocket = new TcpListener( ipAddress, port ); + socket = serverSocket.AcceptSocket(); + socket.NoDelay = true; + + System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); + socket.Send( encoding.GetBytes( "ANTLR " + DebugEventListenerConstants.ProtocolVersion + "\n" ) ); + socket.Send( encoding.GetBytes( "grammar \"" + grammarFileName + "\n" ) ); + Ack(); + + //serverSocket = new ServerSocket( port ); + //socket = serverSocket.accept(); + //socket.setTcpNoDelay( true ); + //OutputStream os = socket.getOutputStream(); + //OutputStreamWriter osw = new OutputStreamWriter( os, "UTF8" ); + //@out = new PrintWriter( new BufferedWriter( osw ) ); + //InputStream @is = socket.getInputStream(); + //InputStreamReader isr = new InputStreamReader( @is, "UTF8" ); + //@in = new BufferedReader( isr ); + //@out.println( "ANTLR " + DebugEventListenerConstants.PROTOCOL_VERSION ); + //@out.println( "grammar \"" + grammarFileName ); + //@out.flush(); + //ack(); + } + } + + public override void Commence() + { + // don't bother sending event; listener will trigger upon connection + } + + public override void Terminate() + { + Transmit( "terminate" ); + //@out.close(); + try + { + socket.Close(); + } + catch ( IOException ioe ) + { + ioe.PrintStackTrace( Console.Error ); + } + } + + protected virtual void Ack() + { + try + { + throw new NotImplementedException(); + //@in.readLine(); + } + catch ( IOException ioe ) + { + ioe.PrintStackTrace( Console.Error ); + } + } + + protected virtual void Transmit( string @event ) + { + socket.Send( new System.Text.UTF8Encoding().GetBytes( @event + "\n" ) ); + //@out.println( @event ); + //@out.flush(); + Ack(); + } + + public override void EnterRule( string grammarFileName, string ruleName ) + { + Transmit( "enterRule\t" + grammarFileName + "\t" + ruleName ); + } + + public override void EnterAlt( int alt ) + { + Transmit( "enterAlt\t" + alt ); + } + + public override void ExitRule( string grammarFileName, string ruleName ) + { + Transmit( "exitRule\t" + grammarFileName + "\t" + ruleName ); + } + + public override void EnterSubRule( int decisionNumber ) + { + Transmit( "enterSubRule\t" + decisionNumber ); + } + + public override void ExitSubRule( int decisionNumber ) + { + Transmit( "exitSubRule\t" + decisionNumber ); + } + + public override void EnterDecision( int decisionNumber ) + { + Transmit( "enterDecision\t" + decisionNumber ); + } + + public override void ExitDecision( int decisionNumber ) + { + Transmit( "exitDecision\t" + decisionNumber ); + } + + public override void ConsumeToken( IToken t ) + { + string buf = SerializeToken( t ); + Transmit( "consumeToken\t" + buf ); + } + + public override void ConsumeHiddenToken( IToken t ) + { + string buf = SerializeToken( t ); + Transmit( "consumeHiddenToken\t" + buf ); + } + + public override void LT( int i, IToken t ) + { + if ( t != null ) + Transmit( "LT\t" + i + "\t" + SerializeToken( t ) ); + } + + public override void Mark( int i ) + { + Transmit( "mark\t" + i ); + } + + public override void Rewind( int i ) + { + Transmit( "rewind\t" + i ); + } + + public override void Rewind() + { + Transmit( "rewind" ); + } + + public override void BeginBacktrack( int level ) + { + Transmit( "beginBacktrack\t" + level ); + } + + public override void EndBacktrack( int level, bool successful ) + { + Transmit( "endBacktrack\t" + level + "\t" + ( successful ? DebugEventListenerConstants.True : DebugEventListenerConstants.False ) ); + } + + public override void Location( int line, int pos ) + { + Transmit( "location\t" + line + "\t" + pos ); + } + + public override void RecognitionException( RecognitionException e ) + { + StringBuilder buf = new StringBuilder( 50 ); + buf.Append( "exception\t" ); + buf.Append( e.GetType().Name ); + // dump only the data common to all exceptions for now + buf.Append( "\t" ); + buf.Append( e.index ); + buf.Append( "\t" ); + buf.Append( e.line ); + buf.Append( "\t" ); + buf.Append( e.charPositionInLine ); + Transmit( buf.ToString() ); + } + + public override void BeginResync() + { + Transmit( "beginResync" ); + } + + public override void EndResync() + { + Transmit( "endResync" ); + } + + public override void SemanticPredicate( bool result, string predicate ) + { + StringBuilder buf = new StringBuilder( 50 ); + buf.Append( "semanticPredicate\t" ); + buf.Append( result ); + SerializeText( buf, predicate ); + Transmit( buf.ToString() ); + } + + #region AST Parsing Events + + public override void ConsumeNode( object t ) + { + StringBuilder buf = new StringBuilder( 50 ); + buf.Append( "consumeNode" ); + SerializeNode( buf, t ); + Transmit( buf.ToString() ); + } + + public override void LT( int i, object t ) + { + int ID = adaptor.GetUniqueID( t ); + string text = adaptor.GetText( t ); + int type = adaptor.GetType( t ); + StringBuilder buf = new StringBuilder( 50 ); + buf.Append( "LN\t" ); // lookahead node; distinguish from LT in protocol + buf.Append( i ); + SerializeNode( buf, t ); + Transmit( buf.ToString() ); + } + + protected virtual void SerializeNode( StringBuilder buf, object t ) + { + int ID = adaptor.GetUniqueID( t ); + string text = adaptor.GetText( t ); + int type = adaptor.GetType( t ); + buf.Append( "\t" ); + buf.Append( ID ); + buf.Append( "\t" ); + buf.Append( type ); + IToken token = adaptor.GetToken( t ); + int line = -1; + int pos = -1; + if ( token != null ) + { + line = token.Line; + pos = token.CharPositionInLine; + } + buf.Append( "\t" ); + buf.Append( line ); + buf.Append( "\t" ); + buf.Append( pos ); + int tokenIndex = adaptor.GetTokenStartIndex( t ); + buf.Append( "\t" ); + buf.Append( tokenIndex ); + SerializeText( buf, text ); + } + + #endregion + + + #region AST Events + + public override void NilNode( object t ) + { + int ID = adaptor.GetUniqueID( t ); + Transmit( "nilNode\t" + ID ); + } + + public override void ErrorNode( object t ) + { + int ID = adaptor.GetUniqueID( t ); + string text = t.ToString(); + StringBuilder buf = new StringBuilder( 50 ); + buf.Append( "errorNode\t" ); + buf.Append( ID ); + buf.Append( "\t" ); + buf.Append( TokenConstants.INVALID_TOKEN_TYPE ); + SerializeText( buf, text ); + Transmit( buf.ToString() ); + } + + public override void CreateNode( object t ) + { + int ID = adaptor.GetUniqueID( t ); + string text = adaptor.GetText( t ); + int type = adaptor.GetType( t ); + StringBuilder buf = new StringBuilder( 50 ); + buf.Append( "createNodeFromTokenElements\t" ); + buf.Append( ID ); + buf.Append( "\t" ); + buf.Append( type ); + SerializeText( buf, text ); + Transmit( buf.ToString() ); + } + + public override void CreateNode( object node, IToken token ) + { + int ID = adaptor.GetUniqueID( node ); + int tokenIndex = token.TokenIndex; + Transmit( "createNode\t" + ID + "\t" + tokenIndex ); + } + + public override void BecomeRoot( object newRoot, object oldRoot ) + { + int newRootID = adaptor.GetUniqueID( newRoot ); + int oldRootID = adaptor.GetUniqueID( oldRoot ); + Transmit( "becomeRoot\t" + newRootID + "\t" + oldRootID ); + } + + public override void AddChild( object root, object child ) + { + int rootID = adaptor.GetUniqueID( root ); + int childID = adaptor.GetUniqueID( child ); + Transmit( "addChild\t" + rootID + "\t" + childID ); + } + + public override void SetTokenBoundaries( object t, int tokenStartIndex, int tokenStopIndex ) + { + int ID = adaptor.GetUniqueID( t ); + Transmit( "setTokenBoundaries\t" + ID + "\t" + tokenStartIndex + "\t" + tokenStopIndex ); + } + + #endregion + + + #region Support + + protected virtual string SerializeToken( IToken t ) + { + StringBuilder buf = new StringBuilder( 50 ); + buf.Append( t.TokenIndex ); + buf.Append( '\t' ); + buf.Append( t.Type ); + buf.Append( '\t' ); + buf.Append( t.Channel ); + buf.Append( '\t' ); + buf.Append( t.Line ); + buf.Append( '\t' ); + buf.Append( t.CharPositionInLine ); + SerializeText( buf, t.Text ); + return buf.ToString(); + } + + protected virtual void SerializeText( StringBuilder buf, string text ) + { + buf.Append( "\t\"" ); + if ( text == null ) + { + text = ""; + } + // escape \n and \r all text for token appears to exist on one line + // this escape is slow but easy to understand + text = EscapeNewlines( text ); + buf.Append( text ); + } + + protected virtual string EscapeNewlines( string txt ) + { + txt = txt.replaceAll( "%", "%25" ); // escape all escape char ;) + txt = txt.replaceAll( "\n", "%0A" ); // escape \n + txt = txt.replaceAll( "\r", "%0D" ); // escape \r + return txt; + } + + #endregion + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugParser.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugParser.cs new file mode 100644 index 0000000..3e80f57 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugParser.cs @@ -0,0 +1,127 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + using Antlr.Runtime.JavaExtensions; + + using Console = System.Console; + using IOException = System.IO.IOException; + + public class DebugParser : Parser + { + /** <summary>Who to notify when events in the parser occur.</summary> */ + public IDebugEventListener dbg = null; + + /** <summary> + * Used to differentiate between fixed lookahead and cyclic DFA decisions + * while profiling. + * </summary> + */ + public bool isCyclicDecision = false; + + /** <summary> + * Create a normal parser except wrap the token stream in a debug + * proxy that fires consume events. + * </summary> + */ + public DebugParser( ITokenStream input, IDebugEventListener dbg, RecognizerSharedState state ) + : base( input is DebugTokenStream ? input : new DebugTokenStream( input, dbg ), state ) + { + DebugListener = dbg; + } + + public DebugParser( ITokenStream input, RecognizerSharedState state ) + : base( input is DebugTokenStream ? input : new DebugTokenStream( input, null ), state ) + { + } + + public DebugParser( ITokenStream input, IDebugEventListener dbg ) + : this( input is DebugTokenStream ? input : new DebugTokenStream( input, dbg ), dbg, null ) + { + } + + /** <summary> + * Provide a new debug event listener for this parser. Notify the + * input stream too that it should send events to this listener. + * </summary> + */ + public virtual IDebugEventListener DebugListener + { + get + { + return dbg; + } + set + { + DebugTokenStream debugTokenStream = input as DebugTokenStream; + if ( debugTokenStream != null ) + debugTokenStream.DebugListener = value; + + dbg = value; + } + } + + public virtual void ReportError( IOException e ) + { + Console.Error.WriteLine( e ); + e.PrintStackTrace( Console.Error ); + } + + public override void BeginResync() + { + dbg.BeginResync(); + base.BeginResync(); + } + + public override void EndResync() + { + dbg.EndResync(); + base.EndResync(); + } + + public virtual void BeginBacktrack( int level ) + { + dbg.BeginBacktrack( level ); + } + + public virtual void EndBacktrack( int level, bool successful ) + { + dbg.EndBacktrack( level, successful ); + } + + public override void ReportError( RecognitionException e ) + { + dbg.RecognitionException( e ); + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugTokenStream.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugTokenStream.cs new file mode 100644 index 0000000..aaaacf1 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugTokenStream.cs @@ -0,0 +1,204 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + + public class DebugTokenStream : ITokenStream + { + protected IDebugEventListener dbg; + public ITokenStream input; + protected bool initialStreamState = true; + + /** <summary>Track the last mark() call result value for use in rewind().</summary> */ + protected int lastMarker; + + public DebugTokenStream( ITokenStream input, IDebugEventListener dbg ) + { + this.input = input; + DebugListener = dbg; + // force TokenStream to get at least first valid token + // so we know if there are any hidden tokens first in the stream + input.LT( 1 ); + } + + #region Properties + public virtual int Index + { + get + { + return input.Index; + } + } + public virtual IDebugEventListener DebugListener + { + get + { + return dbg; + } + set + { + dbg = value; + } + } + #endregion + + [System.Obsolete] + public void SetDebugListener( IDebugEventListener dbg ) + { + DebugListener = dbg; + } + + public virtual void Consume() + { + if ( initialStreamState ) + { + ConsumeInitialHiddenTokens(); + } + int a = input.Index; + IToken t = input.LT( 1 ); + input.Consume(); + int b = input.Index; + dbg.ConsumeToken( t ); + if ( b > a + 1 ) + { + // then we consumed more than one token; must be off channel tokens + for ( int i = a + 1; i < b; i++ ) + { + dbg.ConsumeHiddenToken( input.Get( i ) ); + } + } + } + + /** <summary>Consume all initial off-channel tokens</summary> */ + protected virtual void ConsumeInitialHiddenTokens() + { + int firstOnChannelTokenIndex = input.Index; + for ( int i = 0; i < firstOnChannelTokenIndex; i++ ) + { + dbg.ConsumeHiddenToken( input.Get( i ) ); + } + initialStreamState = false; + } + + public virtual IToken LT( int i ) + { + if ( initialStreamState ) + { + ConsumeInitialHiddenTokens(); + } + dbg.LT( i, input.LT( i ) ); + return input.LT( i ); + } + + public virtual int LA( int i ) + { + if ( initialStreamState ) + { + ConsumeInitialHiddenTokens(); + } + dbg.LT( i, input.LT( i ) ); + return input.LA( i ); + } + + public virtual IToken Get( int i ) + { + return input.Get( i ); + } + + public virtual int Mark() + { + lastMarker = input.Mark(); + dbg.Mark( lastMarker ); + return lastMarker; + } + + public virtual void Rewind( int marker ) + { + dbg.Rewind( marker ); + input.Rewind( marker ); + } + + public virtual void Rewind() + { + dbg.Rewind(); + input.Rewind( lastMarker ); + } + + public virtual void Release( int marker ) + { + } + + public virtual void Seek( int index ) + { + // TODO: implement seek in dbg interface + // db.seek(index); + input.Seek( index ); + } + + public virtual int Size() + { + return input.Size(); + } + + public virtual ITokenSource TokenSource + { + get + { + return input.TokenSource; + } + } + + public virtual string SourceName + { + get + { + return TokenSource.SourceName; + } + } + + public override string ToString() + { + return input.ToString(); + } + + public virtual string ToString( int start, int stop ) + { + return input.ToString( start, stop ); + } + + public virtual string ToString( IToken start, IToken stop ) + { + return input.ToString( start, stop ); + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugTreeAdaptor.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugTreeAdaptor.cs new file mode 100644 index 0000000..25d9aad --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugTreeAdaptor.cs @@ -0,0 +1,301 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; + + /** <summary> + * A TreeAdaptor proxy that fires debugging events to a DebugEventListener + * delegate and uses the TreeAdaptor delegate to do the actual work. All + * AST events are triggered by this adaptor; no code gen changes are needed + * in generated rules. Debugging events are triggered *after* invoking + * tree adaptor routines. + * </summary> + * + * <remarks> + * Trees created with actions in rewrite actions like "-> ^(ADD {foo} {bar})" + * cannot be tracked as they might not use the adaptor to create foo, bar. + * The debug listener has to deal with tree node IDs for which it did + * not see a createNode event. A single <unknown> node is sufficient even + * if it represents a whole tree. + * </remarks> + */ + public class DebugTreeAdaptor : ITreeAdaptor + { + protected IDebugEventListener dbg; + protected ITreeAdaptor adaptor; + + public DebugTreeAdaptor( IDebugEventListener dbg, ITreeAdaptor adaptor ) + { + this.dbg = dbg; + this.adaptor = adaptor; + } + + public virtual object Create( IToken payload ) + { + if ( payload.TokenIndex < 0 ) + { + // could be token conjured up during error recovery + return Create( payload.Type, payload.Text ); + } + object node = adaptor.Create( payload ); + dbg.CreateNode( node, payload ); + return node; + } + + public virtual object ErrorNode( ITokenStream input, IToken start, IToken stop, + RecognitionException e ) + { + object node = adaptor.ErrorNode( input, start, stop, e ); + if ( node != null ) + { + dbg.ErrorNode( node ); + } + return node; + } + + public virtual object DupTree( object tree ) + { + object t = adaptor.DupTree( tree ); + // walk the tree and emit create and add child events + // to simulate what dupTree has done. dupTree does not call this debug + // adapter so I must simulate. + SimulateTreeConstruction( t ); + return t; + } + + /** <summary>^(A B C): emit create A, create B, add child, ...</summary> */ + protected virtual void SimulateTreeConstruction( object t ) + { + dbg.CreateNode( t ); + int n = adaptor.GetChildCount( t ); + for ( int i = 0; i < n; i++ ) + { + object child = adaptor.GetChild( t, i ); + SimulateTreeConstruction( child ); + dbg.AddChild( t, child ); + } + } + + public virtual object DupNode( object treeNode ) + { + object d = adaptor.DupNode( treeNode ); + dbg.CreateNode( d ); + return d; + } + + public virtual object Nil() + { + object node = adaptor.Nil(); + dbg.NilNode( node ); + return node; + } + + public virtual bool IsNil( object tree ) + { + return adaptor.IsNil( tree ); + } + + public virtual void AddChild( object t, object child ) + { + if ( t == null || child == null ) + { + return; + } + adaptor.AddChild( t, child ); + dbg.AddChild( t, child ); + } + + public virtual object BecomeRoot( object newRoot, object oldRoot ) + { + object n = adaptor.BecomeRoot( newRoot, oldRoot ); + dbg.BecomeRoot( newRoot, oldRoot ); + return n; + } + + public virtual object RulePostProcessing( object root ) + { + return adaptor.RulePostProcessing( root ); + } + + public virtual void AddChild( object t, IToken child ) + { + object n = this.Create( child ); + this.AddChild( t, n ); + } + + public virtual object BecomeRoot( IToken newRoot, object oldRoot ) + { + object n = this.Create( newRoot ); + adaptor.BecomeRoot( n, oldRoot ); + dbg.BecomeRoot( newRoot, oldRoot ); + return n; + } + + public virtual object Create( int tokenType, IToken fromToken ) + { + object node = adaptor.Create( tokenType, fromToken ); + dbg.CreateNode( node ); + return node; + } + + public virtual object Create( int tokenType, IToken fromToken, string text ) + { + object node = adaptor.Create( tokenType, fromToken, text ); + dbg.CreateNode( node ); + return node; + } + + public virtual object Create( int tokenType, string text ) + { + object node = adaptor.Create( tokenType, text ); + dbg.CreateNode( node ); + return node; + } + + public virtual int GetType( object t ) + { + return adaptor.GetType( t ); + } + + public virtual void SetType( object t, int type ) + { + adaptor.SetType( t, type ); + } + + public virtual string GetText( object t ) + { + return adaptor.GetText( t ); + } + + public virtual void SetText( object t, string text ) + { + adaptor.SetText( t, text ); + } + + public virtual IToken GetToken( object t ) + { + return adaptor.GetToken( t ); + } + + public virtual void SetTokenBoundaries( object t, IToken startToken, IToken stopToken ) + { + adaptor.SetTokenBoundaries( t, startToken, stopToken ); + if ( t != null && startToken != null && stopToken != null ) + { + dbg.SetTokenBoundaries( + t, startToken.TokenIndex, + stopToken.TokenIndex ); + } + } + + public virtual int GetTokenStartIndex( object t ) + { + return adaptor.GetTokenStartIndex( t ); + } + + public virtual int GetTokenStopIndex( object t ) + { + return adaptor.GetTokenStopIndex( t ); + } + + public virtual object GetChild( object t, int i ) + { + return adaptor.GetChild( t, i ); + } + + public virtual void SetChild( object t, int i, object child ) + { + adaptor.SetChild( t, i, child ); + } + + public virtual object DeleteChild( object t, int i ) + { + return DeleteChild( t, i ); + } + + public virtual int GetChildCount( object t ) + { + return adaptor.GetChildCount( t ); + } + + public virtual int GetUniqueID( object node ) + { + return adaptor.GetUniqueID( node ); + } + + public virtual object GetParent( object t ) + { + return adaptor.GetParent( t ); + } + + public virtual int GetChildIndex( object t ) + { + return adaptor.GetChildIndex( t ); + } + + public virtual void SetParent( object t, object parent ) + { + adaptor.SetParent( t, parent ); + } + + public virtual void SetChildIndex( object t, int index ) + { + adaptor.SetChildIndex( t, index ); + } + + public virtual void ReplaceChildren( object parent, int startChildIndex, int stopChildIndex, object t ) + { + adaptor.ReplaceChildren( parent, startChildIndex, stopChildIndex, t ); + } + + #region support + + public virtual IDebugEventListener GetDebugListener() + { + return dbg; + } + + public virtual void SetDebugListener( IDebugEventListener dbg ) + { + this.dbg = dbg; + } + + public virtual ITreeAdaptor GetTreeAdaptor() + { + return adaptor; + } + + #endregion + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugTreeNodeStream.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugTreeNodeStream.cs new file mode 100644 index 0000000..3ac9c7a --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugTreeNodeStream.cs @@ -0,0 +1,237 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + using Obsolete = System.ObsoleteAttribute; + using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; + using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream; + + /** <summary> + * Debug any tree node stream. The constructor accepts the stream + * and a debug listener. As node stream calls come in, debug events + * are triggered. + * </summary> + */ + public class DebugTreeNodeStream : ITreeNodeStream + { + protected IDebugEventListener dbg; + protected ITreeAdaptor adaptor; + protected ITreeNodeStream input; + protected bool initialStreamState = true; + + /** <summary>Track the last mark() call result value for use in rewind().</summary> */ + protected int lastMarker; + + public DebugTreeNodeStream( ITreeNodeStream input, + IDebugEventListener dbg ) + { + this.input = input; + this.adaptor = input.TreeAdaptor; + this.input.UniqueNavigationNodes = true; + DebugListener = dbg; + } + + #region Properties + public virtual IDebugEventListener DebugListener + { + get + { + return dbg; + } + set + { + dbg = value; + } + } + public virtual int Index + { + get + { + return input.Index; + } + } + public virtual ITokenStream TokenStream + { + get + { + return input.TokenStream; + } + } + public virtual ITreeAdaptor TreeAdaptor + { + get + { + return adaptor; + } + } + public virtual object TreeSource + { + get + { + return input; + } + } + /** <summary> + * It is normally this object that instructs the node stream to + * create unique nav nodes, but to satisfy interface, we have to + * define it. It might be better to ignore the parameter but + * there might be a use for it later, so I'll leave. + * </summary> + */ + public bool UniqueNavigationNodes + { + get + { + return input.UniqueNavigationNodes; + } + set + { + input.UniqueNavigationNodes = value; + } + } + + #endregion + + [Obsolete] + public void SetDebugListener( IDebugEventListener dbg ) + { + DebugListener = dbg; + } + + [Obsolete] + public ITreeAdaptor GetTreeAdaptor() + { + return TreeAdaptor; + } + + public virtual void Consume() + { + object node = input.LT( 1 ); + input.Consume(); + dbg.ConsumeNode( node ); + } + + public virtual object this[int i] + { + get + { + return input[i]; + } + } + + public virtual object LT( int i ) + { + object node = input.LT( i ); + int ID = adaptor.GetUniqueID( node ); + string text = adaptor.GetText( node ); + int type = adaptor.GetType( node ); + dbg.LT( i, node ); + return node; + } + + public virtual int LA( int i ) + { + object node = input.LT( i ); + int ID = adaptor.GetUniqueID( node ); + string text = adaptor.GetText( node ); + int type = adaptor.GetType( node ); + dbg.LT( i, node ); + return type; + } + + public virtual int Mark() + { + lastMarker = input.Mark(); + dbg.Mark( lastMarker ); + return lastMarker; + } + + public virtual void Rewind( int marker ) + { + dbg.Rewind( marker ); + input.Rewind( marker ); + } + + public virtual void Rewind() + { + dbg.Rewind(); + input.Rewind( lastMarker ); + } + + public virtual void Release( int marker ) + { + } + + public virtual void Seek( int index ) + { + // TODO: implement seek in dbg interface + // db.seek(index); + input.Seek( index ); + } + + public virtual int Size() + { + return input.Size(); + } + + [Obsolete] + public object GetTreeSource() + { + return TreeSource; + } + + public virtual string SourceName + { + get + { + return TokenStream.SourceName; + } + } + + [Obsolete] + public ITokenStream GetTokenStream() + { + return TokenStream; + } + + public virtual void ReplaceChildren( object parent, int startChildIndex, int stopChildIndex, object t ) + { + input.ReplaceChildren( parent, startChildIndex, stopChildIndex, t ); + } + + public virtual string ToString( object start, object stop ) + { + return input.ToString( start, stop ); + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugTreeParser.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugTreeParser.cs new file mode 100644 index 0000000..21e5b9b --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugTreeParser.cs @@ -0,0 +1,136 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + using Antlr.Runtime.JavaExtensions; + + using Console = System.Console; + using IOException = System.IO.IOException; + using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream; + using TreeParser = Antlr.Runtime.Tree.TreeParser; + + public class DebugTreeParser : TreeParser + { + /** <summary>Who to notify when events in the parser occur.</summary> */ + public IDebugEventListener dbg = null; + + /** <summary> + * Used to differentiate between fixed lookahead and cyclic DFA decisions + * while profiling. + * </summary> + */ + public bool isCyclicDecision = false; + + /** <summary> + * Create a normal parser except wrap the token stream in a debug + * proxy that fires consume events. + * </summary> + */ + public DebugTreeParser( ITreeNodeStream input, IDebugEventListener dbg, RecognizerSharedState state ) + : base( input is DebugTreeNodeStream ? input : new DebugTreeNodeStream( input, dbg ), state ) + { + DebugListener = dbg; + } + + public DebugTreeParser( ITreeNodeStream input, RecognizerSharedState state ) + : base( input is DebugTreeNodeStream ? input : new DebugTreeNodeStream( input, null ), state ) + { + } + + public DebugTreeParser( ITreeNodeStream input, IDebugEventListener dbg ) + : this( input is DebugTreeNodeStream ? input : new DebugTreeNodeStream( input, dbg ), dbg, null ) + { + } + + /** <summary> + * Provide a new debug event listener for this parser. Notify the + * input stream too that it should send events to this listener. + * </summary> + */ + public virtual IDebugEventListener DebugListener + { + get + { + return dbg; + } + set + { + if ( input is DebugTreeNodeStream ) + ( (DebugTreeNodeStream)input ).DebugListener = value; + + this.dbg = value; + } + } + + public virtual void ReportError( IOException e ) + { + Console.Error.WriteLine( e ); + e.PrintStackTrace( Console.Error ); + } + + public override void ReportError( RecognitionException e ) + { + dbg.RecognitionException( e ); + } + + protected override object GetMissingSymbol( IIntStream input, + RecognitionException e, + int expectedTokenType, + BitSet follow ) + { + object o = base.GetMissingSymbol( input, e, expectedTokenType, follow ); + dbg.ConsumeNode( o ); + return o; + } + + public override void BeginResync() + { + dbg.BeginResync(); + } + + public override void EndResync() + { + dbg.EndResync(); + } + + public virtual void BeginBacktrack( int level ) + { + dbg.BeginBacktrack( level ); + } + + public virtual void EndBacktrack( int level, bool successful ) + { + dbg.EndBacktrack( level, successful ); + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/DictionaryExtensions.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/DictionaryExtensions.cs new file mode 100644 index 0000000..dbd63e6 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/DictionaryExtensions.cs @@ -0,0 +1,141 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System; +using System.Collections.Generic; +#if DEBUG +using System.Linq; +#endif + +using IDictionary = System.Collections.IDictionary; + +namespace Antlr.Runtime.JavaExtensions +{ + public static class DictionaryExtensions + { +#if DEBUG + [Obsolete] + public static bool containsKey( this IDictionary map, object key ) + { + return map.Contains( key ); + } + + [Obsolete] + public static object get( this IDictionary map, object key ) + { + return map[key]; + } +#endif + + public static TValue get<TKey, TValue>( this IDictionary<TKey, TValue> map, TKey key ) + { + TValue value; + if ( map.TryGetValue( key, out value ) ) + return value; + + if ( typeof( TValue ).IsValueType ) + throw new KeyNotFoundException(); + + return default( TValue ); + } + + // disambiguates + public static TValue get<TKey, TValue>( this Dictionary<TKey, TValue> map, TKey key ) + { + TValue value; + if ( map.TryGetValue( key, out value ) ) + return value; + + if ( typeof( TValue ).IsValueType ) + throw new KeyNotFoundException(); + + return default( TValue ); + } + + public static TValue get<TKey, TValue>( this SortedList<TKey, TValue> map, TKey key ) + { + TValue value; + if ( map.TryGetValue( key, out value ) ) + return value; + + if ( typeof( TValue ).IsValueType ) + throw new KeyNotFoundException(); + + return default( TValue ); + } + +#if DEBUG + [Obsolete] + public static void put( this IDictionary map, object key, object value ) + { + map[key] = value; + } + + [Obsolete] + public static void put<TKey, TValue>( this IDictionary<TKey, TValue> map, TKey key, TValue value ) + { + map[key] = value; + } + + [Obsolete] + public static void put<TKey, TValue>( this Dictionary<TKey, TValue> map, TKey key, TValue value ) + { + map[key] = value; + } + + [Obsolete] + public static HashSet<object> keySet( this IDictionary map ) + { + return new HashSet<object>( map.Keys.Cast<object>() ); + } + + [Obsolete] + public static HashSet<TKey> keySet<TKey, TValue>( this IDictionary<TKey, TValue> map ) + { + return new HashSet<TKey>( map.Keys ); + } + + // disambiguates for Dictionary, which implements both IDictionary<T,K> and IDictionary + [Obsolete] + public static HashSet<TKey> keySet<TKey, TValue>( this Dictionary<TKey, TValue> map ) + { + return new HashSet<TKey>( map.Keys ); + } + + [Obsolete] + public static HashSet<object> keySet<TKey, TValue>( this SortedList<TKey, TValue> map ) + { + return new HashSet<object>( map.Keys.Cast<object>() ); + } +#endif + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/EnumeratorExtensions.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/EnumeratorExtensions.cs new file mode 100644 index 0000000..74c77d3 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/EnumeratorExtensions.cs @@ -0,0 +1,44 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using IEnumerable = System.Collections.IEnumerable; + +namespace Antlr.Runtime.JavaExtensions +{ + public static class EnumeratorExtensions + { + public static Iterator iterator( this IEnumerable enumerable ) + { + return new Iterator( enumerable ); + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/ExceptionExtensions.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/ExceptionExtensions.cs new file mode 100644 index 0000000..016a693 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/ExceptionExtensions.cs @@ -0,0 +1,99 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System; +using System.Diagnostics; +using System.Linq; + +using TargetInvocationException = System.Reflection.TargetInvocationException; + +namespace Antlr.Runtime.JavaExtensions +{ + public static class ExceptionExtensions + { +#if DEBUG + [Obsolete] + public static string getMessage( this Exception e ) + { + return e.Message; + } +#endif + + public static StackFrame[] getStackTrace( this Exception e ) + { + StackTrace trace = new StackTrace( e, true ); + StackFrame[] frames = trace.GetFrames(); + if ( frames == null ) + { + // don't include this helper function in the trace + frames = new StackTrace( true ).GetFrames().Skip( 1 ).ToArray(); + } + return frames; + } + +#if DEBUG + [Obsolete] + public static string getMethodName( this StackFrame frame ) + { + return frame.GetMethod().Name; + } + + [Obsolete] + public static string getClassName( this StackFrame frame ) + { + return frame.GetMethod().DeclaringType.Name; + } +#endif + + public static void PrintStackTrace( this Exception e ) + { + e.PrintStackTrace( Console.Out ); + } + public static void PrintStackTrace( this Exception e, System.IO.TextWriter writer ) + { + writer.WriteLine( e.ToString() ); + foreach ( string line in e.StackTrace.Split( '\n', '\r' ) ) + { + if ( !string.IsNullOrEmpty( line ) ) + writer.WriteLine( " " + line ); + } + } + +#if DEBUG + [Obsolete] + public static Exception getTargetException( this TargetInvocationException e ) + { + return e.InnerException ?? e; + } +#endif + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/IOExtensions.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/IOExtensions.cs new file mode 100644 index 0000000..c1fe293 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/IOExtensions.cs @@ -0,0 +1,94 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +#if DEBUG + +using System; + +using TextReader = System.IO.TextReader; +using TextWriter = System.IO.TextWriter; + +namespace Antlr.Runtime.JavaExtensions +{ + public static class IOExtensions + { + [Obsolete] + public static void close( this TextReader reader ) + { + reader.Close(); + } + + [Obsolete] + public static void close( this TextWriter writer ) + { + writer.Close(); + } + + [Obsolete] + public static void print<T>( this TextWriter writer, T value ) + { + writer.Write( value ); + } + + [Obsolete] + public static void println( this TextWriter writer ) + { + writer.WriteLine(); + } + + [Obsolete] + public static void println<T>( this TextWriter writer, T value ) + { + writer.WriteLine( value ); + } + + [Obsolete] + public static void write<T>( this TextWriter writer, T value ) + { + writer.Write( value ); + } + + [Obsolete] + public static int read( this TextReader reader, char[] buffer, int index, int count ) + { + return reader.Read( buffer, index, count ); + } + + [Obsolete] + public static string readLine( this TextReader reader ) + { + return reader.ReadLine(); + } + } +} + +#endif diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/Iterator.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/Iterator.cs new file mode 100644 index 0000000..96316c2 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/Iterator.cs @@ -0,0 +1,115 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using IEnumerable = System.Collections.IEnumerable; +using IEnumerator = System.Collections.IEnumerator; + +namespace Antlr.Runtime.JavaExtensions +{ + public class Iterator : IEnumerator + { + static readonly object[] EmptySource = new object[0]; + + IEnumerable _enumerable; + IEnumerator _iterator; + bool _hasNext; + + public Iterator() + : this( EmptySource ) + { + } + public Iterator( IEnumerable enumerable ) + { + _enumerable = enumerable; + _iterator = enumerable.GetEnumerator(); + _hasNext = _iterator.MoveNext(); + } + + public IEnumerable Source + { + get + { + return _enumerable; + } + } + + public virtual bool hasNext() + { + return _hasNext; + } + + public virtual object next() + { + object current = _iterator.Current; + _hasNext = _iterator.MoveNext(); + return current; + } + + // these are for simulation of ASTEnumeration + public virtual bool hasMoreNodes() + { + return _hasNext; + } + + public virtual object nextNode() + { + return next(); + } + + #region IEnumerator Members + public virtual object Current + { + get + { + return _iterator.Current; + } + } + public virtual bool MoveNext() + { + return _iterator.MoveNext(); + } + public virtual void Reset() + { + _iterator.Reset(); + } + #endregion + + #region IDisposable Members + public void Dispose() + { + _enumerable = null; + _hasNext = false; + _iterator = null; + } + #endregion + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/JSystem.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/JSystem.cs new file mode 100644 index 0000000..d2bc2c0 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/JSystem.cs @@ -0,0 +1,89 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +#if DEBUG +namespace Antlr.Runtime.JavaExtensions +{ + using System; + using System.IO; + + public static class JSystem + { + [Obsolete] + public static TextWriter err + { + get + { + return Console.Error; + } + } + + [Obsolete] + public static TextWriter @out + { + get + { + return Console.Out; + } + } + + [Obsolete] + public static void arraycopy<T>( T[] sourceArray, int sourceIndex, T[] destinationArray, int destinationIndex, int length ) + { + Array.Copy( sourceArray, sourceIndex, destinationArray, destinationIndex, length ); + } + + [Obsolete] + public static string getProperty( string name ) + { + switch ( name ) + { + case "file.encoding": + return System.Text.Encoding.Default.WebName; + + case "line.separator": + return Environment.NewLine; + + case "java.io.tmpdir": + return System.IO.Path.GetTempPath(); + + case "user.home": + return Environment.GetFolderPath( Environment.SpecialFolder.Personal ); + + default: + throw new ArgumentException( string.Format( "Unknown system property: '{0}'", name ) ); + } + } + + } +} +#endif diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/ListExtensions.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/ListExtensions.cs new file mode 100644 index 0000000..90b2987 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/ListExtensions.cs @@ -0,0 +1,239 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System; +using System.Collections.Generic; +using System.Linq; + +using ICollection = System.Collections.ICollection; +using IEnumerable = System.Collections.IEnumerable; +using IList = System.Collections.IList; + +namespace Antlr.Runtime.JavaExtensions +{ + public static class ListExtensions + { +#if DEBUG + [Obsolete] + public static bool add( this IList list, object value ) + { + int count = list.Count; + list.Add( value ); + return list.Count == count + 1; + } + + [Obsolete] + public static void add<T>( this ICollection<T> list, T value ) + { + list.Add( value ); + } + + [Obsolete] + public static void add<T>( this List<T> list, T value ) + { + list.Add( value ); + } + + [Obsolete] + public static void add( this IList list, int index, object value ) + { + list.Insert( index, value ); + } + + [Obsolete] + public static void addAll( this List<object> list, IEnumerable items ) + { + list.AddRange( items.Cast<object>() ); + } +#endif + + public static void addAll( this IList list, IEnumerable items ) + { + foreach ( object item in items ) + list.Add( item ); + } + + public static void addAll<T>( this ICollection<T> list, IEnumerable<T> items ) + { + foreach ( T item in items ) + list.Add( item ); + } + +#if DEBUG + [Obsolete] + public static void addElement( this List<object> list, object value ) + { + list.Add( value ); + } + + [Obsolete] + public static void clear( this IList list ) + { + list.Clear(); + } + + [Obsolete] + public static bool contains( this IList list, object value ) + { + return list.Contains( value ); + } + + [Obsolete] + public static bool contains<T>( this ICollection<T> list, T value ) + { + return list.Contains( value ); + } + + [Obsolete] + public static T elementAt<T>( this IList<T> list, int index ) + { + return list[index]; + } + + [Obsolete] + public static object get( this IList list, int index ) + { + return list[index]; + } + + [Obsolete] + public static T get<T>( this IList<T> list, int index ) + { + return list[index]; + } + + // disambiguate + [Obsolete] + public static T get<T>( this List<T> list, int index ) + { + return list[index]; + } + + [Obsolete] + public static object remove( this IList list, int index ) + { + object o = list[index]; + list.RemoveAt( index ); + return o; + } + + [Obsolete] + public static void remove<T>( this IList<T> list, T item ) + { + list.Remove( item ); + } + + [Obsolete] + public static void set( this IList list, int index, object value ) + { + list[index] = value; + } + + [Obsolete] + public static void set<T>( this IList<T> list, int index, T value ) + { + list[index] = value; + } + + [Obsolete] + public static void set<T>( this List<T> list, int index, T value ) + { + list[index] = value; + } +#endif + + public static void setSize<T>( this List<T> list, int size ) + { + if ( list.Count < size ) + { + list.AddRange( Enumerable.Repeat( default( T ), size - list.Count ) ); + } + else if ( list.Count > size ) + { + T[] items = list.Take( size ).ToArray(); + list.Clear(); + list.AddRange( items ); + } + } + +#if DEBUG + [Obsolete] + public static int size( this ICollection collection ) + { + return collection.Count; + } + + [Obsolete] + public static int size<T>( this ICollection<T> collection ) + { + return collection.Count; + } + + [Obsolete] + public static int size<T>( this List<T> list ) + { + return list.Count; + } +#endif + + public static IList subList( this IList list, int fromIndex, int toIndex ) + { + return new SubList( list, fromIndex, toIndex ); + //return + // list + // .Cast<object>() + // .Skip( fromIndex ) + // .Take( toIndex - fromIndex + 1 ) + // .ToList(); + } + + public static IList<T> subList<T>( this IList<T> list, int fromIndex, int toIndex ) + { + return new SubList<T>( list, fromIndex, toIndex ); + //return + // list + // .Skip( fromIndex ) + // .Take( toIndex - fromIndex + 1 ) + // .ToList(); + } + + public static IList<T> subList<T>( this List<T> list, int fromIndex, int toIndex ) + { + return new SubList<T>( list, fromIndex, toIndex ); + //return + // list + // .Skip( fromIndex ) + // .Take( toIndex - fromIndex + 1 ) + // .ToList(); + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/ObjectExtensions.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/ObjectExtensions.cs new file mode 100644 index 0000000..fe6a274 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/ObjectExtensions.cs @@ -0,0 +1,123 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System; + +namespace Antlr.Runtime.JavaExtensions +{ + public static class ObjectExtensions + { +#if DEBUG + [Obsolete] + public static bool booleanValue( this bool value ) + { + return value; + } + + [Obsolete] + public static Type getClass( this object o ) + { + return o.GetType(); + } +#endif + + public static int ShiftPrimeXOR( int a, int b ) + { + int hash = 23; + hash = ( ( hash << 5 ) * 37 ) ^ a; + hash = ( ( hash << 5 ) * 37 ) ^ b; + return hash; + } + + public static int ShiftPrimeXOR( int a, int b, int c ) + { + int hash = 23; + hash = ( ( hash << 5 ) * 37 ) ^ a; + hash = ( ( hash << 5 ) * 37 ) ^ b; + hash = ( ( hash << 5 ) * 37 ) ^ c; + return hash; + } + + public static int ShiftPrimeXOR( int a, int b, int c, int d ) + { + int hash = 23; + hash = ( ( hash << 5 ) * 37 ) ^ a; + hash = ( ( hash << 5 ) * 37 ) ^ b; + hash = ( ( hash << 5 ) * 37 ) ^ c; + hash = ( ( hash << 5 ) * 37 ) ^ d; + return hash; + } + + public static int ShiftPrimeXOR( params int[] a ) + { + int hash = 23; + foreach ( int i in a ) + hash = ( ( hash << 5 ) * 37 ) ^ i; + return hash; + } + + public static int ShiftPrimeAdd( int a, int b ) + { + int hash = 23; + hash = ( ( hash << 5 ) * 37 ) + a; + hash = ( ( hash << 5 ) * 37 ) + b; + return hash; + } + + public static int ShiftPrimeAdd( int a, int b, int c ) + { + int hash = 23; + hash = ( ( hash << 5 ) * 37 ) + a; + hash = ( ( hash << 5 ) * 37 ) + b; + hash = ( ( hash << 5 ) * 37 ) + c; + return hash; + } + + public static int ShiftPrimeAdd( int a, int b, int c, int d ) + { + int hash = 23; + hash = ( ( hash << 5 ) * 37 ) + a; + hash = ( ( hash << 5 ) * 37 ) + b; + hash = ( ( hash << 5 ) * 37 ) + c; + hash = ( ( hash << 5 ) * 37 ) + d; + return hash; + } + + public static int ShiftPrimeAdd( params int[] a ) + { + int hash = 23; + foreach ( int i in a ) + hash = ( ( hash << 5 ) * 37 ) + i; + return hash; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/SetExtensions.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/SetExtensions.cs new file mode 100644 index 0000000..881326e --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/SetExtensions.cs @@ -0,0 +1,89 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System; +using System.Collections.Generic; +#if DEBUG +using System.Linq; +#endif + +namespace Antlr.Runtime.JavaExtensions +{ + public static class SetExtensions + { +#if DEBUG + [Obsolete] + public static bool add<T>( this HashSet<T> set, T item ) + { + return set.Add( item ); + } +#endif + + public static void addAll<T>( this HashSet<T> set, IEnumerable<T> items ) + { + foreach ( T item in items ) + set.Add( item ); + } + +#if DEBUG + [Obsolete] + public static void clear<T>( this HashSet<T> set ) + { + set.Clear(); + } + + [Obsolete] + public static bool contains<T>( this HashSet<T> set, T value ) + { + return set.Contains( value ); + } + + [Obsolete] + public static bool remove<T>( this HashSet<T> set, T item ) + { + return set.Remove( item ); + } + + [Obsolete] + public static int size<T>( this HashSet<T> set ) + { + return set.Count; + } + + [Obsolete] + public static T[] toArray<T>( this HashSet<T> set ) + { + return set.ToArray(); + } +#endif + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/StackExtensions.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/StackExtensions.cs new file mode 100644 index 0000000..ab9ec71 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/StackExtensions.cs @@ -0,0 +1,84 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System; +using System.Collections.Generic; +#if DEBUG +using System.Linq; +#endif + +namespace Antlr.Runtime.JavaExtensions +{ + public static class StackExtensions + { +#if DEBUG + [Obsolete] + public static T elementAt<T>( this Stack<T> stack, int index ) + { + return stack.ElementAt( index ); + } + + [Obsolete] + public static T peek<T>( this Stack<T> stack ) + { + return stack.Peek(); + } + + [Obsolete] + public static T pop<T>( this Stack<T> stack ) + { + return stack.Pop(); + } + + [Obsolete] + public static void push<T>( this Stack<T> stack, T obj ) + { + stack.Push( obj ); + } + + [Obsolete] + public static int size<T>( this Stack<T> stack ) + { + return stack.Count; + } +#endif + + public static void setSize<T>( this Stack<T> stack, int size ) + { + if ( size > stack.Count ) + throw new ArgumentException(); + + while ( stack.Count > size ) + stack.Pop(); + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/StringBuilderExtensions.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/StringBuilderExtensions.cs new file mode 100644 index 0000000..6ef6f27 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/StringBuilderExtensions.cs @@ -0,0 +1,75 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System; + +using StringBuilder = System.Text.StringBuilder; + +#if DEBUG + +namespace Antlr.Runtime.JavaExtensions +{ + public static class StringBuilderExtensions + { + [Obsolete] + public static void append<T>( this StringBuilder buffer, T value ) + { + buffer.Append( value ); + } + + [Obsolete] + public static char charAt( this StringBuilder buffer, int index ) + { + return buffer[index]; + } + + [Obsolete] + public static int length( this StringBuilder buffer ) + { + return buffer.Length; + } + + [Obsolete] + public static void setCharAt( this StringBuilder buffer, int index, char c ) + { + buffer[index] = c; + } + + [Obsolete] + public static void setLength( this StringBuilder buffer, int length ) + { + buffer.Length = length; + } + } +} + +#endif diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/StringExtensions.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/StringExtensions.cs new file mode 100644 index 0000000..b9b6ac7 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/StringExtensions.cs @@ -0,0 +1,156 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System; +using System.Text; + +namespace Antlr.Runtime.JavaExtensions +{ + public static class StringExtensions + { +#if DEBUG + [Obsolete] + public static char charAt( this string str, int index ) + { + return str[index]; + } + + [Obsolete] + public static bool endsWith( this string str, string value ) + { + return str.EndsWith( value ); + } + + [Obsolete] + public static int indexOf( this string str, char value ) + { + return str.IndexOf( value ); + } + + [Obsolete] + public static int indexOf( this string str, char value, int startIndex ) + { + return str.IndexOf( value, startIndex ); + } + + [Obsolete] + public static int indexOf( this string str, string value ) + { + return str.IndexOf( value ); + } + + [Obsolete] + public static int indexOf( this string str, string value, int startIndex ) + { + return str.IndexOf( value, startIndex ); + } + + [Obsolete] + public static int lastIndexOf( this string str, char value ) + { + return str.LastIndexOf( value ); + } + + [Obsolete] + public static int lastIndexOf( this string str, string value ) + { + return str.LastIndexOf( value ); + } + + [Obsolete] + public static int length( this string str ) + { + return str.Length; + } +#endif + + public static string replace( this string str, char oldValue, char newValue ) + { + int index = str.IndexOf( oldValue ); + if ( index == -1 ) + return str; + + System.Text.StringBuilder builder = new StringBuilder( str ); + builder[index] = newValue; + return builder.ToString(); + } + + public static string replaceAll( this string str, string regex, string newValue ) + { + return System.Text.RegularExpressions.Regex.Replace( str, regex, newValue ); + } + + public static string replaceFirst( this string str, string regex, string replacement ) + { + return System.Text.RegularExpressions.Regex.Replace( str, regex, replacement ); + } + +#if DEBUG + [Obsolete] + public static bool startsWith( this string str, string value ) + { + return str.StartsWith( value ); + } +#endif + + [Obsolete] + public static string substring( this string str, int startOffset ) + { + return str.Substring( startOffset ); + } + + public static string substring( this string str, int startOffset, int endOffset ) + { + return str.Substring( startOffset, endOffset - startOffset ); + } + +#if DEBUG + [Obsolete] + public static char[] toCharArray( this string str ) + { + return str.ToCharArray(); + } + + [Obsolete] + public static string toUpperCase( this string str ) + { + return str.ToUpperInvariant(); + } + + [Obsolete] + public static string trim( this string str ) + { + return str.Trim(); + } +#endif + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/StringTokenizer.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/StringTokenizer.cs new file mode 100644 index 0000000..0145df5 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/StringTokenizer.cs @@ -0,0 +1,87 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System; +using System.Linq; + +namespace Antlr.Runtime.JavaExtensions +{ + public class StringTokenizer + { + string[] _tokens; + int _current; + + public StringTokenizer( string str, string separator ) + : this( str, separator, false ) + { + } + public StringTokenizer( string str, string separator, bool returnDelims ) + { + _tokens = str.Split( separator.ToCharArray(), StringSplitOptions.None ); + if ( returnDelims ) + { + char[] delims = separator.ToCharArray(); + _tokens = _tokens.SelectMany( ( token, i ) => + { + if ( i == _tokens.Length - 1 ) + { + if ( delims.Contains( str[str.Length - 1] ) ) + return new string[0]; + else + return new string[] { token }; + } + else if ( i == 0 ) + { + if ( delims.Contains( str[0] ) ) + return new string[] { str[0].ToString() }; + else + return new string[] { token }; + } + else + { + return new string[] { token, str[_tokens.Take( i + 1 ).Select( t => t.Length + 1 ).Sum() - 1].ToString() }; + } + } ).ToArray(); + } + } + + public bool hasMoreTokens() + { + return _current < _tokens.Length; + } + + public string nextToken() + { + return _tokens[_current++]; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/SubList.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/SubList.cs new file mode 100644 index 0000000..97025bf --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/SubList.cs @@ -0,0 +1,423 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Collections; + +namespace Antlr.Runtime.JavaExtensions +{ + public class SubList + : IList + { + IList _source; + int _startIndex; + int _endIndex; + + public SubList( IList source, int startIndex, int endIndex ) + { + if ( source == null ) + throw new ArgumentNullException( "source" ); + if ( startIndex < 0 || endIndex < 0 ) + throw new ArgumentOutOfRangeException(); + if ( startIndex > endIndex || endIndex >= source.Count ) + throw new ArgumentException(); + + _source = source; + _startIndex = startIndex; + _endIndex = endIndex; + } + + #region IList Members + + public int Add( object value ) + { + throw new NotSupportedException(); + } + + public void Clear() + { + throw new NotSupportedException(); + } + + public bool Contains( object value ) + { + return _source + .Cast<object>() + .Skip( _startIndex ) + .Take( _endIndex - _startIndex + 1 ) + .Contains( value ); + } + + public int IndexOf( object value ) + { + for ( int i = 0; i < Count; i++ ) + { + if ( object.Equals( this[i], value ) ) + return i; + } + + return -1; + } + + public void Insert( int index, object value ) + { + throw new NotSupportedException(); + } + + public bool IsFixedSize + { + get + { + return true; + } + } + + public bool IsReadOnly + { + get + { + return true; + } + } + + public void Remove( object value ) + { + throw new NotSupportedException(); + } + + public void RemoveAt( int index ) + { + throw new NotSupportedException(); + } + + public object this[int index] + { + get + { + if ( index < 0 || index >= Count ) + throw new ArgumentOutOfRangeException(); + + return _source[index + _startIndex]; + } + set + { + if ( index < 0 || index >= Count ) + throw new ArgumentOutOfRangeException(); + + _source[index + _startIndex] = value; + } + } + + #endregion + + #region ICollection Members + + public void CopyTo( Array array, int index ) + { + if ( array == null ) + throw new ArgumentNullException( "array" ); + + if ( index < 0 ) + throw new ArgumentOutOfRangeException(); + + if ( index + Count > array.Length ) + throw new ArgumentException(); + + for ( int i = 0; i < Count; i++ ) + { + array.SetValue( this[i], index + i ); + } + } + + public int Count + { + get + { + return _endIndex - _startIndex + 1; + } + } + + public bool IsSynchronized + { + get + { + return false; + } + } + + public object SyncRoot + { + get + { + return _source.SyncRoot; + } + } + + #endregion + + #region IEnumerable Members + + public System.Collections.IEnumerator GetEnumerator() + { + return _source.Cast<object>() + .Skip( _startIndex ) + .Take( _endIndex - _startIndex + 1 ) + .GetEnumerator(); + } + + #endregion + } + + public class SubList<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable + { + IList<T> _source; + int _startIndex; + int _endIndex; + + public SubList( IList<T> source, int startIndex, int endIndex ) + { + if ( source == null ) + throw new ArgumentNullException( "source" ); + if ( startIndex < 0 || endIndex < 0 ) + throw new ArgumentOutOfRangeException(); + if ( startIndex > endIndex || endIndex >= source.Count ) + throw new ArgumentException(); + + _source = source; + _startIndex = startIndex; + _endIndex = endIndex; + } + + #region IEnumerable Members + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + #endregion + + #region ICollection Members + + void ICollection.CopyTo( Array array, int index ) + { + if ( array == null ) + throw new ArgumentNullException( "array" ); + + if ( index < 0 ) + throw new ArgumentOutOfRangeException(); + + if ( index + Count > array.Length ) + throw new ArgumentException(); + + for ( int i = 0; i < Count; i++ ) + { + array.SetValue( this[i], index + i ); + } + } + + public int Count + { + get + { + return _endIndex - _startIndex + 1; + } + } + + public bool IsSynchronized + { + get + { + ICollection sourceCollection = _source as ICollection; + if ( sourceCollection != null ) + return sourceCollection.IsSynchronized; + + return false; + } + } + + public object SyncRoot + { + get + { + ICollection sourceCollection = _source as ICollection; + if ( sourceCollection != null ) + return sourceCollection.SyncRoot; + + return _source; + } + } + + #endregion + + #region IList Members + + int IList.Add( object value ) + { + throw new NotSupportedException(); + } + + void IList.Clear() + { + throw new NotSupportedException(); + } + + public bool Contains( object value ) + { + return _source.Cast<object>().Skip( _startIndex ).Take( Count ).Contains( value ); + } + + public int IndexOf( object value ) + { + for ( int i = _startIndex; i <= _endIndex; i++ ) + { + if ( object.Equals( _source[i], value ) ) + return i - _startIndex; + } + + return -1; + } + + void IList.Insert( int index, object value ) + { + throw new NotSupportedException(); + } + + public bool IsFixedSize + { + get + { + var sourceCollection = _source as IList; + if ( sourceCollection != null ) + return sourceCollection.IsFixedSize; + + return false; + } + } + + public bool IsReadOnly + { + get + { + return true; + } + } + + void IList.Remove( object value ) + { + throw new NotSupportedException(); + } + + void IList.RemoveAt( int index ) + { + throw new NotSupportedException(); + } + + object IList.this[int index] + { + get + { + return this[index]; + } + set + { + this[index] = (T)value; + } + } + + #endregion + + #region IEnumerable<T> Members + + public IEnumerator<T> GetEnumerator() + { + return _source.Skip( _startIndex ).Take( Count ).GetEnumerator(); + } + + #endregion + + #region ICollection<T> Members + + void ICollection<T>.Add( T item ) + { + throw new NotSupportedException(); + } + + void ICollection<T>.Clear() + { + throw new NotSupportedException(); + } + + public bool Contains( T item ) + { + return _source.Skip( _startIndex ).Take( Count ).Contains( item ); + } + + public void CopyTo( T[] array, int arrayIndex ) + { + if ( array == null ) + throw new ArgumentNullException( "array" ); + + if ( arrayIndex < 0 ) + throw new ArgumentOutOfRangeException(); + + if ( arrayIndex + Count > array.Length ) + throw new ArgumentException(); + + for ( int i = 0; i < Count; i++ ) + { + array[arrayIndex + i] = this[i]; + } + } + + bool ICollection<T>.Remove( T item ) + { + throw new NotSupportedException(); + } + + #endregion + + #region IList<T> Members + + public int IndexOf( T item ) + { + for ( int i = 0; i < Count; i++ ) + { + if ( object.Equals( this[i], item ) ) + return i; + } + + return -1; + } + + void IList<T>.Insert( int index, T item ) + { + throw new NotSupportedException(); + } + + void IList<T>.RemoveAt( int index ) + { + throw new NotSupportedException(); + } + + public T this[int index] + { + get + { + if ( index < 0 || index >= Count ) + throw new ArgumentOutOfRangeException(); + + return _source[index + _startIndex]; + } + set + { + if ( index < 0 || index >= Count ) + throw new ArgumentOutOfRangeException(); + + _source[index + _startIndex] = value; + } + } + + #endregion + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/TreeExtensions.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/TreeExtensions.cs new file mode 100644 index 0000000..6f7182f --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/TreeExtensions.cs @@ -0,0 +1,53 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using ITree = Antlr.Runtime.Tree.ITree; + +namespace Antlr.Runtime.JavaExtensions +{ + public static class TreeExtensions + { + public static ITree getNextSibling( this ITree tree ) + { + return tree.Parent.GetChild( tree.ChildIndex + 1 ); + //throw new NotImplementedException(); + } + + public static void setFirstChild( this ITree tree, ITree child ) + { + if ( tree.ChildCount == 0 ) + tree.AddChild( child ); + else + tree.SetChild( 0, child ); + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/TypeExtensions.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/TypeExtensions.cs new file mode 100644 index 0000000..42c0368 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/JavaExtensions/TypeExtensions.cs @@ -0,0 +1,102 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System; +using System.Reflection; + +#if DEBUG + +namespace Antlr.Runtime.JavaExtensions +{ + public static class TypeExtensions + { + [Obsolete] + public static object get( this FieldInfo field, object obj ) + { + return field.GetValue( obj ); + } + + [Obsolete] + public static Type getComponentType( this Type type ) + { + return type.GetElementType(); + } + + [Obsolete] + public static ConstructorInfo getConstructor( this Type type, Type[] argumentTypes ) + { + return type.GetConstructor( argumentTypes ); + } + + [Obsolete] + public static FieldInfo getField( this Type type, string name ) + { + FieldInfo field = type.GetField( name ); + if ( field == null ) + throw new TargetException(); + + return field; + } + + [Obsolete] + public static string getName( this Type type ) + { + return type.Name; + } + + [Obsolete] + public static object invoke( this MethodInfo method, object obj, params object[] parameters ) + { + return method.Invoke( obj, parameters ); + } + + [Obsolete] + public static bool isArray( this Type type ) + { + return type.IsArray; + } + + [Obsolete] + public static bool isPrimitive( this Type type ) + { + return type.IsPrimitive; + } + + [Obsolete] + public static object newInstance( this Type type ) + { + return type.GetConstructor( new Type[0] ).Invoke( new object[0] ); + } + } +} + +#endif diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Misc/Stats.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Misc/Stats.cs new file mode 100644 index 0000000..8f163b1 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Misc/Stats.cs @@ -0,0 +1,143 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Misc +{ + using System.Collections.Generic; + using System.Linq; + using Antlr.Runtime.JavaExtensions; + + using Math = System.Math; + + /** <summary>Stats routines needed by profiler etc...</summary> + * + * <remarks> + * note that these routines return 0.0 if no values exist in the X[] + * which is not "correct", but it is useful so I don't generate NaN + * in my output + * </remarks> + */ + public class Stats + { + public const string ANTLRWORKS_DIR = "antlrworks"; + + /** <summary>Compute the sample (unbiased estimator) standard deviation following:</summary> + * + * <remarks> + * Computing Deviations: Standard Accuracy + * Tony F. Chan and John Gregg Lewis + * Stanford University + * Communications of ACM September 1979 of Volume 22 the ACM Number 9 + * + * The "two-pass" method from the paper; supposed to have better + * numerical properties than the textbook summation/sqrt. To me + * this looks like the textbook method, but I ain't no numerical + * methods guy. + * </remarks> + */ + public static double Stddev( int[] X ) + { + int m = X.Length; + if ( m <= 1 ) + { + return 0; + } + double xbar = X.Average(); + double s2 = 0.0; + for ( int i = 0; i < m; i++ ) + { + s2 += ( X[i] - xbar ) * ( X[i] - xbar ); + } + s2 = s2 / ( m - 1 ); + return Math.Sqrt( s2 ); + } + public static double Stddev( List<int> X ) + { + int m = X.Count; + if ( m <= 1 ) + { + return 0; + } + double xbar = X.Average(); + double s2 = 0.0; + for ( int i = 0; i < m; i++ ) + { + s2 += ( X[i] - xbar ) * ( X[i] - xbar ); + } + s2 = s2 / ( m - 1 ); + return Math.Sqrt( s2 ); + } + +#if DEBUG + /** <summary>Compute the sample mean</summary> */ + [System.Obsolete] + public static double avg( int[] X ) + { + return X.DefaultIfEmpty( 0 ).Average(); + } + + [System.Obsolete] + public static int min( int[] X ) + { + return X.DefaultIfEmpty( int.MaxValue ).Min(); + } + + [System.Obsolete] + public static int max( int[] X ) + { + return X.DefaultIfEmpty( int.MinValue ).Max(); + } + + [System.Obsolete] + public static int sum( int[] X ) + { + return X.Sum(); + } +#endif + + public static void WriteReport( string filename, string data ) + { + string absoluteFilename = GetAbsoluteFileName( filename ); + + System.IO.Directory.CreateDirectory( System.IO.Path.GetDirectoryName( absoluteFilename ) ); + System.IO.File.AppendAllText( absoluteFilename, data ); + } + + public static string GetAbsoluteFileName( string filename ) + { + string personalFolder = System.Environment.GetFolderPath( System.Environment.SpecialFolder.Personal ); + return personalFolder + System.IO.Path.DirectorySeparatorChar + + ANTLRWORKS_DIR + System.IO.Path.DirectorySeparatorChar + + filename; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/ParseTreeBuilder.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/ParseTreeBuilder.cs new file mode 100644 index 0000000..3206091 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/ParseTreeBuilder.cs @@ -0,0 +1,140 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + using System.Collections.Generic; + using Antlr.Runtime.JavaExtensions; + + using ParseTree = Antlr.Runtime.Tree.ParseTree; + + /** <summary> + * This parser listener tracks rule entry/exit and token matches + * to build a simple parse tree using ParseTree nodes. + * </summary> + */ + public class ParseTreeBuilder : BlankDebugEventListener + { + public const string EPSILON_PAYLOAD = "<epsilon>"; + + Stack<ParseTree> callStack = new Stack<ParseTree>(); + List<IToken> hiddenTokens = new List<IToken>(); + int backtracking = 0; + + public ParseTreeBuilder( string grammarName ) + { + ParseTree root = Create( "<grammar " + grammarName + ">" ); + callStack.Push( root ); + } + + public virtual ParseTree GetTree() + { + var enumerator = callStack.GetEnumerator(); + enumerator.MoveNext(); + return enumerator.Current; + } + + /** <summary> + * What kind of node to create. You might want to override + * so I factored out creation here. + * </summary> + */ + public virtual ParseTree Create( object payload ) + { + return new ParseTree( payload ); + } + + public virtual ParseTree EpsilonNode() + { + return Create( EPSILON_PAYLOAD ); + } + + /** <summary>Backtracking or cyclic DFA, don't want to add nodes to tree</summary> */ + public override void EnterDecision( int d ) + { + backtracking++; + } + public override void ExitDecision( int i ) + { + backtracking--; + } + + public override void EnterRule( string filename, string ruleName ) + { + if ( backtracking > 0 ) + return; + ParseTree parentRuleNode = callStack.Peek(); + ParseTree ruleNode = Create( ruleName ); + parentRuleNode.AddChild( ruleNode ); + callStack.Push( ruleNode ); + } + + public override void ExitRule( string filename, string ruleName ) + { + if ( backtracking > 0 ) + return; + ParseTree ruleNode = callStack.Peek(); + if ( ruleNode.ChildCount == 0 ) + { + ruleNode.AddChild( EpsilonNode() ); + } + callStack.Pop(); + } + + public override void ConsumeToken( IToken token ) + { + if ( backtracking > 0 ) + return; + ParseTree ruleNode = callStack.Peek(); + ParseTree elementNode = Create( token ); + elementNode.hiddenTokens = this.hiddenTokens; + this.hiddenTokens = new List<IToken>(); + ruleNode.AddChild( elementNode ); + } + + public override void ConsumeHiddenToken( IToken token ) + { + if ( backtracking > 0 ) + return; + hiddenTokens.Add( token ); + } + + public override void RecognitionException( RecognitionException e ) + { + if ( backtracking > 0 ) + return; + ParseTree ruleNode = callStack.Peek(); + ParseTree errorNode = Create( e ); + ruleNode.AddChild( errorNode ); + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/ParserDebugger.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/ParserDebugger.cs new file mode 100644 index 0000000..0b60f2b --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/ParserDebugger.cs @@ -0,0 +1,49 @@ +namespace Antlr.Runtime.Debug +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + + using IOException = System.IO.IOException; + using Antlr.Runtime.Tree; + + public class ParserDebugger + { + IDebugEventListener dbg; + + public ParserDebugger( Parser parser ) + : this( parser, DebugEventSocketProxy.DEFAULT_DEBUGGER_PORT ) + { + } + public ParserDebugger( Parser parser, int port ) + { + DebugEventSocketProxy proxy = new DebugEventSocketProxy( parser, port, null ); + DebugListener = proxy; + parser.TokenStream = new DebugTokenStream( parser.TokenStream, proxy ); + try + { + proxy.handshake(); + } + catch ( IOException e ) + { + reportError( ioe ); + } + ITreeAdaptor adap = new CommonTreeAdaptor(); + TreeAdaptor = adap; + proxy.TreeAdaptor = adap; + } + public ParserDebugger( Parser parser, IDebugEventListener dbg ) + { + ITreeAdaptor adap = new CommonTreeAdaptor(); + TreeAdaptor = adap; + } + + protected virtual bool EvalPredicate( bool result, string predicate ) + { + dbg.SemanticPredicate( result, predicate ); + return result; + } + + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Profiler.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Profiler.cs new file mode 100644 index 0000000..501d4b2 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Profiler.cs @@ -0,0 +1,565 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + using System.Collections.Generic; + using System.Linq; + using Antlr.Runtime.JavaExtensions; + + using Array = System.Array; + using Console = System.Console; + using IOException = System.IO.IOException; + using Stats = Antlr.Runtime.Misc.Stats; + using StringBuilder = System.Text.StringBuilder; + + /** <summary>Using the debug event interface, track what is happening in the parser + * and record statistics about the runtime. + */ + public class Profiler : BlankDebugEventListener + { + /** <summary>Because I may change the stats, I need to track that for later + * computations to be consistent. + */ + public const string Version = "2"; + public const string RUNTIME_STATS_FILENAME = "runtime.stats"; + public const int NUM_RUNTIME_STATS = 29; + + public DebugParser parser = null; + + #region working variables + + protected int ruleLevel = 0; + protected int decisionLevel = 0; + protected int maxLookaheadInCurrentDecision = 0; + protected CommonToken lastTokenConsumed = null; + + protected Stack<int> lookaheadStack = new Stack<int>(); + + #endregion + + + #region stats variables + + public int numRuleInvocations = 0; + public int numGuessingRuleInvocations = 0; + public int maxRuleInvocationDepth = 0; + public int numFixedDecisions = 0; + public int numCyclicDecisions = 0; + public int numBacktrackDecisions = 0; + public int[] decisionMaxFixedLookaheads = new int[200]; // TODO: make List + public int[] decisionMaxCyclicLookaheads = new int[200]; + public List<int> decisionMaxSynPredLookaheads = new List<int>(); + public int numHiddenTokens = 0; + public int numCharsMatched = 0; + public int numHiddenCharsMatched = 0; + public int numSemanticPredicates = 0; + public int numSyntacticPredicates = 0; + protected int numberReportedErrors = 0; + public int numMemoizationCacheMisses = 0; + public int numMemoizationCacheHits = 0; + public int numMemoizationCacheEntries = 0; + + #endregion + + public Profiler() + { + } + + public Profiler( DebugParser parser ) + { + this.parser = parser; + } + + public override void EnterRule( string grammarFileName, string ruleName ) + { + //System.out.println("enterRule "+ruleName); + ruleLevel++; + numRuleInvocations++; + if ( ruleLevel > maxRuleInvocationDepth ) + { + maxRuleInvocationDepth = ruleLevel; + } + + } + + /** <summary> + * Track memoization; this is not part of standard debug interface + * but is triggered by profiling. Code gen inserts an override + * for this method in the recognizer, which triggers this method. + * </summary> + */ + public virtual void ExamineRuleMemoization( IIntStream input, + int ruleIndex, + string ruleName ) + { + //System.out.println("examine memo "+ruleName); + int stopIndex = parser.GetRuleMemoization( ruleIndex, input.Index ); + if ( stopIndex == BaseRecognizer.MEMO_RULE_UNKNOWN ) + { + //System.out.println("rule "+ruleIndex+" missed @ "+input.index()); + numMemoizationCacheMisses++; + numGuessingRuleInvocations++; // we'll have to enter + } + else + { + // regardless of rule success/failure, if in cache, we have a cache hit + //System.out.println("rule "+ruleIndex+" hit @ "+input.index()); + numMemoizationCacheHits++; + } + } + + public virtual void Memoize( IIntStream input, + int ruleIndex, + int ruleStartIndex, + string ruleName ) + { + // count how many entries go into table + //System.out.println("memoize "+ruleName); + numMemoizationCacheEntries++; + } + + public override void ExitRule( string grammarFileName, string ruleName ) + { + ruleLevel--; + } + + public override void EnterDecision( int decisionNumber ) + { + decisionLevel++; + int startingLookaheadIndex = parser.TokenStream.Index; + //System.out.println("enterDecision "+decisionNumber+" @ index "+startingLookaheadIndex); + lookaheadStack.Push( startingLookaheadIndex ); + } + + public override void ExitDecision( int decisionNumber ) + { + //System.out.println("exitDecision "+decisionNumber); + // track how many of acyclic, cyclic here as we don't know what kind + // yet in enterDecision event. + if ( parser.isCyclicDecision ) + { + numCyclicDecisions++; + } + else + { + numFixedDecisions++; + } + lookaheadStack.Pop(); // pop lookahead depth counter + decisionLevel--; + if ( parser.isCyclicDecision ) + { + if ( numCyclicDecisions >= decisionMaxCyclicLookaheads.Length ) + { + int[] bigger = new int[decisionMaxCyclicLookaheads.Length * 2]; + Array.Copy( decisionMaxCyclicLookaheads, bigger, decisionMaxCyclicLookaheads.Length ); + decisionMaxCyclicLookaheads = bigger; + } + decisionMaxCyclicLookaheads[numCyclicDecisions - 1] = maxLookaheadInCurrentDecision; + } + else + { + if ( numFixedDecisions >= decisionMaxFixedLookaheads.Length ) + { + int[] bigger = new int[decisionMaxFixedLookaheads.Length * 2]; + Array.Copy( decisionMaxFixedLookaheads, bigger, decisionMaxFixedLookaheads.Length ); + decisionMaxFixedLookaheads = bigger; + } + decisionMaxFixedLookaheads[numFixedDecisions - 1] = maxLookaheadInCurrentDecision; + } + parser.isCyclicDecision = false; // can't nest so just reset to false + maxLookaheadInCurrentDecision = 0; + } + + public override void ConsumeToken( IToken token ) + { + //System.out.println("consume token "+token); + lastTokenConsumed = (CommonToken)token; + } + + /** <summary> + * The parser is in a decision if the decision depth > 0. This + * works for backtracking also, which can have nested decisions. + * </summary> + */ + public virtual bool InDecision + { + get + { + return decisionLevel > 0; + } + } + + public override void ConsumeHiddenToken( IToken token ) + { + //System.out.println("consume hidden token "+token); + lastTokenConsumed = (CommonToken)token; + } + + /** <summary>Track refs to lookahead if in a fixed/nonfixed decision.</summary> */ + public override void LT( int i, IToken t ) + { + if ( InDecision ) + { + // get starting index off stack + int startingIndex = lookaheadStack.Peek(); + // compute lookahead depth + int thisRefIndex = parser.TokenStream.Index; + int numHidden = GetNumberOfHiddenTokens( startingIndex, thisRefIndex ); + int depth = i + thisRefIndex - startingIndex - numHidden; + /* + System.out.println("LT("+i+") @ index "+thisRefIndex+" is depth "+depth+ + " max is "+maxLookaheadInCurrentDecision); + */ + if ( depth > maxLookaheadInCurrentDecision ) + { + maxLookaheadInCurrentDecision = depth; + } + } + } + + /** <summary> + * Track backtracking decisions. You'll see a fixed or cyclic decision + * and then a backtrack. + * </summary> + * + * <remarks> + * enter rule + * ... + * enter decision + * LA and possibly consumes (for cyclic DFAs) + * begin backtrack level + * mark m + * rewind m + * end backtrack level, success + * exit decision + * ... + * exit rule + * </remarks> + */ + public override void BeginBacktrack( int level ) + { + //System.out.println("enter backtrack "+level); + numBacktrackDecisions++; + } + + /** <summary>Successful or not, track how much lookahead synpreds use</summary> */ + public override void EndBacktrack( int level, bool successful ) + { + //System.out.println("exit backtrack "+level+": "+successful); + decisionMaxSynPredLookaheads.Add( + maxLookaheadInCurrentDecision + ); + } + +#if false + public void mark( int marker ) + { + int i = parser.TokenStream.Index; + JSystem.@out.println( "mark @ index " + i ); + synPredLookaheadStack.Push( i ); + } + + public void rewind( int marker ) + { + // pop starting index off stack + int startingIndex = synPredLookaheadStack.Pop(); + // compute lookahead depth + int stopIndex = parser.TokenStream.Index; + JSystem.@out.println( "rewind @ index " + stopIndex ); + int depth = stopIndex - startingIndex; + JSystem.@out.println( "depth of lookahead for synpred: " + depth ); + decisionMaxSynPredLookaheads.Add( depth ); + } +#endif + + public override void RecognitionException( RecognitionException e ) + { + numberReportedErrors++; + } + + public override void SemanticPredicate( bool result, string predicate ) + { + if ( InDecision ) + { + numSemanticPredicates++; + } + } + + public override void Terminate() + { + string stats = ToNotifyString(); + try + { + Stats.WriteReport( RUNTIME_STATS_FILENAME, stats ); + } + catch ( IOException ioe ) + { + Console.Error.WriteLine( ioe ); + ioe.PrintStackTrace( Console.Error ); + } + Console.Out.WriteLine( ToString( stats ) ); + } + + public virtual void SetParser( DebugParser parser ) + { + this.parser = parser; + } + + #region Reporting + + public virtual string ToNotifyString() + { + ITokenStream input = parser.TokenStream; + for ( int i = 0; i < input.Size() && lastTokenConsumed != null && i <= lastTokenConsumed.TokenIndex; i++ ) + { + IToken t = input.Get( i ); + if ( t.Channel != TokenConstants.DEFAULT_CHANNEL ) + { + numHiddenTokens++; + numHiddenCharsMatched += t.Text.Length; + } + } + numCharsMatched = lastTokenConsumed.StopIndex + 1; + decisionMaxFixedLookaheads = Trim( decisionMaxFixedLookaheads, numFixedDecisions ); + decisionMaxCyclicLookaheads = Trim( decisionMaxCyclicLookaheads, numCyclicDecisions ); + StringBuilder buf = new StringBuilder(); + buf.Append( Version ); + buf.Append( '\t' ); + buf.Append( parser.GetType().Name ); + buf.Append( '\t' ); + buf.Append( numRuleInvocations ); + buf.Append( '\t' ); + buf.Append( maxRuleInvocationDepth ); + buf.Append( '\t' ); + buf.Append( numFixedDecisions ); + buf.Append( '\t' ); + buf.Append( decisionMaxFixedLookaheads.DefaultIfEmpty( int.MaxValue ).Min() ); + buf.Append( '\t' ); + buf.Append( decisionMaxFixedLookaheads.DefaultIfEmpty( int.MinValue ).Max() ); + buf.Append( '\t' ); + buf.Append( decisionMaxFixedLookaheads.DefaultIfEmpty( 0 ).Average() ); + buf.Append( '\t' ); + buf.Append( Stats.Stddev( decisionMaxFixedLookaheads ) ); + buf.Append( '\t' ); + buf.Append( numCyclicDecisions ); + buf.Append( '\t' ); + buf.Append( decisionMaxCyclicLookaheads.DefaultIfEmpty( int.MaxValue ).Min() ); + buf.Append( '\t' ); + buf.Append( decisionMaxCyclicLookaheads.DefaultIfEmpty( int.MinValue ).Max() ); + buf.Append( '\t' ); + buf.Append( decisionMaxCyclicLookaheads.DefaultIfEmpty( 0 ).Average() ); + buf.Append( '\t' ); + buf.Append( Stats.Stddev( decisionMaxCyclicLookaheads ) ); + buf.Append( '\t' ); + buf.Append( numBacktrackDecisions ); + buf.Append( '\t' ); + buf.Append( decisionMaxSynPredLookaheads.DefaultIfEmpty( int.MaxValue ).Min() ); + buf.Append( '\t' ); + buf.Append( decisionMaxSynPredLookaheads.DefaultIfEmpty( int.MinValue ).Max() ); + buf.Append( '\t' ); + buf.Append( decisionMaxSynPredLookaheads.DefaultIfEmpty( 0 ).Average() ); + buf.Append( '\t' ); + buf.Append( Stats.Stddev( decisionMaxSynPredLookaheads ) ); + buf.Append( '\t' ); + buf.Append( numSemanticPredicates ); + buf.Append( '\t' ); + buf.Append( parser.TokenStream.Size() ); + buf.Append( '\t' ); + buf.Append( numHiddenTokens ); + buf.Append( '\t' ); + buf.Append( numCharsMatched ); + buf.Append( '\t' ); + buf.Append( numHiddenCharsMatched ); + buf.Append( '\t' ); + buf.Append( numberReportedErrors ); + buf.Append( '\t' ); + buf.Append( numMemoizationCacheHits ); + buf.Append( '\t' ); + buf.Append( numMemoizationCacheMisses ); + buf.Append( '\t' ); + buf.Append( numGuessingRuleInvocations ); + buf.Append( '\t' ); + buf.Append( numMemoizationCacheEntries ); + return buf.ToString(); + } + + public override string ToString() + { + return ToString( ToNotifyString() ); + } + + protected static string[] DecodeReportData( string data ) + { + string[] fields = new string[NUM_RUNTIME_STATS]; + StringTokenizer st = new StringTokenizer( data, "\t" ); + int i = 0; + while ( st.hasMoreTokens() ) + { + fields[i] = st.nextToken(); + i++; + } + if ( i != NUM_RUNTIME_STATS ) + { + return null; + } + return fields; + } + + public static string ToString( string notifyDataLine ) + { + string[] fields = DecodeReportData( notifyDataLine ); + if ( fields == null ) + { + return null; + } + StringBuilder buf = new StringBuilder(); + buf.Append( "ANTLR Runtime Report; Profile Version " ); + buf.Append( fields[0] ); + buf.Append( '\n' ); + buf.Append( "parser name " ); + buf.Append( fields[1] ); + buf.Append( '\n' ); + buf.Append( "Number of rule invocations " ); + buf.Append( fields[2] ); + buf.Append( '\n' ); + buf.Append( "Number of rule invocations in \"guessing\" mode " ); + buf.Append( fields[27] ); + buf.Append( '\n' ); + buf.Append( "max rule invocation nesting depth " ); + buf.Append( fields[3] ); + buf.Append( '\n' ); + buf.Append( "number of fixed lookahead decisions " ); + buf.Append( fields[4] ); + buf.Append( '\n' ); + buf.Append( "min lookahead used in a fixed lookahead decision " ); + buf.Append( fields[5] ); + buf.Append( '\n' ); + buf.Append( "max lookahead used in a fixed lookahead decision " ); + buf.Append( fields[6] ); + buf.Append( '\n' ); + buf.Append( "average lookahead depth used in fixed lookahead decisions " ); + buf.Append( fields[7] ); + buf.Append( '\n' ); + buf.Append( "standard deviation of depth used in fixed lookahead decisions " ); + buf.Append( fields[8] ); + buf.Append( '\n' ); + buf.Append( "number of arbitrary lookahead decisions " ); + buf.Append( fields[9] ); + buf.Append( '\n' ); + buf.Append( "min lookahead used in an arbitrary lookahead decision " ); + buf.Append( fields[10] ); + buf.Append( '\n' ); + buf.Append( "max lookahead used in an arbitrary lookahead decision " ); + buf.Append( fields[11] ); + buf.Append( '\n' ); + buf.Append( "average lookahead depth used in arbitrary lookahead decisions " ); + buf.Append( fields[12] ); + buf.Append( '\n' ); + buf.Append( "standard deviation of depth used in arbitrary lookahead decisions " ); + buf.Append( fields[13] ); + buf.Append( '\n' ); + buf.Append( "number of evaluated syntactic predicates " ); + buf.Append( fields[14] ); + buf.Append( '\n' ); + buf.Append( "min lookahead used in a syntactic predicate " ); + buf.Append( fields[15] ); + buf.Append( '\n' ); + buf.Append( "max lookahead used in a syntactic predicate " ); + buf.Append( fields[16] ); + buf.Append( '\n' ); + buf.Append( "average lookahead depth used in syntactic predicates " ); + buf.Append( fields[17] ); + buf.Append( '\n' ); + buf.Append( "standard deviation of depth used in syntactic predicates " ); + buf.Append( fields[18] ); + buf.Append( '\n' ); + buf.Append( "rule memoization cache size " ); + buf.Append( fields[28] ); + buf.Append( '\n' ); + buf.Append( "number of rule memoization cache hits " ); + buf.Append( fields[25] ); + buf.Append( '\n' ); + buf.Append( "number of rule memoization cache misses " ); + buf.Append( fields[26] ); + buf.Append( '\n' ); + buf.Append( "number of evaluated semantic predicates " ); + buf.Append( fields[19] ); + buf.Append( '\n' ); + buf.Append( "number of tokens " ); + buf.Append( fields[20] ); + buf.Append( '\n' ); + buf.Append( "number of hidden tokens " ); + buf.Append( fields[21] ); + buf.Append( '\n' ); + buf.Append( "number of char " ); + buf.Append( fields[22] ); + buf.Append( '\n' ); + buf.Append( "number of hidden char " ); + buf.Append( fields[23] ); + buf.Append( '\n' ); + buf.Append( "number of syntax errors " ); + buf.Append( fields[24] ); + buf.Append( '\n' ); + return buf.ToString(); + } + + #endregion + + protected virtual int[] Trim( int[] X, int n ) + { + if ( n < X.Length ) + { + int[] trimmed = new int[n]; + Array.Copy( X, trimmed, n ); + X = trimmed; + } + return X; + } + + /** <summary>Get num hidden tokens between i..j inclusive</summary> */ + public virtual int GetNumberOfHiddenTokens( int i, int j ) + { + int n = 0; + ITokenStream input = parser.TokenStream; + for ( int ti = i; ti < input.Size() && ti <= j; ti++ ) + { + IToken t = input.Get( ti ); + if ( t.Channel != TokenConstants.DEFAULT_CHANNEL ) + { + n++; + } + } + return n; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Properties/AssemblyInfo.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..d01e826 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Properties/AssemblyInfo.cs @@ -0,0 +1,68 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle( "Antlr3.Runtime.Debug" )] +[assembly: AssemblyDescription( "" )] +[assembly: AssemblyConfiguration( "" )] +[assembly: AssemblyCompany( "Pixel Mine, Inc." )] +[assembly: AssemblyProduct( "Antlr3.Runtime.Debug" )] +[assembly: AssemblyCopyright( "Copyright © Pixel Mine 2008" )] +[assembly: AssemblyTrademark( "" )] +[assembly: AssemblyCulture( "" )] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible( false )] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid( "9f8fa018-6766-404c-9e72-551407e1b173" )] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion( "1.0.0.0" )] +[assembly: AssemblyFileVersion( "1.0.0.0" )] diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/RemoteDebugEventSocketListener.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/RemoteDebugEventSocketListener.cs new file mode 100644 index 0000000..7d7decc --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/RemoteDebugEventSocketListener.cs @@ -0,0 +1,728 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + using Antlr.Runtime.JavaExtensions; + + using BaseTree = Antlr.Runtime.Tree.BaseTree; + using Console = System.Console; + using SocketException = System.Net.Sockets.SocketException; + using Exception = System.Exception; + using IOException = System.IO.IOException; + using Socket = System.Net.Sockets.Socket; + using ITree = Antlr.Runtime.Tree.ITree; + + using TextWriter = System.IO.TextWriter; + using TextReader = System.IO.TextReader; + + public class RemoteDebugEventSocketListener + { + const int MAX_EVENT_ELEMENTS = 8; + IDebugEventListener listener; + string machine; + int port; + Socket channel = null; + TextWriter @out; + TextReader @in; + string @event; + /** <summary>Version of ANTLR (dictates events)</summary> */ + public string version; + public string grammarFileName; + /** <summary> + * Track the last token index we saw during a consume. If same, then + * set a flag that we have a problem. + * </summary> + */ + int previousTokenIndex = -1; + bool tokenIndexesInvalid = false; + + public class ProxyToken : IToken + { + int index; + int type; + int channel; + int line; + int charPos; + string text; + public ProxyToken( int index ) + { + this.index = index; + } + public ProxyToken( int index, int type, int channel, + int line, int charPos, string text ) + { + this.index = index; + this.type = type; + this.channel = channel; + this.line = line; + this.charPos = charPos; + this.text = text; + } + + #region IToken Members + public string Text + { + get + { + return text; + } + set + { + text = value; + } + } + + public int Type + { + get + { + return type; + } + set + { + type = value; + } + } + + public int Line + { + get + { + return line; + } + set + { + line = value; + } + } + + public int CharPositionInLine + { + get + { + return charPos; + } + set + { + charPos = value; + } + } + + public int Channel + { + get + { + return channel; + } + set + { + channel = value; + } + } + + public int TokenIndex + { + get + { + return index; + } + set + { + index = value; + } + } + + public ICharStream InputStream + { + get + { + return null; + } + set + { + } + } + + #endregion + + public override string ToString() + { + string channelStr = ""; + if ( channel != TokenConstants.DEFAULT_CHANNEL ) + { + channelStr = ",channel=" + channel; + } + return "[" + Text + "/<" + type + ">" + channelStr + "," + line + ":" + CharPositionInLine + ",@" + index + "]"; + } + } + + public class ProxyTree : BaseTree + { + public int ID; + public int type; + public int line = 0; + public int charPos = -1; + public int tokenIndex = -1; + public string text; + + public ProxyTree( int ID, int type, int line, int charPos, int tokenIndex, string text ) + { + this.ID = ID; + this.type = type; + this.line = line; + this.charPos = charPos; + this.tokenIndex = tokenIndex; + this.text = text; + } + + public ProxyTree( int ID ) + { + this.ID = ID; + } + + #region Properties + public override string Text + { + get + { + return text; + } + set + { + } + } + public override int TokenStartIndex + { + get + { + return tokenIndex; + } + set + { + } + } + public override int TokenStopIndex + { + get + { + return 0; + } + set + { + } + } + public override int Type + { + get + { + return type; + } + set + { + } + } + #endregion + + public override ITree DupNode() + { + return null; + } + + public override string ToString() + { + return "fix this"; + } + } + + public RemoteDebugEventSocketListener( IDebugEventListener listener, + string machine, + int port ) + { + this.listener = listener; + this.machine = machine; + this.port = port; + + if ( !OpenConnection() ) + { + throw new SocketException(); + } + } + + protected virtual void EventHandler() + { + try + { + Handshake(); + @event = @in.ReadLine(); + while ( @event != null ) + { + Dispatch( @event ); + Ack(); + @event = @in.ReadLine(); + } + } + catch ( Exception e ) + { + Console.Error.WriteLine( e ); + e.PrintStackTrace( Console.Error ); + } + finally + { + CloseConnection(); + } + } + + protected virtual bool OpenConnection() + { + bool success = false; + try + { + throw new System.NotImplementedException(); + //channel = new Socket( machine, port ); + //channel.setTcpNoDelay( true ); + //OutputStream os = channel.getOutputStream(); + //OutputStreamWriter osw = new OutputStreamWriter( os, "UTF8" ); + //@out = new PrintWriter( new BufferedWriter( osw ) ); + //InputStream @is = channel.getInputStream(); + //InputStreamReader isr = new InputStreamReader( @is, "UTF8" ); + //@in = new BufferedReader( isr ); + //success = true; + } + catch ( Exception e ) + { + Console.Error.WriteLine( e ); + } + return success; + } + + protected virtual void CloseConnection() + { + try + { + @in.Close(); + @in = null; + @out.Close(); + @out = null; + channel.Close(); + channel = null; + } + catch ( Exception e ) + { + Console.Error.WriteLine( e ); + e.PrintStackTrace( Console.Error ); + } + finally + { + if ( @in != null ) + { + try + { + @in.Close(); + } + catch ( IOException ioe ) + { + Console.Error.WriteLine( ioe ); + } + } + if ( @out != null ) + { + @out.Close(); + } + if ( channel != null ) + { + try + { + channel.Close(); + } + catch ( IOException ioe ) + { + Console.Error.WriteLine( ioe ); + } + } + } + + } + + protected virtual void Handshake() + { + string antlrLine = @in.ReadLine(); + string[] antlrElements = GetEventElements( antlrLine ); + version = antlrElements[1]; + string grammarLine = @in.ReadLine(); + string[] grammarElements = GetEventElements( grammarLine ); + grammarFileName = grammarElements[1]; + Ack(); + listener.Commence(); // inform listener after handshake + } + + protected virtual void Ack() + { + @out.WriteLine( "ack" ); + @out.Flush(); + } + + protected virtual void Dispatch( string line ) + { + //JSystem.@out.println( "event: " + line ); + string[] elements = GetEventElements( line ); + if ( elements == null || elements[0] == null ) + { + Console.Error.WriteLine( "unknown debug event: " + line ); + return; + } + if ( elements[0].Equals( "enterRule" ) ) + { + listener.EnterRule( elements[1], elements[2] ); + } + else if ( elements[0].Equals( "exitRule" ) ) + { + listener.ExitRule( elements[1], elements[2] ); + } + else if ( elements[0].Equals( "enterAlt" ) ) + { + listener.EnterAlt( int.Parse( elements[1] ) ); + } + else if ( elements[0].Equals( "enterSubRule" ) ) + { + listener.EnterSubRule( int.Parse( elements[1] ) ); + } + else if ( elements[0].Equals( "exitSubRule" ) ) + { + listener.ExitSubRule( int.Parse( elements[1] ) ); + } + else if ( elements[0].Equals( "enterDecision" ) ) + { + listener.EnterDecision( int.Parse( elements[1] ) ); + } + else if ( elements[0].Equals( "exitDecision" ) ) + { + listener.ExitDecision( int.Parse( elements[1] ) ); + } + else if ( elements[0].Equals( "location" ) ) + { + listener.Location( int.Parse( elements[1] ), + int.Parse( elements[2] ) ); + } + else if ( elements[0].Equals( "consumeToken" ) ) + { + ProxyToken t = DeserializeToken( elements, 1 ); + if ( t.TokenIndex == previousTokenIndex ) + { + tokenIndexesInvalid = true; + } + previousTokenIndex = t.TokenIndex; + listener.ConsumeToken( t ); + } + else if ( elements[0].Equals( "consumeHiddenToken" ) ) + { + ProxyToken t = DeserializeToken( elements, 1 ); + if ( t.TokenIndex == previousTokenIndex ) + { + tokenIndexesInvalid = true; + } + previousTokenIndex = t.TokenIndex; + listener.ConsumeHiddenToken( t ); + } + else if ( elements[0].Equals( "LT" ) ) + { + IToken t = DeserializeToken( elements, 2 ); + listener.LT( int.Parse( elements[1] ), t ); + } + else if ( elements[0].Equals( "mark" ) ) + { + listener.Mark( int.Parse( elements[1] ) ); + } + else if ( elements[0].Equals( "rewind" ) ) + { + if ( elements[1] != null ) + { + listener.Rewind( int.Parse( elements[1] ) ); + } + else + { + listener.Rewind(); + } + } + else if ( elements[0].Equals( "beginBacktrack" ) ) + { + listener.BeginBacktrack( int.Parse( elements[1] ) ); + } + else if ( elements[0].Equals( "endBacktrack" ) ) + { + int level = int.Parse( elements[1] ); + int successI = int.Parse( elements[2] ); + listener.EndBacktrack( level, successI == DebugEventListenerConstants.True ); + } + else if ( elements[0].Equals( "exception" ) ) + { +#if true + throw new System.NotImplementedException(); +#else + string excName = elements[1]; + string indexS = elements[2]; + string lineS = elements[3]; + string posS = elements[4]; + Class excClass = null; + try + { + excClass = Class.forName( excName ); + RecognitionException e = + (RecognitionException)excClass.newInstance(); + e.index = int.Parse( indexS ); + e.line = int.Parse( lineS ); + e.charPositionInLine = int.Parse( posS ); + listener.recognitionException( e ); + } + catch ( ClassNotFoundException cnfe ) + { + Console.Error.println( "can't find class " + cnfe ); + cnfe.printStackTrace( Console.Error ); + } + catch ( InstantiationException ie ) + { + Console.Error.println( "can't instantiate class " + ie ); + ie.printStackTrace( Console.Error ); + } + catch ( IllegalAccessException iae ) + { + Console.Error.println( "can't access class " + iae ); + iae.printStackTrace( Console.Error ); + } +#endif + } + else if ( elements[0].Equals( "beginResync" ) ) + { + listener.BeginResync(); + } + else if ( elements[0].Equals( "endResync" ) ) + { + listener.EndResync(); + } + else if ( elements[0].Equals( "terminate" ) ) + { + listener.Terminate(); + } + else if ( elements[0].Equals( "semanticPredicate" ) ) + { + bool result = bool.Parse( elements[1] ); + string predicateText = elements[2]; + predicateText = UnEscapeNewlines( predicateText ); + listener.SemanticPredicate( result, + predicateText ); + } + else if ( elements[0].Equals( "consumeNode" ) ) + { + ProxyTree node = DeserializeNode( elements, 1 ); + listener.ConsumeNode( node ); + } + else if ( elements[0].Equals( "LN" ) ) + { + int i = int.Parse( elements[1] ); + ProxyTree node = DeserializeNode( elements, 2 ); + listener.LT( i, node ); + } + else if ( elements[0].Equals( "createNodeFromTokenElements" ) ) + { + int ID = int.Parse( elements[1] ); + int type = int.Parse( elements[2] ); + string text = elements[3]; + text = UnEscapeNewlines( text ); + ProxyTree node = new ProxyTree( ID, type, -1, -1, -1, text ); + listener.CreateNode( node ); + } + else if ( elements[0].Equals( "createNode" ) ) + { + int ID = int.Parse( elements[1] ); + int tokenIndex = int.Parse( elements[2] ); + // create dummy node/token filled with ID, tokenIndex + ProxyTree node = new ProxyTree( ID ); + ProxyToken token = new ProxyToken( tokenIndex ); + listener.CreateNode( node, token ); + } + else if ( elements[0].Equals( "nilNode" ) ) + { + int ID = int.Parse( elements[1] ); + ProxyTree node = new ProxyTree( ID ); + listener.NilNode( node ); + } + else if ( elements[0].Equals( "errorNode" ) ) + { + // TODO: do we need a special tree here? + int ID = int.Parse( elements[1] ); + int type = int.Parse( elements[2] ); + string text = elements[3]; + text = UnEscapeNewlines( text ); + ProxyTree node = new ProxyTree( ID, type, -1, -1, -1, text ); + listener.ErrorNode( node ); + } + else if ( elements[0].Equals( "becomeRoot" ) ) + { + int newRootID = int.Parse( elements[1] ); + int oldRootID = int.Parse( elements[2] ); + ProxyTree newRoot = new ProxyTree( newRootID ); + ProxyTree oldRoot = new ProxyTree( oldRootID ); + listener.BecomeRoot( newRoot, oldRoot ); + } + else if ( elements[0].Equals( "addChild" ) ) + { + int rootID = int.Parse( elements[1] ); + int childID = int.Parse( elements[2] ); + ProxyTree root = new ProxyTree( rootID ); + ProxyTree child = new ProxyTree( childID ); + listener.AddChild( root, child ); + } + else if ( elements[0].Equals( "setTokenBoundaries" ) ) + { + int ID = int.Parse( elements[1] ); + ProxyTree node = new ProxyTree( ID ); + listener.SetTokenBoundaries( + node, + int.Parse( elements[2] ), + int.Parse( elements[3] ) ); + } + else + { + Console.Error.WriteLine( "unknown debug event: " + line ); + } + } + + protected virtual ProxyTree DeserializeNode( string[] elements, int offset ) + { + int ID = int.Parse( elements[offset + 0] ); + int type = int.Parse( elements[offset + 1] ); + int tokenLine = int.Parse( elements[offset + 2] ); + int charPositionInLine = int.Parse( elements[offset + 3] ); + int tokenIndex = int.Parse( elements[offset + 4] ); + string text = elements[offset + 5]; + text = UnEscapeNewlines( text ); + return new ProxyTree( ID, type, tokenLine, charPositionInLine, tokenIndex, text ); + } + + protected virtual ProxyToken DeserializeToken( string[] elements, + int offset ) + { + string indexS = elements[offset + 0]; + string typeS = elements[offset + 1]; + string channelS = elements[offset + 2]; + string lineS = elements[offset + 3]; + string posS = elements[offset + 4]; + string text = elements[offset + 5]; + text = UnEscapeNewlines( text ); + int index = int.Parse( indexS ); + ProxyToken t = + new ProxyToken( index, + int.Parse( typeS ), + int.Parse( channelS ), + int.Parse( lineS ), + int.Parse( posS ), + text ); + return t; + } + + /** <summary>Create a thread to listen to the remote running recognizer</summary> */ + public virtual void Start() + { + System.Threading.Thread t = new System.Threading.Thread( Run ); + t.Start(); + } + + public virtual void Run() + { + EventHandler(); + } + + #region Misc + + public virtual string[] GetEventElements( string @event ) + { + if ( @event == null ) + { + return null; + } + string[] elements = new string[MAX_EVENT_ELEMENTS]; + string str = null; // a string element if present (must be last) + try + { + int firstQuoteIndex = @event.IndexOf( '"' ); + if ( firstQuoteIndex >= 0 ) + { + // treat specially; has a string argument like "a comment\n + // Note that the string is terminated by \n not end quote. + // Easier to parse that way. + string eventWithoutString = @event.Substring( 0, firstQuoteIndex ); + str = @event.Substring( firstQuoteIndex + 1 ); + @event = eventWithoutString; + } + StringTokenizer st = new StringTokenizer( @event, "\t", false ); + int i = 0; + while ( st.hasMoreTokens() ) + { + if ( i >= MAX_EVENT_ELEMENTS ) + { + // ErrorManager.internalError("event has more than "+MAX_EVENT_ELEMENTS+" args: "+event); + return elements; + } + elements[i] = st.nextToken(); + i++; + } + if ( str != null ) + { + elements[i] = str; + } + } + catch ( Exception e ) + { + e.PrintStackTrace( Console.Error ); + } + return elements; + } + + protected virtual string UnEscapeNewlines( string txt ) + { + // this unescape is slow but easy to understand + txt = txt.replaceAll( "%0A", "\n" ); // unescape \n + txt = txt.replaceAll( "%0D", "\r" ); // unescape \r + txt = txt.replaceAll( "%25", "%" ); // undo escaped escape chars + return txt; + } + + public virtual bool TokenIndexesAreInvalid() + { + return false; + //return tokenIndexesInvalid; + } + + #endregion + + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/TraceDebugEventListener.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/TraceDebugEventListener.cs new file mode 100644 index 0000000..c71da4f --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/TraceDebugEventListener.cs @@ -0,0 +1,135 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + using Antlr.Runtime.JavaExtensions; + + using Console = System.Console; + using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; + + /** <summary>Print out (most of) the events... Useful for debugging, testing...</summary> */ + public class TraceDebugEventListener : BlankDebugEventListener + { + ITreeAdaptor adaptor; + + public TraceDebugEventListener( ITreeAdaptor adaptor ) + { + this.adaptor = adaptor; + } + + public void EnterRule( string ruleName ) + { + Console.Out.WriteLine( "enterRule " + ruleName ); + } + public void ExitRule( string ruleName ) + { + Console.Out.WriteLine( "exitRule " + ruleName ); + } + public override void EnterSubRule( int decisionNumber ) + { + Console.Out.WriteLine( "enterSubRule" ); + } + public override void ExitSubRule( int decisionNumber ) + { + Console.Out.WriteLine( "exitSubRule" ); + } + public override void Location( int line, int pos ) + { + Console.Out.WriteLine( "location " + line + ":" + pos ); + } + + #region Tree parsing stuff + + public override void ConsumeNode( object t ) + { + int ID = adaptor.GetUniqueID( t ); + string text = adaptor.GetText( t ); + int type = adaptor.GetType( t ); + Console.Out.WriteLine( "consumeNode " + ID + " " + text + " " + type ); + } + + public override void LT( int i, object t ) + { + int ID = adaptor.GetUniqueID( t ); + string text = adaptor.GetText( t ); + int type = adaptor.GetType( t ); + Console.Out.WriteLine( "LT " + i + " " + ID + " " + text + " " + type ); + } + + #endregion + + + #region AST stuff + + public override void NilNode( object t ) + { + Console.Out.WriteLine( "nilNode " + adaptor.GetUniqueID( t ) ); + } + + public override void CreateNode( object t ) + { + int ID = adaptor.GetUniqueID( t ); + string text = adaptor.GetText( t ); + int type = adaptor.GetType( t ); + Console.Out.WriteLine( "create " + ID + ": " + text + ", " + type ); + } + + public override void CreateNode( object node, IToken token ) + { + int ID = adaptor.GetUniqueID( node ); + string text = adaptor.GetText( node ); + int tokenIndex = token.TokenIndex; + Console.Out.WriteLine( "create " + ID + ": " + tokenIndex ); + } + + public override void BecomeRoot( object newRoot, object oldRoot ) + { + Console.Out.WriteLine( "becomeRoot " + adaptor.GetUniqueID( newRoot ) + ", " + + adaptor.GetUniqueID( oldRoot ) ); + } + + public override void AddChild( object root, object child ) + { + Console.Out.WriteLine( "addChild " + adaptor.GetUniqueID( root ) + ", " + + adaptor.GetUniqueID( child ) ); + } + + public override void SetTokenBoundaries( object t, int tokenStartIndex, int tokenStopIndex ) + { + Console.Out.WriteLine( "setTokenBoundaries " + adaptor.GetUniqueID( t ) + ", " + + tokenStartIndex + ", " + tokenStopIndex ); + } + + #endregion + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Tracer.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Tracer.cs new file mode 100644 index 0000000..c99d4f7 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Tracer.cs @@ -0,0 +1,85 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + using Antlr.Runtime.JavaExtensions; + + using Console = System.Console; + + /** <summary> + * The default tracer mimics the traceParser behavior of ANTLR 2.x. + * This listens for debugging events from the parser and implies + * that you cannot debug and trace at the same time. + * </summary> + */ + public class Tracer : BlankDebugEventListener + { + public IIntStream input; + protected int level = 0; + + public Tracer( IIntStream input ) + { + this.input = input; + } + + public virtual void EnterRule( string ruleName ) + { + for ( int i = 1; i <= level; i++ ) + { + Console.Out.Write( " " ); + } + Console.Out.WriteLine( "> " + ruleName + " lookahead(1)=" + GetInputSymbol( 1 ) ); + level++; + } + + public virtual void ExitRule( string ruleName ) + { + level--; + for ( int i = 1; i <= level; i++ ) + { + Console.Out.Write( " " ); + } + Console.Out.WriteLine( "< " + ruleName + " lookahead(1)=" + GetInputSymbol( 1 ) ); + } + + public virtual object GetInputSymbol( int k ) + { + if ( input is ITokenStream ) + { + return ( (ITokenStream)input ).LT( k ); + } + return (char)input.LA( k ); + } + } +} + diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Antlr3.Runtime.JavaExtensions.csproj b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Antlr3.Runtime.JavaExtensions.csproj new file mode 100644 index 0000000..1b4a177 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Antlr3.Runtime.JavaExtensions.csproj @@ -0,0 +1,72 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProductVersion>9.0.30729</ProductVersion> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{A7EEC557-EB14-451C-9616-B7A61F4ECE69}</ProjectGuid> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>Antlr3.Runtime.JavaExtensions</RootNamespace> + <AssemblyName>Antlr3.Runtime.JavaExtensions</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + <SccProjectName>SAK</SccProjectName> + <SccLocalPath>SAK</SccLocalPath> + <SccAuxPath>SAK</SccAuxPath> + <SccProvider>SAK</SccProvider> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + <Reference Include="System.Core"> + <RequiredTargetFramework>3.5</RequiredTargetFramework> + </Reference> + <Reference Include="System.Xml.Linq"> + <RequiredTargetFramework>3.5</RequiredTargetFramework> + </Reference> + <Reference Include="System.Data.DataSetExtensions"> + <RequiredTargetFramework>3.5</RequiredTargetFramework> + </Reference> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="DictionaryExtensions.cs" /> + <Compile Include="IOExtensions.cs" /> + <Compile Include="LexerExtensions.cs" /> + <Compile Include="JSystem.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Antlr3.Runtime\Antlr3.Runtime.csproj"> + <Project>{8FDC0A87-9005-4D5A-AB75-E55CEB575559}</Project> + <Name>Antlr3.Runtime</Name> + </ProjectReference> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Antlr3.Runtime.JavaExtensions.csproj.vspscc b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Antlr3.Runtime.JavaExtensions.csproj.vspscc new file mode 100644 index 0000000..b27adcc --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Antlr3.Runtime.JavaExtensions.csproj.vspscc @@ -0,0 +1,10 @@ +"" +{ +"FILE_VERSION" = "9237" +"ENLISTMENT_CHOICE" = "NEVER" +"PROJECT_FILE_RELATIVE_PATH" = "relative:antlr\\main\\runtime\\CSharp3\\Sources\\Antlr3.Runtime.JavaExtensions" +"NUMBER_OF_EXCLUDED_FILES" = "0" +"ORIGINAL_PROJECT_FILE_PATH" = "" +"NUMBER_OF_NESTED_PROJECTS" = "0" +"SOURCE_CONTROL_SETTINGS_PROVIDER" = "PROVIDER" +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/DictionaryExtensions.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/DictionaryExtensions.cs new file mode 100644 index 0000000..f2f8f84 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/DictionaryExtensions.cs @@ -0,0 +1,102 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +#if !DEBUG + +using System; +using System.Collections.Generic; +using System.Linq; + +using IDictionary = System.Collections.IDictionary; + +namespace Antlr.Runtime.JavaExtensions +{ + public static class DictionaryExtensions2 + { + [Obsolete] + public static bool containsKey( this IDictionary map, object key ) + { + return map.Contains( key ); + } + + [Obsolete] + public static object get( this IDictionary map, object key ) + { + return map[key]; + } + + [Obsolete] + public static void put( this IDictionary map, object key, object value ) + { + map[key] = value; + } + + [Obsolete] + public static void put<TKey, TValue>( this IDictionary<TKey, TValue> map, TKey key, TValue value ) + { + map[key] = value; + } + + [Obsolete] + public static void put<TKey, TValue>( this Dictionary<TKey, TValue> map, TKey key, TValue value ) + { + map[key] = value; + } + + [Obsolete] + public static HashSet<object> keySet( this IDictionary map ) + { + return new HashSet<object>( map.Keys.Cast<object>() ); + } + + [Obsolete] + public static HashSet<TKey> keySet<TKey, TValue>( this IDictionary<TKey, TValue> map ) + { + return new HashSet<TKey>( map.Keys ); + } + + // disambiguates for Dictionary, which implements both IDictionary<T,K> and IDictionary + [Obsolete] + public static HashSet<TKey> keySet<TKey, TValue>( this Dictionary<TKey, TValue> map ) + { + return new HashSet<TKey>( map.Keys ); + } + + [Obsolete] + public static HashSet<object> keySet<TKey, TValue>( this SortedList<TKey, TValue> map ) + { + return new HashSet<object>( map.Keys.Cast<object>() ); + } + } +} + +#endif diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/IOExtensions.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/IOExtensions.cs new file mode 100644 index 0000000..540af55 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/IOExtensions.cs @@ -0,0 +1,94 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +#if !DEBUG + +using System; + +using TextReader = System.IO.TextReader; +using TextWriter = System.IO.TextWriter; + +namespace Antlr.Runtime.JavaExtensions +{ + public static class IOExtensions + { + [Obsolete] + public static void close( this TextReader reader ) + { + reader.Close(); + } + + [Obsolete] + public static void close( this TextWriter writer ) + { + writer.Close(); + } + + [Obsolete] + public static void print<T>( this TextWriter writer, T value ) + { + writer.Write( value ); + } + + [Obsolete] + public static void println( this TextWriter writer ) + { + writer.WriteLine(); + } + + [Obsolete] + public static void println<T>( this TextWriter writer, T value ) + { + writer.WriteLine( value ); + } + + [Obsolete] + public static void write<T>( this TextWriter writer, T value ) + { + writer.Write( value ); + } + + [Obsolete] + public static int read( this TextReader reader, char[] buffer, int index, int count ) + { + return reader.Read( buffer, index, count ); + } + + [Obsolete] + public static string readLine( this TextReader reader ) + { + return reader.ReadLine(); + } + } +} + +#endif diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/JSystem.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/JSystem.cs new file mode 100644 index 0000000..2c27987 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/JSystem.cs @@ -0,0 +1,89 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +#if !DEBUG +namespace Antlr.Runtime.JavaExtensions +{ + using System; + using System.IO; + + public static class JSystem + { + [Obsolete] + public static TextWriter err + { + get + { + return Console.Error; + } + } + + [Obsolete] + public static TextWriter @out + { + get + { + return Console.Out; + } + } + + [Obsolete] + public static void arraycopy<T>( T[] sourceArray, int sourceIndex, T[] destinationArray, int destinationIndex, int length ) + { + Array.Copy( sourceArray, sourceIndex, destinationArray, destinationIndex, length ); + } + + [Obsolete] + public static string getProperty( string name ) + { + switch ( name ) + { + case "file.encoding": + return System.Text.Encoding.Default.WebName; + + case "line.separator": + return Environment.NewLine; + + case "java.io.tmpdir": + return System.IO.Path.GetTempPath(); + + case "user.home": + return Environment.GetFolderPath( Environment.SpecialFolder.Personal ); + + default: + throw new ArgumentException( string.Format( "Unknown system property: '{0}'", name ) ); + } + } + + } +} +#endif diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/LexerExtensions.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/LexerExtensions.cs new file mode 100644 index 0000000..4300b38 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/LexerExtensions.cs @@ -0,0 +1,42 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.JavaExtensions +{ + public static class LexerExtensions + { + public static void skip( this Lexer lexer ) + { + lexer.Skip(); + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Properties/AssemblyInfo.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..1e4eaf8 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/Properties/AssemblyInfo.cs @@ -0,0 +1,68 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle( "Antlr3.Runtime.JavaExtensions" )] +[assembly: AssemblyDescription( "" )] +[assembly: AssemblyConfiguration( "" )] +[assembly: AssemblyCompany( "Pixel Mine, Inc." )] +[assembly: AssemblyProduct( "Antlr3.Runtime.JavaExtensions" )] +[assembly: AssemblyCopyright( "Copyright © Pixel Mine 2009" )] +[assembly: AssemblyTrademark( "" )] +[assembly: AssemblyCulture( "" )] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible( false )] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid( "ad48c7f7-0b1d-4b1e-9602-83425cb5699f" )] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion( "1.0.0.0" )] +[assembly: AssemblyFileVersion( "1.0.0.0" )] diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Antlr3.Runtime.Test.csproj b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Antlr3.Runtime.Test.csproj new file mode 100644 index 0000000..e569407 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Antlr3.Runtime.Test.csproj @@ -0,0 +1,184 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProductVersion>9.0.30729</ProductVersion> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{19B965DE-5100-4064-A580-159644F6980E}</ProjectGuid> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>Antlr3.Runtime.Test</RootNamespace> + <AssemblyName>Antlr3.Runtime.Test</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + <SccProjectName>SAK</SccProjectName> + <SccLocalPath>SAK</SccLocalPath> + <SccAuxPath>SAK</SccAuxPath> + <SccProvider>SAK</SccProvider> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + <Reference Include="System.Core"> + <RequiredTargetFramework>3.5</RequiredTargetFramework> + </Reference> + <Reference Include="System.Xml.Linq"> + <RequiredTargetFramework>3.5</RequiredTargetFramework> + </Reference> + <Reference Include="System.Data.DataSetExtensions"> + <RequiredTargetFramework>3.5</RequiredTargetFramework> + </Reference> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + <Reference Include="vjslib" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Antlr3.Runtime.Debug\Antlr3.Runtime.Debug.csproj"> + <Project>{5EE27A90-B023-42C9-AAF1-52B0424C5D0B}</Project> + <Name>Antlr3.Runtime.Debug</Name> + </ProjectReference> + <ProjectReference Include="..\Antlr3.Runtime.JavaExtensions\Antlr3.Runtime.JavaExtensions.csproj"> + <Project>{A7EEC557-EB14-451C-9616-B7A61F4ECE69}</Project> + <Name>Antlr3.Runtime.JavaExtensions</Name> + </ProjectReference> + <ProjectReference Include="..\Antlr3.Runtime\Antlr3.Runtime.csproj"> + <Project>{8FDC0A87-9005-4D5A-AB75-E55CEB575559}</Project> + <Name>Antlr3.Runtime</Name> + </ProjectReference> + </ItemGroup> + <ItemGroup> + <Antlr3 Include="JavaCompat\Expr.g3"> + <OutputFiles>JavaCompat\ExprLexer.cs;JavaCompat\ExprParser.cs;JavaCompat\Expr.tokens</OutputFiles> + </Antlr3> + <Compile Include="JavaCompat\ExprLexer.cs"> + <AutoGen>True</AutoGen> + <DesignTime>True</DesignTime> + <DependentUpon>Expr.g3</DependentUpon> + </Compile> + <Compile Include="JavaCompat\ExprParser.cs"> + <AutoGen>True</AutoGen> + <DesignTime>True</DesignTime> + <DependentUpon>Expr.g3</DependentUpon> + </Compile> + </ItemGroup> + <ItemGroup> + <Antlr3 Include="BuildOptions\DebugGrammar.g3"> + <GrammarOptions>-debug</GrammarOptions> + <OutputFiles>BuildOptions\DebugGrammarLexer.cs;BuildOptions\DebugGrammarParser.cs;BuildOptions\DebugGrammar.tokens</OutputFiles> + </Antlr3> + <Compile Include="BuildOptions\DebugGrammarLexer.cs"> + <AutoGen>True</AutoGen> + <DesignTime>True</DesignTime> + <DependentUpon>DebugGrammar.g3</DependentUpon> + </Compile> + <Compile Include="BuildOptions\DebugGrammarLexerHelper.cs"> + <DependentUpon>DebugGrammar.g3</DependentUpon> + </Compile> + <Compile Include="BuildOptions\DebugGrammarParser.cs"> + <AutoGen>True</AutoGen> + <DesignTime>True</DesignTime> + <DependentUpon>DebugGrammar.g3</DependentUpon> + </Compile> + <Compile Include="BuildOptions\DebugGrammarParserHelper.cs"> + <DependentUpon>DebugGrammar.g3</DependentUpon> + </Compile> + </ItemGroup> + <ItemGroup> + <Antlr3 Include="BuildOptions\DebugTreeGrammar.g3"> + <GrammarOptions>-debug</GrammarOptions> + <OutputFiles>BuildOptions\DebugTreeGrammar.cs;BuildOptions\DebugTreeGrammar.tokens</OutputFiles> + </Antlr3> + <Compile Include="BuildOptions\DebugTreeGrammar.cs"> + <AutoGen>True</AutoGen> + <DesignTime>True</DesignTime> + <DependentUpon>DebugTreeGrammar.g3</DependentUpon> + </Compile> + <Compile Include="BuildOptions\DebugTreeGrammarHelper.cs"> + <DependentUpon>DebugTreeGrammar.g3</DependentUpon> + </Compile> + </ItemGroup> + <ItemGroup> + <Antlr3 Include="BuildOptions\ProfileGrammar.g3"> + <GrammarOptions>-profile</GrammarOptions> + <OutputFiles>BuildOptions\ProfileGrammarLexer.cs;BuildOptions\ProfileGrammarParser.cs;BuildOptions\ProfileGrammar.tokens</OutputFiles> + </Antlr3> + <None Include="BuildOptions\ProfileGrammarLexer.cs"> + <AutoGen>True</AutoGen> + <DesignTime>True</DesignTime> + <DependentUpon>ProfileGrammar.g3</DependentUpon> + </None> + <None Include="BuildOptions\ProfileGrammarLexerHelper.cs"> + <DependentUpon>ProfileGrammar.g3</DependentUpon> + </None> + <None Include="BuildOptions\ProfileGrammarParser.cs"> + <AutoGen>True</AutoGen> + <DesignTime>True</DesignTime> + <DependentUpon>ProfileGrammar.g3</DependentUpon> + </None> + <None Include="BuildOptions\ProfileGrammarParserHelper.cs"> + <DependentUpon>ProfileGrammar.g3</DependentUpon> + </None> + </ItemGroup> + <ItemGroup> + <Antlr3 Include="BuildOptions\ProfileTreeGrammar.g3"> + <GrammarOptions>-profile</GrammarOptions> + <OutputFiles>BuildOptions\ProfileTreeGrammar.cs;BuildOptions\ProfileTreeGrammar.tokens</OutputFiles> + </Antlr3> + <None Include="BuildOptions\ProfileTreeGrammar.cs"> + <AutoGen>True</AutoGen> + <DesignTime>True</DesignTime> + <DependentUpon>ProfileTreeGrammar.g3</DependentUpon> + </None> + <None Include="BuildOptions\ProfileTreeGrammarHelper.cs"> + <DependentUpon>ProfileTreeGrammar.g3</DependentUpon> + </None> + </ItemGroup> + <PropertyGroup> + <UseHostCompilerIfAvailable>False</UseHostCompilerIfAvailable> + </PropertyGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <PropertyGroup> + <Antlr3ToolPath>$(MSBuildProjectDirectory)\..\..\..\..\..\..\antlrcs\main\bin\Bootstrap</Antlr3ToolPath> + <CoreCompileDependsOn>$(CoreCompileDependsOn);GenerateAntlrCode</CoreCompileDependsOn> + <CoreCleanDependsOn>$(CoreCleanDependsOn);CleanAntlrCode</CoreCleanDependsOn> + </PropertyGroup> + <Target Name="GenerateAntlrCode" Inputs="@(Antlr3)" Outputs="%(OutputFiles)"> + <Message Importance="normal" Text="Antlr: Transforming '@(Antlr3)' to '%(Antlr3.OutputFiles)'" /> + <!--<Exec Command="java -cp %22$(Antlr3ToolPath)\antlr3.jar;$(Antlr3ToolPath)\antlr-2.7.7.jar;$(Antlr3ToolPath)\stringtemplate-3.1b1.jar%22 org.antlr.Tool -lib %22%(RootDir)%(Directory).%22 -message-format vs2005 @(Antlr3)" Outputs="%(OutputFiles)" />--> + <Exec Command="%22$(Antlr3ToolPath)\Antlr3.exe%22 -Xconversiontimeout 60000 -lib %22%(RootDir)%(Directory).%22 -message-format vs2005 %(Antlr3.GrammarOptions) @(Antlr3)" Outputs="%(OutputFiles)" /> + </Target> + <Target Name="CleanAntlrCode"> + <ItemGroup> + <_CleanAntlrFileWrites Include="@(Antlr3->'%(RelativeDir)%(Filename).tokens')" /> + </ItemGroup> + <Message Importance="normal" Text="Antlr: Deleting output files '@(_CleanAntlrFileWrites)'" /> + <!--<Delete Files="@(_CleanAntlrFileWrites)" />--> + </Target> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Antlr3.Runtime.Test.csproj.vspscc b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Antlr3.Runtime.Test.csproj.vspscc new file mode 100644 index 0000000..162a4c7 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Antlr3.Runtime.Test.csproj.vspscc @@ -0,0 +1,10 @@ +"" +{ +"FILE_VERSION" = "9237" +"ENLISTMENT_CHOICE" = "NEVER" +"PROJECT_FILE_RELATIVE_PATH" = "relative:antlr\\main\\runtime\\CSharp3\\Sources\\Antlr3.Runtime.Test" +"NUMBER_OF_EXCLUDED_FILES" = "0" +"ORIGINAL_PROJECT_FILE_PATH" = "" +"NUMBER_OF_NESTED_PROJECTS" = "0" +"SOURCE_CONTROL_SETTINGS_PROVIDER" = "PROVIDER" +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammar.g3 b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammar.g3 new file mode 100644 index 0000000..36b1884 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammar.g3 @@ -0,0 +1,100 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +grammar DebugGrammar; + +options +{ + language=CSharp3; + output=AST; + ASTLabelType=CommonTree; +} + +tokens +{ + // define pseudo-operations + FUNC; + CALL; +} + +// START:stat +prog: ( stat )* + ; + +stat: expr NEWLINE -> expr + | ID '=' expr NEWLINE -> ^('=' ID expr) + | func NEWLINE -> func + | NEWLINE -> // ignore + ; + +func: ID '(' formalPar ')' '=' expr -> ^(FUNC ID formalPar expr) + ; + finally { + functionDefinitions.Add($func.tree); + } + +formalPar + : ID + | INT + ; + +// END:stat + +// START:expr +expr: multExpr (('+'^|'-'^) multExpr)* + ; + +multExpr + : atom (('*'|'/'|'%')^ atom)* + ; + +atom: INT + | ID + | '(' expr ')' -> expr + | ID '(' expr ')' -> ^(CALL ID expr) + ; +// END:expr + +// START:tokens +ID : ('a'..'z'|'A'..'Z')+ + ; + +INT : '0'..'9'+ + ; + +NEWLINE + : '\r'? '\n' + ; + +WS : (' '|'\t')+ { Skip(); } + ; +// END:tokens diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarLexer.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarLexer.cs new file mode 100644 index 0000000..b707696 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarLexer.cs @@ -0,0 +1,691 @@ +// $ANTLR 3.1.2 BuildOptions\\DebugGrammar.g3 2009-03-16 13:19:17 + +// The variable 'variable' is assigned but its value is never used. +#pragma warning disable 219 +// Unreachable code detected. +#pragma warning disable 162 + + +using System.Collections.Generic; +using Antlr.Runtime; +using Stack = System.Collections.Generic.Stack<object>; +using List = System.Collections.IList; +using ArrayList = System.Collections.Generic.List<object>; + +public partial class DebugGrammarLexer : Lexer +{ + public const int EOF=-1; + public const int T__10=10; + public const int T__11=11; + public const int T__12=12; + public const int T__13=13; + public const int T__14=14; + public const int T__15=15; + public const int T__16=16; + public const int T__17=17; + public const int CALL=4; + public const int FUNC=5; + public const int ID=6; + public const int INT=7; + public const int NEWLINE=8; + public const int WS=9; + + // delegates + // delegators + + public DebugGrammarLexer() {} + public DebugGrammarLexer( ICharStream input ) + : this( input, new RecognizerSharedState() ) + { + } + public DebugGrammarLexer( ICharStream input, RecognizerSharedState state ) + : base( input, state ) + { + + } + public override string GrammarFileName { get { return "BuildOptions\\DebugGrammar.g3"; } } + + // $ANTLR start "T__10" + private void mT__10() + { + try + { + int _type = T__10; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\DebugGrammar.g3:7:9: ( '-' ) + // BuildOptions\\DebugGrammar.g3:7:9: '-' + { + Match('-'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__10" + + // $ANTLR start "T__11" + private void mT__11() + { + try + { + int _type = T__11; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\DebugGrammar.g3:8:9: ( '%' ) + // BuildOptions\\DebugGrammar.g3:8:9: '%' + { + Match('%'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__11" + + // $ANTLR start "T__12" + private void mT__12() + { + try + { + int _type = T__12; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\DebugGrammar.g3:9:9: ( '(' ) + // BuildOptions\\DebugGrammar.g3:9:9: '(' + { + Match('('); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__12" + + // $ANTLR start "T__13" + private void mT__13() + { + try + { + int _type = T__13; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\DebugGrammar.g3:10:9: ( ')' ) + // BuildOptions\\DebugGrammar.g3:10:9: ')' + { + Match(')'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__13" + + // $ANTLR start "T__14" + private void mT__14() + { + try + { + int _type = T__14; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\DebugGrammar.g3:11:9: ( '*' ) + // BuildOptions\\DebugGrammar.g3:11:9: '*' + { + Match('*'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__14" + + // $ANTLR start "T__15" + private void mT__15() + { + try + { + int _type = T__15; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\DebugGrammar.g3:12:9: ( '/' ) + // BuildOptions\\DebugGrammar.g3:12:9: '/' + { + Match('/'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__15" + + // $ANTLR start "T__16" + private void mT__16() + { + try + { + int _type = T__16; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\DebugGrammar.g3:13:9: ( '+' ) + // BuildOptions\\DebugGrammar.g3:13:9: '+' + { + Match('+'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__16" + + // $ANTLR start "T__17" + private void mT__17() + { + try + { + int _type = T__17; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\DebugGrammar.g3:14:9: ( '=' ) + // BuildOptions\\DebugGrammar.g3:14:9: '=' + { + Match('='); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__17" + + // $ANTLR start "ID" + private void mID() + { + try + { + int _type = ID; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\DebugGrammar.g3:88:9: ( ( 'a' .. 'z' | 'A' .. 'Z' )+ ) + // BuildOptions\\DebugGrammar.g3:88:9: ( 'a' .. 'z' | 'A' .. 'Z' )+ + { + // BuildOptions\\DebugGrammar.g3:88:9: ( 'a' .. 'z' | 'A' .. 'Z' )+ + int cnt1=0; + for ( ; ; ) + { + int alt1=2; + int LA1_0 = input.LA(1); + + if ( ((LA1_0>='A' && LA1_0<='Z')||(LA1_0>='a' && LA1_0<='z')) ) + { + alt1=1; + } + + + switch ( alt1 ) + { + case 1: + // BuildOptions\\DebugGrammar.g3: + { + input.Consume(); + + + } + break; + + default: + if ( cnt1 >= 1 ) + goto loop1; + + EarlyExitException eee1 = new EarlyExitException( 1, input ); + throw eee1; + } + cnt1++; + } + loop1: + ; + + + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "ID" + + // $ANTLR start "INT" + private void mINT() + { + try + { + int _type = INT; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\DebugGrammar.g3:91:9: ( ( '0' .. '9' )+ ) + // BuildOptions\\DebugGrammar.g3:91:9: ( '0' .. '9' )+ + { + // BuildOptions\\DebugGrammar.g3:91:9: ( '0' .. '9' )+ + int cnt2=0; + for ( ; ; ) + { + int alt2=2; + int LA2_0 = input.LA(1); + + if ( ((LA2_0>='0' && LA2_0<='9')) ) + { + alt2=1; + } + + + switch ( alt2 ) + { + case 1: + // BuildOptions\\DebugGrammar.g3: + { + input.Consume(); + + + } + break; + + default: + if ( cnt2 >= 1 ) + goto loop2; + + EarlyExitException eee2 = new EarlyExitException( 2, input ); + throw eee2; + } + cnt2++; + } + loop2: + ; + + + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "INT" + + // $ANTLR start "NEWLINE" + private void mNEWLINE() + { + try + { + int _type = NEWLINE; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\DebugGrammar.g3:95:7: ( ( '\\r' )? '\\n' ) + // BuildOptions\\DebugGrammar.g3:95:7: ( '\\r' )? '\\n' + { + // BuildOptions\\DebugGrammar.g3:95:7: ( '\\r' )? + int alt3=2; + int LA3_0 = input.LA(1); + + if ( (LA3_0=='\r') ) + { + alt3=1; + } + switch ( alt3 ) + { + case 1: + // BuildOptions\\DebugGrammar.g3:95:0: '\\r' + { + Match('\r'); + + } + break; + + } + + Match('\n'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "NEWLINE" + + // $ANTLR start "WS" + private void mWS() + { + try + { + int _type = WS; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\DebugGrammar.g3:98:9: ( ( ' ' | '\\t' )+ ) + // BuildOptions\\DebugGrammar.g3:98:9: ( ' ' | '\\t' )+ + { + // BuildOptions\\DebugGrammar.g3:98:9: ( ' ' | '\\t' )+ + int cnt4=0; + for ( ; ; ) + { + int alt4=2; + int LA4_0 = input.LA(1); + + if ( (LA4_0=='\t'||LA4_0==' ') ) + { + alt4=1; + } + + + switch ( alt4 ) + { + case 1: + // BuildOptions\\DebugGrammar.g3: + { + input.Consume(); + + + } + break; + + default: + if ( cnt4 >= 1 ) + goto loop4; + + EarlyExitException eee4 = new EarlyExitException( 4, input ); + throw eee4; + } + cnt4++; + } + loop4: + ; + + + Skip(); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "WS" + + public override void mTokens() + { + // BuildOptions\\DebugGrammar.g3:1:10: ( T__10 | T__11 | T__12 | T__13 | T__14 | T__15 | T__16 | T__17 | ID | INT | NEWLINE | WS ) + int alt5=12; + switch ( input.LA(1) ) + { + case '-': + { + alt5=1; + } + break; + case '%': + { + alt5=2; + } + break; + case '(': + { + alt5=3; + } + break; + case ')': + { + alt5=4; + } + break; + case '*': + { + alt5=5; + } + break; + case '/': + { + alt5=6; + } + break; + case '+': + { + alt5=7; + } + break; + case '=': + { + alt5=8; + } + break; + case 'A': + case 'B': + case 'C': + case 'D': + case 'E': + case 'F': + case 'G': + case 'H': + case 'I': + case 'J': + case 'K': + case 'L': + case 'M': + case 'N': + case 'O': + case 'P': + case 'Q': + case 'R': + case 'S': + case 'T': + case 'U': + case 'V': + case 'W': + case 'X': + case 'Y': + case 'Z': + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + case 'g': + case 'h': + case 'i': + case 'j': + case 'k': + case 'l': + case 'm': + case 'n': + case 'o': + case 'p': + case 'q': + case 'r': + case 's': + case 't': + case 'u': + case 'v': + case 'w': + case 'x': + case 'y': + case 'z': + { + alt5=9; + } + break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + { + alt5=10; + } + break; + case '\n': + case '\r': + { + alt5=11; + } + break; + case '\t': + case ' ': + { + alt5=12; + } + break; + default: + { + NoViableAltException nvae = new NoViableAltException("", 5, 0, input); + + throw nvae; + } + } + + switch ( alt5 ) + { + case 1: + // BuildOptions\\DebugGrammar.g3:1:10: T__10 + { + mT__10(); + + } + break; + case 2: + // BuildOptions\\DebugGrammar.g3:1:16: T__11 + { + mT__11(); + + } + break; + case 3: + // BuildOptions\\DebugGrammar.g3:1:22: T__12 + { + mT__12(); + + } + break; + case 4: + // BuildOptions\\DebugGrammar.g3:1:28: T__13 + { + mT__13(); + + } + break; + case 5: + // BuildOptions\\DebugGrammar.g3:1:34: T__14 + { + mT__14(); + + } + break; + case 6: + // BuildOptions\\DebugGrammar.g3:1:40: T__15 + { + mT__15(); + + } + break; + case 7: + // BuildOptions\\DebugGrammar.g3:1:46: T__16 + { + mT__16(); + + } + break; + case 8: + // BuildOptions\\DebugGrammar.g3:1:52: T__17 + { + mT__17(); + + } + break; + case 9: + // BuildOptions\\DebugGrammar.g3:1:58: ID + { + mID(); + + } + break; + case 10: + // BuildOptions\\DebugGrammar.g3:1:61: INT + { + mINT(); + + } + break; + case 11: + // BuildOptions\\DebugGrammar.g3:1:65: NEWLINE + { + mNEWLINE(); + + } + break; + case 12: + // BuildOptions\\DebugGrammar.g3:1:73: WS + { + mWS(); + + } + break; + + } + + } + + + #region DFA + + protected override void InitDFAs() + { + base.InitDFAs(); + } + + + #endregion + +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarLexerHelper.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarLexerHelper.cs new file mode 100644 index 0000000..7271295 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarLexerHelper.cs @@ -0,0 +1,32 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarParser.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarParser.cs new file mode 100644 index 0000000..87710f6 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarParser.cs @@ -0,0 +1,1551 @@ +// $ANTLR 3.1.2 BuildOptions\\DebugGrammar.g3 2009-03-16 13:19:16 + +// The variable 'variable' is assigned but its value is never used. +#pragma warning disable 219 +// Unreachable code detected. +#pragma warning disable 162 + + +using System.Collections.Generic; +using Antlr.Runtime; +using Stack = System.Collections.Generic.Stack<object>; +using List = System.Collections.IList; +using ArrayList = System.Collections.Generic.List<object>; + +using Antlr.Runtime.Debug; +using IOException = System.IO.IOException; + +using Antlr.Runtime.Tree; +using RewriteRuleITokenStream = Antlr.Runtime.Tree.RewriteRuleTokenStream; + +public partial class DebugGrammarParser : DebugParser +{ + public static readonly string[] tokenNames = new string[] { + "<invalid>", "<EOR>", "<DOWN>", "<UP>", "CALL", "FUNC", "ID", "INT", "NEWLINE", "WS", "'-'", "'%'", "'('", "')'", "'*'", "'/'", "'+'", "'='" + }; + public const int EOF=-1; + public const int T__10=10; + public const int T__11=11; + public const int T__12=12; + public const int T__13=13; + public const int T__14=14; + public const int T__15=15; + public const int T__16=16; + public const int T__17=17; + public const int CALL=4; + public const int FUNC=5; + public const int ID=6; + public const int INT=7; + public const int NEWLINE=8; + public const int WS=9; + + // delegates + // delegators + + public static readonly string[] ruleNames = + new string[] + { + "invalidRule", "atom", "expr", "formalPar", "func", "multExpr", "prog", + "stat" + }; + + int ruleLevel = 0; + public virtual int RuleLevel { get { return ruleLevel; } } + public virtual void IncRuleLevel() { ruleLevel++; } + public virtual void DecRuleLevel() { ruleLevel--; } + public DebugGrammarParser( ITokenStream input ) + : this( input, DebugEventSocketProxy.DEFAULT_DEBUGGER_PORT, new RecognizerSharedState() ) + { + } + public DebugGrammarParser( ITokenStream input, int port, RecognizerSharedState state ) + : base( input, state ) + { + DebugEventSocketProxy proxy = new DebugEventSocketProxy( this, port, adaptor ); + DebugListener = proxy; + // TODO: I had to manually correct this line from ITokenStream + TokenStream = new DebugTokenStream( input, proxy ); + try + { + proxy.Handshake(); + } + catch ( IOException ioe ) + { + ReportError( ioe ); + } + ITreeAdaptor adap = new CommonTreeAdaptor(); + TreeAdaptor = adap; + proxy.TreeAdaptor = adap; + } + public DebugGrammarParser( ITokenStream input, IDebugEventListener dbg ) + : base( input, dbg ) + { + + + ITreeAdaptor adap = new CommonTreeAdaptor(); + TreeAdaptor = adap; + + } + protected virtual bool EvalPredicate( bool result, string predicate ) + { + dbg.SemanticPredicate( result, predicate ); + return result; + } + + protected DebugTreeAdaptor adaptor; + public ITreeAdaptor TreeAdaptor + { + get + { + return adaptor; + } + set + { + this.adaptor = new DebugTreeAdaptor(dbg,adaptor); + + } + } + + + public override string[] GetTokenNames() { return DebugGrammarParser.tokenNames; } + public override string GrammarFileName { get { return "BuildOptions\\DebugGrammar.g3"; } } + + + #region Rules + public class prog_return : ParserRuleReturnScope + { + public CommonTree tree; + public override object Tree { get { return tree; } } + } + + // $ANTLR start "prog" + // BuildOptions\\DebugGrammar.g3:50:0: prog : ( stat )* ; + private DebugGrammarParser.prog_return prog( ) + { + DebugGrammarParser.prog_return retval = new DebugGrammarParser.prog_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + DebugGrammarParser.stat_return stat1 = default(DebugGrammarParser.stat_return); + + + try + { + dbg.EnterRule( GrammarFileName, "prog" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 50, -1 ); + + try + { + // BuildOptions\\DebugGrammar.g3:50:7: ( ( stat )* ) + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugGrammar.g3:50:7: ( stat )* + { + root_0 = (CommonTree)adaptor.Nil(); + + dbg.Location( 50, 6 ); + // BuildOptions\\DebugGrammar.g3:50:7: ( stat )* + try + { + dbg.EnterSubRule( 1 ); + + for ( ; ; ) + { + int alt1=2; + try + { + dbg.EnterDecision( 1 ); + + int LA1_0 = input.LA(1); + + if ( ((LA1_0>=ID && LA1_0<=NEWLINE)||LA1_0==12) ) + { + alt1=1; + } + + + } + finally + { + dbg.ExitDecision( 1 ); + } + + switch ( alt1 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugGrammar.g3:50:9: stat + { + dbg.Location( 50, 8 ); + PushFollow(Follow._stat_in_prog53); + stat1=stat(); + + state._fsp--; + + adaptor.AddChild(root_0, stat1.Tree); + + } + break; + + default: + goto loop1; + } + } + + loop1: + ; + + } + finally + { + dbg.ExitSubRule( 1 ); + } + + + } + + retval.stop = input.LT(-1); + + retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); + adaptor.SetTokenBoundaries(retval.tree, retval.start, retval.stop); + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + retval.tree = (CommonTree)adaptor.ErrorNode(input, retval.start, input.LT(-1), re); + + } + finally + { + } + dbg.Location(51, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "prog" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return retval; + } + // $ANTLR end "prog" + + public class stat_return : ParserRuleReturnScope + { + public CommonTree tree; + public override object Tree { get { return tree; } } + } + + // $ANTLR start "stat" + // BuildOptions\\DebugGrammar.g3:53:0: stat : ( expr NEWLINE -> expr | ID '=' expr NEWLINE -> ^( '=' ID expr ) | func NEWLINE -> func | NEWLINE ->); + private DebugGrammarParser.stat_return stat( ) + { + DebugGrammarParser.stat_return retval = new DebugGrammarParser.stat_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + IToken NEWLINE3=null; + IToken ID4=null; + IToken char_literal5=null; + IToken NEWLINE7=null; + IToken NEWLINE9=null; + IToken NEWLINE10=null; + DebugGrammarParser.expr_return expr2 = default(DebugGrammarParser.expr_return); + DebugGrammarParser.expr_return expr6 = default(DebugGrammarParser.expr_return); + DebugGrammarParser.func_return func8 = default(DebugGrammarParser.func_return); + + CommonTree NEWLINE3_tree=null; + CommonTree ID4_tree=null; + CommonTree char_literal5_tree=null; + CommonTree NEWLINE7_tree=null; + CommonTree NEWLINE9_tree=null; + CommonTree NEWLINE10_tree=null; + RewriteRuleITokenStream stream_NEWLINE=new RewriteRuleITokenStream(adaptor,"token NEWLINE"); + RewriteRuleITokenStream stream_ID=new RewriteRuleITokenStream(adaptor,"token ID"); + RewriteRuleITokenStream stream_17=new RewriteRuleITokenStream(adaptor,"token 17"); + RewriteRuleSubtreeStream stream_expr=new RewriteRuleSubtreeStream(adaptor,"rule expr"); + RewriteRuleSubtreeStream stream_func=new RewriteRuleSubtreeStream(adaptor,"rule func"); + try + { + dbg.EnterRule( GrammarFileName, "stat" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 53, -1 ); + + try + { + // BuildOptions\\DebugGrammar.g3:53:9: ( expr NEWLINE -> expr | ID '=' expr NEWLINE -> ^( '=' ID expr ) | func NEWLINE -> func | NEWLINE ->) + int alt2=4; + try + { + dbg.EnterDecision( 2 ); + + try + { + isCyclicDecision = true; + alt2 = dfa2.Predict(input); + } + catch ( NoViableAltException nvae ) + { + dbg.RecognitionException( nvae ); + throw nvae; + } + } + finally + { + dbg.ExitDecision( 2 ); + } + + switch ( alt2 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugGrammar.g3:53:9: expr NEWLINE + { + dbg.Location( 53, 8 ); + PushFollow(Follow._expr_in_stat70); + expr2=expr(); + + state._fsp--; + + stream_expr.Add(expr2.Tree); + dbg.Location( 53, 13 ); + NEWLINE3=(IToken)Match(input,NEWLINE,Follow._NEWLINE_in_stat72); + stream_NEWLINE.Add(NEWLINE3); + + + + { + // AST REWRITE + // elements: expr + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + // wildcard labels: + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"rule retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.Nil(); + // 53:41: -> expr + { + dbg.Location( 53, 43 ); + adaptor.AddChild(root_0, stream_expr.NextTree()); + + } + + retval.tree = root_0; + } + + } + break; + case 2: + dbg.EnterAlt( 2 ); + + // BuildOptions\\DebugGrammar.g3:54:9: ID '=' expr NEWLINE + { + dbg.Location( 54, 8 ); + ID4=(IToken)Match(input,ID,Follow._ID_in_stat105); + stream_ID.Add(ID4); + + dbg.Location( 54, 11 ); + char_literal5=(IToken)Match(input,17,Follow._17_in_stat107); + stream_17.Add(char_literal5); + + dbg.Location( 54, 15 ); + PushFollow(Follow._expr_in_stat109); + expr6=expr(); + + state._fsp--; + + stream_expr.Add(expr6.Tree); + dbg.Location( 54, 20 ); + NEWLINE7=(IToken)Match(input,NEWLINE,Follow._NEWLINE_in_stat111); + stream_NEWLINE.Add(NEWLINE7); + + + + { + // AST REWRITE + // elements: 17, ID, expr + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + // wildcard labels: + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"rule retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.Nil(); + // 54:41: -> ^( '=' ID expr ) + { + dbg.Location( 54, 43 ); + // BuildOptions\\DebugGrammar.g3:54:44: ^( '=' ID expr ) + { + CommonTree root_1 = (CommonTree)adaptor.Nil(); + dbg.Location( 54, 45 ); + root_1 = (CommonTree)adaptor.BecomeRoot(stream_17.NextNode(), root_1); + + dbg.Location( 54, 49 ); + adaptor.AddChild(root_1, stream_ID.NextNode()); + dbg.Location( 54, 52 ); + adaptor.AddChild(root_1, stream_expr.NextTree()); + + adaptor.AddChild(root_0, root_1); + } + + } + + retval.tree = root_0; + } + + } + break; + case 3: + dbg.EnterAlt( 3 ); + + // BuildOptions\\DebugGrammar.g3:55:9: func NEWLINE + { + dbg.Location( 55, 8 ); + PushFollow(Follow._func_in_stat143); + func8=func(); + + state._fsp--; + + stream_func.Add(func8.Tree); + dbg.Location( 55, 13 ); + NEWLINE9=(IToken)Match(input,NEWLINE,Follow._NEWLINE_in_stat145); + stream_NEWLINE.Add(NEWLINE9); + + + + { + // AST REWRITE + // elements: func + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + // wildcard labels: + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"rule retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.Nil(); + // 55:41: -> func + { + dbg.Location( 55, 43 ); + adaptor.AddChild(root_0, stream_func.NextTree()); + + } + + retval.tree = root_0; + } + + } + break; + case 4: + dbg.EnterAlt( 4 ); + + // BuildOptions\\DebugGrammar.g3:56:9: NEWLINE + { + dbg.Location( 56, 8 ); + NEWLINE10=(IToken)Match(input,NEWLINE,Follow._NEWLINE_in_stat178); + stream_NEWLINE.Add(NEWLINE10); + + + + { + // AST REWRITE + // elements: + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + // wildcard labels: + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"rule retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.Nil(); + // 56:41: -> + { + dbg.Location( 57, 4 ); + root_0 = null; + } + + retval.tree = root_0; + } + + } + break; + + } + retval.stop = input.LT(-1); + + retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); + adaptor.SetTokenBoundaries(retval.tree, retval.start, retval.stop); + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + retval.tree = (CommonTree)adaptor.ErrorNode(input, retval.start, input.LT(-1), re); + + } + finally + { + } + dbg.Location(57, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "stat" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return retval; + } + // $ANTLR end "stat" + + public class func_return : ParserRuleReturnScope + { + public CommonTree tree; + public override object Tree { get { return tree; } } + } + + // $ANTLR start "func" + // BuildOptions\\DebugGrammar.g3:59:0: func : ID '(' formalPar ')' '=' expr -> ^( FUNC ID formalPar expr ) ; + private DebugGrammarParser.func_return func( ) + { + DebugGrammarParser.func_return retval = new DebugGrammarParser.func_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + IToken ID11=null; + IToken char_literal12=null; + IToken char_literal14=null; + IToken char_literal15=null; + DebugGrammarParser.formalPar_return formalPar13 = default(DebugGrammarParser.formalPar_return); + DebugGrammarParser.expr_return expr16 = default(DebugGrammarParser.expr_return); + + CommonTree ID11_tree=null; + CommonTree char_literal12_tree=null; + CommonTree char_literal14_tree=null; + CommonTree char_literal15_tree=null; + RewriteRuleITokenStream stream_ID=new RewriteRuleITokenStream(adaptor,"token ID"); + RewriteRuleITokenStream stream_12=new RewriteRuleITokenStream(adaptor,"token 12"); + RewriteRuleITokenStream stream_13=new RewriteRuleITokenStream(adaptor,"token 13"); + RewriteRuleITokenStream stream_17=new RewriteRuleITokenStream(adaptor,"token 17"); + RewriteRuleSubtreeStream stream_formalPar=new RewriteRuleSubtreeStream(adaptor,"rule formalPar"); + RewriteRuleSubtreeStream stream_expr=new RewriteRuleSubtreeStream(adaptor,"rule expr"); + try + { + dbg.EnterRule( GrammarFileName, "func" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 59, -1 ); + + try + { + // BuildOptions\\DebugGrammar.g3:59:9: ( ID '(' formalPar ')' '=' expr -> ^( FUNC ID formalPar expr ) ) + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugGrammar.g3:59:9: ID '(' formalPar ')' '=' expr + { + dbg.Location( 59, 8 ); + ID11=(IToken)Match(input,ID,Follow._ID_in_func219); + stream_ID.Add(ID11); + + dbg.Location( 59, 12 ); + char_literal12=(IToken)Match(input,12,Follow._12_in_func222); + stream_12.Add(char_literal12); + + dbg.Location( 59, 16 ); + PushFollow(Follow._formalPar_in_func224); + formalPar13=formalPar(); + + state._fsp--; + + stream_formalPar.Add(formalPar13.Tree); + dbg.Location( 59, 26 ); + char_literal14=(IToken)Match(input,13,Follow._13_in_func226); + stream_13.Add(char_literal14); + + dbg.Location( 59, 30 ); + char_literal15=(IToken)Match(input,17,Follow._17_in_func228); + stream_17.Add(char_literal15); + + dbg.Location( 59, 34 ); + PushFollow(Follow._expr_in_func230); + expr16=expr(); + + state._fsp--; + + stream_expr.Add(expr16.Tree); + + + { + // AST REWRITE + // elements: ID, formalPar, expr + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + // wildcard labels: + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"rule retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.Nil(); + // 59:41: -> ^( FUNC ID formalPar expr ) + { + dbg.Location( 59, 43 ); + // BuildOptions\\DebugGrammar.g3:59:44: ^( FUNC ID formalPar expr ) + { + CommonTree root_1 = (CommonTree)adaptor.Nil(); + dbg.Location( 59, 45 ); + root_1 = (CommonTree)adaptor.BecomeRoot((CommonTree)adaptor.Create(FUNC, "FUNC"), root_1); + + dbg.Location( 59, 50 ); + adaptor.AddChild(root_1, stream_ID.NextNode()); + dbg.Location( 59, 53 ); + adaptor.AddChild(root_1, stream_formalPar.NextTree()); + dbg.Location( 59, 63 ); + adaptor.AddChild(root_1, stream_expr.NextTree()); + + adaptor.AddChild(root_0, root_1); + } + + } + + retval.tree = root_0; + } + + } + + retval.stop = input.LT(-1); + + retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); + adaptor.SetTokenBoundaries(retval.tree, retval.start, retval.stop); + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + retval.tree = (CommonTree)adaptor.ErrorNode(input, retval.start, input.LT(-1), re); + + } + finally + { + + functionDefinitions.Add(((CommonTree)retval.tree)); + + } + dbg.Location(60, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "func" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return retval; + } + // $ANTLR end "func" + + public class formalPar_return : ParserRuleReturnScope + { + public CommonTree tree; + public override object Tree { get { return tree; } } + } + + // $ANTLR start "formalPar" + // BuildOptions\\DebugGrammar.g3:65:0: formalPar : ( ID | INT ); + private DebugGrammarParser.formalPar_return formalPar( ) + { + DebugGrammarParser.formalPar_return retval = new DebugGrammarParser.formalPar_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + IToken set17=null; + + CommonTree set17_tree=null; + + try + { + dbg.EnterRule( GrammarFileName, "formalPar" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 65, -1 ); + + try + { + // BuildOptions\\DebugGrammar.g3:66:9: ( ID | INT ) + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugGrammar.g3: + { + root_0 = (CommonTree)adaptor.Nil(); + + dbg.Location( 66, 8 ); + set17=(IToken)input.LT(1); + if ( (input.LA(1)>=ID && input.LA(1)<=INT) ) + { + input.Consume(); + adaptor.AddChild(root_0, (CommonTree)adaptor.Create(set17)); + state.errorRecovery=false; + } + else + { + MismatchedSetException mse = new MismatchedSetException(null,input); + dbg.RecognitionException( mse ); + throw mse; + } + + + } + + retval.stop = input.LT(-1); + + retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); + adaptor.SetTokenBoundaries(retval.tree, retval.start, retval.stop); + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + retval.tree = (CommonTree)adaptor.ErrorNode(input, retval.start, input.LT(-1), re); + + } + finally + { + } + dbg.Location(68, 1); + + } + finally + { + dbg.ExitRule( GrammarFileName, "formalPar" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return retval; + } + // $ANTLR end "formalPar" + + public class expr_return : ParserRuleReturnScope + { + public CommonTree tree; + public override object Tree { get { return tree; } } + } + + // $ANTLR start "expr" + // BuildOptions\\DebugGrammar.g3:73:0: expr : multExpr ( ( '+' | '-' ) multExpr )* ; + private DebugGrammarParser.expr_return expr( ) + { + DebugGrammarParser.expr_return retval = new DebugGrammarParser.expr_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + IToken char_literal19=null; + IToken char_literal20=null; + DebugGrammarParser.multExpr_return multExpr18 = default(DebugGrammarParser.multExpr_return); + DebugGrammarParser.multExpr_return multExpr21 = default(DebugGrammarParser.multExpr_return); + + CommonTree char_literal19_tree=null; + CommonTree char_literal20_tree=null; + + try + { + dbg.EnterRule( GrammarFileName, "expr" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 73, -1 ); + + try + { + // BuildOptions\\DebugGrammar.g3:73:9: ( multExpr ( ( '+' | '-' ) multExpr )* ) + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugGrammar.g3:73:9: multExpr ( ( '+' | '-' ) multExpr )* + { + root_0 = (CommonTree)adaptor.Nil(); + + dbg.Location( 73, 8 ); + PushFollow(Follow._multExpr_in_expr288); + multExpr18=multExpr(); + + state._fsp--; + + adaptor.AddChild(root_0, multExpr18.Tree); + dbg.Location( 73, 17 ); + // BuildOptions\\DebugGrammar.g3:73:18: ( ( '+' | '-' ) multExpr )* + try + { + dbg.EnterSubRule( 4 ); + + for ( ; ; ) + { + int alt4=2; + try + { + dbg.EnterDecision( 4 ); + + int LA4_0 = input.LA(1); + + if ( (LA4_0==10||LA4_0==16) ) + { + alt4=1; + } + + + } + finally + { + dbg.ExitDecision( 4 ); + } + + switch ( alt4 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugGrammar.g3:73:19: ( '+' | '-' ) multExpr + { + dbg.Location( 73, 18 ); + // BuildOptions\\DebugGrammar.g3:73:19: ( '+' | '-' ) + int alt3=2; + try + { + dbg.EnterSubRule( 3 ); + try + { + dbg.EnterDecision( 3 ); + + int LA3_0 = input.LA(1); + + if ( (LA3_0==16) ) + { + alt3=1; + } + else if ( (LA3_0==10) ) + { + alt3=2; + } + else + { + NoViableAltException nvae = new NoViableAltException("", 3, 0, input); + + dbg.RecognitionException( nvae ); + throw nvae; + } + } + finally + { + dbg.ExitDecision( 3 ); + } + + switch ( alt3 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugGrammar.g3:73:20: '+' + { + dbg.Location( 73, 22 ); + char_literal19=(IToken)Match(input,16,Follow._16_in_expr292); + char_literal19_tree = (CommonTree)adaptor.Create(char_literal19); + root_0 = (CommonTree)adaptor.BecomeRoot(char_literal19_tree, root_0); + + + } + break; + case 2: + dbg.EnterAlt( 2 ); + + // BuildOptions\\DebugGrammar.g3:73:25: '-' + { + dbg.Location( 73, 27 ); + char_literal20=(IToken)Match(input,10,Follow._10_in_expr295); + char_literal20_tree = (CommonTree)adaptor.Create(char_literal20); + root_0 = (CommonTree)adaptor.BecomeRoot(char_literal20_tree, root_0); + + + } + break; + + } + } + finally + { + dbg.ExitSubRule( 3 ); + } + + dbg.Location( 73, 30 ); + PushFollow(Follow._multExpr_in_expr299); + multExpr21=multExpr(); + + state._fsp--; + + adaptor.AddChild(root_0, multExpr21.Tree); + + } + break; + + default: + goto loop4; + } + } + + loop4: + ; + + } + finally + { + dbg.ExitSubRule( 4 ); + } + + + } + + retval.stop = input.LT(-1); + + retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); + adaptor.SetTokenBoundaries(retval.tree, retval.start, retval.stop); + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + retval.tree = (CommonTree)adaptor.ErrorNode(input, retval.start, input.LT(-1), re); + + } + finally + { + } + dbg.Location(74, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "expr" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return retval; + } + // $ANTLR end "expr" + + public class multExpr_return : ParserRuleReturnScope + { + public CommonTree tree; + public override object Tree { get { return tree; } } + } + + // $ANTLR start "multExpr" + // BuildOptions\\DebugGrammar.g3:76:0: multExpr : atom ( ( '*' | '/' | '%' ) atom )* ; + private DebugGrammarParser.multExpr_return multExpr( ) + { + DebugGrammarParser.multExpr_return retval = new DebugGrammarParser.multExpr_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + IToken set23=null; + DebugGrammarParser.atom_return atom22 = default(DebugGrammarParser.atom_return); + DebugGrammarParser.atom_return atom24 = default(DebugGrammarParser.atom_return); + + CommonTree set23_tree=null; + + try + { + dbg.EnterRule( GrammarFileName, "multExpr" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 76, -1 ); + + try + { + // BuildOptions\\DebugGrammar.g3:77:9: ( atom ( ( '*' | '/' | '%' ) atom )* ) + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugGrammar.g3:77:9: atom ( ( '*' | '/' | '%' ) atom )* + { + root_0 = (CommonTree)adaptor.Nil(); + + dbg.Location( 77, 8 ); + PushFollow(Follow._atom_in_multExpr320); + atom22=atom(); + + state._fsp--; + + adaptor.AddChild(root_0, atom22.Tree); + dbg.Location( 77, 13 ); + // BuildOptions\\DebugGrammar.g3:77:14: ( ( '*' | '/' | '%' ) atom )* + try + { + dbg.EnterSubRule( 5 ); + + for ( ; ; ) + { + int alt5=2; + try + { + dbg.EnterDecision( 5 ); + + int LA5_0 = input.LA(1); + + if ( (LA5_0==11||(LA5_0>=14 && LA5_0<=15)) ) + { + alt5=1; + } + + + } + finally + { + dbg.ExitDecision( 5 ); + } + + switch ( alt5 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugGrammar.g3:77:15: ( '*' | '/' | '%' ) atom + { + dbg.Location( 77, 27 ); + set23=(IToken)input.LT(1); + set23=(IToken)input.LT(1); + if ( input.LA(1)==11||(input.LA(1)>=14 && input.LA(1)<=15) ) + { + input.Consume(); + root_0 = (CommonTree)adaptor.BecomeRoot((CommonTree)adaptor.Create(set23), root_0); + state.errorRecovery=false; + } + else + { + MismatchedSetException mse = new MismatchedSetException(null,input); + dbg.RecognitionException( mse ); + throw mse; + } + + dbg.Location( 77, 29 ); + PushFollow(Follow._atom_in_multExpr332); + atom24=atom(); + + state._fsp--; + + adaptor.AddChild(root_0, atom24.Tree); + + } + break; + + default: + goto loop5; + } + } + + loop5: + ; + + } + finally + { + dbg.ExitSubRule( 5 ); + } + + + } + + retval.stop = input.LT(-1); + + retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); + adaptor.SetTokenBoundaries(retval.tree, retval.start, retval.stop); + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + retval.tree = (CommonTree)adaptor.ErrorNode(input, retval.start, input.LT(-1), re); + + } + finally + { + } + dbg.Location(78, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "multExpr" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return retval; + } + // $ANTLR end "multExpr" + + public class atom_return : ParserRuleReturnScope + { + public CommonTree tree; + public override object Tree { get { return tree; } } + } + + // $ANTLR start "atom" + // BuildOptions\\DebugGrammar.g3:80:0: atom : ( INT | ID | '(' expr ')' -> expr | ID '(' expr ')' -> ^( CALL ID expr ) ); + private DebugGrammarParser.atom_return atom( ) + { + DebugGrammarParser.atom_return retval = new DebugGrammarParser.atom_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + IToken INT25=null; + IToken ID26=null; + IToken char_literal27=null; + IToken char_literal29=null; + IToken ID30=null; + IToken char_literal31=null; + IToken char_literal33=null; + DebugGrammarParser.expr_return expr28 = default(DebugGrammarParser.expr_return); + DebugGrammarParser.expr_return expr32 = default(DebugGrammarParser.expr_return); + + CommonTree INT25_tree=null; + CommonTree ID26_tree=null; + CommonTree char_literal27_tree=null; + CommonTree char_literal29_tree=null; + CommonTree ID30_tree=null; + CommonTree char_literal31_tree=null; + CommonTree char_literal33_tree=null; + RewriteRuleITokenStream stream_12=new RewriteRuleITokenStream(adaptor,"token 12"); + RewriteRuleITokenStream stream_13=new RewriteRuleITokenStream(adaptor,"token 13"); + RewriteRuleITokenStream stream_ID=new RewriteRuleITokenStream(adaptor,"token ID"); + RewriteRuleSubtreeStream stream_expr=new RewriteRuleSubtreeStream(adaptor,"rule expr"); + try + { + dbg.EnterRule( GrammarFileName, "atom" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 80, -1 ); + + try + { + // BuildOptions\\DebugGrammar.g3:80:9: ( INT | ID | '(' expr ')' -> expr | ID '(' expr ')' -> ^( CALL ID expr ) ) + int alt6=4; + try + { + dbg.EnterDecision( 6 ); + + switch ( input.LA(1) ) + { + case INT: + { + alt6=1; + } + break; + case ID: + { + int LA6_2 = input.LA(2); + + if ( (LA6_2==12) ) + { + alt6=4; + } + else if ( (LA6_2==NEWLINE||(LA6_2>=10 && LA6_2<=11)||(LA6_2>=13 && LA6_2<=16)) ) + { + alt6=2; + } + else + { + NoViableAltException nvae = new NoViableAltException("", 6, 2, input); + + dbg.RecognitionException( nvae ); + throw nvae; + } + } + break; + case 12: + { + alt6=3; + } + break; + default: + { + NoViableAltException nvae = new NoViableAltException("", 6, 0, input); + + dbg.RecognitionException( nvae ); + throw nvae; + } + } + + } + finally + { + dbg.ExitDecision( 6 ); + } + + switch ( alt6 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugGrammar.g3:80:9: INT + { + root_0 = (CommonTree)adaptor.Nil(); + + dbg.Location( 80, 8 ); + INT25=(IToken)Match(input,INT,Follow._INT_in_atom348); + INT25_tree = (CommonTree)adaptor.Create(INT25); + adaptor.AddChild(root_0, INT25_tree); + + + } + break; + case 2: + dbg.EnterAlt( 2 ); + + // BuildOptions\\DebugGrammar.g3:81:9: ID + { + root_0 = (CommonTree)adaptor.Nil(); + + dbg.Location( 81, 8 ); + ID26=(IToken)Match(input,ID,Follow._ID_in_atom358); + ID26_tree = (CommonTree)adaptor.Create(ID26); + adaptor.AddChild(root_0, ID26_tree); + + + } + break; + case 3: + dbg.EnterAlt( 3 ); + + // BuildOptions\\DebugGrammar.g3:82:9: '(' expr ')' + { + dbg.Location( 82, 8 ); + char_literal27=(IToken)Match(input,12,Follow._12_in_atom368); + stream_12.Add(char_literal27); + + dbg.Location( 82, 12 ); + PushFollow(Follow._expr_in_atom370); + expr28=expr(); + + state._fsp--; + + stream_expr.Add(expr28.Tree); + dbg.Location( 82, 17 ); + char_literal29=(IToken)Match(input,13,Follow._13_in_atom372); + stream_13.Add(char_literal29); + + + + { + // AST REWRITE + // elements: expr + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + // wildcard labels: + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"rule retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.Nil(); + // 82:25: -> expr + { + dbg.Location( 82, 27 ); + adaptor.AddChild(root_0, stream_expr.NextTree()); + + } + + retval.tree = root_0; + } + + } + break; + case 4: + dbg.EnterAlt( 4 ); + + // BuildOptions\\DebugGrammar.g3:83:9: ID '(' expr ')' + { + dbg.Location( 83, 8 ); + ID30=(IToken)Match(input,ID,Follow._ID_in_atom389); + stream_ID.Add(ID30); + + dbg.Location( 83, 11 ); + char_literal31=(IToken)Match(input,12,Follow._12_in_atom391); + stream_12.Add(char_literal31); + + dbg.Location( 83, 15 ); + PushFollow(Follow._expr_in_atom393); + expr32=expr(); + + state._fsp--; + + stream_expr.Add(expr32.Tree); + dbg.Location( 83, 20 ); + char_literal33=(IToken)Match(input,13,Follow._13_in_atom395); + stream_13.Add(char_literal33); + + + + { + // AST REWRITE + // elements: ID, expr + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + // wildcard labels: + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"rule retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.Nil(); + // 83:25: -> ^( CALL ID expr ) + { + dbg.Location( 83, 27 ); + // BuildOptions\\DebugGrammar.g3:83:28: ^( CALL ID expr ) + { + CommonTree root_1 = (CommonTree)adaptor.Nil(); + dbg.Location( 83, 29 ); + root_1 = (CommonTree)adaptor.BecomeRoot((CommonTree)adaptor.Create(CALL, "CALL"), root_1); + + dbg.Location( 83, 34 ); + adaptor.AddChild(root_1, stream_ID.NextNode()); + dbg.Location( 83, 37 ); + adaptor.AddChild(root_1, stream_expr.NextTree()); + + adaptor.AddChild(root_0, root_1); + } + + } + + retval.tree = root_0; + } + + } + break; + + } + retval.stop = input.LT(-1); + + retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); + adaptor.SetTokenBoundaries(retval.tree, retval.start, retval.stop); + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + retval.tree = (CommonTree)adaptor.ErrorNode(input, retval.start, input.LT(-1), re); + + } + finally + { + } + dbg.Location(84, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "atom" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return retval; + } + // $ANTLR end "atom" + #endregion + + // Delegated rules + + #region Synpreds + #endregion + + #region DFA + DFA2 dfa2; + + protected override void InitDFAs() + { + base.InitDFAs(); + dfa2 = new DFA2( this ); + } + + class DFA2 : DFA + { + + const string DFA2_eotS = + "\xA\xFFFF"; + const string DFA2_eofS = + "\xA\xFFFF"; + const string DFA2_minS = + "\x1\x6\x1\xFFFF\x1\x8\x1\xFFFF\x1\x6\x1\xFFFF\x2\xA\x1\x8\x1\xFFFF"; + const string DFA2_maxS = + "\x1\xC\x1\xFFFF\x1\x11\x1\xFFFF\x1\xC\x1\xFFFF\x2\x10\x1\x11\x1\xFFFF"; + const string DFA2_acceptS = + "\x1\xFFFF\x1\x1\x1\xFFFF\x1\x4\x1\xFFFF\x1\x2\x3\xFFFF\x1\x3"; + const string DFA2_specialS = + "\xA\xFFFF}>"; + static readonly string[] DFA2_transitionS = + { + "\x1\x2\x1\x1\x1\x3\x3\xFFFF\x1\x1", + "", + "\x1\x1\x1\xFFFF\x2\x1\x1\x4\x1\xFFFF\x3\x1\x1\x5", + "", + "\x1\x7\x1\x6\x4\xFFFF\x1\x1", + "", + "\x2\x1\x1\xFFFF\x1\x8\x3\x1", + "\x3\x1\x1\x8\x3\x1", + "\x1\x1\x1\xFFFF\x2\x1\x2\xFFFF\x3\x1\x1\x9", + "" + }; + + static readonly short[] DFA2_eot = DFA.UnpackEncodedString(DFA2_eotS); + static readonly short[] DFA2_eof = DFA.UnpackEncodedString(DFA2_eofS); + static readonly char[] DFA2_min = DFA.UnpackEncodedStringToUnsignedChars(DFA2_minS); + static readonly char[] DFA2_max = DFA.UnpackEncodedStringToUnsignedChars(DFA2_maxS); + static readonly short[] DFA2_accept = DFA.UnpackEncodedString(DFA2_acceptS); + static readonly short[] DFA2_special = DFA.UnpackEncodedString(DFA2_specialS); + static readonly short[][] DFA2_transition; + + static DFA2() + { + int numStates = DFA2_transitionS.Length; + DFA2_transition = new short[numStates][]; + for ( int i=0; i < numStates; i++ ) + { + DFA2_transition[i] = DFA.UnpackEncodedString(DFA2_transitionS[i]); + } + } + + public DFA2( BaseRecognizer recognizer ) + { + this.recognizer = recognizer; + this.decisionNumber = 2; + this.eot = DFA2_eot; + this.eof = DFA2_eof; + this.min = DFA2_min; + this.max = DFA2_max; + this.accept = DFA2_accept; + this.special = DFA2_special; + this.transition = DFA2_transition; + } + public override string GetDescription() + { + return "53:0: stat : ( expr NEWLINE -> expr | ID '=' expr NEWLINE -> ^( '=' ID expr ) | func NEWLINE -> func | NEWLINE ->);"; + } + public override void Error( NoViableAltException nvae ) + { + ((DebugParser)recognizer).dbg.RecognitionException( nvae ); + } + } + + + #endregion + + #region Follow Sets + public static class Follow + { + public static readonly BitSet _stat_in_prog53 = new BitSet(new ulong[]{0x11C2UL}); + public static readonly BitSet _expr_in_stat70 = new BitSet(new ulong[]{0x100UL}); + public static readonly BitSet _NEWLINE_in_stat72 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _ID_in_stat105 = new BitSet(new ulong[]{0x20000UL}); + public static readonly BitSet _17_in_stat107 = new BitSet(new ulong[]{0x10C0UL}); + public static readonly BitSet _expr_in_stat109 = new BitSet(new ulong[]{0x100UL}); + public static readonly BitSet _NEWLINE_in_stat111 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _func_in_stat143 = new BitSet(new ulong[]{0x100UL}); + public static readonly BitSet _NEWLINE_in_stat145 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _NEWLINE_in_stat178 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _ID_in_func219 = new BitSet(new ulong[]{0x1000UL}); + public static readonly BitSet _12_in_func222 = new BitSet(new ulong[]{0xC0UL}); + public static readonly BitSet _formalPar_in_func224 = new BitSet(new ulong[]{0x2000UL}); + public static readonly BitSet _13_in_func226 = new BitSet(new ulong[]{0x20000UL}); + public static readonly BitSet _17_in_func228 = new BitSet(new ulong[]{0x10C0UL}); + public static readonly BitSet _expr_in_func230 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _set_in_formalPar267 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _multExpr_in_expr288 = new BitSet(new ulong[]{0x10402UL}); + public static readonly BitSet _16_in_expr292 = new BitSet(new ulong[]{0x10C0UL}); + public static readonly BitSet _10_in_expr295 = new BitSet(new ulong[]{0x10C0UL}); + public static readonly BitSet _multExpr_in_expr299 = new BitSet(new ulong[]{0x10402UL}); + public static readonly BitSet _atom_in_multExpr320 = new BitSet(new ulong[]{0xC802UL}); + public static readonly BitSet _set_in_multExpr323 = new BitSet(new ulong[]{0x10C0UL}); + public static readonly BitSet _atom_in_multExpr332 = new BitSet(new ulong[]{0xC802UL}); + public static readonly BitSet _INT_in_atom348 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _ID_in_atom358 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _12_in_atom368 = new BitSet(new ulong[]{0x10C0UL}); + public static readonly BitSet _expr_in_atom370 = new BitSet(new ulong[]{0x2000UL}); + public static readonly BitSet _13_in_atom372 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _ID_in_atom389 = new BitSet(new ulong[]{0x1000UL}); + public static readonly BitSet _12_in_atom391 = new BitSet(new ulong[]{0x10C0UL}); + public static readonly BitSet _expr_in_atom393 = new BitSet(new ulong[]{0x2000UL}); + public static readonly BitSet _13_in_atom395 = new BitSet(new ulong[]{0x2UL}); + + } + #endregion +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarParserHelper.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarParserHelper.cs new file mode 100644 index 0000000..95beb20 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugGrammarParserHelper.cs @@ -0,0 +1,40 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System.Collections.Generic; +using Antlr.Runtime.Tree; + +partial class DebugGrammarParser +{ + /** List of function definitions. Must point at the FUNC nodes. */ + List<CommonTree> functionDefinitions = new List<CommonTree>(); +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammar.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammar.cs new file mode 100644 index 0000000..38b1064 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammar.cs @@ -0,0 +1,858 @@ +// $ANTLR 3.1.2 BuildOptions\\DebugTreeGrammar.g3 2009-03-16 13:19:18 + +// The variable 'variable' is assigned but its value is never used. +#pragma warning disable 219 +// Unreachable code detected. +#pragma warning disable 162 + + +//import java.util.Map; +//import java.util.HashMap; +using BigInteger = java.math.BigInteger; +using Console = System.Console; + + +using System.Collections.Generic; +using Antlr.Runtime; +using Antlr.Runtime.Tree; +using RewriteRuleITokenStream = Antlr.Runtime.Tree.RewriteRuleTokenStream;using Stack = System.Collections.Generic.Stack<object>; +using List = System.Collections.IList; +using ArrayList = System.Collections.Generic.List<object>; + +using Antlr.Runtime.Debug; +using IOException = System.IO.IOException; +public partial class DebugTreeGrammar : DebugTreeParser +{ + public static readonly string[] tokenNames = new string[] { + "<invalid>", "<EOR>", "<DOWN>", "<UP>", "CALL", "FUNC", "ID", "INT", "NEWLINE", "WS", "'-'", "'%'", "'('", "')'", "'*'", "'/'", "'+'", "'='" + }; + public const int EOF=-1; + public const int T__10=10; + public const int T__11=11; + public const int T__12=12; + public const int T__13=13; + public const int T__14=14; + public const int T__15=15; + public const int T__16=16; + public const int T__17=17; + public const int CALL=4; + public const int FUNC=5; + public const int ID=6; + public const int INT=7; + public const int NEWLINE=8; + public const int WS=9; + + // delegates + // delegators + + public static readonly string[] ruleNames = + new string[] + { + "invalidRule", "call", "expr", "prog", "stat" + }; + + int ruleLevel = 0; + public virtual int RuleLevel { get { return ruleLevel; } } + public virtual void IncRuleLevel() { ruleLevel++; } + public virtual void DecRuleLevel() { ruleLevel--; } + public DebugTreeGrammar( ITreeNodeStream input ) + : this( input, DebugEventSocketProxy.DEFAULT_DEBUGGER_PORT, new RecognizerSharedState() ) + { + } + public DebugTreeGrammar( ITreeNodeStream input, int port, RecognizerSharedState state ) + : base( input, state ) + { + DebugEventSocketProxy proxy = new DebugEventSocketProxy( this, port, input.TreeAdaptor ); + DebugListener = proxy; + try + { + proxy.Handshake(); + } + catch ( IOException ioe ) + { + ReportError( ioe ); + } + } + public DebugTreeGrammar( ITreeNodeStream input, IDebugEventListener dbg ) + : base( input, dbg, new RecognizerSharedState() ) + { + + } + protected virtual bool EvalPredicate( bool result, string predicate ) + { + dbg.SemanticPredicate( result, predicate ); + return result; + } + + + public override string[] GetTokenNames() { return DebugTreeGrammar.tokenNames; } + public override string GrammarFileName { get { return "BuildOptions\\DebugTreeGrammar.g3"; } } + + + #region Rules + + // $ANTLR start "prog" + // BuildOptions\\DebugTreeGrammar.g3:53:0: prog : ( stat )* ; + private void prog( ) + { + try + { + dbg.EnterRule( GrammarFileName, "prog" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 53, -1 ); + + try + { + // BuildOptions\\DebugTreeGrammar.g3:53:9: ( ( stat )* ) + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugTreeGrammar.g3:53:9: ( stat )* + { + dbg.Location( 53, 8 ); + // BuildOptions\\DebugTreeGrammar.g3:53:9: ( stat )* + try + { + dbg.EnterSubRule( 1 ); + + for ( ; ; ) + { + int alt1=2; + try + { + dbg.EnterDecision( 1 ); + + int LA1_0 = input.LA(1); + + if ( ((LA1_0>=CALL && LA1_0<=INT)||(LA1_0>=10 && LA1_0<=11)||(LA1_0>=14 && LA1_0<=17)) ) + { + alt1=1; + } + + + } + finally + { + dbg.ExitDecision( 1 ); + } + + switch ( alt1 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugTreeGrammar.g3:53:0: stat + { + dbg.Location( 53, 8 ); + PushFollow(Follow._stat_in_prog48); + stat(); + + state._fsp--; + + + } + break; + + default: + goto loop1; + } + } + + loop1: + ; + + } + finally + { + dbg.ExitSubRule( 1 ); + } + + + } + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + } + finally + { + } + dbg.Location(54, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "prog" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return ; + } + // $ANTLR end "prog" + + + // $ANTLR start "stat" + // BuildOptions\\DebugTreeGrammar.g3:56:0: stat : ( expr | ^( '=' ID expr ) | ^( FUNC ( . )+ ) ); + private void stat( ) + { + CommonTree ID2=null; + BigInteger expr1 = default(BigInteger); + BigInteger expr3 = default(BigInteger); + + try + { + dbg.EnterRule( GrammarFileName, "stat" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 56, -1 ); + + try + { + // BuildOptions\\DebugTreeGrammar.g3:56:9: ( expr | ^( '=' ID expr ) | ^( FUNC ( . )+ ) ) + int alt3=3; + try + { + dbg.EnterDecision( 3 ); + + switch ( input.LA(1) ) + { + case CALL: + case ID: + case INT: + case 10: + case 11: + case 14: + case 15: + case 16: + { + alt3=1; + } + break; + case 17: + { + alt3=2; + } + break; + case FUNC: + { + alt3=3; + } + break; + default: + { + NoViableAltException nvae = new NoViableAltException("", 3, 0, input); + + dbg.RecognitionException( nvae ); + throw nvae; + } + } + + } + finally + { + dbg.ExitDecision( 3 ); + } + + switch ( alt3 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugTreeGrammar.g3:56:9: expr + { + dbg.Location( 56, 8 ); + PushFollow(Follow._expr_in_stat63); + expr1=expr(); + + state._fsp--; + + dbg.Location( 56, 35 ); + string result = expr1.ToString(); + Console.Out.WriteLine(expr1 + " (about " + result[0] + "*10^" + (result.Length-1) + ")"); + + + } + break; + case 2: + dbg.EnterAlt( 2 ); + + // BuildOptions\\DebugTreeGrammar.g3:59:9: ^( '=' ID expr ) + { + dbg.Location( 59, 8 ); + dbg.Location( 59, 10 ); + Match(input,17,Follow._17_in_stat98); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 59, 14 ); + ID2=(CommonTree)Match(input,ID,Follow._ID_in_stat100); + dbg.Location( 59, 17 ); + PushFollow(Follow._expr_in_stat102); + expr3=expr(); + + state._fsp--; + + + Match(input, TokenConstants.UP, null); + dbg.Location( 59, 35 ); + globalMemory[(ID2!=null?ID2.Text:null)] = expr3; + + } + break; + case 3: + dbg.EnterAlt( 3 ); + + // BuildOptions\\DebugTreeGrammar.g3:60:9: ^( FUNC ( . )+ ) + { + dbg.Location( 60, 8 ); + dbg.Location( 60, 10 ); + Match(input,FUNC,Follow._FUNC_in_stat128); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 60, 15 ); + // BuildOptions\\DebugTreeGrammar.g3:60:16: ( . )+ + int cnt2=0; + try + { + dbg.EnterSubRule( 2 ); + + for ( ; ; ) + { + int alt2=2; + try + { + dbg.EnterDecision( 2 ); + + int LA2_0 = input.LA(1); + + if ( ((LA2_0>=CALL && LA2_0<=17)) ) + { + alt2=1; + } + else if ( (LA2_0==UP) ) + { + alt2=2; + } + + + } + finally + { + dbg.ExitDecision( 2 ); + } + + switch ( alt2 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugTreeGrammar.g3:60:0: . + { + dbg.Location( 60, 15 ); + MatchAny(input); + + } + break; + + default: + if ( cnt2 >= 1 ) + goto loop2; + + EarlyExitException eee2 = new EarlyExitException( 2, input ); + dbg.RecognitionException( eee2 ); + + throw eee2; + } + cnt2++; + } + loop2: + ; + + } + finally + { + dbg.ExitSubRule( 2 ); + } + + + Match(input, TokenConstants.UP, null); + + } + break; + + } + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + } + finally + { + } + dbg.Location(61, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "stat" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return ; + } + // $ANTLR end "stat" + + + // $ANTLR start "expr" + // BuildOptions\\DebugTreeGrammar.g3:63:0: expr returns [BigInteger value] : ( ^( '+' a= expr b= expr ) | ^( '-' a= expr b= expr ) | ^( '*' a= expr b= expr ) | ^( '/' a= expr b= expr ) | ^( '%' a= expr b= expr ) | ID | INT | call ); + private BigInteger expr( ) + { + + BigInteger value = default(BigInteger); + + CommonTree ID4=null; + CommonTree INT5=null; + BigInteger a = default(BigInteger); + BigInteger b = default(BigInteger); + BigInteger call6 = default(BigInteger); + + try + { + dbg.EnterRule( GrammarFileName, "expr" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 63, -1 ); + + try + { + // BuildOptions\\DebugTreeGrammar.g3:64:9: ( ^( '+' a= expr b= expr ) | ^( '-' a= expr b= expr ) | ^( '*' a= expr b= expr ) | ^( '/' a= expr b= expr ) | ^( '%' a= expr b= expr ) | ID | INT | call ) + int alt4=8; + try + { + dbg.EnterDecision( 4 ); + + switch ( input.LA(1) ) + { + case 16: + { + alt4=1; + } + break; + case 10: + { + alt4=2; + } + break; + case 14: + { + alt4=3; + } + break; + case 15: + { + alt4=4; + } + break; + case 11: + { + alt4=5; + } + break; + case ID: + { + alt4=6; + } + break; + case INT: + { + alt4=7; + } + break; + case CALL: + { + alt4=8; + } + break; + default: + { + NoViableAltException nvae = new NoViableAltException("", 4, 0, input); + + dbg.RecognitionException( nvae ); + throw nvae; + } + } + + } + finally + { + dbg.ExitDecision( 4 ); + } + + switch ( alt4 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugTreeGrammar.g3:64:9: ^( '+' a= expr b= expr ) + { + dbg.Location( 64, 8 ); + dbg.Location( 64, 10 ); + Match(input,16,Follow._16_in_expr172); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 64, 15 ); + PushFollow(Follow._expr_in_expr176); + a=expr(); + + state._fsp--; + + dbg.Location( 64, 22 ); + PushFollow(Follow._expr_in_expr180); + b=expr(); + + state._fsp--; + + + Match(input, TokenConstants.UP, null); + dbg.Location( 64, 35 ); + value = a.add(b); + + } + break; + case 2: + dbg.EnterAlt( 2 ); + + // BuildOptions\\DebugTreeGrammar.g3:65:9: ^( '-' a= expr b= expr ) + { + dbg.Location( 65, 8 ); + dbg.Location( 65, 10 ); + Match(input,10,Follow._10_in_expr200); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 65, 15 ); + PushFollow(Follow._expr_in_expr204); + a=expr(); + + state._fsp--; + + dbg.Location( 65, 22 ); + PushFollow(Follow._expr_in_expr208); + b=expr(); + + state._fsp--; + + + Match(input, TokenConstants.UP, null); + dbg.Location( 65, 35 ); + value = a.subtract(b); + + } + break; + case 3: + dbg.EnterAlt( 3 ); + + // BuildOptions\\DebugTreeGrammar.g3:66:9: ^( '*' a= expr b= expr ) + { + dbg.Location( 66, 8 ); + dbg.Location( 66, 10 ); + Match(input,14,Follow._14_in_expr228); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 66, 15 ); + PushFollow(Follow._expr_in_expr232); + a=expr(); + + state._fsp--; + + dbg.Location( 66, 22 ); + PushFollow(Follow._expr_in_expr236); + b=expr(); + + state._fsp--; + + + Match(input, TokenConstants.UP, null); + dbg.Location( 66, 35 ); + value = a.multiply(b); + + } + break; + case 4: + dbg.EnterAlt( 4 ); + + // BuildOptions\\DebugTreeGrammar.g3:67:9: ^( '/' a= expr b= expr ) + { + dbg.Location( 67, 8 ); + dbg.Location( 67, 10 ); + Match(input,15,Follow._15_in_expr256); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 67, 15 ); + PushFollow(Follow._expr_in_expr260); + a=expr(); + + state._fsp--; + + dbg.Location( 67, 22 ); + PushFollow(Follow._expr_in_expr264); + b=expr(); + + state._fsp--; + + + Match(input, TokenConstants.UP, null); + dbg.Location( 67, 35 ); + value = a.divide(b); + + } + break; + case 5: + dbg.EnterAlt( 5 ); + + // BuildOptions\\DebugTreeGrammar.g3:68:9: ^( '%' a= expr b= expr ) + { + dbg.Location( 68, 8 ); + dbg.Location( 68, 10 ); + Match(input,11,Follow._11_in_expr284); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 68, 15 ); + PushFollow(Follow._expr_in_expr288); + a=expr(); + + state._fsp--; + + dbg.Location( 68, 22 ); + PushFollow(Follow._expr_in_expr292); + b=expr(); + + state._fsp--; + + + Match(input, TokenConstants.UP, null); + dbg.Location( 68, 35 ); + value = a.remainder(b); + + } + break; + case 6: + dbg.EnterAlt( 6 ); + + // BuildOptions\\DebugTreeGrammar.g3:69:9: ID + { + dbg.Location( 69, 8 ); + ID4=(CommonTree)Match(input,ID,Follow._ID_in_expr311); + dbg.Location( 69, 35 ); + value = getValue((ID4!=null?ID4.Text:null)); + + } + break; + case 7: + dbg.EnterAlt( 7 ); + + // BuildOptions\\DebugTreeGrammar.g3:70:9: INT + { + dbg.Location( 70, 8 ); + INT5=(CommonTree)Match(input,INT,Follow._INT_in_expr347); + dbg.Location( 70, 35 ); + value = new BigInteger((INT5!=null?INT5.Text:null)); + + } + break; + case 8: + dbg.EnterAlt( 8 ); + + // BuildOptions\\DebugTreeGrammar.g3:71:9: call + { + dbg.Location( 71, 8 ); + PushFollow(Follow._call_in_expr382); + call6=call(); + + state._fsp--; + + dbg.Location( 71, 35 ); + value = call6; + + } + break; + + } + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + } + finally + { + } + dbg.Location(72, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "expr" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return value; + } + // $ANTLR end "expr" + + + // $ANTLR start "call" + // BuildOptions\\DebugTreeGrammar.g3:74:0: call returns [BigInteger value] : ^( CALL ID expr ) ; + private BigInteger call( ) + { + + BigInteger value = default(BigInteger); + + CommonTree ID8=null; + BigInteger expr7 = default(BigInteger); + + try + { + dbg.EnterRule( GrammarFileName, "call" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 74, -1 ); + + try + { + // BuildOptions\\DebugTreeGrammar.g3:75:9: ( ^( CALL ID expr ) ) + dbg.EnterAlt( 1 ); + + // BuildOptions\\DebugTreeGrammar.g3:75:9: ^( CALL ID expr ) + { + dbg.Location( 75, 8 ); + dbg.Location( 75, 10 ); + Match(input,CALL,Follow._CALL_in_call430); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 75, 15 ); + ID8=(CommonTree)Match(input,ID,Follow._ID_in_call432); + dbg.Location( 75, 18 ); + PushFollow(Follow._expr_in_call434); + expr7=expr(); + + state._fsp--; + + + Match(input, TokenConstants.UP, null); + dbg.Location( 75, 35 ); + BigInteger p = expr7; + CommonTree funcRoot = findFunction((ID8!=null?ID8.Text:null), p); + if (funcRoot == null) { + Console.Error.WriteLine("No match found for " + (ID8!=null?ID8.Text:null) + "(" + p + ")"); + } else { + // Here we set up the local evaluator to run over the + // function definition with the parameter value. + // This re-reads a sub-AST of our input AST! + DebugTreeGrammar e = new DebugTreeGrammar(funcRoot, functionDefinitions, globalMemory, p); + value = e.expr(); + } + + + } + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + } + finally + { + } + dbg.Location(87, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "call" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return value; + } + // $ANTLR end "call" + #endregion + + // Delegated rules + + #region Synpreds + #endregion + + #region DFA + + protected override void InitDFAs() + { + base.InitDFAs(); + } + + #endregion + + #region Follow Sets + public static class Follow + { + public static readonly BitSet _stat_in_prog48 = new BitSet(new ulong[]{0x3CCF2UL}); + public static readonly BitSet _expr_in_stat63 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _17_in_stat98 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _ID_in_stat100 = new BitSet(new ulong[]{0x1CCD0UL}); + public static readonly BitSet _expr_in_stat102 = new BitSet(new ulong[]{0x8UL}); + public static readonly BitSet _FUNC_in_stat128 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _16_in_expr172 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _expr_in_expr176 = new BitSet(new ulong[]{0x1CCD0UL}); + public static readonly BitSet _expr_in_expr180 = new BitSet(new ulong[]{0x8UL}); + public static readonly BitSet _10_in_expr200 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _expr_in_expr204 = new BitSet(new ulong[]{0x1CCD0UL}); + public static readonly BitSet _expr_in_expr208 = new BitSet(new ulong[]{0x8UL}); + public static readonly BitSet _14_in_expr228 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _expr_in_expr232 = new BitSet(new ulong[]{0x1CCD0UL}); + public static readonly BitSet _expr_in_expr236 = new BitSet(new ulong[]{0x8UL}); + public static readonly BitSet _15_in_expr256 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _expr_in_expr260 = new BitSet(new ulong[]{0x1CCD0UL}); + public static readonly BitSet _expr_in_expr264 = new BitSet(new ulong[]{0x8UL}); + public static readonly BitSet _11_in_expr284 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _expr_in_expr288 = new BitSet(new ulong[]{0x1CCD0UL}); + public static readonly BitSet _expr_in_expr292 = new BitSet(new ulong[]{0x8UL}); + public static readonly BitSet _ID_in_expr311 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _INT_in_expr347 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _call_in_expr382 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _CALL_in_call430 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _ID_in_call432 = new BitSet(new ulong[]{0x1CCD0UL}); + public static readonly BitSet _expr_in_call434 = new BitSet(new ulong[]{0x8UL}); + + } + #endregion +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammar.g3 b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammar.g3 new file mode 100644 index 0000000..b16a73e --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammar.g3 @@ -0,0 +1,88 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +tree grammar DebugTreeGrammar; + +options +{ + language=CSharp3; + tokenVocab=DebugGrammar; + ASTLabelType=CommonTree; +} + +// START:members +@header +{ +//import java.util.Map; +//import java.util.HashMap; +using BigInteger = java.math.BigInteger; +using Console = System.Console; +} +// END:members + +// START:rules +prog: stat* + ; + +stat: expr { string result = $expr.value.ToString(); + Console.Out.WriteLine($expr.value + " (about " + result[0] + "*10^" + (result.Length-1) + ")"); + } + | ^('=' ID expr) { globalMemory[$ID.text] = $expr.value; } + | ^(FUNC .+) // ignore FUNCs - we added them to functionDefinitions already in parser. + ; + +expr returns [BigInteger value] + : ^('+' a=expr b=expr) { $value = $a.value.add($b.value); } + | ^('-' a=expr b=expr) { $value = $a.value.subtract($b.value); } + | ^('*' a=expr b=expr) { $value = $a.value.multiply($b.value); } + | ^('/' a=expr b=expr) { $value = $a.value.divide($b.value); } + | ^('%' a=expr b=expr) { $value = $a.value.remainder($b.value); } + | ID { $value = getValue($ID.text); } + | INT { $value = new BigInteger($INT.text); } + | call { $value = $call.value; } + ; + +call returns [BigInteger value] + : ^(CALL ID expr) { BigInteger p = $expr.value; + CommonTree funcRoot = findFunction($ID.text, p); + if (funcRoot == null) { + Console.Error.WriteLine("No match found for " + $ID.text + "(" + p + ")"); + } else { + // Here we set up the local evaluator to run over the + // function definition with the parameter value. + // This re-reads a sub-AST of our input AST! + DebugTreeGrammar e = new DebugTreeGrammar(funcRoot, functionDefinitions, globalMemory, p); + $value = e.expr(); + } + } + ; +// END:rules diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammarHelper.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammarHelper.cs new file mode 100644 index 0000000..189e733 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/DebugTreeGrammarHelper.cs @@ -0,0 +1,116 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System.Collections.Generic; +using Antlr.Runtime.Tree; + +using BigInteger = java.math.BigInteger; +using Console = System.Console; + +partial class DebugTreeGrammar +{ + /** Points to functions tracked by tree builder. */ + private List<CommonTree> functionDefinitions; + + /** Remember local variables. Currently, this is only the function parameter. + */ + private readonly IDictionary<string, BigInteger> localMemory = new Dictionary<string, BigInteger>(); + + /** Remember global variables set by =. */ + private IDictionary<string, BigInteger> globalMemory = new Dictionary<string, BigInteger>(); + + /** Set up an evaluator with a node stream; and a set of function definition ASTs. */ + public DebugTreeGrammar( CommonTreeNodeStream nodes, List<CommonTree> functionDefinitions ) + : this( nodes ) + { + this.functionDefinitions = functionDefinitions; + } + + /** Set up a local evaluator for a nested function call. The evaluator gets the definition + * tree of the function; the set of all defined functions (to find locally called ones); a + * pointer to the global variable memory; and the value of the function parameter to be + * added to the local memory. + */ + private DebugTreeGrammar( CommonTree function, + List<CommonTree> functionDefinitions, + IDictionary<string, BigInteger> globalMemory, + BigInteger paramValue ) + // Expected tree for function: ^(FUNC ID ( INT | ID ) expr) + : this( new CommonTreeNodeStream( function.GetChild( 2 ) ), functionDefinitions ) + { + this.globalMemory = globalMemory; + localMemory[function.GetChild( 1 ).Text] = paramValue; + } + + /** Find matching function definition for a function name and parameter + * value. The first definition is returned where (a) the name matches + * and (b) the formal parameter agrees if it is defined as constant. + */ + private CommonTree findFunction( string name, BigInteger paramValue ) + { + foreach ( CommonTree f in functionDefinitions ) + { + // Expected tree for f: ^(FUNC ID (ID | INT) expr) + if ( f.GetChild( 0 ).Text.Equals( name ) ) + { + // Check whether parameter matches + CommonTree formalPar = (CommonTree)f.GetChild( 1 ); + if ( formalPar.Token.Type == INT + && !new BigInteger( formalPar.Token.Text ).Equals( paramValue ) ) + { + // Constant in formalPar list does not match actual value -> no match. + continue; + } + // Parameter (value for INT formal arg) as well as fct name agrees! + return f; + } + } + return null; + } + + /** Get value of name up call stack. */ + public BigInteger getValue( string name ) + { + BigInteger value; + if ( localMemory.TryGetValue( name, out value ) && value != null ) + { + return value; + } + if ( globalMemory.TryGetValue( name, out value ) && value != null ) + { + return value; + } + // not found in local memory or global memory + Console.Error.WriteLine( "undefined variable " + name ); + return new BigInteger( "0" ); + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammar.g3 b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammar.g3 new file mode 100644 index 0000000..5f8de16 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammar.g3 @@ -0,0 +1,100 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +grammar ProfileGrammar; + +options +{ + language=CSharp3; + output=AST; + ASTLabelType=CommonTree; +} + +tokens +{ + // define pseudo-operations + FUNC; + CALL; +} + +// START:stat +prog: ( stat )* + ; + +stat: expr NEWLINE -> expr + | ID '=' expr NEWLINE -> ^('=' ID expr) + | func NEWLINE -> func + | NEWLINE -> // ignore + ; + +func: ID '(' formalPar ')' '=' expr -> ^(FUNC ID formalPar expr) + ; + finally { + functionDefinitions.Add($func.tree); + } + +formalPar + : ID + | INT + ; + +// END:stat + +// START:expr +expr: multExpr (('+'^|'-'^) multExpr)* + ; + +multExpr + : atom (('*'|'/'|'%')^ atom)* + ; + +atom: INT + | ID + | '(' expr ')' -> expr + | ID '(' expr ')' -> ^(CALL ID expr) + ; +// END:expr + +// START:tokens +ID : ('a'..'z'|'A'..'Z')+ + ; + +INT : '0'..'9'+ + ; + +NEWLINE + : '\r'? '\n' + ; + +WS : (' '|'\t')+ { Skip(); } + ; +// END:tokens diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarLexer.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarLexer.cs new file mode 100644 index 0000000..5296172 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarLexer.cs @@ -0,0 +1,691 @@ +// $ANTLR 3.1.2 BuildOptions\\ProfileGrammar.g3 2009-03-16 13:19:19 + +// The variable 'variable' is assigned but its value is never used. +#pragma warning disable 219 +// Unreachable code detected. +#pragma warning disable 162 + + +using System.Collections.Generic; +using Antlr.Runtime; +using Stack = System.Collections.Generic.Stack<object>; +using List = System.Collections.IList; +using ArrayList = System.Collections.Generic.List<object>; + +public partial class ProfileGrammarLexer : Lexer +{ + public const int EOF=-1; + public const int T__10=10; + public const int T__11=11; + public const int T__12=12; + public const int T__13=13; + public const int T__14=14; + public const int T__15=15; + public const int T__16=16; + public const int T__17=17; + public const int CALL=4; + public const int FUNC=5; + public const int ID=6; + public const int INT=7; + public const int NEWLINE=8; + public const int WS=9; + + // delegates + // delegators + + public ProfileGrammarLexer() {} + public ProfileGrammarLexer( ICharStream input ) + : this( input, new RecognizerSharedState() ) + { + } + public ProfileGrammarLexer( ICharStream input, RecognizerSharedState state ) + : base( input, state ) + { + + } + public override string GrammarFileName { get { return "BuildOptions\\ProfileGrammar.g3"; } } + + // $ANTLR start "T__10" + private void mT__10() + { + try + { + int _type = T__10; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\ProfileGrammar.g3:7:9: ( '-' ) + // BuildOptions\\ProfileGrammar.g3:7:9: '-' + { + Match('-'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__10" + + // $ANTLR start "T__11" + private void mT__11() + { + try + { + int _type = T__11; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\ProfileGrammar.g3:8:9: ( '%' ) + // BuildOptions\\ProfileGrammar.g3:8:9: '%' + { + Match('%'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__11" + + // $ANTLR start "T__12" + private void mT__12() + { + try + { + int _type = T__12; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\ProfileGrammar.g3:9:9: ( '(' ) + // BuildOptions\\ProfileGrammar.g3:9:9: '(' + { + Match('('); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__12" + + // $ANTLR start "T__13" + private void mT__13() + { + try + { + int _type = T__13; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\ProfileGrammar.g3:10:9: ( ')' ) + // BuildOptions\\ProfileGrammar.g3:10:9: ')' + { + Match(')'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__13" + + // $ANTLR start "T__14" + private void mT__14() + { + try + { + int _type = T__14; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\ProfileGrammar.g3:11:9: ( '*' ) + // BuildOptions\\ProfileGrammar.g3:11:9: '*' + { + Match('*'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__14" + + // $ANTLR start "T__15" + private void mT__15() + { + try + { + int _type = T__15; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\ProfileGrammar.g3:12:9: ( '/' ) + // BuildOptions\\ProfileGrammar.g3:12:9: '/' + { + Match('/'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__15" + + // $ANTLR start "T__16" + private void mT__16() + { + try + { + int _type = T__16; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\ProfileGrammar.g3:13:9: ( '+' ) + // BuildOptions\\ProfileGrammar.g3:13:9: '+' + { + Match('+'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__16" + + // $ANTLR start "T__17" + private void mT__17() + { + try + { + int _type = T__17; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\ProfileGrammar.g3:14:9: ( '=' ) + // BuildOptions\\ProfileGrammar.g3:14:9: '=' + { + Match('='); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__17" + + // $ANTLR start "ID" + private void mID() + { + try + { + int _type = ID; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\ProfileGrammar.g3:88:9: ( ( 'a' .. 'z' | 'A' .. 'Z' )+ ) + // BuildOptions\\ProfileGrammar.g3:88:9: ( 'a' .. 'z' | 'A' .. 'Z' )+ + { + // BuildOptions\\ProfileGrammar.g3:88:9: ( 'a' .. 'z' | 'A' .. 'Z' )+ + int cnt1=0; + for ( ; ; ) + { + int alt1=2; + int LA1_0 = input.LA(1); + + if ( ((LA1_0>='A' && LA1_0<='Z')||(LA1_0>='a' && LA1_0<='z')) ) + { + alt1=1; + } + + + switch ( alt1 ) + { + case 1: + // BuildOptions\\ProfileGrammar.g3: + { + input.Consume(); + + + } + break; + + default: + if ( cnt1 >= 1 ) + goto loop1; + + EarlyExitException eee1 = new EarlyExitException( 1, input ); + throw eee1; + } + cnt1++; + } + loop1: + ; + + + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "ID" + + // $ANTLR start "INT" + private void mINT() + { + try + { + int _type = INT; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\ProfileGrammar.g3:91:9: ( ( '0' .. '9' )+ ) + // BuildOptions\\ProfileGrammar.g3:91:9: ( '0' .. '9' )+ + { + // BuildOptions\\ProfileGrammar.g3:91:9: ( '0' .. '9' )+ + int cnt2=0; + for ( ; ; ) + { + int alt2=2; + int LA2_0 = input.LA(1); + + if ( ((LA2_0>='0' && LA2_0<='9')) ) + { + alt2=1; + } + + + switch ( alt2 ) + { + case 1: + // BuildOptions\\ProfileGrammar.g3: + { + input.Consume(); + + + } + break; + + default: + if ( cnt2 >= 1 ) + goto loop2; + + EarlyExitException eee2 = new EarlyExitException( 2, input ); + throw eee2; + } + cnt2++; + } + loop2: + ; + + + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "INT" + + // $ANTLR start "NEWLINE" + private void mNEWLINE() + { + try + { + int _type = NEWLINE; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\ProfileGrammar.g3:95:7: ( ( '\\r' )? '\\n' ) + // BuildOptions\\ProfileGrammar.g3:95:7: ( '\\r' )? '\\n' + { + // BuildOptions\\ProfileGrammar.g3:95:7: ( '\\r' )? + int alt3=2; + int LA3_0 = input.LA(1); + + if ( (LA3_0=='\r') ) + { + alt3=1; + } + switch ( alt3 ) + { + case 1: + // BuildOptions\\ProfileGrammar.g3:95:0: '\\r' + { + Match('\r'); + + } + break; + + } + + Match('\n'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "NEWLINE" + + // $ANTLR start "WS" + private void mWS() + { + try + { + int _type = WS; + int _channel = DEFAULT_TOKEN_CHANNEL; + // BuildOptions\\ProfileGrammar.g3:98:9: ( ( ' ' | '\\t' )+ ) + // BuildOptions\\ProfileGrammar.g3:98:9: ( ' ' | '\\t' )+ + { + // BuildOptions\\ProfileGrammar.g3:98:9: ( ' ' | '\\t' )+ + int cnt4=0; + for ( ; ; ) + { + int alt4=2; + int LA4_0 = input.LA(1); + + if ( (LA4_0=='\t'||LA4_0==' ') ) + { + alt4=1; + } + + + switch ( alt4 ) + { + case 1: + // BuildOptions\\ProfileGrammar.g3: + { + input.Consume(); + + + } + break; + + default: + if ( cnt4 >= 1 ) + goto loop4; + + EarlyExitException eee4 = new EarlyExitException( 4, input ); + throw eee4; + } + cnt4++; + } + loop4: + ; + + + Skip(); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "WS" + + public override void mTokens() + { + // BuildOptions\\ProfileGrammar.g3:1:10: ( T__10 | T__11 | T__12 | T__13 | T__14 | T__15 | T__16 | T__17 | ID | INT | NEWLINE | WS ) + int alt5=12; + switch ( input.LA(1) ) + { + case '-': + { + alt5=1; + } + break; + case '%': + { + alt5=2; + } + break; + case '(': + { + alt5=3; + } + break; + case ')': + { + alt5=4; + } + break; + case '*': + { + alt5=5; + } + break; + case '/': + { + alt5=6; + } + break; + case '+': + { + alt5=7; + } + break; + case '=': + { + alt5=8; + } + break; + case 'A': + case 'B': + case 'C': + case 'D': + case 'E': + case 'F': + case 'G': + case 'H': + case 'I': + case 'J': + case 'K': + case 'L': + case 'M': + case 'N': + case 'O': + case 'P': + case 'Q': + case 'R': + case 'S': + case 'T': + case 'U': + case 'V': + case 'W': + case 'X': + case 'Y': + case 'Z': + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + case 'g': + case 'h': + case 'i': + case 'j': + case 'k': + case 'l': + case 'm': + case 'n': + case 'o': + case 'p': + case 'q': + case 'r': + case 's': + case 't': + case 'u': + case 'v': + case 'w': + case 'x': + case 'y': + case 'z': + { + alt5=9; + } + break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + { + alt5=10; + } + break; + case '\n': + case '\r': + { + alt5=11; + } + break; + case '\t': + case ' ': + { + alt5=12; + } + break; + default: + { + NoViableAltException nvae = new NoViableAltException("", 5, 0, input); + + throw nvae; + } + } + + switch ( alt5 ) + { + case 1: + // BuildOptions\\ProfileGrammar.g3:1:10: T__10 + { + mT__10(); + + } + break; + case 2: + // BuildOptions\\ProfileGrammar.g3:1:16: T__11 + { + mT__11(); + + } + break; + case 3: + // BuildOptions\\ProfileGrammar.g3:1:22: T__12 + { + mT__12(); + + } + break; + case 4: + // BuildOptions\\ProfileGrammar.g3:1:28: T__13 + { + mT__13(); + + } + break; + case 5: + // BuildOptions\\ProfileGrammar.g3:1:34: T__14 + { + mT__14(); + + } + break; + case 6: + // BuildOptions\\ProfileGrammar.g3:1:40: T__15 + { + mT__15(); + + } + break; + case 7: + // BuildOptions\\ProfileGrammar.g3:1:46: T__16 + { + mT__16(); + + } + break; + case 8: + // BuildOptions\\ProfileGrammar.g3:1:52: T__17 + { + mT__17(); + + } + break; + case 9: + // BuildOptions\\ProfileGrammar.g3:1:58: ID + { + mID(); + + } + break; + case 10: + // BuildOptions\\ProfileGrammar.g3:1:61: INT + { + mINT(); + + } + break; + case 11: + // BuildOptions\\ProfileGrammar.g3:1:65: NEWLINE + { + mNEWLINE(); + + } + break; + case 12: + // BuildOptions\\ProfileGrammar.g3:1:73: WS + { + mWS(); + + } + break; + + } + + } + + + #region DFA + + protected override void InitDFAs() + { + base.InitDFAs(); + } + + + #endregion + +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarLexerHelper.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarLexerHelper.cs new file mode 100644 index 0000000..7271295 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarLexerHelper.cs @@ -0,0 +1,32 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarParser.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarParser.cs new file mode 100644 index 0000000..7760525 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarParser.cs @@ -0,0 +1,1554 @@ +// $ANTLR 3.1.2 BuildOptions\\ProfileGrammar.g3 2009-03-16 13:19:19 + +// The variable 'variable' is assigned but its value is never used. +#pragma warning disable 219 +// Unreachable code detected. +#pragma warning disable 162 + + +using System.Collections.Generic; +using Antlr.Runtime; +using Stack = System.Collections.Generic.Stack<object>; +using List = System.Collections.IList; +using ArrayList = System.Collections.Generic.List<object>; + +using Antlr.Runtime.Debug; +using IOException = System.IO.IOException; + +using Antlr.Runtime.Tree; +using RewriteRuleITokenStream = Antlr.Runtime.Tree.RewriteRuleTokenStream; + +public partial class ProfileGrammarParser : DebugParser +{ + public static readonly string[] tokenNames = new string[] { + "<invalid>", "<EOR>", "<DOWN>", "<UP>", "CALL", "FUNC", "ID", "INT", "NEWLINE", "WS", "'-'", "'%'", "'('", "')'", "'*'", "'/'", "'+'", "'='" + }; + public const int EOF=-1; + public const int T__10=10; + public const int T__11=11; + public const int T__12=12; + public const int T__13=13; + public const int T__14=14; + public const int T__15=15; + public const int T__16=16; + public const int T__17=17; + public const int CALL=4; + public const int FUNC=5; + public const int ID=6; + public const int INT=7; + public const int NEWLINE=8; + public const int WS=9; + + // delegates + // delegators + + public static readonly string[] ruleNames = + new string[] + { + "invalidRule", "atom", "expr", "formalPar", "func", "multExpr", "prog", + "stat" + }; + + int ruleLevel = 0; + public virtual int RuleLevel { get { return ruleLevel; } } + public virtual void IncRuleLevel() { ruleLevel++; } + public virtual void DecRuleLevel() { ruleLevel--; } + public ProfileGrammarParser( ITokenStream input ) + : this( input, new Profiler(null), new RecognizerSharedState() ) + { + } + public ProfileGrammarParser( ITokenStream input, IDebugEventListener dbg, RecognizerSharedState state ) + : base( input, dbg, state ) + { + Profiler p = (Profiler)dbg; + p.setParser(this); + ITreeAdaptor adap = new CommonTreeAdaptor(); + TreeAdaptor = adap; + proxy.TreeAdaptor = adap; + } + + public ProfileGrammarParser( ITokenStream input, IDebugEventListener dbg ) + : base( input, dbg ) + { + Profiler p = (Profiler)dbg; + p.setParser(this); + + ITreeAdaptor adap = new CommonTreeAdaptor(); + TreeAdaptor = adap; + + } + public virtual bool AlreadyParsedRule( IIntStream input, int ruleIndex ) + { + ((Profiler)dbg).ExamineRuleMemoization(input, ruleIndex, ProfileGrammarParser.ruleNames[ruleIndex]); + return super.AlreadyParsedRule(input, ruleIndex); + } + + public virtual void Memoize( IIntStream input, int ruleIndex, int ruleStartIndex ) + { + ((Profiler)dbg).Memoize(input, ruleIndex, ruleStartIndex, ProfileGrammarParser.ruleNames[ruleIndex]); + super.Memoize(input, ruleIndex, ruleStartIndex); + } + protected virtual bool EvalPredicate( bool result, string predicate ) + { + dbg.SemanticPredicate( result, predicate ); + return result; + } + + protected DebugTreeAdaptor adaptor; + public ITreeAdaptor TreeAdaptor + { + get + { + return adaptor; + } + set + { + this.adaptor = new DebugTreeAdaptor(dbg,adaptor); + + } + } + + + public override string[] GetTokenNames() { return ProfileGrammarParser.tokenNames; } + public override string GrammarFileName { get { return "BuildOptions\\ProfileGrammar.g3"; } } + + + #region Rules + public class prog_return : ParserRuleReturnScope + { + public CommonTree tree; + public override object Tree { get { return tree; } } + } + + // $ANTLR start "prog" + // BuildOptions\\ProfileGrammar.g3:50:0: prog : ( stat )* ; + private ProfileGrammarParser.prog_return prog( ) + { + ProfileGrammarParser.prog_return retval = new ProfileGrammarParser.prog_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + ProfileGrammarParser.stat_return stat1 = default(ProfileGrammarParser.stat_return); + + + try + { + dbg.EnterRule( GrammarFileName, "prog" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 50, -1 ); + + try + { + // BuildOptions\\ProfileGrammar.g3:50:7: ( ( stat )* ) + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileGrammar.g3:50:7: ( stat )* + { + root_0 = (CommonTree)adaptor.Nil(); + + dbg.Location( 50, 6 ); + // BuildOptions\\ProfileGrammar.g3:50:7: ( stat )* + try + { + dbg.EnterSubRule( 1 ); + + for ( ; ; ) + { + int alt1=2; + try + { + dbg.EnterDecision( 1 ); + + int LA1_0 = input.LA(1); + + if ( ((LA1_0>=ID && LA1_0<=NEWLINE)||LA1_0==12) ) + { + alt1=1; + } + + + } + finally + { + dbg.ExitDecision( 1 ); + } + + switch ( alt1 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileGrammar.g3:50:9: stat + { + dbg.Location( 50, 8 ); + PushFollow(Follow._stat_in_prog53); + stat1=stat(); + + state._fsp--; + + adaptor.AddChild(root_0, stat1.Tree); + + } + break; + + default: + goto loop1; + } + } + + loop1: + ; + + } + finally + { + dbg.ExitSubRule( 1 ); + } + + + } + + retval.stop = input.LT(-1); + + retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); + adaptor.SetTokenBoundaries(retval.tree, retval.start, retval.stop); + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + retval.tree = (CommonTree)adaptor.ErrorNode(input, retval.start, input.LT(-1), re); + + } + finally + { + } + dbg.Location(51, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "prog" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return retval; + } + // $ANTLR end "prog" + + public class stat_return : ParserRuleReturnScope + { + public CommonTree tree; + public override object Tree { get { return tree; } } + } + + // $ANTLR start "stat" + // BuildOptions\\ProfileGrammar.g3:53:0: stat : ( expr NEWLINE -> expr | ID '=' expr NEWLINE -> ^( '=' ID expr ) | func NEWLINE -> func | NEWLINE ->); + private ProfileGrammarParser.stat_return stat( ) + { + ProfileGrammarParser.stat_return retval = new ProfileGrammarParser.stat_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + IToken NEWLINE3=null; + IToken ID4=null; + IToken char_literal5=null; + IToken NEWLINE7=null; + IToken NEWLINE9=null; + IToken NEWLINE10=null; + ProfileGrammarParser.expr_return expr2 = default(ProfileGrammarParser.expr_return); + ProfileGrammarParser.expr_return expr6 = default(ProfileGrammarParser.expr_return); + ProfileGrammarParser.func_return func8 = default(ProfileGrammarParser.func_return); + + CommonTree NEWLINE3_tree=null; + CommonTree ID4_tree=null; + CommonTree char_literal5_tree=null; + CommonTree NEWLINE7_tree=null; + CommonTree NEWLINE9_tree=null; + CommonTree NEWLINE10_tree=null; + RewriteRuleITokenStream stream_NEWLINE=new RewriteRuleITokenStream(adaptor,"token NEWLINE"); + RewriteRuleITokenStream stream_ID=new RewriteRuleITokenStream(adaptor,"token ID"); + RewriteRuleITokenStream stream_17=new RewriteRuleITokenStream(adaptor,"token 17"); + RewriteRuleSubtreeStream stream_expr=new RewriteRuleSubtreeStream(adaptor,"rule expr"); + RewriteRuleSubtreeStream stream_func=new RewriteRuleSubtreeStream(adaptor,"rule func"); + try + { + dbg.EnterRule( GrammarFileName, "stat" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 53, -1 ); + + try + { + // BuildOptions\\ProfileGrammar.g3:53:9: ( expr NEWLINE -> expr | ID '=' expr NEWLINE -> ^( '=' ID expr ) | func NEWLINE -> func | NEWLINE ->) + int alt2=4; + try + { + dbg.EnterDecision( 2 ); + + try + { + isCyclicDecision = true; + alt2 = dfa2.Predict(input); + } + catch ( NoViableAltException nvae ) + { + dbg.RecognitionException( nvae ); + throw nvae; + } + } + finally + { + dbg.ExitDecision( 2 ); + } + + switch ( alt2 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileGrammar.g3:53:9: expr NEWLINE + { + dbg.Location( 53, 8 ); + PushFollow(Follow._expr_in_stat70); + expr2=expr(); + + state._fsp--; + + stream_expr.Add(expr2.Tree); + dbg.Location( 53, 13 ); + NEWLINE3=(IToken)Match(input,NEWLINE,Follow._NEWLINE_in_stat72); + stream_NEWLINE.Add(NEWLINE3); + + + + { + // AST REWRITE + // elements: expr + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + // wildcard labels: + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"rule retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.Nil(); + // 53:41: -> expr + { + dbg.Location( 53, 43 ); + adaptor.AddChild(root_0, stream_expr.NextTree()); + + } + + retval.tree = root_0; + } + + } + break; + case 2: + dbg.EnterAlt( 2 ); + + // BuildOptions\\ProfileGrammar.g3:54:9: ID '=' expr NEWLINE + { + dbg.Location( 54, 8 ); + ID4=(IToken)Match(input,ID,Follow._ID_in_stat105); + stream_ID.Add(ID4); + + dbg.Location( 54, 11 ); + char_literal5=(IToken)Match(input,17,Follow._17_in_stat107); + stream_17.Add(char_literal5); + + dbg.Location( 54, 15 ); + PushFollow(Follow._expr_in_stat109); + expr6=expr(); + + state._fsp--; + + stream_expr.Add(expr6.Tree); + dbg.Location( 54, 20 ); + NEWLINE7=(IToken)Match(input,NEWLINE,Follow._NEWLINE_in_stat111); + stream_NEWLINE.Add(NEWLINE7); + + + + { + // AST REWRITE + // elements: 17, ID, expr + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + // wildcard labels: + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"rule retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.Nil(); + // 54:41: -> ^( '=' ID expr ) + { + dbg.Location( 54, 43 ); + // BuildOptions\\ProfileGrammar.g3:54:44: ^( '=' ID expr ) + { + CommonTree root_1 = (CommonTree)adaptor.Nil(); + dbg.Location( 54, 45 ); + root_1 = (CommonTree)adaptor.BecomeRoot(stream_17.NextNode(), root_1); + + dbg.Location( 54, 49 ); + adaptor.AddChild(root_1, stream_ID.NextNode()); + dbg.Location( 54, 52 ); + adaptor.AddChild(root_1, stream_expr.NextTree()); + + adaptor.AddChild(root_0, root_1); + } + + } + + retval.tree = root_0; + } + + } + break; + case 3: + dbg.EnterAlt( 3 ); + + // BuildOptions\\ProfileGrammar.g3:55:9: func NEWLINE + { + dbg.Location( 55, 8 ); + PushFollow(Follow._func_in_stat143); + func8=func(); + + state._fsp--; + + stream_func.Add(func8.Tree); + dbg.Location( 55, 13 ); + NEWLINE9=(IToken)Match(input,NEWLINE,Follow._NEWLINE_in_stat145); + stream_NEWLINE.Add(NEWLINE9); + + + + { + // AST REWRITE + // elements: func + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + // wildcard labels: + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"rule retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.Nil(); + // 55:41: -> func + { + dbg.Location( 55, 43 ); + adaptor.AddChild(root_0, stream_func.NextTree()); + + } + + retval.tree = root_0; + } + + } + break; + case 4: + dbg.EnterAlt( 4 ); + + // BuildOptions\\ProfileGrammar.g3:56:9: NEWLINE + { + dbg.Location( 56, 8 ); + NEWLINE10=(IToken)Match(input,NEWLINE,Follow._NEWLINE_in_stat178); + stream_NEWLINE.Add(NEWLINE10); + + + + { + // AST REWRITE + // elements: + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + // wildcard labels: + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"rule retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.Nil(); + // 56:41: -> + { + dbg.Location( 57, 4 ); + root_0 = null; + } + + retval.tree = root_0; + } + + } + break; + + } + retval.stop = input.LT(-1); + + retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); + adaptor.SetTokenBoundaries(retval.tree, retval.start, retval.stop); + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + retval.tree = (CommonTree)adaptor.ErrorNode(input, retval.start, input.LT(-1), re); + + } + finally + { + } + dbg.Location(57, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "stat" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return retval; + } + // $ANTLR end "stat" + + public class func_return : ParserRuleReturnScope + { + public CommonTree tree; + public override object Tree { get { return tree; } } + } + + // $ANTLR start "func" + // BuildOptions\\ProfileGrammar.g3:59:0: func : ID '(' formalPar ')' '=' expr -> ^( FUNC ID formalPar expr ) ; + private ProfileGrammarParser.func_return func( ) + { + ProfileGrammarParser.func_return retval = new ProfileGrammarParser.func_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + IToken ID11=null; + IToken char_literal12=null; + IToken char_literal14=null; + IToken char_literal15=null; + ProfileGrammarParser.formalPar_return formalPar13 = default(ProfileGrammarParser.formalPar_return); + ProfileGrammarParser.expr_return expr16 = default(ProfileGrammarParser.expr_return); + + CommonTree ID11_tree=null; + CommonTree char_literal12_tree=null; + CommonTree char_literal14_tree=null; + CommonTree char_literal15_tree=null; + RewriteRuleITokenStream stream_ID=new RewriteRuleITokenStream(adaptor,"token ID"); + RewriteRuleITokenStream stream_12=new RewriteRuleITokenStream(adaptor,"token 12"); + RewriteRuleITokenStream stream_13=new RewriteRuleITokenStream(adaptor,"token 13"); + RewriteRuleITokenStream stream_17=new RewriteRuleITokenStream(adaptor,"token 17"); + RewriteRuleSubtreeStream stream_formalPar=new RewriteRuleSubtreeStream(adaptor,"rule formalPar"); + RewriteRuleSubtreeStream stream_expr=new RewriteRuleSubtreeStream(adaptor,"rule expr"); + try + { + dbg.EnterRule( GrammarFileName, "func" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 59, -1 ); + + try + { + // BuildOptions\\ProfileGrammar.g3:59:9: ( ID '(' formalPar ')' '=' expr -> ^( FUNC ID formalPar expr ) ) + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileGrammar.g3:59:9: ID '(' formalPar ')' '=' expr + { + dbg.Location( 59, 8 ); + ID11=(IToken)Match(input,ID,Follow._ID_in_func219); + stream_ID.Add(ID11); + + dbg.Location( 59, 12 ); + char_literal12=(IToken)Match(input,12,Follow._12_in_func222); + stream_12.Add(char_literal12); + + dbg.Location( 59, 16 ); + PushFollow(Follow._formalPar_in_func224); + formalPar13=formalPar(); + + state._fsp--; + + stream_formalPar.Add(formalPar13.Tree); + dbg.Location( 59, 26 ); + char_literal14=(IToken)Match(input,13,Follow._13_in_func226); + stream_13.Add(char_literal14); + + dbg.Location( 59, 30 ); + char_literal15=(IToken)Match(input,17,Follow._17_in_func228); + stream_17.Add(char_literal15); + + dbg.Location( 59, 34 ); + PushFollow(Follow._expr_in_func230); + expr16=expr(); + + state._fsp--; + + stream_expr.Add(expr16.Tree); + + + { + // AST REWRITE + // elements: ID, formalPar, expr + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + // wildcard labels: + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"rule retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.Nil(); + // 59:41: -> ^( FUNC ID formalPar expr ) + { + dbg.Location( 59, 43 ); + // BuildOptions\\ProfileGrammar.g3:59:44: ^( FUNC ID formalPar expr ) + { + CommonTree root_1 = (CommonTree)adaptor.Nil(); + dbg.Location( 59, 45 ); + root_1 = (CommonTree)adaptor.BecomeRoot((CommonTree)adaptor.Create(FUNC, "FUNC"), root_1); + + dbg.Location( 59, 50 ); + adaptor.AddChild(root_1, stream_ID.NextNode()); + dbg.Location( 59, 53 ); + adaptor.AddChild(root_1, stream_formalPar.NextTree()); + dbg.Location( 59, 63 ); + adaptor.AddChild(root_1, stream_expr.NextTree()); + + adaptor.AddChild(root_0, root_1); + } + + } + + retval.tree = root_0; + } + + } + + retval.stop = input.LT(-1); + + retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); + adaptor.SetTokenBoundaries(retval.tree, retval.start, retval.stop); + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + retval.tree = (CommonTree)adaptor.ErrorNode(input, retval.start, input.LT(-1), re); + + } + finally + { + + functionDefinitions.Add(((CommonTree)retval.tree)); + + } + dbg.Location(60, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "func" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return retval; + } + // $ANTLR end "func" + + public class formalPar_return : ParserRuleReturnScope + { + public CommonTree tree; + public override object Tree { get { return tree; } } + } + + // $ANTLR start "formalPar" + // BuildOptions\\ProfileGrammar.g3:65:0: formalPar : ( ID | INT ); + private ProfileGrammarParser.formalPar_return formalPar( ) + { + ProfileGrammarParser.formalPar_return retval = new ProfileGrammarParser.formalPar_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + IToken set17=null; + + CommonTree set17_tree=null; + + try + { + dbg.EnterRule( GrammarFileName, "formalPar" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 65, -1 ); + + try + { + // BuildOptions\\ProfileGrammar.g3:66:9: ( ID | INT ) + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileGrammar.g3: + { + root_0 = (CommonTree)adaptor.Nil(); + + dbg.Location( 66, 8 ); + set17=(IToken)input.LT(1); + if ( (input.LA(1)>=ID && input.LA(1)<=INT) ) + { + input.Consume(); + adaptor.AddChild(root_0, (CommonTree)adaptor.Create(set17)); + state.errorRecovery=false; + } + else + { + MismatchedSetException mse = new MismatchedSetException(null,input); + dbg.RecognitionException( mse ); + throw mse; + } + + + } + + retval.stop = input.LT(-1); + + retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); + adaptor.SetTokenBoundaries(retval.tree, retval.start, retval.stop); + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + retval.tree = (CommonTree)adaptor.ErrorNode(input, retval.start, input.LT(-1), re); + + } + finally + { + } + dbg.Location(68, 1); + + } + finally + { + dbg.ExitRule( GrammarFileName, "formalPar" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return retval; + } + // $ANTLR end "formalPar" + + public class expr_return : ParserRuleReturnScope + { + public CommonTree tree; + public override object Tree { get { return tree; } } + } + + // $ANTLR start "expr" + // BuildOptions\\ProfileGrammar.g3:73:0: expr : multExpr ( ( '+' | '-' ) multExpr )* ; + private ProfileGrammarParser.expr_return expr( ) + { + ProfileGrammarParser.expr_return retval = new ProfileGrammarParser.expr_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + IToken char_literal19=null; + IToken char_literal20=null; + ProfileGrammarParser.multExpr_return multExpr18 = default(ProfileGrammarParser.multExpr_return); + ProfileGrammarParser.multExpr_return multExpr21 = default(ProfileGrammarParser.multExpr_return); + + CommonTree char_literal19_tree=null; + CommonTree char_literal20_tree=null; + + try + { + dbg.EnterRule( GrammarFileName, "expr" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 73, -1 ); + + try + { + // BuildOptions\\ProfileGrammar.g3:73:9: ( multExpr ( ( '+' | '-' ) multExpr )* ) + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileGrammar.g3:73:9: multExpr ( ( '+' | '-' ) multExpr )* + { + root_0 = (CommonTree)adaptor.Nil(); + + dbg.Location( 73, 8 ); + PushFollow(Follow._multExpr_in_expr288); + multExpr18=multExpr(); + + state._fsp--; + + adaptor.AddChild(root_0, multExpr18.Tree); + dbg.Location( 73, 17 ); + // BuildOptions\\ProfileGrammar.g3:73:18: ( ( '+' | '-' ) multExpr )* + try + { + dbg.EnterSubRule( 4 ); + + for ( ; ; ) + { + int alt4=2; + try + { + dbg.EnterDecision( 4 ); + + int LA4_0 = input.LA(1); + + if ( (LA4_0==10||LA4_0==16) ) + { + alt4=1; + } + + + } + finally + { + dbg.ExitDecision( 4 ); + } + + switch ( alt4 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileGrammar.g3:73:19: ( '+' | '-' ) multExpr + { + dbg.Location( 73, 18 ); + // BuildOptions\\ProfileGrammar.g3:73:19: ( '+' | '-' ) + int alt3=2; + try + { + dbg.EnterSubRule( 3 ); + try + { + dbg.EnterDecision( 3 ); + + int LA3_0 = input.LA(1); + + if ( (LA3_0==16) ) + { + alt3=1; + } + else if ( (LA3_0==10) ) + { + alt3=2; + } + else + { + NoViableAltException nvae = new NoViableAltException("", 3, 0, input); + + dbg.RecognitionException( nvae ); + throw nvae; + } + } + finally + { + dbg.ExitDecision( 3 ); + } + + switch ( alt3 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileGrammar.g3:73:20: '+' + { + dbg.Location( 73, 22 ); + char_literal19=(IToken)Match(input,16,Follow._16_in_expr292); + char_literal19_tree = (CommonTree)adaptor.Create(char_literal19); + root_0 = (CommonTree)adaptor.BecomeRoot(char_literal19_tree, root_0); + + + } + break; + case 2: + dbg.EnterAlt( 2 ); + + // BuildOptions\\ProfileGrammar.g3:73:25: '-' + { + dbg.Location( 73, 27 ); + char_literal20=(IToken)Match(input,10,Follow._10_in_expr295); + char_literal20_tree = (CommonTree)adaptor.Create(char_literal20); + root_0 = (CommonTree)adaptor.BecomeRoot(char_literal20_tree, root_0); + + + } + break; + + } + } + finally + { + dbg.ExitSubRule( 3 ); + } + + dbg.Location( 73, 30 ); + PushFollow(Follow._multExpr_in_expr299); + multExpr21=multExpr(); + + state._fsp--; + + adaptor.AddChild(root_0, multExpr21.Tree); + + } + break; + + default: + goto loop4; + } + } + + loop4: + ; + + } + finally + { + dbg.ExitSubRule( 4 ); + } + + + } + + retval.stop = input.LT(-1); + + retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); + adaptor.SetTokenBoundaries(retval.tree, retval.start, retval.stop); + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + retval.tree = (CommonTree)adaptor.ErrorNode(input, retval.start, input.LT(-1), re); + + } + finally + { + } + dbg.Location(74, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "expr" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return retval; + } + // $ANTLR end "expr" + + public class multExpr_return : ParserRuleReturnScope + { + public CommonTree tree; + public override object Tree { get { return tree; } } + } + + // $ANTLR start "multExpr" + // BuildOptions\\ProfileGrammar.g3:76:0: multExpr : atom ( ( '*' | '/' | '%' ) atom )* ; + private ProfileGrammarParser.multExpr_return multExpr( ) + { + ProfileGrammarParser.multExpr_return retval = new ProfileGrammarParser.multExpr_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + IToken set23=null; + ProfileGrammarParser.atom_return atom22 = default(ProfileGrammarParser.atom_return); + ProfileGrammarParser.atom_return atom24 = default(ProfileGrammarParser.atom_return); + + CommonTree set23_tree=null; + + try + { + dbg.EnterRule( GrammarFileName, "multExpr" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 76, -1 ); + + try + { + // BuildOptions\\ProfileGrammar.g3:77:9: ( atom ( ( '*' | '/' | '%' ) atom )* ) + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileGrammar.g3:77:9: atom ( ( '*' | '/' | '%' ) atom )* + { + root_0 = (CommonTree)adaptor.Nil(); + + dbg.Location( 77, 8 ); + PushFollow(Follow._atom_in_multExpr320); + atom22=atom(); + + state._fsp--; + + adaptor.AddChild(root_0, atom22.Tree); + dbg.Location( 77, 13 ); + // BuildOptions\\ProfileGrammar.g3:77:14: ( ( '*' | '/' | '%' ) atom )* + try + { + dbg.EnterSubRule( 5 ); + + for ( ; ; ) + { + int alt5=2; + try + { + dbg.EnterDecision( 5 ); + + int LA5_0 = input.LA(1); + + if ( (LA5_0==11||(LA5_0>=14 && LA5_0<=15)) ) + { + alt5=1; + } + + + } + finally + { + dbg.ExitDecision( 5 ); + } + + switch ( alt5 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileGrammar.g3:77:15: ( '*' | '/' | '%' ) atom + { + dbg.Location( 77, 27 ); + set23=(IToken)input.LT(1); + set23=(IToken)input.LT(1); + if ( input.LA(1)==11||(input.LA(1)>=14 && input.LA(1)<=15) ) + { + input.Consume(); + root_0 = (CommonTree)adaptor.BecomeRoot((CommonTree)adaptor.Create(set23), root_0); + state.errorRecovery=false; + } + else + { + MismatchedSetException mse = new MismatchedSetException(null,input); + dbg.RecognitionException( mse ); + throw mse; + } + + dbg.Location( 77, 29 ); + PushFollow(Follow._atom_in_multExpr332); + atom24=atom(); + + state._fsp--; + + adaptor.AddChild(root_0, atom24.Tree); + + } + break; + + default: + goto loop5; + } + } + + loop5: + ; + + } + finally + { + dbg.ExitSubRule( 5 ); + } + + + } + + retval.stop = input.LT(-1); + + retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); + adaptor.SetTokenBoundaries(retval.tree, retval.start, retval.stop); + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + retval.tree = (CommonTree)adaptor.ErrorNode(input, retval.start, input.LT(-1), re); + + } + finally + { + } + dbg.Location(78, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "multExpr" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return retval; + } + // $ANTLR end "multExpr" + + public class atom_return : ParserRuleReturnScope + { + public CommonTree tree; + public override object Tree { get { return tree; } } + } + + // $ANTLR start "atom" + // BuildOptions\\ProfileGrammar.g3:80:0: atom : ( INT | ID | '(' expr ')' -> expr | ID '(' expr ')' -> ^( CALL ID expr ) ); + private ProfileGrammarParser.atom_return atom( ) + { + ProfileGrammarParser.atom_return retval = new ProfileGrammarParser.atom_return(); + retval.start = input.LT(1); + + CommonTree root_0 = null; + + IToken INT25=null; + IToken ID26=null; + IToken char_literal27=null; + IToken char_literal29=null; + IToken ID30=null; + IToken char_literal31=null; + IToken char_literal33=null; + ProfileGrammarParser.expr_return expr28 = default(ProfileGrammarParser.expr_return); + ProfileGrammarParser.expr_return expr32 = default(ProfileGrammarParser.expr_return); + + CommonTree INT25_tree=null; + CommonTree ID26_tree=null; + CommonTree char_literal27_tree=null; + CommonTree char_literal29_tree=null; + CommonTree ID30_tree=null; + CommonTree char_literal31_tree=null; + CommonTree char_literal33_tree=null; + RewriteRuleITokenStream stream_12=new RewriteRuleITokenStream(adaptor,"token 12"); + RewriteRuleITokenStream stream_13=new RewriteRuleITokenStream(adaptor,"token 13"); + RewriteRuleITokenStream stream_ID=new RewriteRuleITokenStream(adaptor,"token ID"); + RewriteRuleSubtreeStream stream_expr=new RewriteRuleSubtreeStream(adaptor,"rule expr"); + try + { + dbg.EnterRule( GrammarFileName, "atom" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 80, -1 ); + + try + { + // BuildOptions\\ProfileGrammar.g3:80:9: ( INT | ID | '(' expr ')' -> expr | ID '(' expr ')' -> ^( CALL ID expr ) ) + int alt6=4; + try + { + dbg.EnterDecision( 6 ); + + switch ( input.LA(1) ) + { + case INT: + { + alt6=1; + } + break; + case ID: + { + int LA6_2 = input.LA(2); + + if ( (LA6_2==12) ) + { + alt6=4; + } + else if ( (LA6_2==NEWLINE||(LA6_2>=10 && LA6_2<=11)||(LA6_2>=13 && LA6_2<=16)) ) + { + alt6=2; + } + else + { + NoViableAltException nvae = new NoViableAltException("", 6, 2, input); + + dbg.RecognitionException( nvae ); + throw nvae; + } + } + break; + case 12: + { + alt6=3; + } + break; + default: + { + NoViableAltException nvae = new NoViableAltException("", 6, 0, input); + + dbg.RecognitionException( nvae ); + throw nvae; + } + } + + } + finally + { + dbg.ExitDecision( 6 ); + } + + switch ( alt6 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileGrammar.g3:80:9: INT + { + root_0 = (CommonTree)adaptor.Nil(); + + dbg.Location( 80, 8 ); + INT25=(IToken)Match(input,INT,Follow._INT_in_atom348); + INT25_tree = (CommonTree)adaptor.Create(INT25); + adaptor.AddChild(root_0, INT25_tree); + + + } + break; + case 2: + dbg.EnterAlt( 2 ); + + // BuildOptions\\ProfileGrammar.g3:81:9: ID + { + root_0 = (CommonTree)adaptor.Nil(); + + dbg.Location( 81, 8 ); + ID26=(IToken)Match(input,ID,Follow._ID_in_atom358); + ID26_tree = (CommonTree)adaptor.Create(ID26); + adaptor.AddChild(root_0, ID26_tree); + + + } + break; + case 3: + dbg.EnterAlt( 3 ); + + // BuildOptions\\ProfileGrammar.g3:82:9: '(' expr ')' + { + dbg.Location( 82, 8 ); + char_literal27=(IToken)Match(input,12,Follow._12_in_atom368); + stream_12.Add(char_literal27); + + dbg.Location( 82, 12 ); + PushFollow(Follow._expr_in_atom370); + expr28=expr(); + + state._fsp--; + + stream_expr.Add(expr28.Tree); + dbg.Location( 82, 17 ); + char_literal29=(IToken)Match(input,13,Follow._13_in_atom372); + stream_13.Add(char_literal29); + + + + { + // AST REWRITE + // elements: expr + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + // wildcard labels: + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"rule retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.Nil(); + // 82:25: -> expr + { + dbg.Location( 82, 27 ); + adaptor.AddChild(root_0, stream_expr.NextTree()); + + } + + retval.tree = root_0; + } + + } + break; + case 4: + dbg.EnterAlt( 4 ); + + // BuildOptions\\ProfileGrammar.g3:83:9: ID '(' expr ')' + { + dbg.Location( 83, 8 ); + ID30=(IToken)Match(input,ID,Follow._ID_in_atom389); + stream_ID.Add(ID30); + + dbg.Location( 83, 11 ); + char_literal31=(IToken)Match(input,12,Follow._12_in_atom391); + stream_12.Add(char_literal31); + + dbg.Location( 83, 15 ); + PushFollow(Follow._expr_in_atom393); + expr32=expr(); + + state._fsp--; + + stream_expr.Add(expr32.Tree); + dbg.Location( 83, 20 ); + char_literal33=(IToken)Match(input,13,Follow._13_in_atom395); + stream_13.Add(char_literal33); + + + + { + // AST REWRITE + // elements: ID, expr + // token labels: + // rule labels: retval + // token list labels: + // rule list labels: + // wildcard labels: + retval.tree = root_0; + RewriteRuleSubtreeStream stream_retval=new RewriteRuleSubtreeStream(adaptor,"rule retval",retval!=null?retval.tree:null); + + root_0 = (CommonTree)adaptor.Nil(); + // 83:25: -> ^( CALL ID expr ) + { + dbg.Location( 83, 27 ); + // BuildOptions\\ProfileGrammar.g3:83:28: ^( CALL ID expr ) + { + CommonTree root_1 = (CommonTree)adaptor.Nil(); + dbg.Location( 83, 29 ); + root_1 = (CommonTree)adaptor.BecomeRoot((CommonTree)adaptor.Create(CALL, "CALL"), root_1); + + dbg.Location( 83, 34 ); + adaptor.AddChild(root_1, stream_ID.NextNode()); + dbg.Location( 83, 37 ); + adaptor.AddChild(root_1, stream_expr.NextTree()); + + adaptor.AddChild(root_0, root_1); + } + + } + + retval.tree = root_0; + } + + } + break; + + } + retval.stop = input.LT(-1); + + retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); + adaptor.SetTokenBoundaries(retval.tree, retval.start, retval.stop); + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + retval.tree = (CommonTree)adaptor.ErrorNode(input, retval.start, input.LT(-1), re); + + } + finally + { + } + dbg.Location(84, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "atom" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return retval; + } + // $ANTLR end "atom" + #endregion + + // Delegated rules + + #region Synpreds + #endregion + + #region DFA + DFA2 dfa2; + + protected override void InitDFAs() + { + base.InitDFAs(); + dfa2 = new DFA2( this ); + } + + class DFA2 : DFA + { + + const string DFA2_eotS = + "\xA\xFFFF"; + const string DFA2_eofS = + "\xA\xFFFF"; + const string DFA2_minS = + "\x1\x6\x1\xFFFF\x1\x8\x1\xFFFF\x1\x6\x1\xFFFF\x2\xA\x1\x8\x1\xFFFF"; + const string DFA2_maxS = + "\x1\xC\x1\xFFFF\x1\x11\x1\xFFFF\x1\xC\x1\xFFFF\x2\x10\x1\x11\x1\xFFFF"; + const string DFA2_acceptS = + "\x1\xFFFF\x1\x1\x1\xFFFF\x1\x4\x1\xFFFF\x1\x2\x3\xFFFF\x1\x3"; + const string DFA2_specialS = + "\xA\xFFFF}>"; + static readonly string[] DFA2_transitionS = + { + "\x1\x2\x1\x1\x1\x3\x3\xFFFF\x1\x1", + "", + "\x1\x1\x1\xFFFF\x2\x1\x1\x4\x1\xFFFF\x3\x1\x1\x5", + "", + "\x1\x7\x1\x6\x4\xFFFF\x1\x1", + "", + "\x2\x1\x1\xFFFF\x1\x8\x3\x1", + "\x3\x1\x1\x8\x3\x1", + "\x1\x1\x1\xFFFF\x2\x1\x2\xFFFF\x3\x1\x1\x9", + "" + }; + + static readonly short[] DFA2_eot = DFA.UnpackEncodedString(DFA2_eotS); + static readonly short[] DFA2_eof = DFA.UnpackEncodedString(DFA2_eofS); + static readonly char[] DFA2_min = DFA.UnpackEncodedStringToUnsignedChars(DFA2_minS); + static readonly char[] DFA2_max = DFA.UnpackEncodedStringToUnsignedChars(DFA2_maxS); + static readonly short[] DFA2_accept = DFA.UnpackEncodedString(DFA2_acceptS); + static readonly short[] DFA2_special = DFA.UnpackEncodedString(DFA2_specialS); + static readonly short[][] DFA2_transition; + + static DFA2() + { + int numStates = DFA2_transitionS.Length; + DFA2_transition = new short[numStates][]; + for ( int i=0; i < numStates; i++ ) + { + DFA2_transition[i] = DFA.UnpackEncodedString(DFA2_transitionS[i]); + } + } + + public DFA2( BaseRecognizer recognizer ) + { + this.recognizer = recognizer; + this.decisionNumber = 2; + this.eot = DFA2_eot; + this.eof = DFA2_eof; + this.min = DFA2_min; + this.max = DFA2_max; + this.accept = DFA2_accept; + this.special = DFA2_special; + this.transition = DFA2_transition; + } + public override string GetDescription() + { + return "53:0: stat : ( expr NEWLINE -> expr | ID '=' expr NEWLINE -> ^( '=' ID expr ) | func NEWLINE -> func | NEWLINE ->);"; + } + public override void Error( NoViableAltException nvae ) + { + ((DebugParser)recognizer).dbg.RecognitionException( nvae ); + } + } + + + #endregion + + #region Follow Sets + public static class Follow + { + public static readonly BitSet _stat_in_prog53 = new BitSet(new ulong[]{0x11C2UL}); + public static readonly BitSet _expr_in_stat70 = new BitSet(new ulong[]{0x100UL}); + public static readonly BitSet _NEWLINE_in_stat72 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _ID_in_stat105 = new BitSet(new ulong[]{0x20000UL}); + public static readonly BitSet _17_in_stat107 = new BitSet(new ulong[]{0x10C0UL}); + public static readonly BitSet _expr_in_stat109 = new BitSet(new ulong[]{0x100UL}); + public static readonly BitSet _NEWLINE_in_stat111 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _func_in_stat143 = new BitSet(new ulong[]{0x100UL}); + public static readonly BitSet _NEWLINE_in_stat145 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _NEWLINE_in_stat178 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _ID_in_func219 = new BitSet(new ulong[]{0x1000UL}); + public static readonly BitSet _12_in_func222 = new BitSet(new ulong[]{0xC0UL}); + public static readonly BitSet _formalPar_in_func224 = new BitSet(new ulong[]{0x2000UL}); + public static readonly BitSet _13_in_func226 = new BitSet(new ulong[]{0x20000UL}); + public static readonly BitSet _17_in_func228 = new BitSet(new ulong[]{0x10C0UL}); + public static readonly BitSet _expr_in_func230 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _set_in_formalPar267 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _multExpr_in_expr288 = new BitSet(new ulong[]{0x10402UL}); + public static readonly BitSet _16_in_expr292 = new BitSet(new ulong[]{0x10C0UL}); + public static readonly BitSet _10_in_expr295 = new BitSet(new ulong[]{0x10C0UL}); + public static readonly BitSet _multExpr_in_expr299 = new BitSet(new ulong[]{0x10402UL}); + public static readonly BitSet _atom_in_multExpr320 = new BitSet(new ulong[]{0xC802UL}); + public static readonly BitSet _set_in_multExpr323 = new BitSet(new ulong[]{0x10C0UL}); + public static readonly BitSet _atom_in_multExpr332 = new BitSet(new ulong[]{0xC802UL}); + public static readonly BitSet _INT_in_atom348 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _ID_in_atom358 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _12_in_atom368 = new BitSet(new ulong[]{0x10C0UL}); + public static readonly BitSet _expr_in_atom370 = new BitSet(new ulong[]{0x2000UL}); + public static readonly BitSet _13_in_atom372 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _ID_in_atom389 = new BitSet(new ulong[]{0x1000UL}); + public static readonly BitSet _12_in_atom391 = new BitSet(new ulong[]{0x10C0UL}); + public static readonly BitSet _expr_in_atom393 = new BitSet(new ulong[]{0x2000UL}); + public static readonly BitSet _13_in_atom395 = new BitSet(new ulong[]{0x2UL}); + + } + #endregion +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarParserHelper.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarParserHelper.cs new file mode 100644 index 0000000..ddd7533 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileGrammarParserHelper.cs @@ -0,0 +1,40 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System.Collections.Generic; +using Antlr.Runtime.Tree; + +partial class ProfileGrammarParser +{ + /** List of function definitions. Must point at the FUNC nodes. */ + List<CommonTree> functionDefinitions = new List<CommonTree>(); +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammar.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammar.cs new file mode 100644 index 0000000..4491cb3 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammar.cs @@ -0,0 +1,863 @@ +// $ANTLR 3.1.2 BuildOptions\\ProfileTreeGrammar.g3 2009-03-16 13:19:20 + +// The variable 'variable' is assigned but its value is never used. +#pragma warning disable 219 +// Unreachable code detected. +#pragma warning disable 162 + + +//import java.util.Map; +//import java.util.HashMap; +using BigInteger = java.math.BigInteger; +using Console = System.Console; + + +using System.Collections.Generic; +using Antlr.Runtime; +using Antlr.Runtime.Tree; +using RewriteRuleITokenStream = Antlr.Runtime.Tree.RewriteRuleTokenStream;using Stack = System.Collections.Generic.Stack<object>; +using List = System.Collections.IList; +using ArrayList = System.Collections.Generic.List<object>; + +using Antlr.Runtime.Debug; +using IOException = System.IO.IOException; +public partial class ProfileTreeGrammar : DebugTreeParser +{ + public static readonly string[] tokenNames = new string[] { + "<invalid>", "<EOR>", "<DOWN>", "<UP>", "CALL", "FUNC", "ID", "INT", "NEWLINE", "WS", "'-'", "'%'", "'('", "')'", "'*'", "'/'", "'+'", "'='" + }; + public const int EOF=-1; + public const int T__10=10; + public const int T__11=11; + public const int T__12=12; + public const int T__13=13; + public const int T__14=14; + public const int T__15=15; + public const int T__16=16; + public const int T__17=17; + public const int CALL=4; + public const int FUNC=5; + public const int ID=6; + public const int INT=7; + public const int NEWLINE=8; + public const int WS=9; + + // delegates + // delegators + + public static readonly string[] ruleNames = + new string[] + { + "invalidRule", "call", "expr", "prog", "stat" + }; + + int ruleLevel = 0; + public virtual int RuleLevel { get { return ruleLevel; } } + public virtual void IncRuleLevel() { ruleLevel++; } + public virtual void DecRuleLevel() { ruleLevel--; } + public ProfileTreeGrammar( ITreeNodeStream input ) + : this( input, new Profiler(null), new RecognizerSharedState() ) + { + } + public ProfileTreeGrammar( ITreeNodeStream input, IDebugEventListener dbg, RecognizerSharedState state ) + : base( input, dbg, state ) + { + Profiler p = (Profiler)dbg; + p.setParser(this); + } + + public ProfileTreeGrammar( ITreeNodeStream input, IDebugEventListener dbg ) + : base( input, dbg, new RecognizerSharedState() ) + { + Profiler p = (Profiler)dbg; + p.setParser(this); + } + public virtual bool AlreadyParsedRule( IIntStream input, int ruleIndex ) + { + ((Profiler)dbg).ExamineRuleMemoization(input, ruleIndex, ProfileTreeGrammar.ruleNames[ruleIndex]); + return super.AlreadyParsedRule(input, ruleIndex); + } + + public virtual void Memoize( IIntStream input, int ruleIndex, int ruleStartIndex ) + { + ((Profiler)dbg).Memoize(input, ruleIndex, ruleStartIndex, ProfileTreeGrammar.ruleNames[ruleIndex]); + super.Memoize(input, ruleIndex, ruleStartIndex); + } + protected virtual bool EvalPredicate( bool result, string predicate ) + { + dbg.SemanticPredicate( result, predicate ); + return result; + } + + + public override string[] GetTokenNames() { return ProfileTreeGrammar.tokenNames; } + public override string GrammarFileName { get { return "BuildOptions\\ProfileTreeGrammar.g3"; } } + + + #region Rules + + // $ANTLR start "prog" + // BuildOptions\\ProfileTreeGrammar.g3:53:0: prog : ( stat )* ; + private void prog( ) + { + try + { + dbg.EnterRule( GrammarFileName, "prog" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 53, -1 ); + + try + { + // BuildOptions\\ProfileTreeGrammar.g3:53:9: ( ( stat )* ) + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileTreeGrammar.g3:53:9: ( stat )* + { + dbg.Location( 53, 8 ); + // BuildOptions\\ProfileTreeGrammar.g3:53:9: ( stat )* + try + { + dbg.EnterSubRule( 1 ); + + for ( ; ; ) + { + int alt1=2; + try + { + dbg.EnterDecision( 1 ); + + int LA1_0 = input.LA(1); + + if ( ((LA1_0>=CALL && LA1_0<=INT)||(LA1_0>=10 && LA1_0<=11)||(LA1_0>=14 && LA1_0<=17)) ) + { + alt1=1; + } + + + } + finally + { + dbg.ExitDecision( 1 ); + } + + switch ( alt1 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileTreeGrammar.g3:53:0: stat + { + dbg.Location( 53, 8 ); + PushFollow(Follow._stat_in_prog48); + stat(); + + state._fsp--; + + + } + break; + + default: + goto loop1; + } + } + + loop1: + ; + + } + finally + { + dbg.ExitSubRule( 1 ); + } + + + } + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + } + finally + { + } + dbg.Location(54, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "prog" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return ; + } + // $ANTLR end "prog" + + + // $ANTLR start "stat" + // BuildOptions\\ProfileTreeGrammar.g3:56:0: stat : ( expr | ^( '=' ID expr ) | ^( FUNC ( . )+ ) ); + private void stat( ) + { + CommonTree ID2=null; + BigInteger expr1 = default(BigInteger); + BigInteger expr3 = default(BigInteger); + + try + { + dbg.EnterRule( GrammarFileName, "stat" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 56, -1 ); + + try + { + // BuildOptions\\ProfileTreeGrammar.g3:56:9: ( expr | ^( '=' ID expr ) | ^( FUNC ( . )+ ) ) + int alt3=3; + try + { + dbg.EnterDecision( 3 ); + + switch ( input.LA(1) ) + { + case CALL: + case ID: + case INT: + case 10: + case 11: + case 14: + case 15: + case 16: + { + alt3=1; + } + break; + case 17: + { + alt3=2; + } + break; + case FUNC: + { + alt3=3; + } + break; + default: + { + NoViableAltException nvae = new NoViableAltException("", 3, 0, input); + + dbg.RecognitionException( nvae ); + throw nvae; + } + } + + } + finally + { + dbg.ExitDecision( 3 ); + } + + switch ( alt3 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileTreeGrammar.g3:56:9: expr + { + dbg.Location( 56, 8 ); + PushFollow(Follow._expr_in_stat63); + expr1=expr(); + + state._fsp--; + + dbg.Location( 56, 35 ); + string result = expr1.ToString(); + Console.Out.WriteLine(expr1 + " (about " + result[0] + "*10^" + (result.Length-1) + ")"); + + + } + break; + case 2: + dbg.EnterAlt( 2 ); + + // BuildOptions\\ProfileTreeGrammar.g3:59:9: ^( '=' ID expr ) + { + dbg.Location( 59, 8 ); + dbg.Location( 59, 10 ); + Match(input,17,Follow._17_in_stat98); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 59, 14 ); + ID2=(CommonTree)Match(input,ID,Follow._ID_in_stat100); + dbg.Location( 59, 17 ); + PushFollow(Follow._expr_in_stat102); + expr3=expr(); + + state._fsp--; + + + Match(input, TokenConstants.UP, null); + dbg.Location( 59, 35 ); + globalMemory[(ID2!=null?ID2.Text:null)] = expr3; + + } + break; + case 3: + dbg.EnterAlt( 3 ); + + // BuildOptions\\ProfileTreeGrammar.g3:60:9: ^( FUNC ( . )+ ) + { + dbg.Location( 60, 8 ); + dbg.Location( 60, 10 ); + Match(input,FUNC,Follow._FUNC_in_stat128); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 60, 15 ); + // BuildOptions\\ProfileTreeGrammar.g3:60:16: ( . )+ + int cnt2=0; + try + { + dbg.EnterSubRule( 2 ); + + for ( ; ; ) + { + int alt2=2; + try + { + dbg.EnterDecision( 2 ); + + int LA2_0 = input.LA(1); + + if ( ((LA2_0>=CALL && LA2_0<=17)) ) + { + alt2=1; + } + else if ( (LA2_0==UP) ) + { + alt2=2; + } + + + } + finally + { + dbg.ExitDecision( 2 ); + } + + switch ( alt2 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileTreeGrammar.g3:60:0: . + { + dbg.Location( 60, 15 ); + MatchAny(input); + + } + break; + + default: + if ( cnt2 >= 1 ) + goto loop2; + + EarlyExitException eee2 = new EarlyExitException( 2, input ); + dbg.RecognitionException( eee2 ); + + throw eee2; + } + cnt2++; + } + loop2: + ; + + } + finally + { + dbg.ExitSubRule( 2 ); + } + + + Match(input, TokenConstants.UP, null); + + } + break; + + } + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + } + finally + { + } + dbg.Location(61, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "stat" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return ; + } + // $ANTLR end "stat" + + + // $ANTLR start "expr" + // BuildOptions\\ProfileTreeGrammar.g3:63:0: expr returns [BigInteger value] : ( ^( '+' a= expr b= expr ) | ^( '-' a= expr b= expr ) | ^( '*' a= expr b= expr ) | ^( '/' a= expr b= expr ) | ^( '%' a= expr b= expr ) | ID | INT | call ); + private BigInteger expr( ) + { + + BigInteger value = default(BigInteger); + + CommonTree ID4=null; + CommonTree INT5=null; + BigInteger a = default(BigInteger); + BigInteger b = default(BigInteger); + BigInteger call6 = default(BigInteger); + + try + { + dbg.EnterRule( GrammarFileName, "expr" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 63, -1 ); + + try + { + // BuildOptions\\ProfileTreeGrammar.g3:64:9: ( ^( '+' a= expr b= expr ) | ^( '-' a= expr b= expr ) | ^( '*' a= expr b= expr ) | ^( '/' a= expr b= expr ) | ^( '%' a= expr b= expr ) | ID | INT | call ) + int alt4=8; + try + { + dbg.EnterDecision( 4 ); + + switch ( input.LA(1) ) + { + case 16: + { + alt4=1; + } + break; + case 10: + { + alt4=2; + } + break; + case 14: + { + alt4=3; + } + break; + case 15: + { + alt4=4; + } + break; + case 11: + { + alt4=5; + } + break; + case ID: + { + alt4=6; + } + break; + case INT: + { + alt4=7; + } + break; + case CALL: + { + alt4=8; + } + break; + default: + { + NoViableAltException nvae = new NoViableAltException("", 4, 0, input); + + dbg.RecognitionException( nvae ); + throw nvae; + } + } + + } + finally + { + dbg.ExitDecision( 4 ); + } + + switch ( alt4 ) + { + case 1: + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileTreeGrammar.g3:64:9: ^( '+' a= expr b= expr ) + { + dbg.Location( 64, 8 ); + dbg.Location( 64, 10 ); + Match(input,16,Follow._16_in_expr172); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 64, 15 ); + PushFollow(Follow._expr_in_expr176); + a=expr(); + + state._fsp--; + + dbg.Location( 64, 22 ); + PushFollow(Follow._expr_in_expr180); + b=expr(); + + state._fsp--; + + + Match(input, TokenConstants.UP, null); + dbg.Location( 64, 35 ); + value = a.add(b); + + } + break; + case 2: + dbg.EnterAlt( 2 ); + + // BuildOptions\\ProfileTreeGrammar.g3:65:9: ^( '-' a= expr b= expr ) + { + dbg.Location( 65, 8 ); + dbg.Location( 65, 10 ); + Match(input,10,Follow._10_in_expr200); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 65, 15 ); + PushFollow(Follow._expr_in_expr204); + a=expr(); + + state._fsp--; + + dbg.Location( 65, 22 ); + PushFollow(Follow._expr_in_expr208); + b=expr(); + + state._fsp--; + + + Match(input, TokenConstants.UP, null); + dbg.Location( 65, 35 ); + value = a.subtract(b); + + } + break; + case 3: + dbg.EnterAlt( 3 ); + + // BuildOptions\\ProfileTreeGrammar.g3:66:9: ^( '*' a= expr b= expr ) + { + dbg.Location( 66, 8 ); + dbg.Location( 66, 10 ); + Match(input,14,Follow._14_in_expr228); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 66, 15 ); + PushFollow(Follow._expr_in_expr232); + a=expr(); + + state._fsp--; + + dbg.Location( 66, 22 ); + PushFollow(Follow._expr_in_expr236); + b=expr(); + + state._fsp--; + + + Match(input, TokenConstants.UP, null); + dbg.Location( 66, 35 ); + value = a.multiply(b); + + } + break; + case 4: + dbg.EnterAlt( 4 ); + + // BuildOptions\\ProfileTreeGrammar.g3:67:9: ^( '/' a= expr b= expr ) + { + dbg.Location( 67, 8 ); + dbg.Location( 67, 10 ); + Match(input,15,Follow._15_in_expr256); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 67, 15 ); + PushFollow(Follow._expr_in_expr260); + a=expr(); + + state._fsp--; + + dbg.Location( 67, 22 ); + PushFollow(Follow._expr_in_expr264); + b=expr(); + + state._fsp--; + + + Match(input, TokenConstants.UP, null); + dbg.Location( 67, 35 ); + value = a.divide(b); + + } + break; + case 5: + dbg.EnterAlt( 5 ); + + // BuildOptions\\ProfileTreeGrammar.g3:68:9: ^( '%' a= expr b= expr ) + { + dbg.Location( 68, 8 ); + dbg.Location( 68, 10 ); + Match(input,11,Follow._11_in_expr284); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 68, 15 ); + PushFollow(Follow._expr_in_expr288); + a=expr(); + + state._fsp--; + + dbg.Location( 68, 22 ); + PushFollow(Follow._expr_in_expr292); + b=expr(); + + state._fsp--; + + + Match(input, TokenConstants.UP, null); + dbg.Location( 68, 35 ); + value = a.remainder(b); + + } + break; + case 6: + dbg.EnterAlt( 6 ); + + // BuildOptions\\ProfileTreeGrammar.g3:69:9: ID + { + dbg.Location( 69, 8 ); + ID4=(CommonTree)Match(input,ID,Follow._ID_in_expr311); + dbg.Location( 69, 35 ); + value = getValue((ID4!=null?ID4.Text:null)); + + } + break; + case 7: + dbg.EnterAlt( 7 ); + + // BuildOptions\\ProfileTreeGrammar.g3:70:9: INT + { + dbg.Location( 70, 8 ); + INT5=(CommonTree)Match(input,INT,Follow._INT_in_expr347); + dbg.Location( 70, 35 ); + value = new BigInteger((INT5!=null?INT5.Text:null)); + + } + break; + case 8: + dbg.EnterAlt( 8 ); + + // BuildOptions\\ProfileTreeGrammar.g3:71:9: call + { + dbg.Location( 71, 8 ); + PushFollow(Follow._call_in_expr382); + call6=call(); + + state._fsp--; + + dbg.Location( 71, 35 ); + value = call6; + + } + break; + + } + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + } + finally + { + } + dbg.Location(72, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "expr" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return value; + } + // $ANTLR end "expr" + + + // $ANTLR start "call" + // BuildOptions\\ProfileTreeGrammar.g3:74:0: call returns [BigInteger value] : ^( CALL ID expr ) ; + private BigInteger call( ) + { + + BigInteger value = default(BigInteger); + + CommonTree ID8=null; + BigInteger expr7 = default(BigInteger); + + try + { + dbg.EnterRule( GrammarFileName, "call" ); + if ( RuleLevel == 0 ) + { + dbg.Commence(); + } + IncRuleLevel(); + dbg.Location( 74, -1 ); + + try + { + // BuildOptions\\ProfileTreeGrammar.g3:75:9: ( ^( CALL ID expr ) ) + dbg.EnterAlt( 1 ); + + // BuildOptions\\ProfileTreeGrammar.g3:75:9: ^( CALL ID expr ) + { + dbg.Location( 75, 8 ); + dbg.Location( 75, 10 ); + Match(input,CALL,Follow._CALL_in_call430); + + Match(input, TokenConstants.DOWN, null); + dbg.Location( 75, 15 ); + ID8=(CommonTree)Match(input,ID,Follow._ID_in_call432); + dbg.Location( 75, 18 ); + PushFollow(Follow._expr_in_call434); + expr7=expr(); + + state._fsp--; + + + Match(input, TokenConstants.UP, null); + dbg.Location( 75, 35 ); + BigInteger p = expr7; + CommonTree funcRoot = findFunction((ID8!=null?ID8.Text:null), p); + if (funcRoot == null) { + Console.Error.WriteLine("No match found for " + (ID8!=null?ID8.Text:null) + "(" + p + ")"); + } else { + // Here we set up the local evaluator to run over the + // function definition with the parameter value. + // This re-reads a sub-AST of our input AST! + ProfileTreeGrammar e = new ProfileTreeGrammar(funcRoot, functionDefinitions, globalMemory, p); + value = e.expr(); + } + + + } + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + } + finally + { + } + dbg.Location(87, 4); + + } + finally + { + dbg.ExitRule( GrammarFileName, "call" ); + DecRuleLevel(); + if ( RuleLevel == 0 ) + { + dbg.Terminate(); + } + } + + return value; + } + // $ANTLR end "call" + #endregion + + // Delegated rules + + #region Synpreds + #endregion + + #region DFA + + protected override void InitDFAs() + { + base.InitDFAs(); + } + + #endregion + + #region Follow Sets + public static class Follow + { + public static readonly BitSet _stat_in_prog48 = new BitSet(new ulong[]{0x3CCF2UL}); + public static readonly BitSet _expr_in_stat63 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _17_in_stat98 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _ID_in_stat100 = new BitSet(new ulong[]{0x1CCD0UL}); + public static readonly BitSet _expr_in_stat102 = new BitSet(new ulong[]{0x8UL}); + public static readonly BitSet _FUNC_in_stat128 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _16_in_expr172 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _expr_in_expr176 = new BitSet(new ulong[]{0x1CCD0UL}); + public static readonly BitSet _expr_in_expr180 = new BitSet(new ulong[]{0x8UL}); + public static readonly BitSet _10_in_expr200 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _expr_in_expr204 = new BitSet(new ulong[]{0x1CCD0UL}); + public static readonly BitSet _expr_in_expr208 = new BitSet(new ulong[]{0x8UL}); + public static readonly BitSet _14_in_expr228 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _expr_in_expr232 = new BitSet(new ulong[]{0x1CCD0UL}); + public static readonly BitSet _expr_in_expr236 = new BitSet(new ulong[]{0x8UL}); + public static readonly BitSet _15_in_expr256 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _expr_in_expr260 = new BitSet(new ulong[]{0x1CCD0UL}); + public static readonly BitSet _expr_in_expr264 = new BitSet(new ulong[]{0x8UL}); + public static readonly BitSet _11_in_expr284 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _expr_in_expr288 = new BitSet(new ulong[]{0x1CCD0UL}); + public static readonly BitSet _expr_in_expr292 = new BitSet(new ulong[]{0x8UL}); + public static readonly BitSet _ID_in_expr311 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _INT_in_expr347 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _call_in_expr382 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _CALL_in_call430 = new BitSet(new ulong[]{0x4UL}); + public static readonly BitSet _ID_in_call432 = new BitSet(new ulong[]{0x1CCD0UL}); + public static readonly BitSet _expr_in_call434 = new BitSet(new ulong[]{0x8UL}); + + } + #endregion +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammar.g3 b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammar.g3 new file mode 100644 index 0000000..f6786db --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammar.g3 @@ -0,0 +1,88 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +tree grammar ProfileTreeGrammar; + +options +{ + language=CSharp3; + tokenVocab=ProfileGrammar; + ASTLabelType=CommonTree; +} + +// START:members +@header +{ +//import java.util.Map; +//import java.util.HashMap; +using BigInteger = java.math.BigInteger; +using Console = System.Console; +} +// END:members + +// START:rules +prog: stat* + ; + +stat: expr { string result = $expr.value.ToString(); + Console.Out.WriteLine($expr.value + " (about " + result[0] + "*10^" + (result.Length-1) + ")"); + } + | ^('=' ID expr) { globalMemory[$ID.text] = $expr.value; } + | ^(FUNC .+) // ignore FUNCs - we added them to functionDefinitions already in parser. + ; + +expr returns [BigInteger value] + : ^('+' a=expr b=expr) { $value = $a.value.add($b.value); } + | ^('-' a=expr b=expr) { $value = $a.value.subtract($b.value); } + | ^('*' a=expr b=expr) { $value = $a.value.multiply($b.value); } + | ^('/' a=expr b=expr) { $value = $a.value.divide($b.value); } + | ^('%' a=expr b=expr) { $value = $a.value.remainder($b.value); } + | ID { $value = getValue($ID.text); } + | INT { $value = new BigInteger($INT.text); } + | call { $value = $call.value; } + ; + +call returns [BigInteger value] + : ^(CALL ID expr) { BigInteger p = $expr.value; + CommonTree funcRoot = findFunction($ID.text, p); + if (funcRoot == null) { + Console.Error.WriteLine("No match found for " + $ID.text + "(" + p + ")"); + } else { + // Here we set up the local evaluator to run over the + // function definition with the parameter value. + // This re-reads a sub-AST of our input AST! + ProfileTreeGrammar e = new ProfileTreeGrammar(funcRoot, functionDefinitions, globalMemory, p); + $value = e.expr(); + } + } + ; +// END:rules diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammarHelper.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammarHelper.cs new file mode 100644 index 0000000..47cc8a8 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/BuildOptions/ProfileTreeGrammarHelper.cs @@ -0,0 +1,116 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System.Collections.Generic; +using Antlr.Runtime.Tree; + +using BigInteger = java.math.BigInteger; +using Console = System.Console; + +partial class ProfileTreeGrammar +{ + /** Points to functions tracked by tree builder. */ + private List<CommonTree> functionDefinitions; + + /** Remember local variables. Currently, this is only the function parameter. + */ + private readonly IDictionary<string, BigInteger> localMemory = new Dictionary<string, BigInteger>(); + + /** Remember global variables set by =. */ + private IDictionary<string, BigInteger> globalMemory = new Dictionary<string, BigInteger>(); + + /** Set up an evaluator with a node stream; and a set of function definition ASTs. */ + public ProfileTreeGrammar( CommonTreeNodeStream nodes, List<CommonTree> functionDefinitions ) + : this( nodes ) + { + this.functionDefinitions = functionDefinitions; + } + + /** Set up a local evaluator for a nested function call. The evaluator gets the definition + * tree of the function; the set of all defined functions (to find locally called ones); a + * pointer to the global variable memory; and the value of the function parameter to be + * added to the local memory. + */ + private ProfileTreeGrammar( CommonTree function, + List<CommonTree> functionDefinitions, + IDictionary<string, BigInteger> globalMemory, + BigInteger paramValue ) + // Expected tree for function: ^(FUNC ID ( INT | ID ) expr) + : this( new CommonTreeNodeStream( function.GetChild( 2 ) ), functionDefinitions ) + { + this.globalMemory = globalMemory; + localMemory[function.GetChild( 1 ).Text] = paramValue; + } + + /** Find matching function definition for a function name and parameter + * value. The first definition is returned where (a) the name matches + * and (b) the formal parameter agrees if it is defined as constant. + */ + private CommonTree findFunction( string name, BigInteger paramValue ) + { + foreach ( CommonTree f in functionDefinitions ) + { + // Expected tree for f: ^(FUNC ID (ID | INT) expr) + if ( f.GetChild( 0 ).Text.Equals( name ) ) + { + // Check whether parameter matches + CommonTree formalPar = (CommonTree)f.GetChild( 1 ); + if ( formalPar.Token.Type == INT + && !new BigInteger( formalPar.Token.Text ).Equals( paramValue ) ) + { + // Constant in formalPar list does not match actual value -> no match. + continue; + } + // Parameter (value for INT formal arg) as well as fct name agrees! + return f; + } + } + return null; + } + + /** Get value of name up call stack. */ + public BigInteger getValue( string name ) + { + BigInteger value; + if ( localMemory.TryGetValue( name, out value ) && value != null ) + { + return value; + } + if ( globalMemory.TryGetValue( name, out value ) && value != null ) + { + return value; + } + // not found in local memory or global memory + Console.Error.WriteLine( "undefined variable " + name ); + return new BigInteger( "0" ); + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/JavaCompat/Expr.g3 b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/JavaCompat/Expr.g3 new file mode 100644 index 0000000..65e7c5d --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/JavaCompat/Expr.g3 @@ -0,0 +1,110 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +grammar Expr; + +/* + I had to make the following changes to the basic Expr grammar to make it work with the CSharp3 target in "Java compatibility mode". + For reference, see http://www.antlr.org/wiki/display/ANTLR3/Expression+evaluator. + + Add an options section to set the language to CSharp3. + + In the @header section, add: + // 'member' is obsolete + #pragma warning disable 612 + using Antlr.Runtime.JavaExtensions; + + In the @header section, replace: + import java.util.HashMap; + with: + using HashMap = System.Collections.Generic.Dictionary<object,object>; + + Change all instances of "System.out" with "JSystem.@out". + + Change all instances of "System.err" with "JSystem.err". + + Change all instances of "skip()" with "Skip()". + */ + +options +{ + language=CSharp3; +} + +@header { +// 'member' is obsolete +#pragma warning disable 612 + +using Antlr.Runtime.JavaExtensions; +using HashMap = System.Collections.Generic.Dictionary<object,object>; +using Integer = java.lang.Integer; +} + +@members { +/** Map variable name to Integer object holding value */ +HashMap memory = new HashMap(); +} + +prog: stat+ ; + +stat: expr NEWLINE {JSystem.@out.println($expr.value);} + | ID '=' expr NEWLINE + {memory.put($ID.text, new Integer($expr.value));} + | NEWLINE + ; + +expr returns [int value] + : e=multExpr {$value = $e.value;} + ( '+' e=multExpr {$value += $e.value;} + | '-' e=multExpr {$value -= $e.value;} + )* + ; + +multExpr returns [int value] + : e=atom {$value = $e.value;} ('*' e=atom {$value *= $e.value;})* + ; + +atom returns [int value] + : INT {$value = Integer.parseInt($INT.text);} + | ID + { + Integer v = (Integer)memory.get($ID.text); + if ( v!=null ) $value = v.intValue(); + else JSystem.err.println("undefined variable "+$ID.text); + } + | '(' expr ')' {$value = $expr.value;} + ; + +ID : ('a'..'z'|'A'..'Z')+ ; +INT : '0'..'9'+ ; +NEWLINE:'\r'? '\n' ; +WS : (' '|'\t')+ {Skip();} ; diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/JavaCompat/ExprLexer.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/JavaCompat/ExprLexer.cs new file mode 100644 index 0000000..98b22e1 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/JavaCompat/ExprLexer.cs @@ -0,0 +1,617 @@ +// $ANTLR 3.1.2 JavaCompat\\Expr.g3 2009-03-16 13:19:15 + +// The variable 'variable' is assigned but its value is never used. +#pragma warning disable 219 +// Unreachable code detected. +#pragma warning disable 162 + + +using System.Collections.Generic; +using Antlr.Runtime; +using Stack = System.Collections.Generic.Stack<object>; +using List = System.Collections.IList; +using ArrayList = System.Collections.Generic.List<object>; + +public partial class ExprLexer : Lexer +{ + public const int EOF=-1; + public const int T__8=8; + public const int T__9=9; + public const int T__10=10; + public const int T__11=11; + public const int T__12=12; + public const int T__13=13; + public const int ID=4; + public const int INT=5; + public const int NEWLINE=6; + public const int WS=7; + + // delegates + // delegators + + public ExprLexer() {} + public ExprLexer( ICharStream input ) + : this( input, new RecognizerSharedState() ) + { + } + public ExprLexer( ICharStream input, RecognizerSharedState state ) + : base( input, state ) + { + + } + public override string GrammarFileName { get { return "JavaCompat\\Expr.g3"; } } + + // $ANTLR start "T__8" + private void mT__8() + { + try + { + int _type = T__8; + int _channel = DEFAULT_TOKEN_CHANNEL; + // JavaCompat\\Expr.g3:7:8: ( '-' ) + // JavaCompat\\Expr.g3:7:8: '-' + { + Match('-'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__8" + + // $ANTLR start "T__9" + private void mT__9() + { + try + { + int _type = T__9; + int _channel = DEFAULT_TOKEN_CHANNEL; + // JavaCompat\\Expr.g3:8:8: ( '(' ) + // JavaCompat\\Expr.g3:8:8: '(' + { + Match('('); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__9" + + // $ANTLR start "T__10" + private void mT__10() + { + try + { + int _type = T__10; + int _channel = DEFAULT_TOKEN_CHANNEL; + // JavaCompat\\Expr.g3:9:9: ( ')' ) + // JavaCompat\\Expr.g3:9:9: ')' + { + Match(')'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__10" + + // $ANTLR start "T__11" + private void mT__11() + { + try + { + int _type = T__11; + int _channel = DEFAULT_TOKEN_CHANNEL; + // JavaCompat\\Expr.g3:10:9: ( '*' ) + // JavaCompat\\Expr.g3:10:9: '*' + { + Match('*'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__11" + + // $ANTLR start "T__12" + private void mT__12() + { + try + { + int _type = T__12; + int _channel = DEFAULT_TOKEN_CHANNEL; + // JavaCompat\\Expr.g3:11:9: ( '+' ) + // JavaCompat\\Expr.g3:11:9: '+' + { + Match('+'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__12" + + // $ANTLR start "T__13" + private void mT__13() + { + try + { + int _type = T__13; + int _channel = DEFAULT_TOKEN_CHANNEL; + // JavaCompat\\Expr.g3:12:9: ( '=' ) + // JavaCompat\\Expr.g3:12:9: '=' + { + Match('='); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "T__13" + + // $ANTLR start "ID" + private void mID() + { + try + { + int _type = ID; + int _channel = DEFAULT_TOKEN_CHANNEL; + // JavaCompat\\Expr.g3:107:9: ( ( 'a' .. 'z' | 'A' .. 'Z' )+ ) + // JavaCompat\\Expr.g3:107:9: ( 'a' .. 'z' | 'A' .. 'Z' )+ + { + // JavaCompat\\Expr.g3:107:9: ( 'a' .. 'z' | 'A' .. 'Z' )+ + int cnt1=0; + for ( ; ; ) + { + int alt1=2; + int LA1_0 = input.LA(1); + + if ( ((LA1_0>='A' && LA1_0<='Z')||(LA1_0>='a' && LA1_0<='z')) ) + { + alt1=1; + } + + + switch ( alt1 ) + { + case 1: + // JavaCompat\\Expr.g3: + { + input.Consume(); + + + } + break; + + default: + if ( cnt1 >= 1 ) + goto loop1; + + EarlyExitException eee1 = new EarlyExitException( 1, input ); + throw eee1; + } + cnt1++; + } + loop1: + ; + + + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "ID" + + // $ANTLR start "INT" + private void mINT() + { + try + { + int _type = INT; + int _channel = DEFAULT_TOKEN_CHANNEL; + // JavaCompat\\Expr.g3:108:9: ( ( '0' .. '9' )+ ) + // JavaCompat\\Expr.g3:108:9: ( '0' .. '9' )+ + { + // JavaCompat\\Expr.g3:108:9: ( '0' .. '9' )+ + int cnt2=0; + for ( ; ; ) + { + int alt2=2; + int LA2_0 = input.LA(1); + + if ( ((LA2_0>='0' && LA2_0<='9')) ) + { + alt2=1; + } + + + switch ( alt2 ) + { + case 1: + // JavaCompat\\Expr.g3: + { + input.Consume(); + + + } + break; + + default: + if ( cnt2 >= 1 ) + goto loop2; + + EarlyExitException eee2 = new EarlyExitException( 2, input ); + throw eee2; + } + cnt2++; + } + loop2: + ; + + + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "INT" + + // $ANTLR start "NEWLINE" + private void mNEWLINE() + { + try + { + int _type = NEWLINE; + int _channel = DEFAULT_TOKEN_CHANNEL; + // JavaCompat\\Expr.g3:109:9: ( ( '\\r' )? '\\n' ) + // JavaCompat\\Expr.g3:109:9: ( '\\r' )? '\\n' + { + // JavaCompat\\Expr.g3:109:9: ( '\\r' )? + int alt3=2; + int LA3_0 = input.LA(1); + + if ( (LA3_0=='\r') ) + { + alt3=1; + } + switch ( alt3 ) + { + case 1: + // JavaCompat\\Expr.g3:109:0: '\\r' + { + Match('\r'); + + } + break; + + } + + Match('\n'); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "NEWLINE" + + // $ANTLR start "WS" + private void mWS() + { + try + { + int _type = WS; + int _channel = DEFAULT_TOKEN_CHANNEL; + // JavaCompat\\Expr.g3:110:9: ( ( ' ' | '\\t' )+ ) + // JavaCompat\\Expr.g3:110:9: ( ' ' | '\\t' )+ + { + // JavaCompat\\Expr.g3:110:9: ( ' ' | '\\t' )+ + int cnt4=0; + for ( ; ; ) + { + int alt4=2; + int LA4_0 = input.LA(1); + + if ( (LA4_0=='\t'||LA4_0==' ') ) + { + alt4=1; + } + + + switch ( alt4 ) + { + case 1: + // JavaCompat\\Expr.g3: + { + input.Consume(); + + + } + break; + + default: + if ( cnt4 >= 1 ) + goto loop4; + + EarlyExitException eee4 = new EarlyExitException( 4, input ); + throw eee4; + } + cnt4++; + } + loop4: + ; + + + Skip(); + + } + + state.type = _type; + state.channel = _channel; + } + finally + { + } + } + // $ANTLR end "WS" + + public override void mTokens() + { + // JavaCompat\\Expr.g3:1:10: ( T__8 | T__9 | T__10 | T__11 | T__12 | T__13 | ID | INT | NEWLINE | WS ) + int alt5=10; + switch ( input.LA(1) ) + { + case '-': + { + alt5=1; + } + break; + case '(': + { + alt5=2; + } + break; + case ')': + { + alt5=3; + } + break; + case '*': + { + alt5=4; + } + break; + case '+': + { + alt5=5; + } + break; + case '=': + { + alt5=6; + } + break; + case 'A': + case 'B': + case 'C': + case 'D': + case 'E': + case 'F': + case 'G': + case 'H': + case 'I': + case 'J': + case 'K': + case 'L': + case 'M': + case 'N': + case 'O': + case 'P': + case 'Q': + case 'R': + case 'S': + case 'T': + case 'U': + case 'V': + case 'W': + case 'X': + case 'Y': + case 'Z': + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + case 'g': + case 'h': + case 'i': + case 'j': + case 'k': + case 'l': + case 'm': + case 'n': + case 'o': + case 'p': + case 'q': + case 'r': + case 's': + case 't': + case 'u': + case 'v': + case 'w': + case 'x': + case 'y': + case 'z': + { + alt5=7; + } + break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + { + alt5=8; + } + break; + case '\n': + case '\r': + { + alt5=9; + } + break; + case '\t': + case ' ': + { + alt5=10; + } + break; + default: + { + NoViableAltException nvae = new NoViableAltException("", 5, 0, input); + + throw nvae; + } + } + + switch ( alt5 ) + { + case 1: + // JavaCompat\\Expr.g3:1:10: T__8 + { + mT__8(); + + } + break; + case 2: + // JavaCompat\\Expr.g3:1:15: T__9 + { + mT__9(); + + } + break; + case 3: + // JavaCompat\\Expr.g3:1:20: T__10 + { + mT__10(); + + } + break; + case 4: + // JavaCompat\\Expr.g3:1:26: T__11 + { + mT__11(); + + } + break; + case 5: + // JavaCompat\\Expr.g3:1:32: T__12 + { + mT__12(); + + } + break; + case 6: + // JavaCompat\\Expr.g3:1:38: T__13 + { + mT__13(); + + } + break; + case 7: + // JavaCompat\\Expr.g3:1:44: ID + { + mID(); + + } + break; + case 8: + // JavaCompat\\Expr.g3:1:47: INT + { + mINT(); + + } + break; + case 9: + // JavaCompat\\Expr.g3:1:51: NEWLINE + { + mNEWLINE(); + + } + break; + case 10: + // JavaCompat\\Expr.g3:1:59: WS + { + mWS(); + + } + break; + + } + + } + + + #region DFA + + protected override void InitDFAs() + { + base.InitDFAs(); + } + + + #endregion + +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/JavaCompat/ExprParser.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/JavaCompat/ExprParser.cs new file mode 100644 index 0000000..752dcef --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/JavaCompat/ExprParser.cs @@ -0,0 +1,533 @@ +// $ANTLR 3.1.2 JavaCompat\\Expr.g3 2009-03-16 13:19:15 + +// The variable 'variable' is assigned but its value is never used. +#pragma warning disable 219 +// Unreachable code detected. +#pragma warning disable 162 + + +// 'member' is obsolete +#pragma warning disable 612 + +using Antlr.Runtime.JavaExtensions; +using HashMap = System.Collections.Generic.Dictionary<object,object>; +using Integer = java.lang.Integer; + + +using System.Collections.Generic; +using Antlr.Runtime; +using Stack = System.Collections.Generic.Stack<object>; +using List = System.Collections.IList; +using ArrayList = System.Collections.Generic.List<object>; + +public partial class ExprParser : Parser +{ + public static readonly string[] tokenNames = new string[] { + "<invalid>", "<EOR>", "<DOWN>", "<UP>", "ID", "INT", "NEWLINE", "WS", "'-'", "'('", "')'", "'*'", "'+'", "'='" + }; + public const int EOF=-1; + public const int T__8=8; + public const int T__9=9; + public const int T__10=10; + public const int T__11=11; + public const int T__12=12; + public const int T__13=13; + public const int ID=4; + public const int INT=5; + public const int NEWLINE=6; + public const int WS=7; + + // delegates + // delegators + + public ExprParser( ITokenStream input ) + : this( input, new RecognizerSharedState() ) + { + } + public ExprParser( ITokenStream input, RecognizerSharedState state ) + : base( input, state ) + { + } + + + public override string[] GetTokenNames() { return ExprParser.tokenNames; } + public override string GrammarFileName { get { return "JavaCompat\\Expr.g3"; } } + + + /** Map variable name to Integer object holding value */ + HashMap memory = new HashMap(); + + + #region Rules + + // $ANTLR start "prog" + // JavaCompat\\Expr.g3:77:0: prog : ( stat )+ ; + private void prog( ) + { + try + { + // JavaCompat\\Expr.g3:77:9: ( ( stat )+ ) + // JavaCompat\\Expr.g3:77:9: ( stat )+ + { + // JavaCompat\\Expr.g3:77:9: ( stat )+ + int cnt1=0; + for ( ; ; ) + { + int alt1=2; + int LA1_0 = input.LA(1); + + if ( ((LA1_0>=ID && LA1_0<=NEWLINE)||LA1_0==9) ) + { + alt1=1; + } + + + switch ( alt1 ) + { + case 1: + // JavaCompat\\Expr.g3:77:0: stat + { + PushFollow(Follow._stat_in_prog40); + stat(); + + state._fsp--; + + + } + break; + + default: + if ( cnt1 >= 1 ) + goto loop1; + + EarlyExitException eee1 = new EarlyExitException( 1, input ); + throw eee1; + } + cnt1++; + } + loop1: + ; + + + + } + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + } + finally + { + } + return ; + } + // $ANTLR end "prog" + + + // $ANTLR start "stat" + // JavaCompat\\Expr.g3:79:0: stat : ( expr NEWLINE | ID '=' expr NEWLINE | NEWLINE ); + private void stat( ) + { + IToken ID2=null; + int expr1 = default(int); + int expr3 = default(int); + + try + { + // JavaCompat\\Expr.g3:79:9: ( expr NEWLINE | ID '=' expr NEWLINE | NEWLINE ) + int alt2=3; + switch ( input.LA(1) ) + { + case INT: + case 9: + { + alt2=1; + } + break; + case ID: + { + int LA2_2 = input.LA(2); + + if ( (LA2_2==13) ) + { + alt2=2; + } + else if ( (LA2_2==NEWLINE||LA2_2==8||(LA2_2>=11 && LA2_2<=12)) ) + { + alt2=1; + } + else + { + NoViableAltException nvae = new NoViableAltException("", 2, 2, input); + + throw nvae; + } + } + break; + case NEWLINE: + { + alt2=3; + } + break; + default: + { + NoViableAltException nvae = new NoViableAltException("", 2, 0, input); + + throw nvae; + } + } + + switch ( alt2 ) + { + case 1: + // JavaCompat\\Expr.g3:79:9: expr NEWLINE + { + PushFollow(Follow._expr_in_stat51); + expr1=expr(); + + state._fsp--; + + Match(input,NEWLINE,Follow._NEWLINE_in_stat53); + JSystem.@out.println(expr1); + + } + break; + case 2: + // JavaCompat\\Expr.g3:80:9: ID '=' expr NEWLINE + { + ID2=(IToken)Match(input,ID,Follow._ID_in_stat65); + Match(input,13,Follow._13_in_stat67); + PushFollow(Follow._expr_in_stat69); + expr3=expr(); + + state._fsp--; + + Match(input,NEWLINE,Follow._NEWLINE_in_stat71); + memory.put((ID2!=null?ID2.Text:null), new Integer(expr3)); + + } + break; + case 3: + // JavaCompat\\Expr.g3:82:9: NEWLINE + { + Match(input,NEWLINE,Follow._NEWLINE_in_stat91); + + } + break; + + } + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + } + finally + { + } + return ; + } + // $ANTLR end "stat" + + + // $ANTLR start "expr" + // JavaCompat\\Expr.g3:85:0: expr returns [int value] : e= multExpr ( '+' e= multExpr | '-' e= multExpr )* ; + private int expr( ) + { + + int value = default(int); + + int e = default(int); + + try + { + // JavaCompat\\Expr.g3:86:9: (e= multExpr ( '+' e= multExpr | '-' e= multExpr )* ) + // JavaCompat\\Expr.g3:86:9: e= multExpr ( '+' e= multExpr | '-' e= multExpr )* + { + PushFollow(Follow._multExpr_in_expr116); + e=multExpr(); + + state._fsp--; + + value = e; + // JavaCompat\\Expr.g3:87:9: ( '+' e= multExpr | '-' e= multExpr )* + for ( ; ; ) + { + int alt3=3; + int LA3_0 = input.LA(1); + + if ( (LA3_0==12) ) + { + alt3=1; + } + else if ( (LA3_0==8) ) + { + alt3=2; + } + + + switch ( alt3 ) + { + case 1: + // JavaCompat\\Expr.g3:87:13: '+' e= multExpr + { + Match(input,12,Follow._12_in_expr132); + PushFollow(Follow._multExpr_in_expr136); + e=multExpr(); + + state._fsp--; + + value += e; + + } + break; + case 2: + // JavaCompat\\Expr.g3:88:13: '-' e= multExpr + { + Match(input,8,Follow._8_in_expr152); + PushFollow(Follow._multExpr_in_expr156); + e=multExpr(); + + state._fsp--; + + value -= e; + + } + break; + + default: + goto loop3; + } + } + + loop3: + ; + + + + } + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + } + finally + { + } + return value; + } + // $ANTLR end "expr" + + + // $ANTLR start "multExpr" + // JavaCompat\\Expr.g3:92:0: multExpr returns [int value] : e= atom ( '*' e= atom )* ; + private int multExpr( ) + { + + int value = default(int); + + int e = default(int); + + try + { + // JavaCompat\\Expr.g3:93:9: (e= atom ( '*' e= atom )* ) + // JavaCompat\\Expr.g3:93:9: e= atom ( '*' e= atom )* + { + PushFollow(Follow._atom_in_multExpr194); + e=atom(); + + state._fsp--; + + value = e; + // JavaCompat\\Expr.g3:93:37: ( '*' e= atom )* + for ( ; ; ) + { + int alt4=2; + int LA4_0 = input.LA(1); + + if ( (LA4_0==11) ) + { + alt4=1; + } + + + switch ( alt4 ) + { + case 1: + // JavaCompat\\Expr.g3:93:38: '*' e= atom + { + Match(input,11,Follow._11_in_multExpr199); + PushFollow(Follow._atom_in_multExpr203); + e=atom(); + + state._fsp--; + + value *= e; + + } + break; + + default: + goto loop4; + } + } + + loop4: + ; + + + + } + + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + } + finally + { + } + return value; + } + // $ANTLR end "multExpr" + + + // $ANTLR start "atom" + // JavaCompat\\Expr.g3:96:0: atom returns [int value] : ( INT | ID | '(' expr ')' ); + private int atom( ) + { + + int value = default(int); + + IToken INT4=null; + IToken ID5=null; + int expr6 = default(int); + + try + { + // JavaCompat\\Expr.g3:97:9: ( INT | ID | '(' expr ')' ) + int alt5=3; + switch ( input.LA(1) ) + { + case INT: + { + alt5=1; + } + break; + case ID: + { + alt5=2; + } + break; + case 9: + { + alt5=3; + } + break; + default: + { + NoViableAltException nvae = new NoViableAltException("", 5, 0, input); + + throw nvae; + } + } + + switch ( alt5 ) + { + case 1: + // JavaCompat\\Expr.g3:97:9: INT + { + INT4=(IToken)Match(input,INT,Follow._INT_in_atom231); + value = Integer.parseInt((INT4!=null?INT4.Text:null)); + + } + break; + case 2: + // JavaCompat\\Expr.g3:98:9: ID + { + ID5=(IToken)Match(input,ID,Follow._ID_in_atom243); + + Integer v = (Integer)memory.get((ID5!=null?ID5.Text:null)); + if ( v!=null ) value = v.intValue(); + else JSystem.err.println("undefined variable "+(ID5!=null?ID5.Text:null)); + + + } + break; + case 3: + // JavaCompat\\Expr.g3:104:9: '(' expr ')' + { + Match(input,9,Follow._9_in_atom263); + PushFollow(Follow._expr_in_atom265); + expr6=expr(); + + state._fsp--; + + Match(input,10,Follow._10_in_atom267); + value = expr6; + + } + break; + + } + } + catch ( RecognitionException re ) + { + ReportError(re); + Recover(input,re); + } + finally + { + } + return value; + } + // $ANTLR end "atom" + #endregion + + // Delegated rules + + #region Synpreds + #endregion + + #region DFA + + protected override void InitDFAs() + { + base.InitDFAs(); + } + + #endregion + + #region Follow Sets + public static class Follow + { + public static readonly BitSet _stat_in_prog40 = new BitSet(new ulong[]{0x272UL}); + public static readonly BitSet _expr_in_stat51 = new BitSet(new ulong[]{0x40UL}); + public static readonly BitSet _NEWLINE_in_stat53 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _ID_in_stat65 = new BitSet(new ulong[]{0x2000UL}); + public static readonly BitSet _13_in_stat67 = new BitSet(new ulong[]{0x230UL}); + public static readonly BitSet _expr_in_stat69 = new BitSet(new ulong[]{0x40UL}); + public static readonly BitSet _NEWLINE_in_stat71 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _NEWLINE_in_stat91 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _multExpr_in_expr116 = new BitSet(new ulong[]{0x1102UL}); + public static readonly BitSet _12_in_expr132 = new BitSet(new ulong[]{0x230UL}); + public static readonly BitSet _multExpr_in_expr136 = new BitSet(new ulong[]{0x1102UL}); + public static readonly BitSet _8_in_expr152 = new BitSet(new ulong[]{0x230UL}); + public static readonly BitSet _multExpr_in_expr156 = new BitSet(new ulong[]{0x1102UL}); + public static readonly BitSet _atom_in_multExpr194 = new BitSet(new ulong[]{0x802UL}); + public static readonly BitSet _11_in_multExpr199 = new BitSet(new ulong[]{0x230UL}); + public static readonly BitSet _atom_in_multExpr203 = new BitSet(new ulong[]{0x802UL}); + public static readonly BitSet _INT_in_atom231 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _ID_in_atom243 = new BitSet(new ulong[]{0x2UL}); + public static readonly BitSet _9_in_atom263 = new BitSet(new ulong[]{0x230UL}); + public static readonly BitSet _expr_in_atom265 = new BitSet(new ulong[]{0x400UL}); + public static readonly BitSet _10_in_atom267 = new BitSet(new ulong[]{0x2UL}); + + } + #endregion +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Properties/AssemblyInfo.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..402410e --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Properties/AssemblyInfo.cs @@ -0,0 +1,68 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle( "Antlr3.Runtime.Test" )] +[assembly: AssemblyDescription( "" )] +[assembly: AssemblyConfiguration( "" )] +[assembly: AssemblyCompany( "Pixel Mine, Inc." )] +[assembly: AssemblyProduct( "Antlr3.Runtime.Test" )] +[assembly: AssemblyCopyright( "Copyright © Pixel Mine 2009" )] +[assembly: AssemblyTrademark( "" )] +[assembly: AssemblyCulture( "" )] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible( false )] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid( "1352b15b-eded-4380-9122-acde32f7ff38" )] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion( "1.0.0.0" )] +[assembly: AssemblyFileVersion( "1.0.0.0" )] diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.sln b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.sln new file mode 100644 index 0000000..1550134 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.sln @@ -0,0 +1,36 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual Studio 2008 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Antlr3.Runtime", "Antlr3.Runtime\Antlr3.Runtime.csproj", "{8FDC0A87-9005-4D5A-AB75-E55CEB575559}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Antlr3.Runtime.Debug", "Antlr3.Runtime.Debug\Antlr3.Runtime.Debug.csproj", "{5EE27A90-B023-42C9-AAF1-52B0424C5D0B}" +EndProject +Global + GlobalSection(SourceCodeControl) = preSolution + SccNumberOfProjects = 3 + SccLocalPath0 = . + SccProjectUniqueName1 = Antlr3.Runtime\\Antlr3.Runtime.csproj + SccLocalPath1 = . + SccProjectFilePathRelativizedFromConnection1 = Antlr3.Runtime\\ + SccProjectUniqueName2 = Antlr3.Runtime.Debug\\Antlr3.Runtime.Debug.csproj + SccLocalPath2 = . + SccProjectFilePathRelativizedFromConnection2 = Antlr3.Runtime.Debug\\ + EndGlobalSection + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8FDC0A87-9005-4D5A-AB75-E55CEB575559}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8FDC0A87-9005-4D5A-AB75-E55CEB575559}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8FDC0A87-9005-4D5A-AB75-E55CEB575559}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8FDC0A87-9005-4D5A-AB75-E55CEB575559}.Release|Any CPU.Build.0 = Release|Any CPU + {5EE27A90-B023-42C9-AAF1-52B0424C5D0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5EE27A90-B023-42C9-AAF1-52B0424C5D0B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5EE27A90-B023-42C9-AAF1-52B0424C5D0B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5EE27A90-B023-42C9-AAF1-52B0424C5D0B}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.vssscc b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.vssscc new file mode 100644 index 0000000..6cb031b --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime.vssscc @@ -0,0 +1,10 @@ +"" +{ +"FILE_VERSION" = "9237" +"ENLISTMENT_CHOICE" = "NEVER" +"PROJECT_FILE_RELATIVE_PATH" = "" +"NUMBER_OF_EXCLUDED_FILES" = "0" +"ORIGINAL_PROJECT_FILE_PATH" = "" +"NUMBER_OF_NESTED_PROJECTS" = "0" +"SOURCE_CONTROL_SETTINGS_PROVIDER" = "PROJECT" +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRFileStream.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRFileStream.cs new file mode 100644 index 0000000..e7730ee --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRFileStream.cs @@ -0,0 +1,80 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + using Encoding = System.Text.Encoding; + + /** <summary> + * This is a char buffer stream that is loaded from a file + * all at once when you construct the object. This looks very + * much like an ANTLReader or ANTLRInputStream, but it's a special case + * since we know the exact size of the object to load. We can avoid lots + * of data copying. + * </summary> + */ + [System.Serializable] + public class ANTLRFileStream : ANTLRStringStream + { + protected string fileName; + + public ANTLRFileStream( string fileName ) + : this( fileName, null ) + { + } + + public ANTLRFileStream( string fileName, Encoding encoding ) + { + this.fileName = fileName; + Load( fileName, encoding ); + } + + public virtual void Load( string fileName, Encoding encoding ) + { + if ( fileName == null ) + { + return; + } + + data = System.IO.File.ReadAllText( fileName, encoding ).ToCharArray(); + n = data.Length; + } + + public override string SourceName + { + get + { + return fileName; + } + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRInputStream.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRInputStream.cs new file mode 100644 index 0000000..fdd8298 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRInputStream.cs @@ -0,0 +1,85 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + using Encoding = System.Text.Encoding; + using Stream = System.IO.Stream; + using StreamReader = System.IO.StreamReader; + + /** <summary> + * A kind of ReaderStream that pulls from an InputStream. + * Useful for reading from stdin and specifying file encodings etc... + * </summary> + */ + [System.Serializable] + public class ANTLRInputStream : ANTLRReaderStream + { + public ANTLRInputStream() + { + } + + public ANTLRInputStream( Stream input ) + : this( input, null ) + { + } + + public ANTLRInputStream( Stream input, int size ) + : this( input, size, null ) + { + } + + public ANTLRInputStream( Stream input, Encoding encoding ) + : this( input, INITIAL_BUFFER_SIZE, encoding ) + { + } + + public ANTLRInputStream( Stream input, int size, Encoding encoding ) + : this( input, size, READ_BUFFER_SIZE, encoding ) + { + } + + public ANTLRInputStream( Stream input, int size, int readBufferSize, Encoding encoding ) + { + StreamReader isr; + if ( encoding != null ) + { + isr = new StreamReader( input, encoding ); + } + else + { + isr = new StreamReader( input ); + } + Load( isr, size, readBufferSize ); + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRReaderStream.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRReaderStream.cs new file mode 100644 index 0000000..b16f295 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRReaderStream.cs @@ -0,0 +1,97 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + using TextReader = System.IO.TextReader; + + /** <summary> + * Vacuum all input from a Reader and then treat it like a StringStream. + * Manage the buffer manually to avoid unnecessary data copying. + * </summary> + * + * <remarks> + * If you need encoding, use ANTLRInputStream. + * </remarks> + */ + [System.Serializable] + public class ANTLRReaderStream : ANTLRStringStream + { + public const int READ_BUFFER_SIZE = 1024; + public const int INITIAL_BUFFER_SIZE = 1024; + + public ANTLRReaderStream() + { + } + + public ANTLRReaderStream( TextReader r ) + : this( r, INITIAL_BUFFER_SIZE, READ_BUFFER_SIZE ) + { + } + + public ANTLRReaderStream( TextReader r, int size ) + : this( r, size, READ_BUFFER_SIZE ) + { + } + + public ANTLRReaderStream( TextReader r, int size, int readChunkSize ) + { + Load( r, size, readChunkSize ); + } + + public virtual void Load( TextReader r, int size, int readChunkSize ) + { + if ( r == null ) + { + return; + } + if ( size <= 0 ) + { + size = INITIAL_BUFFER_SIZE; + } + if ( readChunkSize <= 0 ) + { + readChunkSize = READ_BUFFER_SIZE; + } + // System.out.println("load "+size+" in chunks of "+readChunkSize); + try + { + data = r.ReadToEnd().ToCharArray(); + base.n = data.Length; + } + finally + { + r.Close(); + } + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRStringStream.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRStringStream.cs new file mode 100644 index 0000000..f334877 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ANTLRStringStream.cs @@ -0,0 +1,293 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + using System.Collections.Generic; + + /** <summary> + * A pretty quick CharStream that pulls all data from an array + * directly. Every method call counts in the lexer. Java's + * strings aren't very good so I'm avoiding. + * </summary> + */ + [System.Serializable] + public class ANTLRStringStream : ICharStream + { + /** <summary>The data being scanned</summary> */ + protected char[] data; + + /** <summary>How many characters are actually in the buffer</summary> */ + protected int n; + + /** <summary>0..n-1 index into string of next char</summary> */ + protected int p = 0; + + /** <summary>line number 1..n within the input</summary> */ + protected int line = 1; + + /** <summary>The index of the character relative to the beginning of the line 0..n-1</summary> */ + protected int charPositionInLine = 0; + + /** <summary>tracks how deep mark() calls are nested</summary> */ + protected int markDepth = 0; + + /** <summary> + * A list of CharStreamState objects that tracks the stream state + * values line, charPositionInLine, and p that can change as you + * move through the input stream. Indexed from 1..markDepth. + * A null is kept @ index 0. Create upon first call to mark(). + * </summary> + */ + protected IList<CharStreamState> markers; + + /** <summary>Track the last mark() call result value for use in rewind().</summary> */ + protected int lastMarker; + + /** <summary>What is name or source of this char stream?</summary> */ + public string name; + + public ANTLRStringStream() + { + } + + /** <summary>Copy data in string to a local char array</summary> */ + public ANTLRStringStream( string input ) + : this( input, null ) + { + } + + public ANTLRStringStream( string input, string sourceName ) + : this( input.ToCharArray(), input.Length, sourceName ) + { + } + + /** <summary>This is the preferred constructor as no data is copied</summary> */ + public ANTLRStringStream( char[] data, int numberOfActualCharsInArray ) + : this( data, numberOfActualCharsInArray, null ) + { + } + + public ANTLRStringStream( char[] data, int numberOfActualCharsInArray, string sourceName ) + : this() + { + this.data = data; + this.n = numberOfActualCharsInArray; + this.name = sourceName; + } + + /** <summary> + * Return the current input symbol index 0..n where n indicates the + * last symbol has been read. The index is the index of char to + * be returned from LA(1). + * </summary> + */ + public virtual int Index + { + get + { + return p; + } + } + public virtual int Line + { + get + { + return line; + } + set + { + line = value; + } + } + public virtual int CharPositionInLine + { + get + { + return charPositionInLine; + } + set + { + charPositionInLine = value; + } + } + + /** <summary> + * Reset the stream so that it's in the same state it was + * when the object was created *except* the data array is not + * touched. + * </summary> + */ + public virtual void Reset() + { + p = 0; + line = 1; + charPositionInLine = 0; + markDepth = 0; + } + + public virtual void Consume() + { + //System.out.println("prev p="+p+", c="+(char)data[p]); + if ( p < n ) + { + charPositionInLine++; + if ( data[p] == '\n' ) + { + /* + System.out.println("newline char found on line: "+line+ + "@ pos="+charPositionInLine); + */ + line++; + charPositionInLine = 0; + } + p++; + //System.out.println("p moves to "+p+" (c='"+(char)data[p]+"')"); + } + } + + public virtual int LA( int i ) + { + if ( i == 0 ) + { + return 0; // undefined + } + if ( i < 0 ) + { + i++; // e.g., translate LA(-1) to use offset i=0; then data[p+0-1] + if ( ( p + i - 1 ) < 0 ) + { + return CharStreamConstants.EOF; // invalid; no char before first char + } + } + + if ( ( p + i - 1 ) >= n ) + { + //System.out.println("char LA("+i+")=EOF; p="+p); + return CharStreamConstants.EOF; + } + //System.out.println("char LA("+i+")="+(char)data[p+i-1]+"; p="+p); + //System.out.println("LA("+i+"); p="+p+" n="+n+" data.length="+data.length); + return data[p + i - 1]; + } + + public virtual int LT( int i ) + { + return LA( i ); + } + + public virtual int Size() + { + return n; + } + + public virtual int Mark() + { + if ( markers == null ) + { + markers = new List<CharStreamState>(); + markers.Add( null ); // depth 0 means no backtracking, leave blank + } + markDepth++; + CharStreamState state = null; + if ( markDepth >= markers.Count ) + { + state = new CharStreamState(); + markers.Add( state ); + } + else + { + state = markers[markDepth]; + } + state.p = p; + state.line = line; + state.charPositionInLine = charPositionInLine; + lastMarker = markDepth; + return markDepth; + } + + public virtual void Rewind( int m ) + { + CharStreamState state = markers[m]; + // restore stream state + Seek( state.p ); + line = state.line; + charPositionInLine = state.charPositionInLine; + Release( m ); + } + + public virtual void Rewind() + { + Rewind( lastMarker ); + } + + public virtual void Release( int marker ) + { + // unwind any other markers made after m and release m + markDepth = marker; + // release this marker + markDepth--; + } + + /** <summary> + * consume() ahead until p==index; can't just set p=index as we must + * update line and charPositionInLine. + * </summary> + */ + public virtual void Seek( int index ) + { + if ( index <= p ) + { + p = index; // just jump; don't update stream state (line, ...) + return; + } + // seek forward, consume until p hits index + while ( p < index ) + { + Consume(); + } + } + + public virtual string substring( int start, int stop ) + { + return new string( data, start, stop - start + 1 ); + } + + public virtual string SourceName + { + get + { + return name; + } + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Antlr3.Runtime.csproj b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Antlr3.Runtime.csproj new file mode 100644 index 0000000..6608358 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Antlr3.Runtime.csproj @@ -0,0 +1,128 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProductVersion>9.0.30729</ProductVersion> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{8FDC0A87-9005-4D5A-AB75-E55CEB575559}</ProjectGuid> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>Antlr.Runtime</RootNamespace> + <AssemblyName>Antlr3.Runtime</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + <SccProjectName>SAK</SccProjectName> + <SccLocalPath>SAK</SccLocalPath> + <SccAuxPath>SAK</SccAuxPath> + <SccProvider>SAK</SccProvider> + <SignAssembly>true</SignAssembly> + <AssemblyOriginatorKeyFile>Key.snk</AssemblyOriginatorKeyFile> + <TargetFrameworkSubset>Client</TargetFrameworkSubset> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + <Reference Include="System.Core"> + <RequiredTargetFramework>3.5</RequiredTargetFramework> + </Reference> + </ItemGroup> + <ItemGroup> + <Compile Include="ANTLRFileStream.cs" /> + <Compile Include="ANTLRInputStream.cs" /> + <Compile Include="ANTLRReaderStream.cs" /> + <Compile Include="ANTLRStringStream.cs" /> + <Compile Include="BaseRecognizer.cs" /> + <Compile Include="BitSet.cs" /> + <Compile Include="CharStreamConstants.cs" /> + <Compile Include="CharStreamState.cs" /> + <Compile Include="ClassicToken.cs" /> + <Compile Include="CommonToken.cs" /> + <Compile Include="CommonTokenStream.cs" /> + <Compile Include="Debug\DebugEventListenerConstants.cs" /> + <Compile Include="Debug\IDebugEventListener.cs" /> + <Compile Include="DFA.cs" /> + <Compile Include="EarlyExitException.cs" /> + <Compile Include="FailedPredicateException.cs" /> + <Compile Include="ICharStream.cs" /> + <Compile Include="IIntStream.cs" /> + <Compile Include="IToken.cs" /> + <Compile Include="ITokenSource.cs" /> + <Compile Include="ITokenStream.cs" /> + <Compile Include="Lexer.cs" /> + <Compile Include="Misc\FastQueue.cs" /> + <Compile Include="Misc\LookaheadStream.cs" /> + <Compile Include="MismatchedNotSetException.cs" /> + <Compile Include="MismatchedRangeException.cs" /> + <Compile Include="MismatchedSetException.cs" /> + <Compile Include="MismatchedTokenException.cs" /> + <Compile Include="MismatchedTreeNodeException.cs" /> + <Compile Include="MissingTokenException.cs" /> + <Compile Include="NoViableAltException.cs" /> + <Compile Include="Parser.cs" /> + <Compile Include="ParserRuleReturnScope.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="RecognitionException.cs" /> + <Compile Include="RecognizerSharedState.cs" /> + <Compile Include="RuleReturnScope.cs" /> + <Compile Include="TokenConstants.cs" /> + <Compile Include="TokenRewriteStream.cs" /> + <Compile Include="Tree\BaseTree.cs" /> + <Compile Include="Tree\BaseTreeAdaptor.cs" /> + <Compile Include="Tree\BufferedTreeNodeStream.cs" /> + <Compile Include="Tree\CommonErrorNode.cs" /> + <Compile Include="Tree\CommonTree.cs" /> + <Compile Include="Tree\CommonTreeAdaptor.cs" /> + <Compile Include="Tree\CommonTreeNodeStream.cs" /> + <Compile Include="Tree\ITree.cs" /> + <Compile Include="Tree\ITreeAdaptor.cs" /> + <Compile Include="Tree\ITreeNodeStream.cs" /> + <Compile Include="Tree\ITreeVisitorAction.cs" /> + <Compile Include="Tree\ParseTree.cs" /> + <Compile Include="Tree\RewriteCardinalityException.cs" /> + <Compile Include="Tree\RewriteEarlyExitException.cs" /> + <Compile Include="Tree\RewriteEmptyStreamException.cs" /> + <Compile Include="Tree\RewriteRuleElementStream.cs" /> + <Compile Include="Tree\RewriteRuleNodeStream.cs" /> + <Compile Include="Tree\RewriteRuleSubtreeStream.cs" /> + <Compile Include="Tree\RewriteRuleTokenStream.cs" /> + <Compile Include="Tree\TreeConstants.cs" /> + <Compile Include="Tree\TreeFilter.cs" /> + <Compile Include="Tree\TreeIterator.cs" /> + <Compile Include="Tree\TreeParser.cs" /> + <Compile Include="Tree\TreePatternLexer.cs" /> + <Compile Include="Tree\TreePatternParser.cs" /> + <Compile Include="Tree\TreeRewriter.cs" /> + <Compile Include="Tree\TreeRuleReturnScope.cs" /> + <Compile Include="Tree\TreeVisitor.cs" /> + <Compile Include="Tree\TreeWizard.cs" /> + <Compile Include="UnwantedTokenException.cs" /> + </ItemGroup> + <ItemGroup> + <None Include="Key.snk" /> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Antlr3.Runtime.csproj.vspscc b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Antlr3.Runtime.csproj.vspscc new file mode 100644 index 0000000..b6d3289 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Antlr3.Runtime.csproj.vspscc @@ -0,0 +1,10 @@ +"" +{ +"FILE_VERSION" = "9237" +"ENLISTMENT_CHOICE" = "NEVER" +"PROJECT_FILE_RELATIVE_PATH" = "" +"NUMBER_OF_EXCLUDED_FILES" = "0" +"ORIGINAL_PROJECT_FILE_PATH" = "" +"NUMBER_OF_NESTED_PROJECTS" = "0" +"SOURCE_CONTROL_SETTINGS_PROVIDER" = "PROVIDER" +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/BaseRecognizer.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/BaseRecognizer.cs new file mode 100644 index 0000000..7701609 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/BaseRecognizer.cs @@ -0,0 +1,1148 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + using System.Collections.Generic; + + using Array = System.Array; + using Conditional = System.Diagnostics.ConditionalAttribute; + using Console = System.Console; + using Exception = System.Exception; + using IDebugEventListener = Antlr.Runtime.Debug.IDebugEventListener; + using NotSupportedException = System.NotSupportedException; + using Regex = System.Text.RegularExpressions.Regex; + using StackFrame = System.Diagnostics.StackFrame; + using StackTrace = System.Diagnostics.StackTrace; + + /** <summary> + * A generic recognizer that can handle recognizers generated from + * lexer, parser, and tree grammars. This is all the parsing + * support code essentially; most of it is error recovery stuff and + * backtracking. + * </summary> + */ + public abstract class BaseRecognizer + { + public const int MEMO_RULE_FAILED = -2; + public const int MEMO_RULE_UNKNOWN = -1; + public const int INITIAL_FOLLOW_STACK_SIZE = 100; + + // copies from Token object for convenience in actions + public const int DEFAULT_TOKEN_CHANNEL = TokenConstants.DEFAULT_CHANNEL; + public const int HIDDEN = TokenConstants.HIDDEN_CHANNEL; + + public const string NEXT_TOKEN_RULE_NAME = "nextToken"; + + /** <summary> + * State of a lexer, parser, or tree parser are collected into a state + * object so the state can be shared. This sharing is needed to + * have one grammar import others and share same error variables + * and other state variables. It's a kind of explicit multiple + * inheritance via delegation of methods and shared state. + * </summary> + */ + protected internal RecognizerSharedState state; + + public BaseRecognizer() + { + state = new RecognizerSharedState(); + Initialize(); + InitDFAs(); + } + + public BaseRecognizer( RecognizerSharedState state ) + { + if ( state == null ) + { + state = new RecognizerSharedState(); + } + this.state = state; + Initialize(); + InitDFAs(); + } + + protected virtual void InitDFAs() + { + } + + protected virtual void Initialize() + { + } + + /** <summary>reset the parser's state; subclasses must rewinds the input stream</summary> */ + public virtual void Reset() + { + // wack everything related to error recovery + if ( state == null ) + { + return; // no shared state work to do + } + state._fsp = -1; + state.errorRecovery = false; + state.lastErrorIndex = -1; + state.failed = false; + state.syntaxErrors = 0; + // wack everything related to backtracking and memoization + state.backtracking = 0; + for ( int i = 0; state.ruleMemo != null && i < state.ruleMemo.Length; i++ ) + { // wipe cache + state.ruleMemo[i] = null; + } + } + + + /** <summary> + * Match current input symbol against ttype. Attempt + * single token insertion or deletion error recovery. If + * that fails, throw MismatchedTokenException. + * </summary> + * + * <remarks> + * To turn off single token insertion or deletion error + * recovery, override recoverFromMismatchedToken() and have it + * throw an exception. See TreeParser.recoverFromMismatchedToken(). + * This way any error in a rule will cause an exception and + * immediate exit from rule. Rule would recover by resynchronizing + * to the set of symbols that can follow rule ref. + * </remarks> + */ + public virtual object Match( IIntStream input, int ttype, BitSet follow ) + { + //System.out.println("match "+((TokenStream)input).LT(1)); + object matchedSymbol = GetCurrentInputSymbol( input ); + if ( input.LA( 1 ) == ttype ) + { + input.Consume(); + state.errorRecovery = false; + state.failed = false; + return matchedSymbol; + } + if ( state.backtracking > 0 ) + { + state.failed = true; + return matchedSymbol; + } + matchedSymbol = RecoverFromMismatchedToken( input, ttype, follow ); + return matchedSymbol; + } + + /** <summary>Match the wildcard: in a symbol</summary> */ + public virtual void MatchAny( IIntStream input ) + { + state.errorRecovery = false; + state.failed = false; + input.Consume(); + } + + public virtual bool MismatchIsUnwantedToken( IIntStream input, int ttype ) + { + return input.LA( 2 ) == ttype; + } + + public virtual bool MismatchIsMissingToken( IIntStream input, BitSet follow ) + { + if ( follow == null ) + { + // we have no information about the follow; we can only consume + // a single token and hope for the best + return false; + } + // compute what can follow this grammar element reference + if ( follow.Member( TokenConstants.EOR_TOKEN_TYPE ) ) + { + BitSet viableTokensFollowingThisRule = ComputeContextSensitiveRuleFOLLOW(); + follow = follow.Or( viableTokensFollowingThisRule ); + if ( state._fsp >= 0 ) + { // remove EOR if we're not the start symbol + follow.Remove( TokenConstants.EOR_TOKEN_TYPE ); + } + } + // if current token is consistent with what could come after set + // then we know we're missing a token; error recovery is free to + // "insert" the missing token + + //System.out.println("viable tokens="+follow.toString(getTokenNames())); + //System.out.println("LT(1)="+((TokenStream)input).LT(1)); + + // BitSet cannot handle negative numbers like -1 (EOF) so I leave EOR + // in follow set to indicate that the fall of the start symbol is + // in the set (EOF can follow). + if ( follow.Member( input.LA( 1 ) ) || follow.Member( TokenConstants.EOR_TOKEN_TYPE ) ) + { + //System.out.println("LT(1)=="+((TokenStream)input).LT(1)+" is consistent with what follows; inserting..."); + return true; + } + return false; + } + + /** <summary>Report a recognition problem.</summary> + * + * <remarks> + * This method sets errorRecovery to indicate the parser is recovering + * not parsing. Once in recovery mode, no errors are generated. + * To get out of recovery mode, the parser must successfully match + * a token (after a resync). So it will go: + * + * 1. error occurs + * 2. enter recovery mode, report error + * 3. consume until token found in resynch set + * 4. try to resume parsing + * 5. next match() will reset errorRecovery mode + * + * If you override, make sure to update syntaxErrors if you care about that. + * </remarks> + */ + public virtual void ReportError( RecognitionException e ) + { + // if we've already reported an error and have not matched a token + // yet successfully, don't report any errors. + if ( state.errorRecovery ) + { + //System.err.print("[SPURIOUS] "); + return; + } + state.syntaxErrors++; // don't count spurious + state.errorRecovery = true; + + DisplayRecognitionError( this.GetTokenNames(), e ); + } + + public virtual void DisplayRecognitionError( string[] tokenNames, + RecognitionException e ) + { + string hdr = GetErrorHeader( e ); + string msg = GetErrorMessage( e, tokenNames ); + EmitErrorMessage( hdr + " " + msg ); + } + + /** <summary>What error message should be generated for the various exception types?</summary> + * + * <remarks> + * Not very object-oriented code, but I like having all error message + * generation within one method rather than spread among all of the + * exception classes. This also makes it much easier for the exception + * handling because the exception classes do not have to have pointers back + * to this object to access utility routines and so on. Also, changing + * the message for an exception type would be difficult because you + * would have to subclassing exception, but then somehow get ANTLR + * to make those kinds of exception objects instead of the default. + * This looks weird, but trust me--it makes the most sense in terms + * of flexibility. + * + * For grammar debugging, you will want to override this to add + * more information such as the stack frame with + * getRuleInvocationStack(e, this.getClass().getName()) and, + * for no viable alts, the decision description and state etc... + * + * Override this to change the message generated for one or more + * exception types. + * </remarks> + */ + public virtual string GetErrorMessage( RecognitionException e, string[] tokenNames ) + { + string msg = e.Message; + if ( e is UnwantedTokenException ) + { + UnwantedTokenException ute = (UnwantedTokenException)e; + string tokenName = "<unknown>"; + if ( ute.expecting == TokenConstants.EOF ) + { + tokenName = "EOF"; + } + else + { + tokenName = tokenNames[ute.expecting]; + } + msg = "extraneous input " + GetTokenErrorDisplay( ute.UnexpectedToken ) + + " expecting " + tokenName; + } + else if ( e is MissingTokenException ) + { + MissingTokenException mte = (MissingTokenException)e; + string tokenName = "<unknown>"; + if ( mte.expecting == TokenConstants.EOF ) + { + tokenName = "EOF"; + } + else + { + tokenName = tokenNames[mte.expecting]; + } + msg = "missing " + tokenName + " at " + GetTokenErrorDisplay( e.token ); + } + else if ( e is MismatchedTokenException ) + { + MismatchedTokenException mte = (MismatchedTokenException)e; + string tokenName = "<unknown>"; + if ( mte.expecting == TokenConstants.EOF ) + { + tokenName = "EOF"; + } + else + { + tokenName = tokenNames[mte.expecting]; + } + msg = "mismatched input " + GetTokenErrorDisplay( e.token ) + + " expecting " + tokenName; + } + else if ( e is MismatchedTreeNodeException ) + { + MismatchedTreeNodeException mtne = (MismatchedTreeNodeException)e; + string tokenName = "<unknown>"; + if ( mtne.expecting == TokenConstants.EOF ) + { + tokenName = "EOF"; + } + else + { + tokenName = tokenNames[mtne.expecting]; + } + // workaround for a .NET framework bug (NullReferenceException) + string nodeText = ( mtne.node != null ) ? mtne.node.ToString() ?? string.Empty : string.Empty; + msg = "mismatched tree node: " + nodeText + " expecting " + tokenName; + } + else if ( e is NoViableAltException ) + { + //NoViableAltException nvae = (NoViableAltException)e; + // for development, can add "decision=<<"+nvae.grammarDecisionDescription+">>" + // and "(decision="+nvae.decisionNumber+") and + // "state "+nvae.stateNumber + msg = "no viable alternative at input " + GetTokenErrorDisplay( e.token ); + } + else if ( e is EarlyExitException ) + { + //EarlyExitException eee = (EarlyExitException)e; + // for development, can add "(decision="+eee.decisionNumber+")" + msg = "required (...)+ loop did not match anything at input " + + GetTokenErrorDisplay( e.token ); + } + else if ( e is MismatchedSetException ) + { + MismatchedSetException mse = (MismatchedSetException)e; + msg = "mismatched input " + GetTokenErrorDisplay( e.token ) + + " expecting set " + mse.expecting; + } + else if ( e is MismatchedNotSetException ) + { + MismatchedNotSetException mse = (MismatchedNotSetException)e; + msg = "mismatched input " + GetTokenErrorDisplay( e.token ) + + " expecting set " + mse.expecting; + } + else if ( e is FailedPredicateException ) + { + FailedPredicateException fpe = (FailedPredicateException)e; + msg = "rule " + fpe.ruleName + " failed predicate: {" + + fpe.predicateText + "}?"; + } + return msg; + } + + /** <summary> + * Get number of recognition errors (lexer, parser, tree parser). Each + * recognizer tracks its own number. So parser and lexer each have + * separate count. Does not count the spurious errors found between + * an error and next valid token match + * </summary> + * + * <seealso cref="reportError()"/> + */ + public virtual int NumberOfSyntaxErrors + { + get + { + return state.syntaxErrors; + } + } + + /** <summary>What is the error header, normally line/character position information?</summary> */ + public virtual string GetErrorHeader( RecognitionException e ) + { + return "line " + e.line + ":" + e.charPositionInLine; + } + + /** <summary> + * How should a token be displayed in an error message? The default + * is to display just the text, but during development you might + * want to have a lot of information spit out. Override in that case + * to use t.toString() (which, for CommonToken, dumps everything about + * the token). This is better than forcing you to override a method in + * your token objects because you don't have to go modify your lexer + * so that it creates a new Java type. + * </summary> + */ + public virtual string GetTokenErrorDisplay( IToken t ) + { + string s = t.Text; + if ( s == null ) + { + if ( t.Type == TokenConstants.EOF ) + { + s = "<EOF>"; + } + else + { + s = "<" + t.Type + ">"; + } + } + s = Regex.Replace( s, "\n", "\\\\n" ); + s = Regex.Replace( s, "\r", "\\\\r" ); + s = Regex.Replace( s, "\t", "\\\\t" ); + return "'" + s + "'"; + } + + /** <summary>Override this method to change where error messages go</summary> */ + public virtual void EmitErrorMessage( string msg ) + { + Console.Error.WriteLine( msg ); + } + + /** <summary> + * Recover from an error found on the input stream. This is + * for NoViableAlt and mismatched symbol exceptions. If you enable + * single token insertion and deletion, this will usually not + * handle mismatched symbol exceptions but there could be a mismatched + * token that the match() routine could not recover from. + * </summary> + */ + public virtual void Recover( IIntStream input, RecognitionException re ) + { + if ( state.lastErrorIndex == input.Index ) + { + // uh oh, another error at same token index; must be a case + // where LT(1) is in the recovery token set so nothing is + // consumed; consume a single token so at least to prevent + // an infinite loop; this is a failsafe. + input.Consume(); + } + state.lastErrorIndex = input.Index; + BitSet followSet = ComputeErrorRecoverySet(); + BeginResync(); + ConsumeUntil( input, followSet ); + EndResync(); + } + + /** <summary> + * A hook to listen in on the token consumption during error recovery. + * The DebugParser subclasses this to fire events to the listenter. + * </summary> + */ + public virtual void BeginResync() + { + } + + public virtual void EndResync() + { + } + + /* Compute the error recovery set for the current rule. During + * rule invocation, the parser pushes the set of tokens that can + * follow that rule reference on the stack; this amounts to + * computing FIRST of what follows the rule reference in the + * enclosing rule. This local follow set only includes tokens + * from within the rule; i.e., the FIRST computation done by + * ANTLR stops at the end of a rule. + * + * EXAMPLE + * + * When you find a "no viable alt exception", the input is not + * consistent with any of the alternatives for rule r. The best + * thing to do is to consume tokens until you see something that + * can legally follow a call to r *or* any rule that called r. + * You don't want the exact set of viable next tokens because the + * input might just be missing a token--you might consume the + * rest of the input looking for one of the missing tokens. + * + * Consider grammar: + * + * a : '[' b ']' + * | '(' b ')' + * ; + * b : c '^' INT ; + * c : ID + * | INT + * ; + * + * At each rule invocation, the set of tokens that could follow + * that rule is pushed on a stack. Here are the various "local" + * follow sets: + * + * FOLLOW(b1_in_a) = FIRST(']') = ']' + * FOLLOW(b2_in_a) = FIRST(')') = ')' + * FOLLOW(c_in_b) = FIRST('^') = '^' + * + * Upon erroneous input "[]", the call chain is + * + * a -> b -> c + * + * and, hence, the follow context stack is: + * + * depth local follow set after call to rule + * 0 <EOF> a (from main()) + * 1 ']' b + * 3 '^' c + * + * Notice that ')' is not included, because b would have to have + * been called from a different context in rule a for ')' to be + * included. + * + * For error recovery, we cannot consider FOLLOW(c) + * (context-sensitive or otherwise). We need the combined set of + * all context-sensitive FOLLOW sets--the set of all tokens that + * could follow any reference in the call chain. We need to + * resync to one of those tokens. Note that FOLLOW(c)='^' and if + * we resync'd to that token, we'd consume until EOF. We need to + * sync to context-sensitive FOLLOWs for a, b, and c: {']','^'}. + * In this case, for input "[]", LA(1) is in this set so we would + * not consume anything and after printing an error rule c would + * return normally. It would not find the required '^' though. + * At this point, it gets a mismatched token error and throws an + * exception (since LA(1) is not in the viable following token + * set). The rule exception handler tries to recover, but finds + * the same recovery set and doesn't consume anything. Rule b + * exits normally returning to rule a. Now it finds the ']' (and + * with the successful match exits errorRecovery mode). + * + * So, you cna see that the parser walks up call chain looking + * for the token that was a member of the recovery set. + * + * Errors are not generated in errorRecovery mode. + * + * ANTLR's error recovery mechanism is based upon original ideas: + * + * "Algorithms + Data Structures = Programs" by Niklaus Wirth + * + * and + * + * "A note on error recovery in recursive descent parsers": + * http://portal.acm.org/citation.cfm?id=947902.947905 + * + * Later, Josef Grosch had some good ideas: + * + * "Efficient and Comfortable Error Recovery in Recursive Descent + * Parsers": + * ftp://www.cocolab.com/products/cocktail/doca4.ps/ell.ps.zip + * + * Like Grosch I implemented local FOLLOW sets that are combined + * at run-time upon error to avoid overhead during parsing. + */ + protected virtual BitSet ComputeErrorRecoverySet() + { + return CombineFollows( false ); + } + + /** <summary> + * Compute the context-sensitive FOLLOW set for current rule. + * This is set of token types that can follow a specific rule + * reference given a specific call chain. You get the set of + * viable tokens that can possibly come next (lookahead depth 1) + * given the current call chain. Contrast this with the + * definition of plain FOLLOW for rule r: + * </summary> + * + * FOLLOW(r)={x | S=>*alpha r beta in G and x in FIRST(beta)} + * + * where x in T* and alpha, beta in V*; T is set of terminals and + * V is the set of terminals and nonterminals. In other words, + * FOLLOW(r) is the set of all tokens that can possibly follow + * references to r in *any* sentential form (context). At + * runtime, however, we know precisely which context applies as + * we have the call chain. We may compute the exact (rather + * than covering superset) set of following tokens. + * + * For example, consider grammar: + * + * stat : ID '=' expr ';' // FOLLOW(stat)=={EOF} + * | "return" expr '.' + * ; + * expr : atom ('+' atom)* ; // FOLLOW(expr)=={';','.',')'} + * atom : INT // FOLLOW(atom)=={'+',')',';','.'} + * | '(' expr ')' + * ; + * + * The FOLLOW sets are all inclusive whereas context-sensitive + * FOLLOW sets are precisely what could follow a rule reference. + * For input input "i=(3);", here is the derivation: + * + * stat => ID '=' expr ';' + * => ID '=' atom ('+' atom)* ';' + * => ID '=' '(' expr ')' ('+' atom)* ';' + * => ID '=' '(' atom ')' ('+' atom)* ';' + * => ID '=' '(' INT ')' ('+' atom)* ';' + * => ID '=' '(' INT ')' ';' + * + * At the "3" token, you'd have a call chain of + * + * stat -> expr -> atom -> expr -> atom + * + * What can follow that specific nested ref to atom? Exactly ')' + * as you can see by looking at the derivation of this specific + * input. Contrast this with the FOLLOW(atom)={'+',')',';','.'}. + * + * You want the exact viable token set when recovering from a + * token mismatch. Upon token mismatch, if LA(1) is member of + * the viable next token set, then you know there is most likely + * a missing token in the input stream. "Insert" one by just not + * throwing an exception. + */ + protected virtual BitSet ComputeContextSensitiveRuleFOLLOW() + { + return CombineFollows( true ); + } + + protected virtual BitSet CombineFollows( bool exact ) + { + int top = state._fsp; + BitSet followSet = new BitSet(); + for ( int i = top; i >= 0; i-- ) + { + BitSet localFollowSet = (BitSet)state.following[i]; + /* + System.out.println("local follow depth "+i+"="+ + localFollowSet.toString(getTokenNames())+")"); + */ + followSet.OrInPlace( localFollowSet ); + if ( exact ) + { + // can we see end of rule? + if ( localFollowSet.Member( TokenConstants.EOR_TOKEN_TYPE ) ) + { + // Only leave EOR in set if at top (start rule); this lets + // us know if have to include follow(start rule); i.e., EOF + if ( i > 0 ) + { + followSet.Remove( TokenConstants.EOR_TOKEN_TYPE ); + } + } + else + { // can't see end of rule, quit + break; + } + } + } + return followSet; + } + + /** <summary>Attempt to recover from a single missing or extra token.</summary> + * + * EXTRA TOKEN + * + * LA(1) is not what we are looking for. If LA(2) has the right token, + * however, then assume LA(1) is some extra spurious token. Delete it + * and LA(2) as if we were doing a normal match(), which advances the + * input. + * + * MISSING TOKEN + * + * If current token is consistent with what could come after + * ttype then it is ok to "insert" the missing token, else throw + * exception For example, Input "i=(3;" is clearly missing the + * ')'. When the parser returns from the nested call to expr, it + * will have call chain: + * + * stat -> expr -> atom + * + * and it will be trying to match the ')' at this point in the + * derivation: + * + * => ID '=' '(' INT ')' ('+' atom)* ';' + * ^ + * match() will see that ';' doesn't match ')' and report a + * mismatched token error. To recover, it sees that LA(1)==';' + * is in the set of tokens that can follow the ')' token + * reference in rule atom. It can assume that you forgot the ')'. + */ + protected virtual object RecoverFromMismatchedToken( IIntStream input, int ttype, BitSet follow ) + { + RecognitionException e = null; + // if next token is what we are looking for then "delete" this token + if ( MismatchIsUnwantedToken( input, ttype ) ) + { + e = new UnwantedTokenException( ttype, input ) + { + tokenNames = GetTokenNames() + }; + /* + System.err.println("recoverFromMismatchedToken deleting "+ + ((TokenStream)input).LT(1)+ + " since "+((TokenStream)input).LT(2)+" is what we want"); + */ + BeginResync(); + input.Consume(); // simply delete extra token + EndResync(); + ReportError( e ); // report after consuming so AW sees the token in the exception + // we want to return the token we're actually matching + object matchedSymbol = GetCurrentInputSymbol( input ); + input.Consume(); // move past ttype token as if all were ok + return matchedSymbol; + } + // can't recover with single token deletion, try insertion + if ( MismatchIsMissingToken( input, follow ) ) + { + object inserted = GetMissingSymbol( input, e, ttype, follow ); + e = new MissingTokenException( ttype, input, inserted ); + ReportError( e ); // report after inserting so AW sees the token in the exception + return inserted; + } + // even that didn't work; must throw the exception + e = new MismatchedTokenException( ttype, input ) + { + tokenNames = GetTokenNames() + }; + throw e; + } + + /** Not currently used */ + public virtual object RecoverFromMismatchedSet( IIntStream input, + RecognitionException e, + BitSet follow ) + { + if ( MismatchIsMissingToken( input, follow ) ) + { + // System.out.println("missing token"); + ReportError( e ); + // we don't know how to conjure up a token for sets yet + return GetMissingSymbol( input, e, TokenConstants.INVALID_TOKEN_TYPE, follow ); + } + // TODO do single token deletion like above for Token mismatch + throw e; + } + + /** <summary> + * Match needs to return the current input symbol, which gets put + * into the label for the associated token ref; e.g., x=ID. Token + * and tree parsers need to return different objects. Rather than test + * for input stream type or change the IntStream interface, I use + * a simple method to ask the recognizer to tell me what the current + * input symbol is. + * </summary> + * + * <remarks>This is ignored for lexers.</remarks> + */ + protected virtual object GetCurrentInputSymbol( IIntStream input ) + { + return null; + } + + /** <summary>Conjure up a missing token during error recovery.</summary> + * + * <remarks> + * The recognizer attempts to recover from single missing + * symbols. But, actions might refer to that missing symbol. + * For example, x=ID {f($x);}. The action clearly assumes + * that there has been an identifier matched previously and that + * $x points at that token. If that token is missing, but + * the next token in the stream is what we want we assume that + * this token is missing and we keep going. Because we + * have to return some token to replace the missing token, + * we have to conjure one up. This method gives the user control + * over the tokens returned for missing tokens. Mostly, + * you will want to create something special for identifier + * tokens. For literals such as '{' and ',', the default + * action in the parser or tree parser works. It simply creates + * a CommonToken of the appropriate type. The text will be the token. + * If you change what tokens must be created by the lexer, + * override this method to create the appropriate tokens. + * </remarks> + */ + protected virtual object GetMissingSymbol( IIntStream input, + RecognitionException e, + int expectedTokenType, + BitSet follow ) + { + return null; + } + + public virtual void ConsumeUntil( IIntStream input, int tokenType ) + { + //System.out.println("consumeUntil "+tokenType); + int ttype = input.LA( 1 ); + while ( ttype != TokenConstants.EOF && ttype != tokenType ) + { + input.Consume(); + ttype = input.LA( 1 ); + } + } + + /** <summary>Consume tokens until one matches the given token set</summary> */ + public virtual void ConsumeUntil( IIntStream input, BitSet set ) + { + //System.out.println("consumeUntil("+set.toString(getTokenNames())+")"); + int ttype = input.LA( 1 ); + while ( ttype != TokenConstants.EOF && !set.Member( ttype ) ) + { + //System.out.println("consume during recover LA(1)="+getTokenNames()[input.LA(1)]); + input.Consume(); + ttype = input.LA( 1 ); + } + } + + /** <summary>Push a rule's follow set using our own hardcoded stack</summary> */ + protected virtual void PushFollow( BitSet fset ) + { + if ( ( state._fsp + 1 ) >= state.following.Length ) + { + BitSet[] f = new BitSet[state.following.Length * 2]; + Array.Copy( state.following, f, state.following.Length ); + state.following = f; + } + state.following[++state._fsp] = fset; + } + + /** <summary> + * Return List<String> of the rules in your parser instance + * leading up to a call to this method. You could override if + * you want more details such as the file/line info of where + * in the parser java code a rule is invoked. + * </summary> + * + * <remarks> + * This is very useful for error messages and for context-sensitive + * error recovery. + * </remarks> + */ + public virtual IList<string> GetRuleInvocationStack() + { + string parserClassName = this.GetType().Name; + return GetRuleInvocationStack( new Exception(), parserClassName ); + } + + /** <summary> + * A more general version of getRuleInvocationStack where you can + * pass in, for example, a RecognitionException to get it's rule + * stack trace. This routine is shared with all recognizers, hence, + * static. + * </summary> + * + * <remarks> + * TODO: move to a utility class or something; weird having lexer call this + * </remarks> + */ + public static IList<string> GetRuleInvocationStack( Exception e, + string recognizerClassName ) + { + IList<string> rules = new List<string>(); + + StackTrace trace = new StackTrace( e, true ); + StackFrame[] stack = trace.GetFrames(); + if ( stack == null ) + stack = new StackTrace( true ).GetFrames(); + + int i = 0; + for ( i = stack.Length - 1; i >= 0; i-- ) + { + StackFrame t = stack[i]; + if ( t.GetMethod().DeclaringType.Name.StartsWith( "Antlr.Runtime." ) ) + { + continue; // skip support code such as this method + } + if ( t.GetMethod().Name.Equals( NEXT_TOKEN_RULE_NAME ) ) + { + continue; + } + if ( !t.GetMethod().DeclaringType.Name.Equals( recognizerClassName ) ) + { + continue; // must not be part of this parser + } + rules.Add( t.GetMethod().Name ); + } + return rules; + } + + public virtual int BacktrackingLevel + { + get + { + return state.backtracking; + } + set + { + state.backtracking = value; + } + } + + /** <summary>Return whether or not a backtracking attempt failed.</summary> */ + public virtual bool Failed + { + get + { + return state.failed; + } + } + + /** <summary> + * Used to print out token names like ID during debugging and + * error reporting. The generated parsers implement a method + * that overrides this to point to their String[] tokenNames. + * </summary> + */ + public virtual string[] GetTokenNames() + { + return null; + } + + /** <summary> + * For debugging and other purposes, might want the grammar name. + * Have ANTLR generate an implementation for this method. + * </summary> + */ + public virtual string GrammarFileName + { + get + { + return null; + } + } + + public abstract string SourceName + { + get; + } + + /** <summary> + * A convenience method for use most often with template rewrites. + * Convert a List<Token> to List<String> + * </summary> + */ + public virtual List<string> ToStrings( ICollection<IToken> tokens ) + { + if ( tokens == null ) + return null; + + List<string> strings = new List<string>( tokens.Count ); + foreach ( IToken token in tokens ) + { + strings.Add( token.Text ); + } + + return strings; + } + + /** <summary> + * Given a rule number and a start token index number, return + * MEMO_RULE_UNKNOWN if the rule has not parsed input starting from + * start index. If this rule has parsed input starting from the + * start index before, then return where the rule stopped parsing. + * It returns the index of the last token matched by the rule. + * </summary> + * + * <remarks> + * For now we use a hashtable and just the slow Object-based one. + * Later, we can make a special one for ints and also one that + * tosses out data after we commit past input position i. + * </remarks> + */ + public virtual int GetRuleMemoization( int ruleIndex, int ruleStartIndex ) + { + if ( state.ruleMemo[ruleIndex] == null ) + { + state.ruleMemo[ruleIndex] = new Dictionary<int, int>(); + } + + int stopIndex; + if ( !state.ruleMemo[ruleIndex].TryGetValue( ruleStartIndex, out stopIndex ) ) + return MEMO_RULE_UNKNOWN; + + return stopIndex; + } + + /** <summary> + * Has this rule already parsed input at the current index in the + * input stream? Return the stop token index or MEMO_RULE_UNKNOWN. + * If we attempted but failed to parse properly before, return + * MEMO_RULE_FAILED. + * </summary> + * + * <remarks> + * This method has a side-effect: if we have seen this input for + * this rule and successfully parsed before, then seek ahead to + * 1 past the stop token matched for this rule last time. + * </remarks> + */ + public virtual bool AlreadyParsedRule( IIntStream input, int ruleIndex ) + { + int stopIndex = GetRuleMemoization( ruleIndex, input.Index ); + if ( stopIndex == MEMO_RULE_UNKNOWN ) + { + return false; + } + if ( stopIndex == MEMO_RULE_FAILED ) + { + //System.out.println("rule "+ruleIndex+" will never succeed"); + state.failed = true; + } + else + { + //System.out.println("seen rule "+ruleIndex+" before; skipping ahead to @"+(stopIndex+1)+" failed="+state.failed); + input.Seek( stopIndex + 1 ); // jump to one past stop token + } + return true; + } + + /** <summary> + * Record whether or not this rule parsed the input at this position + * successfully. Use a standard java hashtable for now. + * </summary> + */ + public virtual void Memoize( IIntStream input, + int ruleIndex, + int ruleStartIndex ) + { + int stopTokenIndex = state.failed ? MEMO_RULE_FAILED : input.Index - 1; + if ( state.ruleMemo == null ) + { + Console.Error.WriteLine( "!!!!!!!!! memo array is null for " + GrammarFileName ); + } + if ( ruleIndex >= state.ruleMemo.Length ) + { + Console.Error.WriteLine( "!!!!!!!!! memo size is " + state.ruleMemo.Length + ", but rule index is " + ruleIndex ); + } + if ( state.ruleMemo[ruleIndex] != null ) + { + state.ruleMemo[ruleIndex][ruleStartIndex] = stopTokenIndex; + } + } + + /** <summary>return how many rule/input-index pairs there are in total.</summary> + * TODO: this includes synpreds. :( + */ + public virtual int GetRuleMemoizationCacheSize() + { + int n = 0; + for ( int i = 0; state.ruleMemo != null && i < state.ruleMemo.Length; i++ ) + { + var ruleMap = state.ruleMemo[i]; + if ( ruleMap != null ) + { + n += ruleMap.Count; // how many input indexes are recorded? + } + } + return n; + } + + public virtual void TraceIn( string ruleName, int ruleIndex, object inputSymbol ) + { + Console.Out.Write( "enter " + ruleName + " " + inputSymbol ); + if ( state.backtracking > 0 ) + { + Console.Out.Write( " backtracking=" + state.backtracking ); + } + Console.Out.WriteLine(); + } + + public virtual void TraceOut( string ruleName, + int ruleIndex, + object inputSymbol ) + { + Console.Out.Write( "exit " + ruleName + " " + inputSymbol ); + if ( state.backtracking > 0 ) + { + Console.Out.Write( " backtracking=" + state.backtracking ); + if ( state.failed ) + Console.Out.Write( " failed" ); + else + Console.Out.Write( " succeeded" ); + } + Console.Out.WriteLine(); + } + +#if NEW_DEBUGGER + #region Debugging support + public virtual IDebugEventListener DebugListener + { + get; + set; + } + + [Conditional( "DEBUG_GRAMMAR" )] + protected static void DebugEnterRule( IDebugEventListener dbg, string grammarFileName, string ruleName ) + { + if ( dbg != null ) + dbg.EnterRule( grammarFileName, ruleName ); + } + [Conditional( "DEBUG_GRAMMAR" )] + protected static void DebugExitRule( IDebugEventListener dbg, string grammarFileName, string ruleName ) + { + if ( dbg != null ) + dbg.ExitRule( grammarFileName, ruleName ); + } + + [Conditional( "DEBUG_GRAMMAR" )] + protected static void DebugEnterSubRule( IDebugEventListener dbg, int decisionNumber ) + { + if ( dbg != null ) + dbg.EnterSubRule( decisionNumber ); + } + [Conditional( "DEBUG_GRAMMAR" )] + protected static void DebugExitSubRule( IDebugEventListener dbg, int decisionNumber ) + { + if ( dbg != null ) + dbg.ExitSubRule( decisionNumber ); + } + + [Conditional( "DEBUG_GRAMMAR" )] + protected static void DebugEnterAlt( IDebugEventListener dbg, int alt ) + { + if ( dbg != null ) + dbg.EnterAlt( alt ); + } + + [Conditional( "DEBUG_GRAMMAR" )] + protected static void DebugEnterDecision( IDebugEventListener dbg, int decisionNumber ) + { + if ( dbg != null ) + dbg.EnterDecision( decisionNumber ); + } + [Conditional( "DEBUG_GRAMMAR" )] + protected static void DebugExitDecision( IDebugEventListener dbg, int decisionNumber ) + { + if ( dbg != null ) + dbg.ExitDecision( decisionNumber ); + } + + [Conditional( "DEBUG_GRAMMAR" )] + protected static void DebugLocation( IDebugEventListener dbg, int line, int charPositionInLine ) + { + if ( dbg != null ) + dbg.Location( line, charPositionInLine ); + } + + [Conditional( "DEBUG_GRAMMAR" )] + protected static void DebugSemanticPredicate( IDebugEventListener dbg, bool result, string predicate ) + { + if ( dbg != null ) + dbg.SemanticPredicate( result, predicate ); + } + #endregion +#endif + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/BitSet.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/BitSet.cs new file mode 100644 index 0000000..2f1e272 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/BitSet.cs @@ -0,0 +1,388 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + using System.Collections.Generic; + + using Array = System.Array; + using ICloneable = System.ICloneable; + using Math = System.Math; + using StringBuilder = System.Text.StringBuilder; + + /** <summary> + * A stripped-down version of org.antlr.misc.BitSet that is just + * good enough to handle runtime requirements such as FOLLOW sets + * for automatic error recovery. + * </summary> + */ + [System.Serializable] + public class BitSet : ICloneable + { + protected const int BITS = 64; // number of bits / long + protected const int LOG_BITS = 6; // 2^6 == 64 + + /** <summary> + * We will often need to do a mod operator (i mod nbits). Its + * turns out that, for powers of two, this mod operation is + * same as (i & (nbits-1)). Since mod is slow, we use a + * precomputed mod mask to do the mod instead. + * </summary> + */ + protected const int MOD_MASK = BITS - 1; + + /** <summary>The actual data bits</summary> */ + ulong[] _bits; + + /** <summary>Construct a bitset of size one word (64 bits)</summary> */ + public BitSet() + : this( BITS ) + { + } + + /** <summary>Construction from a static array of longs</summary> */ + public BitSet( ulong[] bits ) + { + _bits = bits; + } + + /** <summary>Construction from a list of integers</summary> */ + public BitSet( IEnumerable<int> items ) + : this() + { + foreach ( int i in items ) + Add( i ); + } + + /** <summary>Construct a bitset given the size</summary> + * <param name="nbits">The size of the bitset in bits</param> + */ + public BitSet( int nbits ) + { + _bits = new ulong[( ( nbits - 1 ) >> LOG_BITS ) + 1]; + } + + public static BitSet Of( int el ) + { + BitSet s = new BitSet( el + 1 ); + s.Add( el ); + return s; + } + + public static BitSet Of( int a, int b ) + { + BitSet s = new BitSet( Math.Max( a, b ) + 1 ); + s.Add( a ); + s.Add( b ); + return s; + } + + public static BitSet Of( int a, int b, int c ) + { + BitSet s = new BitSet(); + s.Add( a ); + s.Add( b ); + s.Add( c ); + return s; + } + + public static BitSet Of( int a, int b, int c, int d ) + { + BitSet s = new BitSet(); + s.Add( a ); + s.Add( b ); + s.Add( c ); + s.Add( d ); + return s; + } + + /** <summary>return this | a in a new set</summary> */ + public virtual BitSet Or( BitSet a ) + { + if ( a == null ) + { + return this; + } + BitSet s = (BitSet)this.Clone(); + s.OrInPlace( a ); + return s; + } + + /** <summary>or this element into this set (grow as necessary to accommodate)</summary> */ + public virtual void Add( int el ) + { + int n = WordNumber( el ); + if ( n >= _bits.Length ) + { + GrowToInclude( el ); + } + _bits[n] |= BitMask( el ); + } + + /** <summary>Grows the set to a larger number of bits.</summary> + * <param name="bit">element that must fit in set</param> + */ + public virtual void GrowToInclude( int bit ) + { + int newSize = Math.Max( _bits.Length << 1, NumWordsToHold( bit ) ); + ulong[] newbits = new ulong[newSize]; + Array.Copy( _bits, newbits, _bits.Length ); + _bits = newbits; + } + + public virtual void OrInPlace( BitSet a ) + { + if ( a == null ) + { + return; + } + // If this is smaller than a, grow this first + if ( a._bits.Length > _bits.Length ) + { + SetSize( a._bits.Length ); + } + int min = Math.Min( _bits.Length, a._bits.Length ); + for ( int i = min - 1; i >= 0; i-- ) + { + _bits[i] |= a._bits[i]; + } + } + + /** <summary>Sets the size of a set.</summary> + * <param name="nwords">how many words the new set should be</param> + */ + private void SetSize( int nwords ) + { + ulong[] newbits = new ulong[nwords]; + int n = Math.Min( nwords, _bits.Length ); + Array.Copy( _bits, newbits, n ); + _bits = newbits; + } + + private static ulong BitMask( int bitNumber ) + { + int bitPosition = bitNumber & MOD_MASK; // bitNumber mod BITS + return 1UL << bitPosition; + } + + public virtual object Clone() + { + return new BitSet( (ulong[])_bits.Clone() ); + } + + public virtual int Size() + { + int deg = 0; + for ( int i = _bits.Length - 1; i >= 0; i-- ) + { + ulong word = _bits[i]; + if ( word != 0L ) + { + for ( int bit = BITS - 1; bit >= 0; bit-- ) + { + if ( ( word & ( 1UL << bit ) ) != 0 ) + { + deg++; + } + } + } + } + return deg; + } + + public override int GetHashCode() + { + throw new System.NotImplementedException(); + } + + public override bool Equals( object other ) + { + if ( other == null || !( other is BitSet ) ) + { + return false; + } + + BitSet otherSet = (BitSet)other; + + int n = Math.Min( this._bits.Length, otherSet._bits.Length ); + + // for any bits in common, compare + for ( int i = 0; i < n; i++ ) + { + if ( this._bits[i] != otherSet._bits[i] ) + { + return false; + } + } + + // make sure any extra bits are off + + if ( this._bits.Length > n ) + { + for ( int i = n + 1; i < this._bits.Length; i++ ) + { + if ( this._bits[i] != 0 ) + { + return false; + } + } + } + else if ( otherSet._bits.Length > n ) + { + for ( int i = n + 1; i < otherSet._bits.Length; i++ ) + { + if ( otherSet._bits[i] != 0 ) + { + return false; + } + } + } + + return true; + } + + public virtual bool Member( int el ) + { + if ( el < 0 ) + { + return false; + } + int n = WordNumber( el ); + if ( n >= _bits.Length ) + return false; + return ( _bits[n] & BitMask( el ) ) != 0; + } + + // remove this element from this set + public virtual void Remove( int el ) + { + int n = WordNumber( el ); + if ( n < _bits.Length ) + { + _bits[n] &= ~BitMask( el ); + } + } + + public virtual bool IsNil() + { + for ( int i = _bits.Length - 1; i >= 0; i-- ) + { + if ( _bits[i] != 0 ) + return false; + } + return true; + } + + private int NumWordsToHold( int el ) + { + return ( el >> LOG_BITS ) + 1; + } + + public virtual int NumBits() + { + return _bits.Length << LOG_BITS; // num words * bits per word + } + + /** <summary>return how much space is being used by the bits array not how many actually have member bits on.</summary> */ + public virtual int LengthInLongWords() + { + return _bits.Length; + } + + /**Is this contained within a? */ + /* + public boolean subset(BitSet a) { + if (a == null || !(a instanceof BitSet)) return false; + return this.and(a).equals(this); + } + */ + + public virtual int[] ToArray() + { + int[] elems = new int[Size()]; + int en = 0; + for ( int i = 0; i < ( _bits.Length << LOG_BITS ); i++ ) + { + if ( Member( i ) ) + { + elems[en++] = i; + } + } + return elems; + } + + public virtual ulong[] ToPackedArray() + { + return _bits; + } + + private static int WordNumber( int bit ) + { + return bit >> LOG_BITS; // bit / BITS + } + + public override string ToString() + { + return ToString( null ); + } + + public virtual string ToString( string[] tokenNames ) + { + StringBuilder buf = new StringBuilder(); + string separator = ","; + bool havePrintedAnElement = false; + buf.Append( '{' ); + + for ( int i = 0; i < ( _bits.Length << LOG_BITS ); i++ ) + { + if ( Member( i ) ) + { + if ( i > 0 && havePrintedAnElement ) + { + buf.Append( separator ); + } + if ( tokenNames != null ) + { + buf.Append( tokenNames[i] ); + } + else + { + buf.Append( i ); + } + havePrintedAnElement = true; + } + } + buf.Append( '}' ); + return buf.ToString(); + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/CharStreamConstants.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/CharStreamConstants.cs new file mode 100644 index 0000000..041af72 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/CharStreamConstants.cs @@ -0,0 +1,41 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + + public static class CharStreamConstants + { + public const int EOF = -1; + } + +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/CharStreamState.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/CharStreamState.cs new file mode 100644 index 0000000..939e1f0 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/CharStreamState.cs @@ -0,0 +1,56 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + + /** <summary> + * When walking ahead with cyclic DFA or for syntactic predicates, + * we need to record the state of the input stream (char index, + * line, etc...) so that we can rewind the state after scanning ahead. + * </summary> + * + * <remarks>This is the complete state of a stream.</remarks> + */ + [System.Serializable] + public class CharStreamState + { + /** <summary>Index into the char stream of next lookahead char</summary> */ + internal int p; + + /** <summary>What line number is the scanner at before processing buffer[p]?</summary> */ + internal int line; + + /** <summary>What char position 0..n-1 in line is scanner before processing buffer[p]?</summary> */ + internal int charPositionInLine; + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ClassicToken.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ClassicToken.cs new file mode 100644 index 0000000..25da2e9 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ClassicToken.cs @@ -0,0 +1,192 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + using Regex = System.Text.RegularExpressions.Regex; + + /** <summary> + * A Token object like we'd use in ANTLR 2.x; has an actual string created + * and associated with this object. These objects are needed for imaginary + * tree nodes that have payload objects. We need to create a Token object + * that has a string; the tree node will point at this token. CommonToken + * has indexes into a char stream and hence cannot be used to introduce + * new strings. + * </summary> + */ + [System.Serializable] + public class ClassicToken : IToken + { + protected string text; + protected int type; + protected int line; + protected int charPositionInLine; + protected int channel = TokenConstants.DEFAULT_CHANNEL; + + /** <summary>What token number is this from 0..n-1 tokens</summary> */ + protected int index; + + public ClassicToken( int type ) + { + this.type = type; + } + + public ClassicToken( IToken oldToken ) + { + text = oldToken.Text; + type = oldToken.Type; + line = oldToken.Line; + charPositionInLine = oldToken.CharPositionInLine; + channel = oldToken.Channel; + } + + public ClassicToken( int type, string text ) + { + this.type = type; + this.text = text; + } + + public ClassicToken( int type, string text, int channel ) + { + this.type = type; + this.text = text; + this.channel = channel; + } + + #region IToken Members + public string Text + { + get + { + return text; + } + set + { + text = value; + } + } + + public int Type + { + get + { + return type; + } + set + { + type = value; + } + } + + public int Line + { + get + { + return line; + } + set + { + line = value; + } + } + + public int CharPositionInLine + { + get + { + return charPositionInLine; + } + set + { + charPositionInLine = value; + } + } + + public int Channel + { + get + { + return channel; + } + set + { + channel = value; + } + } + + public int TokenIndex + { + get + { + return index; + } + set + { + index = value; + } + } + + public ICharStream InputStream + { + get + { + return null; + } + set + { + } + } + + #endregion + + public override string ToString() + { + string channelStr = ""; + if ( channel > 0 ) + { + channelStr = ",channel=" + channel; + } + string txt = Text; + if ( txt != null ) + { + txt = Regex.Replace( txt, "\n", "\\\\n" ); + txt = Regex.Replace( txt, "\r", "\\\\r" ); + txt = Regex.Replace( txt, "\t", "\\\\t" ); + } + else + { + txt = "<no text>"; + } + return "[@" + TokenIndex + ",'" + txt + "',<" + type + ">" + channelStr + "," + line + ":" + CharPositionInLine + "]"; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/CommonToken.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/CommonToken.cs new file mode 100644 index 0000000..9e1f9ce --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/CommonToken.cs @@ -0,0 +1,255 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + using NonSerialized = System.NonSerializedAttribute; + using Regex = System.Text.RegularExpressions.Regex; + using Serializable = System.SerializableAttribute; + + [Serializable] + public class CommonToken : IToken + { + int type; + int line; + int charPositionInLine = -1; // set to invalid position + int channel = TokenConstants.DEFAULT_CHANNEL; + [NonSerialized] + ICharStream input; + + /** <summary> + * We need to be able to change the text once in a while. If + * this is non-null, then getText should return this. Note that + * start/stop are not affected by changing this. + * </summary> + */ + string text; + + /** <summary>What token number is this from 0..n-1 tokens; < 0 implies invalid index</summary> */ + int index = -1; + + /** <summary>The char position into the input buffer where this token starts</summary> */ + int start; + + /** <summary>The char position into the input buffer where this token stops</summary> */ + int stop; + + public CommonToken( int type ) + { + this.type = type; + } + + public CommonToken( ICharStream input, int type, int channel, int start, int stop ) + { + this.input = input; + this.type = type; + this.channel = channel; + this.start = start; + this.stop = stop; + } + + public CommonToken( int type, string text ) + { + this.type = type; + this.channel = TokenConstants.DEFAULT_CHANNEL; + this.text = text; + } + + public CommonToken( IToken oldToken ) + { + text = oldToken.Text; + type = oldToken.Type; + line = oldToken.Line; + index = oldToken.TokenIndex; + charPositionInLine = oldToken.CharPositionInLine; + channel = oldToken.Channel; + if ( oldToken is CommonToken ) + { + start = ( (CommonToken)oldToken ).start; + stop = ( (CommonToken)oldToken ).stop; + } + } + + #region IToken Members + public string Text + { + get + { + if ( text != null ) + { + return text; + } + if ( input == null ) + { + return null; + } + text = input.substring( start, stop ); + return text; + } + set + { + /** Override the text for this token. getText() will return this text + * rather than pulling from the buffer. Note that this does not mean + * that start/stop indexes are not valid. It means that that input + * was converted to a new string in the token object. + */ + text = value; + } + } + + public int Type + { + get + { + return type; + } + set + { + type = value; + } + } + + public int Line + { + get + { + return line; + } + set + { + line = value; + } + } + + public int CharPositionInLine + { + get + { + return charPositionInLine; + } + set + { + charPositionInLine = value; + } + } + + public int Channel + { + get + { + return channel; + } + set + { + channel = value; + } + } + + public int TokenIndex + { + get + { + return index; + } + set + { + index = value; + } + } + + public ICharStream InputStream + { + get + { + return input; + } + set + { + input = value; + } + } + + #endregion + + public int StartIndex + { + get + { + return start; + } + set + { + start = value; + } + } + + public int StopIndex + { + get + { + return stop; + } + set + { + stop = value; + } + } + + public override string ToString() + { + string channelStr = ""; + if ( channel > 0 ) + { + channelStr = ",channel=" + channel; + } + string txt = Text; + if ( txt != null ) + { + txt = Regex.Replace( txt, "\n", "\\\\n" ); + txt = Regex.Replace( txt, "\r", "\\\\r" ); + txt = Regex.Replace( txt, "\t", "\\\\t" ); + } + else + { + txt = "<no text>"; + } + return "[@" + TokenIndex + "," + start + ":" + stop + "='" + txt + "',<" + type + ">" + channelStr + "," + line + ":" + CharPositionInLine + "]"; + } + + [System.Runtime.Serialization.OnSerializing] + internal void OnSerializing( System.Runtime.Serialization.StreamingContext context ) + { + if ( text == null ) + text = Text; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/CommonTokenStream.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/CommonTokenStream.cs new file mode 100644 index 0000000..25c3bce --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/CommonTokenStream.cs @@ -0,0 +1,489 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + using System.Collections.Generic; + + using StringBuilder = System.Text.StringBuilder; + + /** <summary> + * The most common stream of tokens is one where every token is buffered up + * and tokens are prefiltered for a certain channel (the parser will only + * see these tokens and cannot change the filter channel number during the + * parse). + * </summary> + * + * <remarks>TODO: how to access the full token stream? How to track all tokens matched per rule?</remarks> + */ + [System.Serializable] + public class CommonTokenStream : ITokenStream + { + [System.NonSerialized] + protected ITokenSource tokenSource; + + /** <summary> + * Record every single token pulled from the source so we can reproduce + * chunks of it later. + * </summary> + */ + protected List<IToken> tokens; + + /** <summary>Map<tokentype, channel> to override some Tokens' channel numbers</summary> */ + protected IDictionary<int, int> channelOverrideMap; + + /** <summary>Set<tokentype>; discard any tokens with this type</summary> */ + protected HashSet<int> discardSet; + + /** <summary>Skip tokens on any channel but this one; this is how we skip whitespace...</summary> */ + protected int channel = TokenConstants.DEFAULT_CHANNEL; + + /** <summary>By default, track all incoming tokens</summary> */ + protected bool discardOffChannelTokens = false; + + /** <summary>Track the last mark() call result value for use in rewind().</summary> */ + protected int lastMarker; + + /** <summary> + * The index into the tokens list of the current token (next token + * to consume). p==-1 indicates that the tokens list is empty + * </summary> + */ + protected int p = -1; + + public CommonTokenStream() + { + tokens = new List<IToken>( 500 ); + } + + public CommonTokenStream( ITokenSource tokenSource ) + : this() + { + this.tokenSource = tokenSource; + } + + public CommonTokenStream( ITokenSource tokenSource, int channel ) + : this( tokenSource ) + { + this.channel = channel; + } + + public virtual int Index + { + get + { + return p; + } + } + + /** <summary>Reset this token stream by setting its token source.</summary> */ + public virtual void SetTokenSource( ITokenSource tokenSource ) + { + this.tokenSource = tokenSource; + tokens.Clear(); + p = -1; + channel = TokenConstants.DEFAULT_CHANNEL; + } + + /** <summary> + * Load all tokens from the token source and put in tokens. + * This is done upon first LT request because you might want to + * set some token type / channel overrides before filling buffer. + * </summary> + */ + protected virtual void FillBuffer() + { + int index = 0; + IToken t = tokenSource.NextToken(); + while ( t != null && t.Type != CharStreamConstants.EOF ) + { + bool discard = false; + // is there a channel override for token type? + int channelI; + if ( channelOverrideMap != null && channelOverrideMap.TryGetValue( t.Type, out channelI ) ) + t.Channel = channelI; + + //if ( channelOverrideMap != null && channelOverrideMap.ContainsKey( t.getType() ) ) + //{ + // object channelI = channelOverrideMap.get( t.getType() ); + // if ( channelI != null ) + // { + // t.setChannel( (int)channelI ); + // } + //} + if ( discardSet != null && + discardSet.Contains( t.Type ) ) + { + discard = true; + } + else if ( discardOffChannelTokens && t.Channel != this.channel ) + { + discard = true; + } + if ( !discard ) + { + t.TokenIndex = index; + tokens.Add( t ); + index++; + } + t = tokenSource.NextToken(); + } + // leave p pointing at first token on channel + p = 0; + p = SkipOffTokenChannels( p ); + } + + /** <summary> + * Move the input pointer to the next incoming token. The stream + * must become active with LT(1) available. consume() simply + * moves the input pointer so that LT(1) points at the next + * input symbol. Consume at least one token. + * </summary> + * + * <remarks> + * Walk past any token not on the channel the parser is listening to. + * </remarks> + */ + public virtual void Consume() + { + if ( p < tokens.Count ) + { + p++; + p = SkipOffTokenChannels( p ); // leave p on valid token + } + } + + /** <summary>Given a starting index, return the index of the first on-channel token.</summary> */ + protected virtual int SkipOffTokenChannels( int i ) + { + int n = tokens.Count; + while ( i < n && ( (IToken)tokens[i] ).Channel != channel ) + { + i++; + } + return i; + } + + protected virtual int SkipOffTokenChannelsReverse( int i ) + { + while ( i >= 0 && ( (IToken)tokens[i] ).Channel != channel ) + { + i--; + } + return i; + } + + /** <summary> + * A simple filter mechanism whereby you can tell this token stream + * to force all tokens of type ttype to be on channel. For example, + * when interpreting, we cannot exec actions so we need to tell + * the stream to force all WS and NEWLINE to be a different, ignored + * channel. + * </summary> + */ + public virtual void SetTokenTypeChannel( int ttype, int channel ) + { + if ( channelOverrideMap == null ) + { + channelOverrideMap = new Dictionary<int, int>(); + } + channelOverrideMap[ttype] = channel; + } + + public virtual void DiscardTokenType( int ttype ) + { + if ( discardSet == null ) + { + discardSet = new HashSet<int>(); + } + discardSet.Add( ttype ); + } + + public virtual void SetDiscardOffChannelTokens( bool discardOffChannelTokens ) + { + this.discardOffChannelTokens = discardOffChannelTokens; + } + + public virtual IList<IToken> GetTokens() + { + if ( p == -1 ) + { + FillBuffer(); + } + return tokens; + } + + public virtual IList<IToken> GetTokens( int start, int stop ) + { + return GetTokens( start, stop, (BitSet)null ); + } + + /** <summary> + * Given a start and stop index, return a List of all tokens in + * the token type BitSet. Return null if no tokens were found. This + * method looks at both on and off channel tokens. + * </summary> + */ + public virtual IList<IToken> GetTokens( int start, int stop, BitSet types ) + { + if ( p == -1 ) + { + FillBuffer(); + } + if ( stop >= tokens.Count ) + { + stop = tokens.Count - 1; + } + if ( start < 0 ) + { + start = 0; + } + if ( start > stop ) + { + return null; + } + + // list = tokens[start:stop]:{Token t, t.getType() in types} + IList<IToken> filteredTokens = new List<IToken>(); + for ( int i = start; i <= stop; i++ ) + { + IToken t = tokens[i]; + if ( types == null || types.Member( t.Type ) ) + { + filteredTokens.Add( t ); + } + } + if ( filteredTokens.Count == 0 ) + { + filteredTokens = null; + } + return filteredTokens; + } + + public virtual IList<IToken> GetTokens( int start, int stop, IList<int> types ) + { + return GetTokens( start, stop, new BitSet( types ) ); + } + + public virtual IList<IToken> GetTokens( int start, int stop, int ttype ) + { + return GetTokens( start, stop, BitSet.Of( ttype ) ); + } + + /** <summary> + * Get the ith token from the current position 1..n where k=1 is the + * first symbol of lookahead. + * </summary> + */ + public virtual IToken LT( int k ) + { + if ( p == -1 ) + { + FillBuffer(); + } + if ( k == 0 ) + { + return null; + } + if ( k < 0 ) + { + return LB( -k ); + } + //System.out.print("LT(p="+p+","+k+")="); + if ( ( p + k - 1 ) >= tokens.Count ) + { + return TokenConstants.EOF_TOKEN; + } + //System.out.println(tokens.get(p+k-1)); + int i = p; + int n = 1; + // find k good tokens + while ( n < k ) + { + // skip off-channel tokens + i = SkipOffTokenChannels( i + 1 ); // leave p on valid token + n++; + } + if ( i >= tokens.Count ) + { + return TokenConstants.EOF_TOKEN; + } + return (IToken)tokens[i]; + } + + /** <summary>Look backwards k tokens on-channel tokens</summary> */ + protected virtual IToken LB( int k ) + { + //System.out.print("LB(p="+p+","+k+") "); + if ( p == -1 ) + { + FillBuffer(); + } + if ( k == 0 ) + { + return null; + } + if ( ( p - k ) < 0 ) + { + return null; + } + + int i = p; + int n = 1; + // find k good tokens looking backwards + while ( n <= k ) + { + // skip off-channel tokens + i = SkipOffTokenChannelsReverse( i - 1 ); // leave p on valid token + n++; + } + if ( i < 0 ) + { + return null; + } + return (IToken)tokens[i]; + } + + /** <summary> + * Return absolute token i; ignore which channel the tokens are on; + * that is, count all tokens not just on-channel tokens. + * </summary> + */ + public virtual IToken Get( int i ) + { + return (IToken)tokens[i]; + } + + public virtual int LA( int i ) + { + return LT( i ).Type; + } + + public virtual int Mark() + { + if ( p == -1 ) + { + FillBuffer(); + } + lastMarker = Index; + return lastMarker; + } + + public virtual void Release( int marker ) + { + // no resources to release + } + + public virtual int Size() + { + return tokens.Count; + } + + public virtual void Rewind( int marker ) + { + Seek( marker ); + } + + public virtual void Rewind() + { + Seek( lastMarker ); + } + + public virtual void Reset() + { + p = 0; + lastMarker = 0; + } + + public virtual void Seek( int index ) + { + p = index; + } + + public virtual ITokenSource TokenSource + { + get + { + return tokenSource; + } + } + + public virtual string SourceName + { + get + { + return TokenSource.SourceName; + } + } + + public override string ToString() + { + if ( p == -1 ) + { + FillBuffer(); + } + return ToString( 0, tokens.Count - 1 ); + } + + public virtual string ToString( int start, int stop ) + { + if ( start < 0 || stop < 0 ) + { + return null; + } + if ( p == -1 ) + { + FillBuffer(); + } + if ( stop >= tokens.Count ) + { + stop = tokens.Count - 1; + } + StringBuilder buf = new StringBuilder(); + for ( int i = start; i <= stop; i++ ) + { + IToken t = tokens[i]; + buf.Append( t.Text ); + } + return buf.ToString(); + } + + public virtual string ToString( IToken start, IToken stop ) + { + if ( start != null && stop != null ) + { + return ToString( start.TokenIndex, stop.TokenIndex ); + } + return null; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/DFA.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/DFA.cs new file mode 100644 index 0000000..e9f2ff9 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/DFA.cs @@ -0,0 +1,291 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + using Console = System.Console; + + public delegate int SpecialStateTransitionHandler( DFA dfa, int s, IIntStream input ); + + /** <summary>A DFA implemented as a set of transition tables.</summary> + * + * <remarks> + * Any state that has a semantic predicate edge is special; those states + * are generated with if-then-else structures in a specialStateTransition() + * which is generated by cyclicDFA template. + * + * There are at most 32767 states (16-bit signed short). + * Could get away with byte sometimes but would have to generate different + * types and the simulation code too. For a point of reference, the Java + * lexer's Tokens rule DFA has 326 states roughly. + * </remarks> + */ + public class DFA + { + public DFA() + : this( new SpecialStateTransitionHandler( SpecialStateTransitionDefault ) ) + { + } + public DFA( SpecialStateTransitionHandler specialStateTransition ) + { + this.SpecialStateTransition = specialStateTransition ?? new SpecialStateTransitionHandler( SpecialStateTransitionDefault ); + } + + protected short[] eot; + protected short[] eof; + protected char[] min; + protected char[] max; + protected short[] accept; + protected short[] special; + protected short[][] transition; + + protected int decisionNumber; + + /** <summary>Which recognizer encloses this DFA? Needed to check backtracking</summary> */ + protected BaseRecognizer recognizer; + + public readonly bool debug = false; + + /** <summary> + * From the input stream, predict what alternative will succeed + * using this DFA (representing the covering regular approximation + * to the underlying CFL). Return an alternative number 1..n. Throw + * an exception upon error. + * </summary> + */ + public virtual int Predict( IIntStream input ) + { + if ( debug ) + { + Console.Error.WriteLine( "Enter DFA.predict for decision " + decisionNumber ); + } + int mark = input.Mark(); // remember where decision started in input + int s = 0; // we always start at s0 + try + { + for ( ; ; ) + { + if ( debug ) + Console.Error.WriteLine( "DFA " + decisionNumber + " state " + s + " LA(1)=" + (char)input.LA( 1 ) + "(" + input.LA( 1 ) + + "), index=" + input.Index ); + int specialState = special[s]; + if ( specialState >= 0 ) + { + if ( debug ) + { + Console.Error.WriteLine( "DFA " + decisionNumber + + " state " + s + " is special state " + specialState ); + } + s = SpecialStateTransition( this, specialState, input ); + if ( debug ) + { + Console.Error.WriteLine( "DFA " + decisionNumber + + " returns from special state " + specialState + " to " + s ); + } + if ( s == -1 ) + { + NoViableAlt( s, input ); + return 0; + } + input.Consume(); + continue; + } + if ( accept[s] >= 1 ) + { + if ( debug ) + Console.Error.WriteLine( "accept; predict " + accept[s] + " from state " + s ); + return accept[s]; + } + // look for a normal char transition + char c = (char)input.LA( 1 ); // -1 == \uFFFF, all tokens fit in 65000 space + if ( c >= min[s] && c <= max[s] ) + { + int snext = transition[s][c - min[s]]; // move to next state + if ( snext < 0 ) + { + // was in range but not a normal transition + // must check EOT, which is like the else clause. + // eot[s]>=0 indicates that an EOT edge goes to another + // state. + if ( eot[s] >= 0 ) + { // EOT Transition to accept state? + if ( debug ) + Console.Error.WriteLine( "EOT transition" ); + s = eot[s]; + input.Consume(); + // TODO: I had this as return accept[eot[s]] + // which assumed here that the EOT edge always + // went to an accept...faster to do this, but + // what about predicated edges coming from EOT + // target? + continue; + } + NoViableAlt( s, input ); + return 0; + } + s = snext; + input.Consume(); + continue; + } + if ( eot[s] >= 0 ) + { // EOT Transition? + if ( debug ) + Console.Error.WriteLine( "EOT transition" ); + s = eot[s]; + input.Consume(); + continue; + } + if ( c == unchecked((char)TokenConstants.EOF) && eof[s] >= 0 ) + { // EOF Transition to accept state? + if ( debug ) + Console.Error.WriteLine( "accept via EOF; predict " + accept[eof[s]] + " from " + eof[s] ); + return accept[eof[s]]; + } + // not in range and not EOF/EOT, must be invalid symbol + if ( debug ) + { + Console.Error.WriteLine( "min[" + s + "]=" + min[s] ); + Console.Error.WriteLine( "max[" + s + "]=" + max[s] ); + Console.Error.WriteLine( "eot[" + s + "]=" + eot[s] ); + Console.Error.WriteLine( "eof[" + s + "]=" + eof[s] ); + for ( int p = 0; p < transition[s].Length; p++ ) + { + Console.Error.Write( transition[s][p] + " " ); + } + Console.Error.WriteLine(); + } + NoViableAlt( s, input ); + return 0; + } + } + finally + { + input.Rewind( mark ); + } + } + + protected virtual void NoViableAlt( int s, IIntStream input ) + { + if ( recognizer.state.backtracking > 0 ) + { + recognizer.state.failed = true; + return; + } + NoViableAltException nvae = + new NoViableAltException( GetDescription(), + decisionNumber, + s, + input ); + Error( nvae ); + throw nvae; + } + + /** <summary>A hook for debugging interface</summary> */ + public virtual void Error( NoViableAltException nvae ) + { + } + + public SpecialStateTransitionHandler SpecialStateTransition + { + get; + private set; + } + //public virtual int specialStateTransition( int s, IntStream input ) + //{ + // return -1; + //} + + static int SpecialStateTransitionDefault( DFA dfa, int s, IIntStream input ) + { + return -1; + } + + public virtual string GetDescription() + { + return "n/a"; + } + + /** <summary> + * Given a String that has a run-length-encoding of some unsigned shorts + * like "\1\2\3\9", convert to short[] {2,9,9,9}. We do this to avoid + * static short[] which generates so much init code that the class won't + * compile. :( + * </summary> + */ + public static short[] UnpackEncodedString( string encodedString ) + { + // walk first to find how big it is. + int size = 0; + for ( int i = 0; i < encodedString.Length; i += 2 ) + { + size += encodedString[i]; + } + short[] data = new short[size]; + int di = 0; + for ( int i = 0; i < encodedString.Length; i += 2 ) + { + char n = encodedString[i]; + char v = encodedString[i + 1]; + // add v n times to data + for ( int j = 1; j <= n; j++ ) + { + data[di++] = (short)v; + } + } + return data; + } + + /** <summary>Hideous duplication of code, but I need different typed arrays out :(</summary> */ + public static char[] UnpackEncodedStringToUnsignedChars( string encodedString ) + { + // walk first to find how big it is. + int size = 0; + for ( int i = 0; i < encodedString.Length; i += 2 ) + { + size += encodedString[i]; + } + char[] data = new char[size]; + int di = 0; + for ( int i = 0; i < encodedString.Length; i += 2 ) + { + char n = encodedString[i]; + char v = encodedString[i + 1]; + // add v n times to data + for ( int j = 1; j <= n; j++ ) + { + data[di++] = v; + } + } + return data; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Debug/DebugEventListenerConstants.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Debug/DebugEventListenerConstants.cs new file mode 100644 index 0000000..9032492 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Debug/DebugEventListenerConstants.cs @@ -0,0 +1,46 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + + public static class DebugEventListenerConstants + { + /** <summary>Moved to version 2 for v3.1: added grammar name to enter/exit Rule</summary> */ + public const string ProtocolVersion = "2"; + + /** <summary>Serialized version of true</summary> */ + public const int True = 1; + public const int False = 0; + } + +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Debug/IDebugEventListener.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Debug/IDebugEventListener.cs new file mode 100644 index 0000000..cf6c5e2 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Debug/IDebugEventListener.cs @@ -0,0 +1,387 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Debug +{ + + /** <summary>All debugging events that a recognizer can trigger.</summary> + * + * <remarks> + * I did not create a separate AST debugging interface as it would create + * lots of extra classes and DebugParser has a dbg var defined, which makes + * it hard to change to ASTDebugEventListener. I looked hard at this issue + * and it is easier to understand as one monolithic event interface for all + * possible events. Hopefully, adding ST debugging stuff won't be bad. Leave + * for future. 4/26/2006. + * </remarks> + */ + public interface IDebugEventListener + { + void Initialize(); + + /** <summary> + * The parser has just entered a rule. No decision has been made about + * which alt is predicted. This is fired AFTER init actions have been + * executed. Attributes are defined and available etc... + * The grammarFileName allows composite grammars to jump around among + * multiple grammar files. + * </summary> + */ + void EnterRule( string grammarFileName, string ruleName ); + + /** <summary> + * Because rules can have lots of alternatives, it is very useful to + * know which alt you are entering. This is 1..n for n alts. + * </summary> + */ + void EnterAlt( int alt ); + + /** <summary> + * This is the last thing executed before leaving a rule. It is + * executed even if an exception is thrown. This is triggered after + * error reporting and recovery have occurred (unless the exception is + * not caught in this rule). This implies an "exitAlt" event. + * The grammarFileName allows composite grammars to jump around among + * multiple grammar files. + * </summary> + */ + void ExitRule( string grammarFileName, string ruleName ); + + /** <summary>Track entry into any (...) subrule other EBNF construct</summary> */ + void EnterSubRule( int decisionNumber ); + + void ExitSubRule( int decisionNumber ); + + /** <summary> + * Every decision, fixed k or arbitrary, has an enter/exit event + * so that a GUI can easily track what LT/consume events are + * associated with prediction. You will see a single enter/exit + * subrule but multiple enter/exit decision events, one for each + * loop iteration. + * </summary> + */ + void EnterDecision( int decisionNumber ); + + void ExitDecision( int decisionNumber ); + + /** <summary> + * An input token was consumed; matched by any kind of element. + * Trigger after the token was matched by things like match(), matchAny(). + * </summary> + */ + void ConsumeToken( IToken t ); + + /** <summary> + * An off-channel input token was consumed. + * Trigger after the token was matched by things like match(), matchAny(). + * (unless of course the hidden token is first stuff in the input stream). + * </summary> + */ + void ConsumeHiddenToken( IToken t ); + + /** <summary> + * Somebody (anybody) looked ahead. Note that this actually gets + * triggered by both LA and LT calls. The debugger will want to know + * which Token object was examined. Like consumeToken, this indicates + * what token was seen at that depth. A remote debugger cannot look + * ahead into a file it doesn't have so LT events must pass the token + * even if the info is redundant. + * </summary> + */ + void LT( int i, IToken t ); + + /** <summary> + * The parser is going to look arbitrarily ahead; mark this location, + * the token stream's marker is sent in case you need it. + * </summary> + */ + void Mark( int marker ); + + /** <summary> + * After an arbitrairly long lookahead as with a cyclic DFA (or with + * any backtrack), this informs the debugger that stream should be + * rewound to the position associated with marker. + * </summary> + */ + void Rewind( int marker ); + + /** <summary> + * Rewind to the input position of the last marker. + * Used currently only after a cyclic DFA and just + * before starting a sem/syn predicate to get the + * input position back to the start of the decision. + * Do not "pop" the marker off the state. mark(i) + * and rewind(i) should balance still. + * </summary> + */ + void Rewind(); + + void BeginBacktrack( int level ); + + void EndBacktrack( int level, bool successful ); + + /** <summary> + * To watch a parser move through the grammar, the parser needs to + * inform the debugger what line/charPos it is passing in the grammar. + * For now, this does not know how to switch from one grammar to the + * other and back for island grammars etc... + * </summary> + * + * <remarks> + * This should also allow breakpoints because the debugger can stop + * the parser whenever it hits this line/pos. + * </remarks> + */ + void Location( int line, int pos ); + + /** <summary> + * A recognition exception occurred such as NoViableAltException. I made + * this a generic event so that I can alter the exception hierachy later + * without having to alter all the debug objects. + * </summary> + * + * <remarks> + * Upon error, the stack of enter rule/subrule must be properly unwound. + * If no viable alt occurs it is within an enter/exit decision, which + * also must be rewound. Even the rewind for each mark must be unwount. + * In the Java target this is pretty easy using try/finally, if a bit + * ugly in the generated code. The rewind is generated in DFA.predict() + * actually so no code needs to be generated for that. For languages + * w/o this "finally" feature (C++?), the target implementor will have + * to build an event stack or something. + * + * Across a socket for remote debugging, only the RecognitionException + * data fields are transmitted. The token object or whatever that + * caused the problem was the last object referenced by LT. The + * immediately preceding LT event should hold the unexpected Token or + * char. + * + * Here is a sample event trace for grammar: + * + * b : C ({;}A|B) // {;} is there to prevent A|B becoming a set + * | D + * ; + * + * The sequence for this rule (with no viable alt in the subrule) for + * input 'c c' (there are 3 tokens) is: + * + * commence + * LT(1) + * enterRule b + * location 7 1 + * enter decision 3 + * LT(1) + * exit decision 3 + * enterAlt1 + * location 7 5 + * LT(1) + * consumeToken [c/<4>,1:0] + * location 7 7 + * enterSubRule 2 + * enter decision 2 + * LT(1) + * LT(1) + * recognitionException NoViableAltException 2 1 2 + * exit decision 2 + * exitSubRule 2 + * beginResync + * LT(1) + * consumeToken [c/<4>,1:1] + * LT(1) + * endResync + * LT(-1) + * exitRule b + * terminate + * </remarks> + */ + void RecognitionException( RecognitionException e ); + + /** <summary> + * Indicates the recognizer is about to consume tokens to resynchronize + * the parser. Any consume events from here until the recovered event + * are not part of the parse--they are dead tokens. + * </summary> + */ + void BeginResync(); + + /** <summary> + * Indicates that the recognizer has finished consuming tokens in order + * to resychronize. There may be multiple beginResync/endResync pairs + * before the recognizer comes out of errorRecovery mode (in which + * multiple errors are suppressed). This will be useful + * in a gui where you want to probably grey out tokens that are consumed + * but not matched to anything in grammar. Anything between + * a beginResync/endResync pair was tossed out by the parser. + * </summary> + */ + void EndResync(); + + /** <summary>A semantic predicate was evaluate with this result and action text</summary> */ + void SemanticPredicate( bool result, string predicate ); + + /** <summary> + * Announce that parsing has begun. Not technically useful except for + * sending events over a socket. A GUI for example will launch a thread + * to connect and communicate with a remote parser. The thread will want + * to notify the GUI when a connection is made. ANTLR parsers + * trigger this upon entry to the first rule (the ruleLevel is used to + * figure this out). + * </summary> + */ + void Commence(); + + /** <summary> + * Parsing is over; successfully or not. Mostly useful for telling + * remote debugging listeners that it's time to quit. When the rule + * invocation level goes to zero at the end of a rule, we are done + * parsing. + * </summary> + */ + void Terminate(); + + + #region Tree Parsing + + /** <summary> + * Input for a tree parser is an AST, but we know nothing for sure + * about a node except its type and text (obtained from the adaptor). + * This is the analog of the consumeToken method. Again, the ID is + * the hashCode usually of the node so it only works if hashCode is + * not implemented. If the type is UP or DOWN, then + * the ID is not really meaningful as it's fixed--there is + * just one UP node and one DOWN navigation node. + * </summary> + * + * <param name="t" /> + */ + void ConsumeNode( object t ); + + /** <summary> + * The tree parser lookedahead. If the type is UP or DOWN, + * then the ID is not really meaningful as it's fixed--there is + * just one UP node and one DOWN navigation node. + * </summary> + */ + void LT( int i, object t ); + + #endregion + + + #region AST Events + + /** <summary> + * A nil was created (even nil nodes have a unique ID... + * they are not "null" per se). As of 4/28/2006, this + * seems to be uniquely triggered when starting a new subtree + * such as when entering a subrule in automatic mode and when + * building a tree in rewrite mode. + * </summary> + * + * <remarks> + * If you are receiving this event over a socket via + * RemoteDebugEventSocketListener then only t.ID is set. + * </remarks> + */ + void NilNode( object t ); + + /** <summary> + * Upon syntax error, recognizers bracket the error with an error node + * if they are building ASTs. + * </summary> + * + * <param name="t"/> + */ + void ErrorNode( object t ); + + /** <summary>Announce a new node built from token elements such as type etc...</summary> + * + * <remarks> + * If you are receiving this event over a socket via + * RemoteDebugEventSocketListener then only t.ID, type, text are + * set. + * </remarks> + */ + void CreateNode( object t ); + + /** <summary>Announce a new node built from an existing token.</summary> + * + * <remarks> + * If you are receiving this event over a socket via + * RemoteDebugEventSocketListener then only node.ID and token.tokenIndex + * are set. + * </remarks> + */ + void CreateNode( object node, IToken token ); + + /** <summary>Make a node the new root of an existing root. See</summary> + * + * <remarks> + * Note: the newRootID parameter is possibly different + * than the TreeAdaptor.becomeRoot() newRoot parameter. + * In our case, it will always be the result of calling + * TreeAdaptor.becomeRoot() and not root_n or whatever. + * + * The listener should assume that this event occurs + * only when the current subrule (or rule) subtree is + * being reset to newRootID. + * + * If you are receiving this event over a socket via + * RemoteDebugEventSocketListener then only IDs are set. + * </remarks> + * + * <seealso cref="Antlr.Runtime.Tree.TreeAdaptor.becomeRoot()"/> + */ + void BecomeRoot( object newRoot, object oldRoot ); + + /** <summary>Make childID a child of rootID.</summary> + * + * <remarks> + * If you are receiving this event over a socket via + * RemoteDebugEventSocketListener then only IDs are set. + * </remarks> + * + * <seealso cref="Antlr.Runtime.Tree.TreeAdaptor.addChild()"/> + */ + void AddChild( object root, object child ); + + /** <summary>Set the token start/stop token index for a subtree root or node.</summary> + * + * <remarks> + * If you are receiving this event over a socket via + * RemoteDebugEventSocketListener then only t.ID is set. + * </remarks> + */ + void SetTokenBoundaries( object t, int tokenStartIndex, int tokenStopIndex ); + + #endregion + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/EarlyExitException.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/EarlyExitException.cs new file mode 100644 index 0000000..8d77ed8 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/EarlyExitException.cs @@ -0,0 +1,52 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + + /** <summary>The recognizer did not match anything for a (..)+ loop.</summary> */ + public class EarlyExitException : RecognitionException + { + public int decisionNumber; + + /** <summary>Used for remote debugger deserialization</summary> */ + public EarlyExitException() + { + } + + public EarlyExitException( int decisionNumber, IIntStream input ) + : base( input ) + { + this.decisionNumber = decisionNumber; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/FailedPredicateException.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/FailedPredicateException.cs new file mode 100644 index 0000000..6f9376e --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/FailedPredicateException.cs @@ -0,0 +1,65 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + + /** <summary> + * A semantic predicate failed during validation. Validation of predicates + * occurs when normally parsing the alternative just like matching a token. + * Disambiguating predicate evaluation occurs when we hoist a predicate into + * a prediction decision. + * </summary> + */ + public class FailedPredicateException : RecognitionException + { + public string ruleName; + public string predicateText; + + /** <summary>Used for remote debugger deserialization</summary> */ + public FailedPredicateException() + { + } + + public FailedPredicateException( IIntStream input, string ruleName, string predicateText ) + : base( input ) + { + this.ruleName = ruleName; + this.predicateText = predicateText; + } + + public override string ToString() + { + return "FailedPredicateException(" + ruleName + ",{" + predicateText + "}?)"; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ICharStream.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ICharStream.cs new file mode 100644 index 0000000..5fa87db --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ICharStream.cs @@ -0,0 +1,72 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + + /** <summary>A source of characters for an ANTLR lexer</summary> */ + public interface ICharStream : IIntStream + { + + /** <summary> + * For infinite streams, you don't need this; primarily I'm providing + * a useful interface for action code. Just make sure actions don't + * use this on streams that don't support it. + * </summary> + */ + string substring( int start, int stop ); + + /** <summary> + * Get the ith character of lookahead. This is the same usually as + * LA(i). This will be used for labels in the generated + * lexer code. I'd prefer to return a char here type-wise, but it's + * probably better to be 32-bit clean and be consistent with LA. + * </summary> + */ + int LT( int i ); + + /** <summary>ANTLR tracks the line information automatically</summary> */ + /** <summary>Because this stream can rewind, we need to be able to reset the line</summary> */ + int Line + { + get; + set; + } + + /** <summary>The index of the character relative to the beginning of the line 0..n-1</summary> */ + int CharPositionInLine + { + get; + set; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/IIntStream.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/IIntStream.cs new file mode 100644 index 0000000..2181798 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/IIntStream.cs @@ -0,0 +1,158 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + + /** <summary> + * A simple stream of integers used when all I care about is the char + * or token type sequence (such as interpretation). + * </summary> + */ + public interface IIntStream + { + void Consume(); + + /** <summary> + * Get int at current input pointer + i ahead where i=1 is next int. + * Negative indexes are allowed. LA(-1) is previous token (token + * just matched). LA(-i) where i is before first token should + * yield -1, invalid char / EOF. + * </summary> + */ + int LA( int i ); + + /** <summary> + * Tell the stream to start buffering if it hasn't already. Return + * current input position, Index, or some other marker so that + * when passed to rewind() you get back to the same spot. + * rewind(mark()) should not affect the input cursor. The Lexer + * track line/col info as well as input index so its markers are + * not pure input indexes. Same for tree node streams. + * </summary> + */ + int Mark(); + + /** <summary> + * Return the current input symbol index 0..n where n indicates the + * last symbol has been read. The index is the symbol about to be + * read not the most recently read symbol. + * </summary> + */ + int Index + { + get; + } + + /** <summary> + * Reset the stream so that next call to index would return marker. + * The marker will usually be Index but it doesn't have to be. It's + * just a marker to indicate what state the stream was in. This is + * essentially calling release() and seek(). If there are markers + * created after this marker argument, this routine must unroll them + * like a stack. Assume the state the stream was in when this marker + * was created. + * </summary> + */ + void Rewind( int marker ); + + /** <summary> + * Rewind to the input position of the last marker. + * Used currently only after a cyclic DFA and just + * before starting a sem/syn predicate to get the + * input position back to the start of the decision. + * Do not "pop" the marker off the state. mark(i) + * and rewind(i) should balance still. It is + * like invoking rewind(last marker) but it should not "pop" + * the marker off. It's like seek(last marker's input position). + * </summary> + */ + void Rewind(); + + /** <summary> + * You may want to commit to a backtrack but don't want to force the + * stream to keep bookkeeping objects around for a marker that is + * no longer necessary. This will have the same behavior as + * rewind() except it releases resources without the backward seek. + * This must throw away resources for all markers back to the marker + * argument. So if you're nested 5 levels of mark(), and then release(2) + * you have to release resources for depths 2..5. + * </summary> + */ + void Release( int marker ); + + /** <summary> + * Set the input cursor to the position indicated by index. This is + * normally used to seek ahead in the input stream. No buffering is + * required to do this unless you know your stream will use seek to + * move backwards such as when backtracking. + * </summary> + * + * <remarks> + * This is different from rewind in its multi-directional + * requirement and in that its argument is strictly an input cursor (index). + * + * For char streams, seeking forward must update the stream state such + * as line number. For seeking backwards, you will be presumably + * backtracking using the mark/rewind mechanism that restores state and + * so this method does not need to update state when seeking backwards. + * + * Currently, this method is only used for efficient backtracking using + * memoization, but in the future it may be used for incremental parsing. + * + * The index is 0..n-1. A seek to position i means that LA(1) will + * return the ith symbol. So, seeking to 0 means LA(1) will return the + * first element in the stream. + * </remarks> + */ + void Seek( int index ); + + /** <summary> + * Only makes sense for streams that buffer everything up probably, but + * might be useful to display the entire stream or for testing. This + * value includes a single EOF. + * </summary> + */ + int Size(); + + /** <summary> + * Where are you getting symbols from? Normally, implementations will + * pass the buck all the way to the lexer who can ask its input stream + * for the file name or whatever. + * </summary> + */ + string SourceName + { + get; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/IToken.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/IToken.cs new file mode 100644 index 0000000..fb7ddfc --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/IToken.cs @@ -0,0 +1,94 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + + public interface IToken + { + /** <summary>Get the text of the token</summary> */ + string Text + { + get; + set; + } + + int Type + { + get; + set; + } + + /** <summary>The line number on which this token was matched; line=1..n</summary> */ + int Line + { + get; + set; + } + + /** <summary>The index of the first character relative to the beginning of the line 0..n-1</summary> */ + int CharPositionInLine + { + get; + set; + } + + int Channel + { + get; + set; + } + + /** <summary> + * An index from 0..n-1 of the token object in the input stream. + * This must be valid in order to use the ANTLRWorks debugger. + * </summary> + */ + int TokenIndex + { + get; + set; + } + + /** <summary> + * From what character stream was this token created? You don't have to + * implement but it's nice to know where a Token comes from if you have + * include files etc... on the input. + * </summary> + */ + ICharStream InputStream + { + get; + set; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ITokenSource.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ITokenSource.cs new file mode 100644 index 0000000..97a9b2c --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ITokenSource.cs @@ -0,0 +1,78 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + + /** <summary> + * A source of tokens must provide a sequence of tokens via nextToken() + * and also must reveal it's source of characters; CommonToken's text is + * computed from a CharStream; it only store indices into the char stream. + * </summary> + * + * <remarks> + * Errors from the lexer are never passed to the parser. Either you want + * to keep going or you do not upon token recognition error. If you do not + * want to continue lexing then you do not want to continue parsing. Just + * throw an exception not under RecognitionException and Java will naturally + * toss you all the way out of the recognizers. If you want to continue + * lexing then you should not throw an exception to the parser--it has already + * requested a token. Keep lexing until you get a valid one. Just report + * errors and keep going, looking for a valid token. + * </summary> + */ + public interface ITokenSource + { + /** <summary> + * Return a Token object from your input stream (usually a CharStream). + * Do not fail/return upon lexing error; keep chewing on the characters + * until you get a good one; errors are not passed through to the parser. + * </summary> + */ + IToken NextToken(); + + /** <summary> + * Where are you getting tokens from? normally the implication will simply + * ask lexers input stream. + * </summary> + */ + string SourceName + { + get; + } + + string[] TokenNames + { + get; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ITokenStream.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ITokenStream.cs new file mode 100644 index 0000000..6a74cbc --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ITokenStream.cs @@ -0,0 +1,86 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ +namespace Antlr.Runtime +{ + + /** <summary>A stream of tokens accessing tokens from a TokenSource</summary> */ + public interface ITokenStream : IIntStream + { + /** <summary>Get Token at current input pointer + i ahead where i=1 is next Token. + * i<0 indicates tokens in the past. So -1 is previous token and -2 is + * two tokens ago. LT(0) is undefined. For i>=n, return Token.EOFToken. + * Return null for LT(0) and any index that results in an absolute address + * that is negative.</summary> + */ + IToken LT( int k ); + + /** <summary> + * Get a token at an absolute index i; 0..n-1. This is really only + * needed for profiling and debugging and token stream rewriting. + * If you don't want to buffer up tokens, then this method makes no + * sense for you. Naturally you can't use the rewrite stream feature. + * I believe DebugTokenStream can easily be altered to not use + * this method, removing the dependency. + * </summary> + */ + IToken Get( int i ); + + /** <summary> + * Where is this stream pulling tokens from? This is not the name, but + * the object that provides Token objects. + * </summary> + */ + ITokenSource TokenSource + { + get; + } + + /** <summary> + * Return the text of all tokens from start to stop, inclusive. + * If the stream does not buffer all the tokens then it can just + * return "" or null; Users should not access $ruleLabel.text in + * an action of course in that case. + * </summary> + */ + string ToString( int start, int stop ); + + /** <summary> + * Because the user is not required to use a token with an index stored + * in it, we must provide a means for two token objects themselves to + * indicate the start/end location. Most often this will just delegate + * to the other toString(int,int). This is also parallel with + * the TreeNodeStream.toString(Object,Object). + * </summary> + */ + string ToString( IToken start, IToken stop ); + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Lexer.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Lexer.cs new file mode 100644 index 0000000..b5d5608 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Lexer.cs @@ -0,0 +1,439 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + /** <summary> + * A lexer is recognizer that draws input symbols from a character stream. + * lexer grammars result in a subclass of this object. A Lexer object + * uses simplified match() and error recovery mechanisms in the interest + * of speed. + * </summary> + */ + public abstract class Lexer : BaseRecognizer, ITokenSource + { + /** <summary>Where is the lexer drawing characters from?</summary> */ + protected ICharStream input; + + public Lexer() + { + } + + public Lexer( ICharStream input ) + { + this.input = input; + } + + public Lexer( ICharStream input, RecognizerSharedState state ) : + base( state ) + { + this.input = input; + } + + #region Properties + public string Text + { + get + { + return GetText(); + } + set + { + SetText( value ); + } + } + public int Line + { + get + { + return input.Line; + } + set + { + input.Line = value; + } + } + public int CharPositionInLine + { + get + { + return input.CharPositionInLine; + } + set + { + input.CharPositionInLine = value; + } + } + public string[] TokenNames + { + get + { + return GetTokenNames(); + } + } + #endregion + + public override void Reset() + { + base.Reset(); // reset all recognizer state variables + // wack Lexer state variables + if ( input != null ) + { + input.Seek( 0 ); // rewind the input + } + if ( state == null ) + { + return; // no shared state work to do + } + state.token = null; + state.type = TokenConstants.INVALID_TOKEN_TYPE; + state.channel = TokenConstants.DEFAULT_CHANNEL; + state.tokenStartCharIndex = -1; + state.tokenStartCharPositionInLine = -1; + state.tokenStartLine = -1; + state.text = null; + } + + /** <summary>Return a token from this source; i.e., match a token on the char stream.</summary> */ + public virtual IToken NextToken() + { + for ( ; ; ) + { + state.token = null; + state.channel = TokenConstants.DEFAULT_CHANNEL; + state.tokenStartCharIndex = input.Index; + state.tokenStartCharPositionInLine = input.CharPositionInLine; + state.tokenStartLine = input.Line; + state.text = null; + if ( input.LA( 1 ) == CharStreamConstants.EOF ) + { + return TokenConstants.EOF_TOKEN; + } + try + { + mTokens(); + if ( state.token == null ) + { + Emit(); + } + else if ( state.token == TokenConstants.SKIP_TOKEN ) + { + continue; + } + return state.token; + } + catch ( NoViableAltException nva ) + { + ReportError( nva ); + Recover( nva ); // throw out current char and try again + } + catch ( RecognitionException re ) + { + ReportError( re ); + // match() routine has already called recover() + } + } + } + + /** <summary> + * Instruct the lexer to skip creating a token for current lexer rule + * and look for another token. nextToken() knows to keep looking when + * a lexer rule finishes with token set to SKIP_TOKEN. Recall that + * if token==null at end of any token rule, it creates one for you + * and emits it. + * </summary> + */ + public virtual void Skip() + { + state.token = TokenConstants.SKIP_TOKEN; + } + + /** <summary>This is the lexer entry point that sets instance var 'token'</summary> */ + public abstract void mTokens(); + + /** <summary>Set the char stream and reset the lexer</summary> */ + public virtual void SetCharStream( ICharStream input ) + { + this.input = null; + Reset(); + this.input = input; + } + + public virtual ICharStream GetCharStream() + { + return this.input; + } + + public override string SourceName + { + get + { + return input.SourceName; + } + } + + /** <summary> + * Currently does not support multiple emits per nextToken invocation + * for efficiency reasons. Subclass and override this method and + * nextToken (to push tokens into a list and pull from that list rather + * than a single variable as this implementation does). + * </summary> + */ + public virtual void Emit( IToken token ) + { + state.token = token; + } + + /** <summary> + * The standard method called to automatically emit a token at the + * outermost lexical rule. The token object should point into the + * char buffer start..stop. If there is a text override in 'text', + * use that to set the token's text. Override this method to emit + * custom Token objects. + * </summary> + * + * <remarks> + * If you are building trees, then you should also override + * Parser or TreeParser.getMissingSymbol(). + * </remarks> + */ + public virtual IToken Emit() + { + IToken t = new CommonToken( input, state.type, state.channel, state.tokenStartCharIndex, GetCharIndex() - 1 ); + t.Line = state.tokenStartLine; + t.Text = state.text; + t.CharPositionInLine = state.tokenStartCharPositionInLine; + Emit( t ); + return t; + } + + public virtual void Match( string s ) + { + int i = 0; + while ( i < s.Length ) + { + if ( input.LA( 1 ) != s[i] ) + { + if ( state.backtracking > 0 ) + { + state.failed = true; + return; + } + MismatchedTokenException mte = + new MismatchedTokenException( s[i], input ) + { + tokenNames = GetTokenNames() + }; + Recover( mte ); + throw mte; + } + i++; + input.Consume(); + state.failed = false; + } + } + + public virtual void MatchAny() + { + input.Consume(); + } + + public virtual void Match( int c ) + { + if ( input.LA( 1 ) != c ) + { + if ( state.backtracking > 0 ) + { + state.failed = true; + return; + } + MismatchedTokenException mte = + new MismatchedTokenException( c, input ) + { + tokenNames = GetTokenNames() + }; + Recover( mte ); // don't really recover; just consume in lexer + throw mte; + } + input.Consume(); + state.failed = false; + } + + public virtual void MatchRange( int a, int b ) + { + if ( input.LA( 1 ) < a || input.LA( 1 ) > b ) + { + if ( state.backtracking > 0 ) + { + state.failed = true; + return; + } + MismatchedRangeException mre = + new MismatchedRangeException( a, b, input ); + Recover( mre ); + throw mre; + } + input.Consume(); + state.failed = false; + } + + /** <summary>What is the index of the current character of lookahead?</summary> */ + public virtual int GetCharIndex() + { + return input.Index; + } + + /** <summary>Return the text matched so far for the current token or any text override.</summary> */ + public virtual string GetText() + { + if ( state.text != null ) + { + return state.text; + } + return input.substring( state.tokenStartCharIndex, GetCharIndex() - 1 ); + } + + /** <summary>Set the complete text of this token; it wipes any previous changes to the text.</summary> */ + public virtual void SetText( string text ) + { + state.text = text; + } + + public override void ReportError( RecognitionException e ) + { + /** TODO: not thought about recovery in lexer yet. + * + // if we've already reported an error and have not matched a token + // yet successfully, don't report any errors. + if ( errorRecovery ) { + //System.err.print("[SPURIOUS] "); + return; + } + errorRecovery = true; + */ + + DisplayRecognitionError( this.GetTokenNames(), e ); + } + + public override string GetErrorMessage( RecognitionException e, string[] tokenNames ) + { + string msg = null; + if ( e is MismatchedTokenException ) + { + MismatchedTokenException mte = (MismatchedTokenException)e; + msg = "mismatched character " + GetCharErrorDisplay( e.c ) + " expecting " + GetCharErrorDisplay( mte.expecting ); + } + else if ( e is NoViableAltException ) + { + NoViableAltException nvae = (NoViableAltException)e; + // for development, can add "decision=<<"+nvae.grammarDecisionDescription+">>" + // and "(decision="+nvae.decisionNumber+") and + // "state "+nvae.stateNumber + msg = "no viable alternative at character " + GetCharErrorDisplay( e.c ); + } + else if ( e is EarlyExitException ) + { + EarlyExitException eee = (EarlyExitException)e; + // for development, can add "(decision="+eee.decisionNumber+")" + msg = "required (...)+ loop did not match anything at character " + GetCharErrorDisplay( e.c ); + } + else if ( e is MismatchedNotSetException ) + { + MismatchedNotSetException mse = (MismatchedNotSetException)e; + msg = "mismatched character " + GetCharErrorDisplay( e.c ) + " expecting set " + mse.expecting; + } + else if ( e is MismatchedSetException ) + { + MismatchedSetException mse = (MismatchedSetException)e; + msg = "mismatched character " + GetCharErrorDisplay( e.c ) + " expecting set " + mse.expecting; + } + else if ( e is MismatchedRangeException ) + { + MismatchedRangeException mre = (MismatchedRangeException)e; + msg = "mismatched character " + GetCharErrorDisplay( e.c ) + " expecting set " + + GetCharErrorDisplay( mre.a ) + ".." + GetCharErrorDisplay( mre.b ); + } + else + { + msg = base.GetErrorMessage( e, tokenNames ); + } + return msg; + } + + public virtual string GetCharErrorDisplay( int c ) + { + string s = c.ToString(); //string.valueOf((char)c); + switch ( c ) + { + case TokenConstants.EOF: + s = "<EOF>"; + break; + case '\n': + s = "\\n"; + break; + case '\t': + s = "\\t"; + break; + case '\r': + s = "\\r"; + break; + } + return "'" + s + "'"; + } + + /** <summary> + * Lexers can normally match any char in it's vocabulary after matching + * a token, so do the easy thing and just kill a character and hope + * it all works out. You can instead use the rule invocation stack + * to do sophisticated error recovery if you are in a fragment rule. + * </summary> + */ + public virtual void Recover( RecognitionException re ) + { + //System.out.println("consuming char "+(char)input.LA(1)+" during recovery"); + //re.printStackTrace(); + input.Consume(); + } + + public virtual void TraceIn( string ruleName, int ruleIndex ) + { + string inputSymbol = ( (char)input.LT( 1 ) ) + " line=" + Line + ":" + CharPositionInLine; + base.TraceIn( ruleName, ruleIndex, inputSymbol ); + } + + public virtual void TraceOut( string ruleName, int ruleIndex ) + { + string inputSymbol = ( (char)input.LT( 1 ) ) + " line=" + Line + ":" + CharPositionInLine; + base.TraceOut( ruleName, ruleIndex, inputSymbol ); + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Misc/FastQueue.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Misc/FastQueue.cs new file mode 100644 index 0000000..9f54ddc --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Misc/FastQueue.cs @@ -0,0 +1,124 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Misc +{ + using System.Collections.Generic; + + /** A queue that can dequeue and get(i) in O(1) and grow arbitrarily large. + * A linked list is fast at dequeue but slow at get(i). An array is + * the reverse. This is O(1) for both operations. + * + * List grows until you dequeue last element at end of buffer. Then + * it resets to start filling at 0 again. If adds/removes are balanced, the + * buffer will not grow too large. + * + * No iterator stuff as that's not how we'll use it. + */ + public class FastQueue<T> + { + /** <summary>dynamically-sized buffer of elements</summary> */ + protected List<T> _data = new List<T>(); + /** <summary>index of next element to fill</summary> */ + protected int _p = 0; + + /** <summary>Get and remove first element in queue</summary> */ + public virtual T Dequeue() + { + T o = this[0]; + _p++; + // have we hit end of buffer? + if ( _p == _data.Count ) + { + // if so, it's an opportunity to start filling at index 0 again + Clear(); // size goes to 0, but retains memory + } + return o; + } + + public virtual void Enqueue( T o ) + { + _data.Add( o ); + } + + public virtual int Count + { + get + { + return _data.Count - _p; + } + } + + public virtual T Peek() + { + return this[0]; + } + + /** <summary> + * Return element i elements ahead of current element. i==0 gets + * current element. This is not an absolute index into the data list + * since p defines the start of the real list. + * </summary> + */ + public T this[int i] + { + get + { + if ( _p + i >= _data.Count ) + { + throw new System.ArgumentException( "queue index " + ( _p + i ) + " > size " + _data.Count ); + } + return _data[_p + i]; + } + } + + protected virtual void Clear() + { + _p = 0; + _data.Clear(); + } + + /** <summary>Return string of current buffer contents; non-destructive</summary> */ + public override string ToString() + { + System.Text.StringBuilder buf = new System.Text.StringBuilder(); + int n = Count; + for ( int i = 0; i < n; i++ ) + { + buf.Append( this[i] ); + if ( ( i + 1 ) < n ) + buf.Append( " " ); + } + return buf.ToString(); + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Misc/LookaheadStream.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Misc/LookaheadStream.cs new file mode 100644 index 0000000..bd8b26c --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Misc/LookaheadStream.cs @@ -0,0 +1,231 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Misc +{ + + /** <summary> + * A lookahead queue that knows how to mark/release locations + * in the buffer for backtracking purposes. Any markers force the FastQueue + * superclass to keep all tokens until no more markers; then can reset + * to avoid growing a huge buffer. + * </summary> + */ + public abstract class LookaheadStream<T> + : FastQueue<T> + where T : class + { + public const int UninitializedEofElementIndex = int.MaxValue; + + /** <summary>Set to buffer index of eof when nextElement returns eof</summary> */ + int _eofElementIndex = UninitializedEofElementIndex; + + /** <summary>Returned by nextElement upon end of stream; we add to buffer also</summary> */ + T _eof = null; + + /** <summary>Track the last mark() call result value for use in rewind().</summary> */ + int _lastMarker; + + /** <summary>tracks how deep mark() calls are nested</summary> */ + int _markDepth; + + public LookaheadStream( T eof ) + { + this._eof = eof; + } + + public T Eof + { + get + { + return _eof; + } + protected set + { + _eof = value; + } + } + + protected override void Clear() + { + _eofElementIndex = UninitializedEofElementIndex; + base.Clear(); + } + + /** <summary> + * Implement nextElement to supply a stream of elements to this + * lookahead buffer. Return eof upon end of the stream we're pulling from. + * </summary> + */ + public abstract T NextElement(); + + /** <summary>Get and remove first element in queue; override FastQueue.remove()</summary> */ + public override T Dequeue() + { + T o = this[0]; + _p++; + // have we hit end of buffer and not backtracking? + if ( _p == _data.Count && _markDepth == 0 ) + { + // if so, it's an opportunity to start filling at index 0 again + Clear(); // size goes to 0, but retains memory + } + return o; + } + + /** <summary>Make sure we have at least one element to remove, even if EOF</summary> */ + public virtual void Consume() + { + Sync( 1 ); + Dequeue(); + } + + /** <summary> + * Make sure we have 'need' elements from current position p. Last valid + * p index is data.size()-1. p+need-1 is the data index 'need' elements + * ahead. If we need 1 element, (p+1-1)==p must be < data.size(). + * </summary> + */ + public virtual void Sync( int need ) + { + int n = ( _p + need - 1 ) - _data.Count + 1; // how many more elements we need? + if ( n > 0 ) + Fill( n ); // out of elements? + } + + /** <summary>add n elements to buffer</summary> */ + public virtual void Fill( int n ) + { + for ( int i = 1; i <= n; i++ ) + { + T o = NextElement(); + if ( o == _eof ) + { + _data.Add( _eof ); + _eofElementIndex = _data.Count - 1; + } + else + _data.Add( o ); + } + } + + /** <summary>Size of entire stream is unknown; we only know buffer size from FastQueue</summary> */ + public override int Count + { + get + { + throw new System.NotSupportedException( "streams are of unknown size" ); + } + } + + public virtual object LT( int k ) + { + if ( k == 0 ) + { + return null; + } + if ( k < 0 ) + { + return LB( -k ); + } + //System.out.print("LT(p="+p+","+k+")="); + if ( ( _p + k - 1 ) >= _eofElementIndex ) + { + // move to super.LT + return _eof; + } + Sync( k ); + return this[k - 1]; + } + + /** <summary>Look backwards k nodes</summary> */ + protected virtual object LB( int k ) + { + if ( k == 0 ) + { + return null; + } + if ( ( _p - k ) < 0 ) + { + return null; + } + return this[-k]; + } + + public virtual object GetCurrentSymbol() + { + return LT( 1 ); + } + + public virtual int Index + { + get + { + return _p; + } + } + + public virtual int Mark() + { + _markDepth++; + _lastMarker = Index; + return _lastMarker; + } + + public virtual void Release( int marker ) + { + _markDepth--; + } + + public virtual void Rewind( int marker ) + { + Seek( marker ); + Release( marker ); + } + + public virtual void Rewind() + { + Seek( _lastMarker ); + } + + /** <summary> + * Seek to a 0-indexed position within data buffer. Can't handle + * case where you seek beyond end of existing buffer. Normally used + * to seek backwards in the buffer. Does not force loading of nodes. + * </summary> + */ + public virtual void Seek( int index ) + { + _p = index; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MismatchedNotSetException.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MismatchedNotSetException.cs new file mode 100644 index 0000000..1eb2287 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MismatchedNotSetException.cs @@ -0,0 +1,53 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + + public class MismatchedNotSetException : MismatchedSetException + { + /** <summary>Used for remote debugger deserialization</summary> */ + public MismatchedNotSetException() + { + } + + public MismatchedNotSetException( BitSet expecting, IIntStream input ) + : base( expecting, input ) + { + } + + public override string ToString() + { + return "MismatchedNotSetException(" + UnexpectedType + "!=" + expecting + ")"; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MismatchedRangeException.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MismatchedRangeException.cs new file mode 100644 index 0000000..f18824c --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MismatchedRangeException.cs @@ -0,0 +1,58 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + + public class MismatchedRangeException : RecognitionException + { + public int a; + public int b; + + /** <summary>Used for remote debugger deserialization</summary> */ + public MismatchedRangeException() + { + } + + public MismatchedRangeException( int a, int b, IIntStream input ) + : base( input ) + { + this.a = a; + this.b = b; + } + + public override string ToString() + { + return "MismatchedNotSetException(" + UnexpectedType + " not in [" + a + "," + b + "])"; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MismatchedSetException.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MismatchedSetException.cs new file mode 100644 index 0000000..5652497 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MismatchedSetException.cs @@ -0,0 +1,56 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + + public class MismatchedSetException : RecognitionException + { + public BitSet expecting; + + /** <summary>Used for remote debugger deserialization</summary> */ + public MismatchedSetException() + { + } + + public MismatchedSetException( BitSet expecting, IIntStream input ) : + base( input ) + { + this.expecting = expecting; + } + + public override string ToString() + { + return "MismatchedSetException(" + UnexpectedType + "!=" + expecting + ")"; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MismatchedTokenException.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MismatchedTokenException.cs new file mode 100644 index 0000000..cbe3d11 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MismatchedTokenException.cs @@ -0,0 +1,62 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + + /** <summary>A mismatched char or Token or tree node</summary> */ + [System.Serializable] + public class MismatchedTokenException : RecognitionException + { + public int expecting = TokenConstants.INVALID_TOKEN_TYPE; + public string[] tokenNames; + + /** <summary>Used for remote debugger deserialization</summary> */ + public MismatchedTokenException() + { + } + + public MismatchedTokenException( int expecting, IIntStream input ) + : base( input ) + { + this.expecting = expecting; + } + + public override string ToString() + { + int unexpectedType = UnexpectedType; + string unexpected = ( tokenNames != null && unexpectedType >= 0 && unexpectedType < tokenNames.Length ) ? tokenNames[unexpectedType] : unexpectedType.ToString(); + string expected = ( tokenNames != null && expecting >= 0 && expecting < tokenNames.Length ) ? tokenNames[expecting] : expecting.ToString(); + return "MismatchedTokenException(" + unexpected + "!=" + expected + ")"; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MismatchedTreeNodeException.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MismatchedTreeNodeException.cs new file mode 100644 index 0000000..bedfb81 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MismatchedTreeNodeException.cs @@ -0,0 +1,58 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream; + + /** + */ + public class MismatchedTreeNodeException : RecognitionException + { + public int expecting; + + public MismatchedTreeNodeException() + { + } + + public MismatchedTreeNodeException( int expecting, ITreeNodeStream input ) : + base( input ) + { + this.expecting = expecting; + } + + public override string ToString() + { + return "MismatchedTreeNodeException(" + UnexpectedType + "!=" + expecting + ")"; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MissingTokenException.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MissingTokenException.cs new file mode 100644 index 0000000..dc0d817 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/MissingTokenException.cs @@ -0,0 +1,73 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + + /** <summary> + * We were expecting a token but it's not found. The current token + * is actually what we wanted next. Used for tree node errors too. + * </summary> + */ + public class MissingTokenException : MismatchedTokenException + { + public object inserted; + /** <summary>Used for remote debugger deserialization</summary> */ + public MissingTokenException() + { + } + + public MissingTokenException( int expecting, IIntStream input, object inserted ) : + base( expecting, input ) + { + this.inserted = inserted; + } + + public virtual int GetMissingType() + { + return expecting; + } + + public override string ToString() + { + if ( inserted != null && token != null ) + { + return "MissingTokenException(inserted " + inserted + " at " + token.Text + ")"; + } + if ( token != null ) + { + return "MissingTokenException(at " + token.Text + ")"; + } + return "MissingTokenException"; + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/NoViableAltException.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/NoViableAltException.cs new file mode 100644 index 0000000..fb07d58 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/NoViableAltException.cs @@ -0,0 +1,70 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + + public class NoViableAltException : RecognitionException + { + public string grammarDecisionDescription; + public int decisionNumber; + public int stateNumber; + + /** <summary>Used for remote debugger deserialization</summary> */ + public NoViableAltException() + { + } + + public NoViableAltException( string grammarDecisionDescription, + int decisionNumber, + int stateNumber, + IIntStream input ) + : base( input ) + { + this.grammarDecisionDescription = grammarDecisionDescription; + this.decisionNumber = decisionNumber; + this.stateNumber = stateNumber; + } + + public override string ToString() + { + if ( input is ICharStream ) + { + return "NoViableAltException('" + (char)UnexpectedType + "'@[" + grammarDecisionDescription + "])"; + } + else + { + return "NoViableAltException(" + UnexpectedType + "@[" + grammarDecisionDescription + "])"; + } + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Parser.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Parser.cs new file mode 100644 index 0000000..3ff0a4f --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Parser.cs @@ -0,0 +1,148 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + /** <summary> + * A parser for TokenStreams. "parser grammars" result in a subclass + * of this. + * </summary> + */ + public class Parser : BaseRecognizer + { + public ITokenStream input; + + public Parser( ITokenStream input ) + : base() + { + //super(); // highlight that we go to super to set state object + TokenStream = input; + } + + public Parser( ITokenStream input, RecognizerSharedState state ) + : base( state ) + { + //super(state); // share the state object with another parser + TokenStream = input; + } + + public override void Reset() + { + base.Reset(); // reset all recognizer state variables + if ( input != null ) + { + input.Seek( 0 ); // rewind the input + } + } + + protected override object GetCurrentInputSymbol( IIntStream input ) + { + return ( (ITokenStream)input ).LT( 1 ); + } + + protected override object GetMissingSymbol( IIntStream input, + RecognitionException e, + int expectedTokenType, + BitSet follow ) + { + string tokenText = null; + if ( expectedTokenType == TokenConstants.EOF ) + tokenText = "<missing EOF>"; + else + tokenText = "<missing " + GetTokenNames()[expectedTokenType] + ">"; + CommonToken t = new CommonToken( expectedTokenType, tokenText ); + IToken current = ( (ITokenStream)input ).LT( 1 ); + if ( current.Type == TokenConstants.EOF ) + { + current = ( (ITokenStream)input ).LT( -1 ); + } + t.Line = current.Line; + t.CharPositionInLine = current.CharPositionInLine; + t.Channel = DEFAULT_TOKEN_CHANNEL; + return t; + } + + /** <summary>Gets or sets the token stream; resets the parser upon a set.</summary> */ + public virtual ITokenStream TokenStream + { + get + { + return input; + } + set + { + input = null; + Reset(); + input = value; + } + } + + public override string SourceName + { + get + { + return input.SourceName; + } + } + + public virtual void TraceIn( string ruleName, int ruleIndex ) + { + base.TraceIn( ruleName, ruleIndex, input.LT( 1 ) ); + } + + public virtual void TraceOut( string ruleName, int ruleIndex ) + { + base.TraceOut( ruleName, ruleIndex, input.LT( 1 ) ); + } + +#if false + protected bool EvaluatePredicate( System.Action predicate ) + { + state.backtracking++; + int start = input.Mark(); + try + { + predicate(); + } + catch ( RecognitionException re ) + { + System.Console.Error.WriteLine( "impossible: " + re ); + } + bool success = !state.failed; + input.Rewind( start ); + state.backtracking--; + state.failed = false; + return success; + } +#endif + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ParserRuleReturnScope.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ParserRuleReturnScope.cs new file mode 100644 index 0000000..4be95eb --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/ParserRuleReturnScope.cs @@ -0,0 +1,79 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + + /** <summary> + * Rules that return more than a single value must return an object + * containing all the values. Besides the properties defined in + * RuleLabelScope.predefinedRulePropertiesScope there may be user-defined + * return values. This class simply defines the minimum properties that + * are always defined and methods to access the others that might be + * available depending on output option such as template and tree. + * </summary> + * + * <remarks> + * Note text is not an actual property of the return value, it is computed + * from start and stop using the input stream's toString() method. I + * could add a ctor to this so that we can pass in and store the input + * stream, but I'm not sure we want to do that. It would seem to be undefined + * to get the .text property anyway if the rule matches tokens from multiple + * input streams. + * + * I do not use getters for fields of objects that are used simply to + * group values such as this aggregate. The getters/setters are there to + * satisfy the superclass interface. + * </remarks> + */ + public class ParserRuleReturnScope : RuleReturnScope + { + public IToken start; + public IToken stop; + + public override object Start + { + get + { + return start; + } + } + + public override object Stop + { + get + { + return stop; + } + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Properties/AssemblyInfo.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..f5143c7 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Properties/AssemblyInfo.cs @@ -0,0 +1,68 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle( "Antlr3.Runtime" )] +[assembly: AssemblyDescription( "" )] +[assembly: AssemblyConfiguration( "" )] +[assembly: AssemblyCompany( "Pixel Mine, Inc." )] +[assembly: AssemblyProduct( "Antlr3.Runtime" )] +[assembly: AssemblyCopyright( "Copyright © Pixel Mine 2008" )] +[assembly: AssemblyTrademark( "" )] +[assembly: AssemblyCulture( "" )] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible( false )] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid( "7a0b4db7-f127-4cf5-ac2c-e294957efcd6" )] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion( "3.2.17961.0" )] +[assembly: AssemblyFileVersion( "3.2.17961.0" )] diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/RecognitionException.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/RecognitionException.cs new file mode 100644 index 0000000..d32617e --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/RecognitionException.cs @@ -0,0 +1,223 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + using Antlr.Runtime.Tree; + + using Exception = System.Exception; + using NonSerialized = System.NonSerializedAttribute; + + /** <summary>The root of the ANTLR exception hierarchy.</summary> + * + * <remarks> + * To avoid English-only error messages and to generally make things + * as flexible as possible, these exceptions are not created with strings, + * but rather the information necessary to generate an error. Then + * the various reporting methods in Parser and Lexer can be overridden + * to generate a localized error message. For example, MismatchedToken + * exceptions are built with the expected token type. + * So, don't expect getMessage() to return anything. + * + * Note that as of Java 1.4, you can access the stack trace, which means + * that you can compute the complete trace of rules from the start symbol. + * This gives you considerable context information with which to generate + * useful error messages. + * + * ANTLR generates code that throws exceptions upon recognition error and + * also generates code to catch these exceptions in each rule. If you + * want to quit upon first error, you can turn off the automatic error + * handling mechanism using rulecatch action, but you still need to + * override methods mismatch and recoverFromMismatchSet. + * + * In general, the recognition exceptions can track where in a grammar a + * problem occurred and/or what was the expected input. While the parser + * knows its state (such as current input symbol and line info) that + * state can change before the exception is reported so current token index + * is computed and stored at exception time. From this info, you can + * perhaps print an entire line of input not just a single token, for example. + * Better to just say the recognizer had a problem and then let the parser + * figure out a fancy report. + * </remarks> + */ + [System.Serializable] + public class RecognitionException : Exception + { + /** <summary>What input stream did the error occur in?</summary> */ + [NonSerialized] + public IIntStream input; + + /** <summary>What is index of token/char were we looking at when the error occurred?</summary> */ + public int index; + + /** <summary> + * The current Token when an error occurred. Since not all streams + * can retrieve the ith Token, we have to track the Token object. + * For parsers. Even when it's a tree parser, token might be set. + * </summary> + */ + public IToken token; + + /** <summary> + * If this is a tree parser exception, node is set to the node with + * the problem. + * </summary> + */ + public object node; + + /** <summary>The current char when an error occurred. For lexers.</summary> */ + public int c; + + /** <summary> + * Track the line at which the error occurred in case this is + * generated from a lexer. We need to track this since the + * unexpected char doesn't carry the line info. + * </summary> + */ + public int line; + + public int charPositionInLine; + + /** <summary> + * If you are parsing a tree node stream, you will encounter som + * imaginary nodes w/o line/col info. We now search backwards looking + * for most recent token with line/col info, but notify getErrorHeader() + * that info is approximate. + * </summary> + */ + public bool approximateLineInfo; + + /** <summary>Used for remote debugger deserialization</summary> */ + public RecognitionException() + { + } + + public RecognitionException( IIntStream input ) + { + this.input = input; + this.index = input.Index; + if ( input is ITokenStream ) + { + this.token = ( (ITokenStream)input ).LT( 1 ); + this.line = token.Line; + this.charPositionInLine = token.CharPositionInLine; + } + if ( input is ITreeNodeStream ) + { + ExtractInformationFromTreeNodeStream( input ); + } + else if ( input is ICharStream ) + { + this.c = input.LA( 1 ); + this.line = ( (ICharStream)input ).Line; + this.charPositionInLine = ( (ICharStream)input ).CharPositionInLine; + } + else + { + this.c = input.LA( 1 ); + } + } + + protected virtual void ExtractInformationFromTreeNodeStream( IIntStream input ) + { + ITreeNodeStream nodes = (ITreeNodeStream)input; + this.node = nodes.LT( 1 ); + ITreeAdaptor adaptor = nodes.TreeAdaptor; + IToken payload = adaptor.GetToken( node ); + if ( payload != null ) + { + this.token = payload; + if ( payload.Line <= 0 ) + { + // imaginary node; no line/pos info; scan backwards + int i = -1; + object priorNode = nodes.LT( i ); + while ( priorNode != null ) + { + IToken priorPayload = adaptor.GetToken( priorNode ); + if ( priorPayload != null && priorPayload.Line > 0 ) + { + // we found the most recent real line / pos info + this.line = priorPayload.Line; + this.charPositionInLine = priorPayload.CharPositionInLine; + this.approximateLineInfo = true; + break; + } + --i; + priorNode = nodes.LT( i ); + } + } + else + { // node created from real token + this.line = payload.Line; + this.charPositionInLine = payload.CharPositionInLine; + } + } + else if ( this.node is Tree.ITree ) + { + this.line = ( (Tree.ITree)this.node ).Line; + this.charPositionInLine = ( (Tree.ITree)this.node ).CharPositionInLine; + if ( this.node is CommonTree ) + { + this.token = ( (CommonTree)this.node ).token; + } + } + else + { + int type = adaptor.GetType( this.node ); + string text = adaptor.GetText( this.node ); + this.token = new CommonToken( type, text ); + } + } + + /** <summary>Return the token type or char of the unexpected input element</summary> */ + public virtual int UnexpectedType + { + get + { + if ( input is ITokenStream ) + { + return token.Type; + } + + ITreeNodeStream treeNodeStream = input as ITreeNodeStream; + if ( treeNodeStream != null ) + { + ITreeAdaptor adaptor = treeNodeStream.TreeAdaptor; + return adaptor.GetType( node ); + } + + return c; + } + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/RecognizerSharedState.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/RecognizerSharedState.cs new file mode 100644 index 0000000..47d8534 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/RecognizerSharedState.cs @@ -0,0 +1,149 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + using System.Collections.Generic; + + /** <summary> + * The set of fields needed by an abstract recognizer to recognize input + * and recover from errors etc... As a separate state object, it can be + * shared among multiple grammars; e.g., when one grammar imports another. + * </summary> + * + * <remarks> + * These fields are publically visible but the actual state pointer per + * parser is protected. + * </remarks> + */ + public class RecognizerSharedState + { + /** <summary> + * Track the set of token types that can follow any rule invocation. + * Stack grows upwards. When it hits the max, it grows 2x in size + * and keeps going. + * </summary> + */ + public BitSet[] following = new BitSet[BaseRecognizer.INITIAL_FOLLOW_STACK_SIZE]; + public int _fsp = -1; + + /** <summary> + * This is true when we see an error and before having successfully + * matched a token. Prevents generation of more than one error message + * per error. + * </summary> + */ + public bool errorRecovery = false; + + /** <summary> + * The index into the input stream where the last error occurred. + * This is used to prevent infinite loops where an error is found + * but no token is consumed during recovery...another error is found, + * ad naseum. This is a failsafe mechanism to guarantee that at least + * one token/tree node is consumed for two errors. + * </summary> + */ + public int lastErrorIndex = -1; + + /** <summary> + * In lieu of a return value, this indicates that a rule or token + * has failed to match. Reset to false upon valid token match. + * </summary> + */ + public bool failed = false; + + /** <summary>Did the recognizer encounter a syntax error? Track how many.</summary> */ + public int syntaxErrors = 0; + + /** <summary> + * If 0, no backtracking is going on. Safe to exec actions etc... + * If >0 then it's the level of backtracking. + * </summary> + */ + public int backtracking = 0; + + /** <summary> + * An array[size num rules] of Map<Integer,Integer> that tracks + * the stop token index for each rule. ruleMemo[ruleIndex] is + * the memoization table for ruleIndex. For key ruleStartIndex, you + * get back the stop token for associated rule or MEMO_RULE_FAILED. + * </summary> + * + * <remarks>This is only used if rule memoization is on (which it is by default).</remarks> + */ + public IDictionary<int, int>[] ruleMemo; + + + // LEXER FIELDS (must be in same state object to avoid casting + // constantly in generated code and Lexer object) :( + + + /** <summary> + * The goal of all lexer rules/methods is to create a token object. + * This is an instance variable as multiple rules may collaborate to + * create a single token. nextToken will return this object after + * matching lexer rule(s). If you subclass to allow multiple token + * emissions, then set this to the last token to be matched or + * something nonnull so that the auto token emit mechanism will not + * emit another token. + * </summary> + */ + public IToken token; + + /** <summary> + * What character index in the stream did the current token start at? + * Needed, for example, to get the text for current token. Set at + * the start of nextToken. + * </summary> + */ + public int tokenStartCharIndex = -1; + + /** <summary>The line on which the first character of the token resides</summary> */ + public int tokenStartLine; + + /** <summary>The character position of first character within the line</summary> */ + public int tokenStartCharPositionInLine; + + /** <summary>The channel number for the current token</summary> */ + public int channel; + + /** <summary>The token type for the current token</summary> */ + public int type; + + /** <summary> + * You can set the text for the current token to override what is in + * the input char buffer. Use setText() or can set this instance var. + * </summary> + */ + public string text; + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/RuleReturnScope.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/RuleReturnScope.cs new file mode 100644 index 0000000..e0ec73c --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/RuleReturnScope.cs @@ -0,0 +1,78 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + /** <summary>Rules can return start/stop info as well as possible trees and templates</summary> */ + public class RuleReturnScope + { + /** <summary>Return the start token or tree</summary> */ + public virtual object Start + { + get + { + return null; + } + } + + /** <summary>Return the stop token or tree</summary> */ + public virtual object Stop + { + get + { + return null; + } + } + + /** <summary>Has a value potentially if output=AST;</summary> */ + public virtual object Tree + { + get + { + return null; + } + } + + /** <summary> + * Has a value potentially if output=template; Don't use StringTemplate + * type as it then causes a dependency with ST lib. + * </summary> + */ + public virtual object Template + { + get + { + return null; + } + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/TokenConstants.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/TokenConstants.cs new file mode 100644 index 0000000..de5f93c --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/TokenConstants.cs @@ -0,0 +1,74 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + public static class TokenConstants + { + public const int EOR_TOKEN_TYPE = 1; + + /** <summary>imaginary tree navigation type; traverse "get child" link</summary> */ + public const int DOWN = 2; + /** <summary>imaginary tree navigation type; finish with a child list</summary> */ + public const int UP = 3; + + public const int MIN_TOKEN_TYPE = UP + 1; + + public const int EOF = CharStreamConstants.EOF; + public static readonly IToken EOF_TOKEN = new CommonToken( EOF ); + + public const int INVALID_TOKEN_TYPE = 0; + public static readonly IToken INVALID_TOKEN = new CommonToken( INVALID_TOKEN_TYPE ); + + /** <summary> + * In an action, a lexer rule can set token to this SKIP_TOKEN and ANTLR + * will avoid creating a token for this symbol and try to fetch another. + * </summary> + */ + public static readonly IToken SKIP_TOKEN = new CommonToken( INVALID_TOKEN_TYPE ); + + /** <summary> + * All tokens go to the parser (unless skip() is called in that rule) + * on a particular "channel". The parser tunes to a particular channel + * so that whitespace etc... can go to the parser on a "hidden" channel. + * </summary> + */ + public const int DEFAULT_CHANNEL = 0; + + /** <summary> + * Anything on different channel than DEFAULT_CHANNEL is not parsed + * by parser. + * </summary> + */ + public const int HIDDEN_CHANNEL = 99; + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/TokenRewriteStream.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/TokenRewriteStream.cs new file mode 100644 index 0000000..122af80 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/TokenRewriteStream.cs @@ -0,0 +1,696 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime +{ + using System.Collections.Generic; + + using ArgumentException = System.ArgumentException; + using Exception = System.Exception; + using StringBuilder = System.Text.StringBuilder; + using Type = System.Type; + + /** Useful for dumping out the input stream after doing some + * augmentation or other manipulations. + * + * You can insert stuff, replace, and delete chunks. Note that the + * operations are done lazily--only if you convert the buffer to a + * String. This is very efficient because you are not moving data around + * all the time. As the buffer of tokens is converted to strings, the + * toString() method(s) check to see if there is an operation at the + * current index. If so, the operation is done and then normal String + * rendering continues on the buffer. This is like having multiple Turing + * machine instruction streams (programs) operating on a single input tape. :) + * + * Since the operations are done lazily at toString-time, operations do not + * screw up the token index values. That is, an insert operation at token + * index i does not change the index values for tokens i+1..n-1. + * + * Because operations never actually alter the buffer, you may always get + * the original token stream back without undoing anything. Since + * the instructions are queued up, you can easily simulate transactions and + * roll back any changes if there is an error just by removing instructions. + * For example, + * + * CharStream input = new ANTLRFileStream("input"); + * TLexer lex = new TLexer(input); + * TokenRewriteStream tokens = new TokenRewriteStream(lex); + * T parser = new T(tokens); + * parser.startRule(); + * + * Then in the rules, you can execute + * Token t,u; + * ... + * input.insertAfter(t, "text to put after t");} + * input.insertAfter(u, "text after u");} + * System.out.println(tokens.toString()); + * + * Actually, you have to cast the 'input' to a TokenRewriteStream. :( + * + * You can also have multiple "instruction streams" and get multiple + * rewrites from a single pass over the input. Just name the instruction + * streams and use that name again when printing the buffer. This could be + * useful for generating a C file and also its header file--all from the + * same buffer: + * + * tokens.insertAfter("pass1", t, "text to put after t");} + * tokens.insertAfter("pass2", u, "text after u");} + * System.out.println(tokens.toString("pass1")); + * System.out.println(tokens.toString("pass2")); + * + * If you don't use named rewrite streams, a "default" stream is used as + * the first example shows. + */ + [System.Serializable] + public class TokenRewriteStream : CommonTokenStream + { + public const string DEFAULT_PROGRAM_NAME = "default"; + public const int PROGRAM_INIT_SIZE = 100; + public const int MIN_TOKEN_INDEX = 0; + + // Define the rewrite operation hierarchy + + protected class RewriteOperation + { + /** <summary>What index into rewrites List are we?</summary> */ + public int instructionIndex; + /** <summary>Token buffer index.</summary> */ + public int index; + public object text; + // outer + protected TokenRewriteStream stream; + + protected RewriteOperation( TokenRewriteStream stream, int index, object text ) + { + this.index = index; + this.text = text; + this.stream = stream; + } + /** <summary> + * Execute the rewrite operation by possibly adding to the buffer. + * Return the index of the next token to operate on. + * </summary> + */ + public virtual int Execute( StringBuilder buf ) + { + return index; + } + public override string ToString() + { + string opName = this.GetType().Name; + int index = opName.IndexOf( '$' ); + opName = opName.Substring( index + 1 ); + return "<" + opName + "@" + this.index + ":\"" + text + "\">"; + } + } + + class InsertBeforeOp : RewriteOperation + { + public InsertBeforeOp( TokenRewriteStream stream, int index, object text ) : + base( stream, index, text ) + { + } + public override int Execute( StringBuilder buf ) + { + buf.Append( text ); + buf.Append( ( (IToken)stream.tokens[index] ).Text ); + return index + 1; + } + } + + /** <summary> + * I'm going to try replacing range from x..y with (y-x)+1 ReplaceOp + * instructions. + * </summary> + */ + class ReplaceOp : RewriteOperation + { + public int lastIndex; + public ReplaceOp( TokenRewriteStream stream, int from, int to, object text ) + : base( stream, from, text ) + { + lastIndex = to; + } + public override int Execute( StringBuilder buf ) + { + if ( text != null ) + { + buf.Append( text ); + } + return lastIndex + 1; + } + public override string ToString() + { + return "<ReplaceOp@" + index + ".." + lastIndex + ":\"" + text + "\">"; + } + } + + class DeleteOp : ReplaceOp + { + public DeleteOp( TokenRewriteStream stream, int from, int to ) : + base( stream, from, to, null ) + { + } + public override string ToString() + { + return "<DeleteOp@" + index + ".." + lastIndex + ">"; + } + } + + /** <summary> + * You may have multiple, named streams of rewrite operations. + * I'm calling these things "programs." + * Maps String (name) -> rewrite (List) + * </summary> + */ + protected IDictionary<string, IList<RewriteOperation>> programs = null; + + /** <summary>Map String (program name) -> Integer index</summary> */ + protected IDictionary<string, int> lastRewriteTokenIndexes = null; + + public TokenRewriteStream() + { + Init(); + } + + protected void Init() + { + programs = new Dictionary<string, IList<RewriteOperation>>(); + programs[DEFAULT_PROGRAM_NAME] = new List<RewriteOperation>( PROGRAM_INIT_SIZE ); + lastRewriteTokenIndexes = new Dictionary<string, int>(); + } + + public TokenRewriteStream( ITokenSource tokenSource ) + : base( tokenSource ) + { + Init(); + } + + public TokenRewriteStream( ITokenSource tokenSource, int channel ) + : base( tokenSource, channel ) + { + Init(); + } + + public virtual void Rollback( int instructionIndex ) + { + Rollback( DEFAULT_PROGRAM_NAME, instructionIndex ); + } + + /** <summary> + * Rollback the instruction stream for a program so that + * the indicated instruction (via instructionIndex) is no + * longer in the stream. UNTESTED! + * </summary> + */ + public virtual void Rollback( string programName, int instructionIndex ) + { + IList<RewriteOperation> @is; + if ( programs.TryGetValue( programName, out @is ) && @is != null ) + { + List<RewriteOperation> sublist = new List<RewriteOperation>(); + for ( int i = MIN_TOKEN_INDEX; i <= instructionIndex; i++ ) + sublist.Add( @is[i] ); + + programs[programName] = sublist; + } + } + + public virtual void DeleteProgram() + { + DeleteProgram( DEFAULT_PROGRAM_NAME ); + } + + /** <summary>Reset the program so that no instructions exist</summary> */ + public virtual void DeleteProgram( string programName ) + { + Rollback( programName, MIN_TOKEN_INDEX ); + } + + public virtual void InsertAfter( IToken t, object text ) + { + InsertAfter( DEFAULT_PROGRAM_NAME, t, text ); + } + + public virtual void InsertAfter( int index, object text ) + { + InsertAfter( DEFAULT_PROGRAM_NAME, index, text ); + } + + public virtual void InsertAfter( string programName, IToken t, object text ) + { + InsertAfter( programName, t.TokenIndex, text ); + } + + public virtual void InsertAfter( string programName, int index, object text ) + { + // to insert after, just insert before next index (even if past end) + InsertBefore( programName, index + 1, text ); + //addToSortedRewriteList(programName, new InsertAfterOp(index,text)); + } + + public virtual void InsertBefore( IToken t, object text ) + { + InsertBefore( DEFAULT_PROGRAM_NAME, t, text ); + } + + public virtual void InsertBefore( int index, object text ) + { + InsertBefore( DEFAULT_PROGRAM_NAME, index, text ); + } + + public virtual void InsertBefore( string programName, IToken t, object text ) + { + InsertBefore( programName, t.TokenIndex, text ); + } + + public virtual void InsertBefore( string programName, int index, object text ) + { + //addToSortedRewriteList(programName, new InsertBeforeOp(index,text)); + RewriteOperation op = new InsertBeforeOp( this, index, text ); + IList<RewriteOperation> rewrites = GetProgram( programName ); + op.instructionIndex = rewrites.Count; + rewrites.Add( op ); + } + + public virtual void Replace( int index, object text ) + { + Replace( DEFAULT_PROGRAM_NAME, index, index, text ); + } + + public virtual void Replace( int from, int to, object text ) + { + Replace( DEFAULT_PROGRAM_NAME, from, to, text ); + } + + public virtual void Replace( IToken indexT, object text ) + { + Replace( DEFAULT_PROGRAM_NAME, indexT, indexT, text ); + } + + public virtual void Replace( IToken from, IToken to, object text ) + { + Replace( DEFAULT_PROGRAM_NAME, from, to, text ); + } + + public virtual void Replace( string programName, int from, int to, object text ) + { + if ( from > to || from < 0 || to < 0 || to >= tokens.Count ) + { + throw new ArgumentException( "replace: range invalid: " + from + ".." + to + "(size=" + tokens.Count + ")" ); + } + RewriteOperation op = new ReplaceOp( this, from, to, text ); + IList<RewriteOperation> rewrites = GetProgram( programName ); + op.instructionIndex = rewrites.Count; + rewrites.Add( op ); + } + + public virtual void Replace( string programName, IToken from, IToken to, object text ) + { + Replace( programName, + from.TokenIndex, + to.TokenIndex, + text ); + } + + public virtual void Delete( int index ) + { + Delete( DEFAULT_PROGRAM_NAME, index, index ); + } + + public virtual void Delete( int from, int to ) + { + Delete( DEFAULT_PROGRAM_NAME, from, to ); + } + + public virtual void Delete( IToken indexT ) + { + Delete( DEFAULT_PROGRAM_NAME, indexT, indexT ); + } + + public virtual void Delete( IToken from, IToken to ) + { + Delete( DEFAULT_PROGRAM_NAME, from, to ); + } + + public virtual void Delete( string programName, int from, int to ) + { + Replace( programName, from, to, null ); + } + + public virtual void Delete( string programName, IToken from, IToken to ) + { + Replace( programName, from, to, null ); + } + + public virtual int GetLastRewriteTokenIndex() + { + return GetLastRewriteTokenIndex( DEFAULT_PROGRAM_NAME ); + } + + protected virtual int GetLastRewriteTokenIndex( string programName ) + { + int value; + if ( lastRewriteTokenIndexes.TryGetValue( programName, out value ) ) + return value; + + return -1; + } + + protected virtual void SetLastRewriteTokenIndex( string programName, int i ) + { + lastRewriteTokenIndexes[programName] = i; + } + + protected virtual IList<RewriteOperation> GetProgram( string name ) + { + IList<RewriteOperation> @is; + if ( !programs.TryGetValue( name, out @is ) || @is == null ) + { + @is = InitializeProgram( name ); + } + return @is; + } + + private IList<RewriteOperation> InitializeProgram( string name ) + { + IList<RewriteOperation> @is = new List<RewriteOperation>( PROGRAM_INIT_SIZE ); + programs[name] = @is; + return @is; + } + + public virtual string ToOriginalString() + { + return ToOriginalString( MIN_TOKEN_INDEX, Size() - 1 ); + } + + public virtual string ToOriginalString( int start, int end ) + { + StringBuilder buf = new StringBuilder(); + for ( int i = start; i >= MIN_TOKEN_INDEX && i <= end && i < tokens.Count; i++ ) + { + buf.Append( Get( i ).Text ); + } + return buf.ToString(); + } + + public override string ToString() + { + return ToString( MIN_TOKEN_INDEX, Size() - 1 ); + } + + public virtual string ToString( string programName ) + { + return ToString( programName, MIN_TOKEN_INDEX, Size() - 1 ); + } + + public override string ToString( int start, int end ) + { + return ToString( DEFAULT_PROGRAM_NAME, start, end ); + } + + public virtual string ToString( string programName, int start, int end ) + { + IList<RewriteOperation> rewrites; + if ( !programs.TryGetValue( programName, out rewrites ) ) + rewrites = null; + + // ensure start/end are in range + if ( end > tokens.Count - 1 ) + end = tokens.Count - 1; + if ( start < 0 ) + start = 0; + + if ( rewrites == null || rewrites.Count == 0 ) + { + return ToOriginalString( start, end ); // no instructions to execute + } + StringBuilder buf = new StringBuilder(); + + // First, optimize instruction stream + IDictionary<int, RewriteOperation> indexToOp = ReduceToSingleOperationPerIndex( rewrites ); + + // Walk buffer, executing instructions and emitting tokens + int i = start; + while ( i <= end && i < tokens.Count ) + { + RewriteOperation op; + bool exists = indexToOp.TryGetValue( i, out op ); + + if ( exists ) + { + // remove so any left have index size-1 + indexToOp.Remove( i ); + } + + if ( !exists || op == null ) + { + IToken t = tokens[i]; + // no operation at that index, just dump token + buf.Append( t.Text ); + i++; // move to next token + } + else + { + i = op.Execute( buf ); // execute operation and skip + } + } + + // include stuff after end if it's last index in buffer + // So, if they did an insertAfter(lastValidIndex, "foo"), include + // foo if end==lastValidIndex. + if ( end == tokens.Count - 1 ) + { + // Scan any remaining operations after last token + // should be included (they will be inserts). + foreach ( RewriteOperation op in indexToOp.Values ) + { + if ( op.index >= tokens.Count - 1 ) + buf.Append( op.text ); + } + } + return buf.ToString(); + } + + /** We need to combine operations and report invalid operations (like + * overlapping replaces that are not completed nested). Inserts to + * same index need to be combined etc... Here are the cases: + * + * I.i.u I.j.v leave alone, nonoverlapping + * I.i.u I.i.v combine: Iivu + * + * R.i-j.u R.x-y.v | i-j in x-y delete first R + * R.i-j.u R.i-j.v delete first R + * R.i-j.u R.x-y.v | x-y in i-j ERROR + * R.i-j.u R.x-y.v | boundaries overlap ERROR + * + * I.i.u R.x-y.v | i in x-y delete I + * I.i.u R.x-y.v | i not in x-y leave alone, nonoverlapping + * R.x-y.v I.i.u | i in x-y ERROR + * R.x-y.v I.x.u R.x-y.uv (combine, delete I) + * R.x-y.v I.i.u | i not in x-y leave alone, nonoverlapping + * + * I.i.u = insert u before op @ index i + * R.x-y.u = replace x-y indexed tokens with u + * + * First we need to examine replaces. For any replace op: + * + * 1. wipe out any insertions before op within that range. + * 2. Drop any replace op before that is contained completely within + * that range. + * 3. Throw exception upon boundary overlap with any previous replace. + * + * Then we can deal with inserts: + * + * 1. for any inserts to same index, combine even if not adjacent. + * 2. for any prior replace with same left boundary, combine this + * insert with replace and delete this replace. + * 3. throw exception if index in same range as previous replace + * + * Don't actually delete; make op null in list. Easier to walk list. + * Later we can throw as we add to index -> op map. + * + * Note that I.2 R.2-2 will wipe out I.2 even though, technically, the + * inserted stuff would be before the replace range. But, if you + * add tokens in front of a method body '{' and then delete the method + * body, I think the stuff before the '{' you added should disappear too. + * + * Return a map from token index to operation. + */ + protected virtual IDictionary<int, RewriteOperation> ReduceToSingleOperationPerIndex( IList<RewriteOperation> rewrites ) + { + //System.out.println("rewrites="+rewrites); + + // WALK REPLACES + for ( int i = 0; i < rewrites.Count; i++ ) + { + RewriteOperation op = rewrites[i]; + if ( op == null ) + continue; + if ( !( op is ReplaceOp ) ) + continue; + ReplaceOp rop = (ReplaceOp)rewrites[i]; + // Wipe prior inserts within range + var inserts = GetKindOfOps( rewrites, typeof( InsertBeforeOp ), i ); + for ( int j = 0; j < inserts.Count; j++ ) + { + InsertBeforeOp iop = (InsertBeforeOp)inserts[j]; + if ( iop.index >= rop.index && iop.index <= rop.lastIndex ) + { + // delete insert as it's a no-op. + rewrites[iop.instructionIndex] = null; + } + } + // Drop any prior replaces contained within + var prevReplaces = GetKindOfOps( rewrites, typeof( ReplaceOp ), i ); + for ( int j = 0; j < prevReplaces.Count; j++ ) + { + ReplaceOp prevRop = (ReplaceOp)prevReplaces[j]; + if ( prevRop.index >= rop.index && prevRop.lastIndex <= rop.lastIndex ) + { + // delete replace as it's a no-op. + rewrites[prevRop.instructionIndex] = null; + continue; + } + // throw exception unless disjoint or identical + bool disjoint = + prevRop.lastIndex < rop.index || prevRop.index > rop.lastIndex; + bool same = + prevRop.index == rop.index && prevRop.lastIndex == rop.lastIndex; + if ( !disjoint && !same ) + { + throw new ArgumentException( "replace op boundaries of " + rop + + " overlap with previous " + prevRop ); + } + } + } + + // WALK INSERTS + for ( int i = 0; i < rewrites.Count; i++ ) + { + RewriteOperation op = (RewriteOperation)rewrites[i]; + if ( op == null ) + continue; + if ( !( op is InsertBeforeOp ) ) + continue; + InsertBeforeOp iop = (InsertBeforeOp)rewrites[i]; + // combine current insert with prior if any at same index + var prevInserts = GetKindOfOps( rewrites, typeof( InsertBeforeOp ), i ); + for ( int j = 0; j < prevInserts.Count; j++ ) + { + InsertBeforeOp prevIop = (InsertBeforeOp)prevInserts[j]; + if ( prevIop.index == iop.index ) + { // combine objects + // convert to strings...we're in process of toString'ing + // whole token buffer so no lazy eval issue with any templates + iop.text = CatOpText( iop.text, prevIop.text ); + // delete redundant prior insert + rewrites[prevIop.instructionIndex] = null; + } + } + // look for replaces where iop.index is in range; error + var prevReplaces = GetKindOfOps( rewrites, typeof( ReplaceOp ), i ); + for ( int j = 0; j < prevReplaces.Count; j++ ) + { + ReplaceOp rop = (ReplaceOp)prevReplaces[j]; + if ( iop.index == rop.index ) + { + rop.text = CatOpText( iop.text, rop.text ); + rewrites[i] = null; // delete current insert + continue; + } + if ( iop.index >= rop.index && iop.index <= rop.lastIndex ) + { + throw new ArgumentException( "insert op " + iop + + " within boundaries of previous " + rop ); + } + } + } + // System.out.println("rewrites after="+rewrites); + IDictionary<int, RewriteOperation> m = new Dictionary<int, RewriteOperation>(); + for ( int i = 0; i < rewrites.Count; i++ ) + { + RewriteOperation op = (RewriteOperation)rewrites[i]; + if ( op == null ) + continue; // ignore deleted ops + + RewriteOperation existing; + if ( m.TryGetValue( op.index, out existing ) && existing != null ) + { + throw new Exception( "should only be one op per index" ); + } + m[op.index] = op; + } + //System.out.println("index to op: "+m); + return m; + } + + protected virtual string CatOpText( object a, object b ) + { + return string.Concat( a, b ); + } + protected virtual IList<RewriteOperation> GetKindOfOps( IList<RewriteOperation> rewrites, Type kind ) + { + return GetKindOfOps( rewrites, kind, rewrites.Count ); + } + + /** <summary>Get all operations before an index of a particular kind</summary> */ + protected virtual IList<RewriteOperation> GetKindOfOps( IList<RewriteOperation> rewrites, Type kind, int before ) + { + IList<RewriteOperation> ops = new List<RewriteOperation>(); + for ( int i = 0; i < before && i < rewrites.Count; i++ ) + { + RewriteOperation op = rewrites[i]; + if ( op == null ) + continue; // ignore deleted + if ( op.GetType() == kind ) + ops.Add( op ); + } + return ops; + } + + public virtual string ToDebugString() + { + return ToDebugString( MIN_TOKEN_INDEX, Size() - 1 ); + } + + public virtual string ToDebugString( int start, int end ) + { + StringBuilder buf = new StringBuilder(); + for ( int i = start; i >= MIN_TOKEN_INDEX && i <= end && i < tokens.Count; i++ ) + { + buf.Append( Get( i ) ); + } + return buf.ToString(); + } + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTree.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTree.cs new file mode 100644 index 0000000..8a51a11 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTree.cs @@ -0,0 +1,509 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Tree +{ + using System; + using System.Collections.Generic; + + using StringBuilder = System.Text.StringBuilder; + + /** <summary> + * A generic tree implementation with no payload. You must subclass to + * actually have any user data. ANTLR v3 uses a list of children approach + * instead of the child-sibling approach in v2. A flat tree (a list) is + * an empty node whose children represent the list. An empty, but + * non-null node is called "nil". + * </summary> + */ + [System.Serializable] + public abstract class BaseTree : ITree + { + protected List<ITree> children; + + public BaseTree() + { + } + + /** <summary> + * Create a new node from an existing node does nothing for BaseTree + * as there are no fields other than the children list, which cannot + * be copied as the children are not considered part of this node. + * </summary> + */ + public BaseTree( ITree node ) + { + } + + public virtual IList<ITree> Children + { + get + { + return children; + } + } + + #region ITree Members + + public virtual int ChildCount + { + get + { + if ( children == null ) + return 0; + + return children.Count; + } + } + + /** <summary>BaseTree doesn't track parent pointers.</summary> */ + public virtual ITree Parent + { + get + { + return null; + } + set + { + } + } + + /** <summary>BaseTree doesn't track child indexes.</summary> */ + public virtual int ChildIndex + { + get + { + return 0; + } + set + { + } + } + + public virtual bool IsNil + { + get + { + return false; + } + } + + public abstract int TokenStartIndex + { + get; + set; + } + + public abstract int TokenStopIndex + { + get; + set; + } + + public abstract int Type + { + get; + set; + } + + public abstract string Text + { + get; + set; + } + + public virtual int Line + { + get; + set; + } + + public virtual int CharPositionInLine + { + get; + set; + } + + #endregion + + public virtual ITree GetChild( int i ) + { + if ( children == null || i >= children.Count ) + return null; + + return children[i]; + } + + /** <summary> + * Get the children internal List; note that if you directly mess with + * the list, do so at your own risk. + * </summary> + */ + [Obsolete] + public IList<ITree> GetChildren() + { + return Children; + } + + public virtual ITree GetFirstChildWithType( int type ) + { + foreach ( ITree child in children ) + { + if ( child.Type == type ) + return child; + } + + return null; + } + + /** <summary>Add t as child of this node.</summary> + * + * <remarks> + * Warning: if t has no children, but child does + * and child isNil then this routine moves children to t via + * t.children = child.children; i.e., without copying the array. + * </remarks> + */ + public virtual void AddChild( ITree t ) + { + //System.out.println("add child "+t.toStringTree()+" "+this.toStringTree()); + //System.out.println("existing children: "+children); + if ( t == null ) + { + return; // do nothing upon addChild(null) + } + if ( t.IsNil ) + { + // t is an empty node possibly with children + BaseTree childTree = t as BaseTree; + if ( childTree != null && this.children != null && this.children == childTree.children ) + { + throw new Exception( "attempt to add child list to itself" ); + } + // just add all of childTree's children to this + if ( t.ChildCount > 0 ) + { + if ( this.children != null || childTree == null ) + { + if ( this.children == null ) + this.children = CreateChildrenList(); + + // must copy, this has children already + int n = t.ChildCount; + for ( int i = 0; i < n; i++ ) + { + ITree c = t.GetChild( i ); + this.children.Add( c ); + // handle double-link stuff for each child of nil root + c.Parent = this; + c.ChildIndex = children.Count - 1; + } + } + else + { + // no children for this but t is a BaseTree with children; + // just set pointer call general freshener routine + this.children = childTree.children; + this.FreshenParentAndChildIndexes(); + } + } + } + else + { + // child is not nil (don't care about children) + if ( children == null ) + { + children = CreateChildrenList(); // create children list on demand + } + children.Add( t ); + t.Parent = this; + t.ChildIndex = children.Count - 1; + } + // System.out.println("now children are: "+children); + } + + /** <summary>Add all elements of kids list as children of this node</summary> */ + public virtual void AddChildren( IEnumerable<ITree> kids ) + { + foreach ( ITree t in kids ) + AddChild( t ); + } + + public virtual void SetChild( int i, ITree t ) + { + if ( t == null ) + { + return; + } + if ( t.IsNil ) + { + throw new ArgumentException( "Can't set single child to a list" ); + } + if ( children == null ) + { + children = CreateChildrenList(); + } + children[i] = t; + t.Parent = this; + t.ChildIndex = i; + } + + public virtual object DeleteChild( int i ) + { + if ( children == null ) + return null; + + ITree killed = children[i]; + children.RemoveAt( i ); + // walk rest and decrement their child indexes + this.FreshenParentAndChildIndexes( i ); + return killed; + } + + /** <summary> + * Delete children from start to stop and replace with t even if t is + * a list (nil-root tree). num of children can increase or decrease. + * For huge child lists, inserting children can force walking rest of + * children to set their childindex; could be slow. + * </summary> + */ + public virtual void ReplaceChildren( int startChildIndex, int stopChildIndex, object t ) + { + /* + System.out.println("replaceChildren "+startChildIndex+", "+stopChildIndex+ + " with "+((BaseTree)t).toStringTree()); + System.out.println("in="+toStringTree()); + */ + if ( children == null ) + { + throw new ArgumentException( "indexes invalid; no children in list" ); + } + int replacingHowMany = stopChildIndex - startChildIndex + 1; + int replacingWithHowMany; + ITree newTree = (ITree)t; + List<ITree> newChildren = null; + // normalize to a list of children to add: newChildren + if ( newTree.IsNil ) + { + BaseTree baseTree = newTree as BaseTree; + if ( baseTree != null ) + { + newChildren = baseTree.children; + } + else + { + newChildren = CreateChildrenList(); + int n = newTree.ChildCount; + for ( int i = 0; i < n; i++ ) + newChildren.Add( newTree.GetChild( i ) ); + } + } + else + { + newChildren = new List<ITree>( 1 ); + newChildren.Add( newTree ); + } + replacingWithHowMany = newChildren.Count; + int numNewChildren = newChildren.Count; + int delta = replacingHowMany - replacingWithHowMany; + // if same number of nodes, do direct replace + if ( delta == 0 ) + { + int j = 0; // index into new children + for ( int i = startChildIndex; i <= stopChildIndex; i++ ) + { + ITree child = newChildren[j]; + children[i] = child; + child.Parent = this; + child.ChildIndex = i; + j++; + } + } + else if ( delta > 0 ) + { + // fewer new nodes than there were + // set children and then delete extra + for ( int j = 0; j < numNewChildren; j++ ) + { + children[startChildIndex + j] = newChildren[j]; + } + int indexToDelete = startChildIndex + numNewChildren; + for ( int c = indexToDelete; c <= stopChildIndex; c++ ) + { + // delete same index, shifting everybody down each time + children.RemoveAt( indexToDelete ); + } + FreshenParentAndChildIndexes( startChildIndex ); + } + else + { + // more new nodes than were there before + // fill in as many children as we can (replacingHowMany) w/o moving data + for ( int j = 0; j < replacingHowMany; j++ ) + { + children[startChildIndex + j] = newChildren[j]; + } + int numToInsert = replacingWithHowMany - replacingHowMany; + for ( int j = replacingHowMany; j < replacingWithHowMany; j++ ) + { + children.Insert( startChildIndex + j, newChildren[j] ); + } + FreshenParentAndChildIndexes( startChildIndex ); + } + //System.out.println("out="+toStringTree()); + } + + /** <summary>Override in a subclass to change the impl of children list</summary> */ + protected virtual List<ITree> CreateChildrenList() + { + return new List<ITree>(); + } + + /** <summary>Set the parent and child index values for all child of t</summary> */ + public virtual void FreshenParentAndChildIndexes() + { + FreshenParentAndChildIndexes( 0 ); + } + + public virtual void FreshenParentAndChildIndexes( int offset ) + { + int n = ChildCount; + for ( int c = offset; c < n; c++ ) + { + ITree child = GetChild( c ); + child.ChildIndex = c; + child.Parent = this; + } + } + + public virtual void SanityCheckParentAndChildIndexes() + { + SanityCheckParentAndChildIndexes( null, -1 ); + } + + public virtual void SanityCheckParentAndChildIndexes( ITree parent, int i ) + { + if ( parent != this.Parent ) + { + throw new InvalidOperationException( "parents don't match; expected " + parent + " found " + this.Parent ); + } + if ( i != this.ChildIndex ) + { + throw new InvalidOperationException( "child indexes don't match; expected " + i + " found " + this.ChildIndex ); + } + int n = this.ChildCount; + for ( int c = 0; c < n; c++ ) + { + BaseTree child = (BaseTree)this.GetChild( c ); + child.SanityCheckParentAndChildIndexes( this, c ); + } + } + + /** <summary>Walk upwards looking for ancestor with this token type.</summary> */ + public virtual bool HasAncestor( int ttype ) + { + return GetAncestor( ttype ) != null; + } + + /** <summary>Walk upwards and get first ancestor with this token type.</summary> */ + public virtual ITree GetAncestor( int ttype ) + { + ITree t = this; + t = t.Parent; + while ( t != null ) + { + if ( t.Type == ttype ) + return t; + t = t.Parent; + } + return null; + } + + /** <summary> + * Return a list of all ancestors of this node. The first node of + * list is the root and the last is the parent of this node. + * </summary> + */ + public virtual IList<ITree> GetAncestors() + { + if ( Parent == null ) + return null; + + List<ITree> ancestors = new List<ITree>(); + ITree t = this; + t = t.Parent; + while ( t != null ) + { + ancestors.Insert( 0, t ); // insert at start + t = t.Parent; + } + return ancestors; + } + + /** <summary>Print out a whole tree not just a node</summary> */ + public virtual string ToStringTree() + { + if ( children == null || children.Count == 0 ) + { + return this.ToString(); + } + StringBuilder buf = new StringBuilder(); + if ( !IsNil ) + { + buf.Append( "(" ); + buf.Append( this.ToString() ); + buf.Append( ' ' ); + } + for ( int i = 0; children != null && i < children.Count; i++ ) + { + ITree t = children[i]; + if ( i > 0 ) + { + buf.Append( ' ' ); + } + buf.Append( t.ToStringTree() ); + } + if ( !IsNil ) + { + buf.Append( ")" ); + } + return buf.ToString(); + } + + /** <summary>Override to say how a node (not a tree) should look as text</summary> */ + public override abstract string ToString(); + + #region Tree Members + public abstract ITree DupNode(); + #endregion + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTreeAdaptor.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTreeAdaptor.cs new file mode 100644 index 0000000..a0cace3 --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTreeAdaptor.cs @@ -0,0 +1,348 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Tree +{ + using System.Collections.Generic; + + using Exception = System.Exception; + using IDictionary = System.Collections.IDictionary; + using NotSupportedException = System.NotSupportedException; + + /** <summary>A TreeAdaptor that works with any Tree implementation.</summary> */ + public abstract class BaseTreeAdaptor : ITreeAdaptor + { + /** <summary> + * System.identityHashCode() is not always unique; we have to + * track ourselves. That's ok, it's only for debugging, though it's + * expensive: we have to create a hashtable with all tree nodes in it. + * </summary> + */ + protected IDictionary<object, int> treeToUniqueIDMap; + protected int uniqueNodeID = 1; + + public virtual object Nil() + { + return Create( null ); + } + + /** <summary> + * Create tree node that holds the start and stop tokens associated + * with an error. + * </summary> + * + * <remarks> + * If you specify your own kind of tree nodes, you will likely have to + * override this method. CommonTree returns Token.INVALID_TOKEN_TYPE + * if no token payload but you might have to set token type for diff + * node type. + * + * You don't have to subclass CommonErrorNode; you will likely need to + * subclass your own tree node class to avoid class cast exception. + * </remarks> + */ + public virtual object ErrorNode( ITokenStream input, IToken start, IToken stop, + RecognitionException e ) + { + CommonErrorNode t = new CommonErrorNode( input, start, stop, e ); + //System.out.println("returning error node '"+t+"' @index="+input.index()); + return t; + } + + public virtual bool IsNil( object tree ) + { + return ( (ITree)tree ).IsNil; + } + + public virtual object DupTree( object tree ) + { + return DupTree( tree, null ); + } + + /** <summary> + * This is generic in the sense that it will work with any kind of + * tree (not just ITree interface). It invokes the adaptor routines + * not the tree node routines to do the construction. + * </summary> + */ + public virtual object DupTree( object t, object parent ) + { + if ( t == null ) + { + return null; + } + object newTree = DupNode( t ); + // ensure new subtree root has parent/child index set + SetChildIndex( newTree, GetChildIndex( t ) ); // same index in new tree + SetParent( newTree, parent ); + int n = GetChildCount( t ); + for ( int i = 0; i < n; i++ ) + { + object child = GetChild( t, i ); + object newSubTree = DupTree( child, t ); + AddChild( newTree, newSubTree ); + } + return newTree; + } + + /** <summary> + * Add a child to the tree t. If child is a flat tree (a list), make all + * in list children of t. Warning: if t has no children, but child does + * and child isNil then you can decide it is ok to move children to t via + * t.children = child.children; i.e., without copying the array. Just + * make sure that this is consistent with have the user will build + * ASTs. + * </summary> + */ + public virtual void AddChild( object t, object child ) + { + if ( t != null && child != null ) + { + ( (ITree)t ).AddChild( (ITree)child ); + } + } + + /** <summary> + * If oldRoot is a nil root, just copy or move the children to newRoot. + * If not a nil root, make oldRoot a child of newRoot. + * </summary> + * + * <remarks> + * old=^(nil a b c), new=r yields ^(r a b c) + * old=^(a b c), new=r yields ^(r ^(a b c)) + * + * If newRoot is a nil-rooted single child tree, use the single + * child as the new root node. + * + * old=^(nil a b c), new=^(nil r) yields ^(r a b c) + * old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) + * + * If oldRoot was null, it's ok, just return newRoot (even if isNil). + * + * old=null, new=r yields r + * old=null, new=^(nil r) yields ^(nil r) + * + * Return newRoot. Throw an exception if newRoot is not a + * simple node or nil root with a single child node--it must be a root + * node. If newRoot is ^(nil x) return x as newRoot. + * + * Be advised that it's ok for newRoot to point at oldRoot's + * children; i.e., you don't have to copy the list. We are + * constructing these nodes so we should have this control for + * efficiency. + * </remarks> + */ + public virtual object BecomeRoot( object newRoot, object oldRoot ) + { + //System.out.println("becomeroot new "+newRoot.toString()+" old "+oldRoot); + ITree newRootTree = (ITree)newRoot; + ITree oldRootTree = (ITree)oldRoot; + if ( oldRoot == null ) + { + return newRoot; + } + // handle ^(nil real-node) + if ( newRootTree.IsNil ) + { + int nc = newRootTree.ChildCount; + if ( nc == 1 ) + newRootTree = (ITree)newRootTree.GetChild( 0 ); + else if ( nc > 1 ) + { + // TODO: make tree run time exceptions hierarchy + throw new Exception( "more than one node as root (TODO: make exception hierarchy)" ); + } + } + // add oldRoot to newRoot; addChild takes care of case where oldRoot + // is a flat list (i.e., nil-rooted tree). All children of oldRoot + // are added to newRoot. + newRootTree.AddChild( oldRootTree ); + return newRootTree; + } + + /** <summary>Transform ^(nil x) to x and nil to null</summary> */ + public virtual object RulePostProcessing( object root ) + { + //System.out.println("rulePostProcessing: "+((Tree)root).toStringTree()); + ITree r = (ITree)root; + if ( r != null && r.IsNil ) + { + if ( r.ChildCount == 0 ) + { + r = null; + } + else if ( r.ChildCount == 1 ) + { + r = (ITree)r.GetChild( 0 ); + // whoever invokes rule will set parent and child index + r.Parent = null; + r.ChildIndex = -1; + } + } + return r; + } + + public virtual object BecomeRoot( IToken newRoot, object oldRoot ) + { + return BecomeRoot( Create( newRoot ), oldRoot ); + } + + public virtual object Create( int tokenType, IToken fromToken ) + { + fromToken = CreateToken( fromToken ); + //((ClassicToken)fromToken).setType(tokenType); + fromToken.Type = tokenType; + ITree t = (ITree)Create( fromToken ); + return t; + } + + public virtual object Create( int tokenType, IToken fromToken, string text ) + { + fromToken = CreateToken( fromToken ); + fromToken.Type = tokenType; + fromToken.Text = text; + ITree t = (ITree)Create( fromToken ); + return t; + } + + public virtual object Create( int tokenType, string text ) + { + IToken fromToken = CreateToken( tokenType, text ); + ITree t = (ITree)Create( fromToken ); + return t; + } + + public virtual int GetType( object t ) + { + return ( (ITree)t ).Type; + } + + public virtual void SetType( object t, int type ) + { + throw new NotSupportedException( "don't know enough about Tree node" ); + } + + public virtual string GetText( object t ) + { + return ( (ITree)t ).Text; + } + + public virtual void SetText( object t, string text ) + { + throw new NotSupportedException( "don't know enough about Tree node" ); + } + + public virtual object GetChild( object t, int i ) + { + return ( (ITree)t ).GetChild( i ); + } + + public virtual void SetChild( object t, int i, object child ) + { + ( (ITree)t ).SetChild( i, (ITree)child ); + } + + public virtual object DeleteChild( object t, int i ) + { + return ( (ITree)t ).DeleteChild( i ); + } + + public virtual int GetChildCount( object t ) + { + return ( (ITree)t ).ChildCount; + } + + public virtual int GetUniqueID( object node ) + { + if ( treeToUniqueIDMap == null ) + { + treeToUniqueIDMap = new Dictionary<object, int>(); + } + int id; + if ( treeToUniqueIDMap.TryGetValue( node, out id ) ) + return id; + + id = uniqueNodeID; + treeToUniqueIDMap[node] = id; + uniqueNodeID++; + return id; + // GC makes these nonunique: + // return System.identityHashCode(node); + } + + /** <summary> + * Tell me how to create a token for use with imaginary token nodes. + * For example, there is probably no input symbol associated with imaginary + * token DECL, but you need to create it as a payload or whatever for + * the DECL node as in ^(DECL type ID). + * </summary> + * + * <remarks> + * If you care what the token payload objects' type is, you should + * override this method and any other createToken variant. + * </remarks> + */ + public abstract IToken CreateToken( int tokenType, string text ); + + /** <summary> + * Tell me how to create a token for use with imaginary token nodes. + * For example, there is probably no input symbol associated with imaginary + * token DECL, but you need to create it as a payload or whatever for + * the DECL node as in ^(DECL type ID). + * </summary> + * + * <remarks> + * This is a variant of createToken where the new token is derived from + * an actual real input token. Typically this is for converting '{' + * tokens to BLOCK etc... You'll see + * + * r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ; + * + * If you care what the token payload objects' type is, you should + * override this method and any other createToken variant. + * </remarks> + */ + public abstract IToken CreateToken( IToken fromToken ); + + public abstract object Create( IToken payload ); + public abstract object DupNode( object treeNode ); + public abstract IToken GetToken( object t ); + public abstract void SetTokenBoundaries( object t, IToken startToken, IToken stopToken ); + public abstract int GetTokenStartIndex( object t ); + public abstract int GetTokenStopIndex( object t ); + public abstract object GetParent( object t ); + public abstract void SetParent( object t, object parent ); + public abstract int GetChildIndex( object t ); + public abstract void SetChildIndex( object t, int index ); + public abstract void ReplaceChildren( object parent, int startChildIndex, int stopChildIndex, object t ); + } +} diff --git a/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BufferedTreeNodeStream.cs b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BufferedTreeNodeStream.cs new file mode 100644 index 0000000..699903f --- /dev/null +++ b/antlr-3.1.3/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BufferedTreeNodeStream.cs @@ -0,0 +1,627 @@ +/* + * [The "BSD licence"] + * Copyright (c) 2005-2008 Terence Parr + * All rights reserved. + * + * Conversion to C#: + * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. + * 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. + */ + +namespace Antlr.Runtime.Tree +{ + using System.Collections.Generic; + + using Console = System.Console; + using IList = System.Collections.IList; + using StringBuilder = System.Text.StringBuilder; + + /** <summary>A buffered stream of tree nodes. Nodes can be from a tree of ANY kind.</summary> + * + * This node stream sucks all nodes out of the tree specified in + * the constructor during construction and makes pointers into + * the tree using an array of Object pointers. The stream necessarily + * includes pointers to DOWN and UP and EOF nodes. + * + * This stream knows how to mark/release for backtracking. + * + * This stream is most suitable for tree interpreters that need to + * jump around a lot or for tree parsers requiring speed (at cost of memory). + * There is some duplicated functionality here with UnBufferedTreeNodeStream + * but just in bookkeeping, not tree walking etc... + * + * TARGET DEVELOPERS: + * + * This is the old CommonTreeNodeStream that buffered up entire node stream. + * No need to implement really as new CommonTreeNodeStream is much better + * and covers what we need. + * + * @see CommonTreeNodeStream + */ + public class BufferedTreeNodeStream : ITreeNodeStream + { + public const int DEFAULT_INITIAL_BUFFER_SIZE = 100; + public const int INITIAL_CALL_STACK_SIZE = 10; + + protected sealed class StreamIterator : IEnumerator<object> + { + BufferedTreeNodeStream _outer; + int _index; + + public StreamIterator( BufferedTreeNodeStream outer ) + { + _outer = outer; + _index = -1; + } + + #region IEnumerator<object> Members + + public object Current + { + get + { + if ( _index < _outer.nodes.Count ) + return _outer.nodes[_index]; + + return _outer.eof; + } + } + + #endregion + + #region IDisposable Members + + public void Dispose() + { + } + + #endregion + + #region IEnumerator Members + + public bool MoveNext() + { + if ( _index < _outer.nodes.Count ) + _index++; + + return _index < _outer.nodes.Count; + } + + public void Reset() + { + _index = -1; + } + + #endregion + } + + // all these navigation nodes are shared and hence they + // cannot contain any line/column info + + protected object down; + protected object up; + protected object eof; + + /** <summary>The complete mapping from stream index to tree node. + * This buffer includes pointers to DOWN, UP, and EOF nodes. + * It is built upon ctor invocation. The elements are type + * Object as we don't what the trees look like.</summary> + * + * Load upon first need of the buffer so we can set token types + * of interest for reverseIndexing. Slows us down a wee bit to + * do all of the if p==-1 testing everywhere though. + */ + protected IList nodes; + + /** <summary>Pull nodes from which tree?</summary> */ + protected object root; + + /** <summary>IF this tree (root) was created from a token stream, track it.</summary> */ + protected ITokenStream tokens; + + /** <summary>What tree adaptor was used to build these trees</summary> */ + ITreeAdaptor adaptor; + + /** <summary>Reuse same DOWN, UP navigation nodes unless this is true</summary> */ + protected bool uniqueNavigationNodes = false; + + /** <summary>The index into the nodes list of the current node (next node + * to consume). If -1, nodes array not filled yet.</summary> + */ + protected int p = -1; + + /** <summary>Track the last mark() call result value for use in rewind().</summary> */ + protected int lastMarker; + + /** <summary>Stack of indexes used for push/pop calls</summary> */ + protected Stack<int> calls; + + public BufferedTreeNodeStream( object tree ) + : this( new CommonTreeAdaptor(), tree ) + { + } + + public BufferedTreeNodeStream( ITreeAdaptor adaptor, object tree ) + : this( adaptor, tree, DEFAULT_INITIAL_BUFFER_SIZE ) + { + } + + public BufferedTreeNodeStream( ITreeAdaptor adaptor, object tree, int initialBufferSize ) + { + this.root = tree; + this.adaptor = adaptor; + nodes = new List<object>( initialBufferSize ); + down = adaptor.Create( TokenConstants.DOWN, "DOWN" ); + up = adaptor.Create( TokenConstants.UP, "UP" ); + eof = adaptor.Create( TokenConstants.EOF, "EOF" ); + } + + #region Properties + + public virtual object TreeSource + { + get + { + return root; + } + } + + public virtual string SourceName + { + get + { + return TokenStream.SourceName; + } + } + + public virtual ITokenStream TokenStream + { + get + { + return tokens; + } + set + { + tokens = value; + } + } + + public virtual ITreeAdaptor TreeAdaptor + { + get + { + return adaptor; + } + set + { + adaptor = value; + } + } + + public virtual bool UniqueNavigationNodes + { + get + { + return uniqueNavigationNodes; + } + set + { + uniqueNavigationNodes = value; + } + } + + #endregion + + /** Walk tree with depth-first-search and fill nodes buffer. + * Don't do DOWN, UP nodes if its a list (t is isNil). + */ + protected virtual void FillBuffer() + { + FillBuffer( root ); + //Console.Out.WriteLine( "revIndex=" + tokenTypeToStreamIndexesMap ); + p = 0; // buffer of nodes intialized now + } + + public virtual void FillBuffer( object t ) + { + bool nil = adaptor.IsNil( t ); + if ( !nil ) + { + nodes.Add( t ); // add this node + } + // add DOWN node if t has children + int n = adaptor.GetChildCount( t ); + if ( !nil && n > 0 ) + { + AddNavigationNode( TokenConstants.DOWN ); + } + // and now add all its children + for ( int c = 0; c < n; c++ ) + { + object child = adaptor.GetChild( t, c ); + FillBuffer( child ); + } + // add UP node if t has children + if ( !nil && n > 0 ) + { + AddNavigationNode( TokenConstants.UP ); + } + } + + /** What is the stream index for node? 0..n-1 + * Return -1 if node not found. + */ + protected virtual int GetNodeIndex( object node ) + { + if ( p == -1 ) + { + FillBuffer(); + } + for ( int i = 0; i < nodes.Count; i++ ) + { + object t = nodes[i]; + if ( t == node ) + { + return i; + } + } + return -1; + } + + /** As we flatten the tree, we use UP, DOWN nodes to represent + * the tree structure. When debugging we need unique nodes + * so instantiate new ones when uniqueNavigationNodes is true. + */ + protected virtual void AddNavigationNode( int ttype ) + { + object navNode = null; + if ( ttype == TokenConstants.DOWN ) + { + if ( UniqueNavigationNodes ) + { + navNode = adaptor.Create( TokenConstants.DOWN, "DOWN" ); + } + else + { + navNode = down; + } + } + else + { + if ( UniqueNavigationNodes ) + { + navNode = adaptor.Create( TokenConstants.UP, "UP" ); + } + else + { + navNode = up; + } + } + nodes.Add( navNode ); + } + + public virtual object this[int i] + { + get + { + if ( p == -1 ) + { + FillBuffer(); + } + return nodes[i]; + } + } + + public virtual object LT( int k ) + { + if ( p == -1 ) + { + FillBuffer(); + } + if ( k == 0 ) + { + return null; + } + if ( k < 0 ) + { + return LB( -k ); + } + //System.out.print("LT(p="+p+","+k+")="); + if ( ( p + k - 1 ) >= nodes.Count ) + { + return eof; + } + return nodes[p + k - 1]; + } + + public virtual object GetCurrentSymbol() + { + return LT( 1 ); + } + +#if false + public virtual object getLastTreeNode() + { + int i = Index; + if ( i >= size() ) + { + i--; // if at EOF, have to start one back + } + Console.Out.WriteLine( "start last node: " + i + " size==" + nodes.Count ); + while ( i >= 0 && + ( adaptor.getType( this[i] ) == TokenConstants.EOF || + adaptor.getType( this[i] ) == TokenConstants.UP || + adaptor.getType( this[i] ) == TokenConstants.DOWN ) ) + { + i--; + } + Console.Out.WriteLine( "stop at node: " + i + " " + nodes[i] ); + return nodes[i]; + } +#endif + + /** <summary>Look backwards k nodes</summary> */ + protected virtual object LB( int k ) + { + if ( k == 0 ) + { + return null; + } + if ( ( p - k ) < 0 ) + { + return null; + } + return nodes[p - k]; + } + + public virtual void Consume() + { + if ( p == -1 ) + { + FillBuffer(); + } + p++; + } + + public virtual int LA( int i ) + { + return adaptor.GetType( LT( i ) ); + } + + public virtual int Mark() + { + if ( p == -1 ) + { + FillBuffer(); + } + lastMarker = Index; + return lastMarker; + } + + public virtual void Release( int marker ) + { + // no resources to release + } + + public virtual int Index + { + get + { + return p; + } + } + + public virtual void Rewind( int marker ) + { + Seek( marker ); + } + + public virtual void Rewind() + { + Seek( lastMarker ); + } + + public virtual void Seek( int index ) + { + if ( p == -1 ) + { + FillBuffer(); + } + p = index; + } + + /** <summary> + * Make stream jump to a new location, saving old location. + * Switch back with pop(). + * </summary> + */ + public virtual void Push( int index ) + { + if ( calls == null ) + { + calls = new Stack<int>(); + } + calls.Push( p ); // save current index + Seek( index ); + } + + /** <summary> + * Seek back to previous index saved during last push() call. + * Return top of stack (return index). + * </summary> + */ + public virtual int Pop() + { + int ret = calls.Pop(); + Seek( ret ); + return ret; + } + + public virtual void Reset() + { + p = 0; + lastMarker = 0; + if ( calls != null ) + { + calls.Clear(); + } + } + + public virtual int Size() + { + if ( p == -1 ) + { + FillBuffer(); + } + return nodes.Count; + } + + public virtual IEnumerator<object> Iterator() + { + if ( p == -1 ) + { + FillBuffer(); + } + + return new StreamIterator( this ); + } + + // TREE REWRITE INTERFACE + + public virtual void ReplaceChildren( object parent, int startChildIndex, int stopChildIndex, object t ) + { + if ( parent != null ) + { + adaptor.ReplaceChildren( parent, startChildIndex, stopChildIndex, t ); + } + } + + /** <summary>Used for testing, just return the token type stream</summary> */ + public virtual string ToTokenTypeString() + { + if ( p == -1 ) + { + FillBuffer(); + } + StringBuilder buf = new StringBuilder(); + for ( int i = 0; i < nodes.Count; i++ ) + { + object t = nodes[i]; + buf.Append( " " ); + buf.Append( adaptor.GetType( t ) ); + } + return buf.ToString(); + } + + /** <summary>Debugging</summary> */ + public virtual string ToTokenString( int start, int stop ) + { + if ( p == -1 ) + { + FillBuffer(); + } + StringBuilder buf = new StringBuilder(); + for ( int i = start; i < nodes.Count && i <= stop; i++ ) + { + object t = nodes[i]; + buf.Append( " " ); + buf.Append( adaptor.GetToken( t ) ); + } + return buf.ToString(); + } + + public virtual string ToString( object start, object stop ) + { + Console.Out.WriteLine( "toString" ); + if ( start == null || stop == null ) + { + return null; + } + if ( p == -1 ) + { + FillBuffer(); + } + //Console.Out.WriteLine( "stop: " + stop ); + if ( start is CommonTree ) + Console.Out.Write( "toString: " + ( (CommonTree)start ).Token + ", " ); + else + Console.Out.WriteLine( start ); + if ( stop is CommonTree ) + Console.Out.WriteLine( ( (CommonTree)stop ).Token ); + else + Console.Out.WriteLine( stop ); + // if we have the token stream, use that to dump text in order + if ( tokens != null ) + { + int beginTokenIndex = adaptor.GetTokenStartIndex( start ); + int endTokenIndex = adaptor.GetTokenStopIndex( stop ); + // if it's a tree, use start/stop index from start node + // else use token range from start/stop nodes + if ( adaptor.GetType( stop ) == TokenConstants.UP ) + { + endTokenIndex = adaptor.GetTokenStopIndex( start ); + } + else if ( adaptor.GetType( stop ) == TokenConstants.EOF ) + { + endTo