]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/cpp/DataBoardTest/libantlr3c-3.2/src/antlr3cyclicdfa.c
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / cpp / DataBoardTest / libantlr3c-3.2 / src / antlr3cyclicdfa.c
diff --git a/bundles/org.simantics.databoard/cpp/DataBoardTest/libantlr3c-3.2/src/antlr3cyclicdfa.c b/bundles/org.simantics.databoard/cpp/DataBoardTest/libantlr3c-3.2/src/antlr3cyclicdfa.c
new file mode 100644 (file)
index 0000000..5c4a930
--- /dev/null
@@ -0,0 +1,204 @@
+/** Support functions for traversing cyclic DFA states as laid\r
+ *  out in static initialized structures by the code generator.\r
+ *\r
+ * A DFA implemented as a set of transition tables.\r
+ *\r
+ *  Any state that has a semantic predicate edge is special; those states\r
+ *  are generated with if-then-else structures in a ->specialStateTransition()\r
+ *  which is generated by cyclicDFA template.\r
+ *\r
+ *  There are at most 32767 states (16-bit signed short).\r
+ *  Could get away with byte sometimes but would have to generate different\r
+ *  types and the simulation code too.  For a point of reference, the Java\r
+ *  lexer's Tokens rule DFA has 326 states roughly.\r
+ */\r
+\r
+// [The "BSD licence"]\r
+// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC\r
+// http://www.temporal-wave.com\r
+// http://www.linkedin.com/in/jimidle\r
+//\r
+// All rights reserved.\r
+//\r
+// Redistribution and use in source and binary forms, with or without\r
+// modification, are permitted provided that the following conditions\r
+// are met:\r
+// 1. Redistributions of source code must retain the above copyright\r
+//    notice, this list of conditions and the following disclaimer.\r
+// 2. Redistributions in binary form must reproduce the above copyright\r
+//    notice, this list of conditions and the following disclaimer in the\r
+//    documentation and/or other materials provided with the distribution.\r
+// 3. The name of the author may not be used to endorse or promote products\r
+//    derived from this software without specific prior written permission.\r
+//\r
+// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
+// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
+// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
+// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
+// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+#include    <antlr3defs.h>\r
+#include    <antlr3cyclicdfa.h>\r
+\r
+#ifdef ANTLR3_WINDOWS\r
+#pragma warning( disable : 4100 )\r
+#endif\r
+\r
+static void\r
+noViableAlt(pANTLR3_BASE_RECOGNIZER rec, pANTLR3_CYCLIC_DFA cdfa, ANTLR3_UINT32        s)\r
+{\r
+       // In backtracking mode, we just set the failed flag so that the\r
+       // alt can just exit right now. If we are parsing though, then \r
+       // we want the exception to be raised.\r
+       //\r
+    if (rec->state->backtracking > 0)\r
+    {\r
+               rec->state->failed = ANTLR3_TRUE;\r
+    }\r
+       else\r
+       {\r
+               rec->exConstruct(rec);\r
+               rec->state->exception->type         = ANTLR3_NO_VIABLE_ALT_EXCEPTION;\r
+               rec->state->exception->message      = cdfa->description;\r
+               rec->state->exception->decisionNum  = cdfa->decisionNumber;\r
+               rec->state->exception->state        = s;\r
+       }\r
+}\r
+\r
+/** From the input stream, predict what alternative will succeed\r
+ *  using this DFA (representing the covering regular approximation\r
+ *  to the underlying CFL).  Return an alternative number 1..n.  Throw\r
+ *  an exception upon error.\r
+ */\r
+ANTLR3_API ANTLR3_INT32\r
+antlr3dfapredict (void * ctx, pANTLR3_BASE_RECOGNIZER rec, pANTLR3_INT_STREAM is, pANTLR3_CYCLIC_DFA cdfa)\r
+{\r
+    ANTLR3_MARKER      mark;\r
+    ANTLR3_INT32       s;\r
+    ANTLR3_INT32       specialState;\r
+    ANTLR3_INT32       c;\r
+\r
+    mark       = is->mark(is);     /* Store where we are right now     */\r
+    s          = 0;                /* Always start with state 0        */\r
+    \r
+       for (;;)\r
+       {\r
+               /* Pick out any special state entry for this state\r
+                */\r
+               specialState    = cdfa->special[s];\r
+\r
+               /* Transition the special state and consume an input token\r
+                */\r
+               if  (specialState >= 0)\r
+               {\r
+                       s = cdfa->specialStateTransition(ctx, rec, is, cdfa, specialState);\r
+\r
+                       // Error?\r
+                       //\r
+                       if      (s<0)\r
+                       {\r
+                               // If the predicate/rule raised an exception then we leave it\r
+                               // in tact, else we have an NVA.\r
+                               //\r
+                               if      (rec->state->error != ANTLR3_TRUE)\r
+                               {\r
+                                       noViableAlt(rec,cdfa, s);\r
+                               }\r
+                               is->rewind(is, mark);\r
+                               return  0;\r
+                       }\r
+                       is->consume(is);\r
+                       continue;\r
+               }\r
+\r
+               /* Accept state?\r
+                */\r
+               if  (cdfa->accept[s] >= 1)\r
+               {\r
+                       is->rewind(is, mark);\r
+                       return  cdfa->accept[s];\r
+               }\r
+\r
+               /* Look for a normal transition state based upon the input token element\r
+                */\r
+               c = is->_LA(is, 1);\r
+\r
+               /* Check against min and max for this state\r
+                */\r
+               if  (c>= cdfa->min[s] && c <= cdfa->max[s])\r
+               {\r
+                       ANTLR3_INT32   snext;\r
+\r
+                       /* What is the next state?\r
+                        */\r
+                       snext = cdfa->transition[s][c - cdfa->min[s]];\r
+\r
+                       if      (snext < 0)\r
+                       {\r
+                               /* Was in range but not a normal transition\r
+                                * must check EOT, which is like the else clause.\r
+                                * eot[s]>=0 indicates that an EOT edge goes to another\r
+                                * state.\r
+                                */\r
+                               if  (cdfa->eot[s] >= 0)\r
+                               {\r
+                                       s = cdfa->eot[s];\r
+                                       is->consume(is);\r
+                                       continue;\r
+                               }\r
+                               noViableAlt(rec,cdfa, s);\r
+                               is->rewind(is, mark);\r
+                               return  0;\r
+                       }\r
+\r
+                       /* New current state - move to it\r
+                        */\r
+                       s       = snext;\r
+                       is->consume(is);\r
+                       continue;\r
+               }\r
+               /* EOT Transition?\r
+                */\r
+               if  (cdfa->eot[s] >= 0)\r
+               {\r
+                       s       = cdfa->eot[s];\r
+                       is->consume(is);\r
+                       continue;\r
+               }\r
+               /* EOF transition to accept state?\r
+                */\r
+               if  ( c == ANTLR3_TOKEN_EOF && cdfa->eof[s] >= 0)\r
+               {\r
+                       is->rewind(is, mark);\r
+                       return  cdfa->accept[cdfa->eof[s]];\r
+               }\r
+\r
+               /* No alt, so bomb\r
+                */\r
+               noViableAlt(rec, cdfa, s);\r
+               is->rewind(is, mark);\r
+               return 0;\r
+       }\r
+\r
+}\r
+\r
+/** Default special state implementation\r
+ */\r
+ANTLR3_API ANTLR3_INT32        \r
+antlr3dfaspecialStateTransition   (void * ctx, pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM is, pANTLR3_CYCLIC_DFA dfa, ANTLR3_INT32 s)\r
+{\r
+    return -1;\r
+}\r
+\r
+/* Default special transition implementation\r
+ */\r
+ANTLR3_API ANTLR3_INT32\r
+antlr3dfaspecialTransition    (void * ctx, pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_INT_STREAM is, pANTLR3_CYCLIC_DFA dfa, ANTLR3_INT32 s)\r
+{\r
+    return 0;\r
+}\r