13447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/* 23447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein [The "BSD license"] 33447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Copyright (c) 2005-2009 Terence Parr 43447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein All rights reserved. 53447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 63447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Redistribution and use in source and binary forms, with or without 73447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein modification, are permitted provided that the following conditions 83447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein are met: 93447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1. Redistributions of source code must retain the above copyright 103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein notice, this list of conditions and the following disclaimer. 113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2. Redistributions in binary form must reproduce the above copyright 123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein notice, this list of conditions and the following disclaimer in the 133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein documentation and/or other materials provided with the distribution. 143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 3. The name of the author may not be used to endorse or promote products 153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein derived from this software without specific prior written permission. 163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */package org.antlr.runtime; 283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport java.util.Map; 303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/** The set of fields needed by an abstract recognizer to recognize input 323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * and recover from errors etc... As a separate state object, it can be 333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * shared among multiple grammars; e.g., when one grammar imports another. 343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * These fields are publically visible but the actual state pointer per 363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * parser is protected. 373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpublic class RecognizerSharedState { 393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** Track the set of token types that can follow any rule invocation. 403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * Stack grows upwards. When it hits the max, it grows 2x in size 413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * and keeps going. 423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public BitSet[] following = new BitSet[BaseRecognizer.INITIAL_FOLLOW_STACK_SIZE]; 443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int _fsp = -1; 453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** This is true when we see an error and before having successfully 473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * matched a token. Prevents generation of more than one error message 483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * per error. 493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public boolean errorRecovery = false; 513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** The index into the input stream where the last error occurred. 533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * This is used to prevent infinite loops where an error is found 543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * but no token is consumed during recovery...another error is found, 553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * ad naseum. This is a failsafe mechanism to guarantee that at least 563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * one token/tree node is consumed for two errors. 573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int lastErrorIndex = -1; 593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** In lieu of a return value, this indicates that a rule or token 613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * has failed to match. Reset to false upon valid token match. 623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public boolean failed = false; 643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** Did the recognizer encounter a syntax error? Track how many. */ 663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int syntaxErrors = 0; 673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** If 0, no backtracking is going on. Safe to exec actions etc... 693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * If >0 then it's the level of backtracking. 703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int backtracking = 0; 723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** An array[size num rules] of Map<Integer,Integer> that tracks 743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * the stop token index for each rule. ruleMemo[ruleIndex] is 753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * the memoization table for ruleIndex. For key ruleStartIndex, you 763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * get back the stop token for associated rule or MEMO_RULE_FAILED. 773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * This is only used if rule memoization is on (which it is by default). 793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Map[] ruleMemo; 813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // LEXER FIELDS (must be in same state object to avoid casting 843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // constantly in generated code and Lexer object) :( 853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** The goal of all lexer rules/methods is to create a token object. 883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * This is an instance variable as multiple rules may collaborate to 893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * create a single token. nextToken will return this object after 903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * matching lexer rule(s). If you subclass to allow multiple token 913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * emissions, then set this to the last token to be matched or 923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * something nonnull so that the auto token emit mechanism will not 933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * emit another token. 943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Token token; 963447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 973447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** What character index in the stream did the current token start at? 983447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * Needed, for example, to get the text for current token. Set at 993447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * the start of nextToken. 1003447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 1013447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int tokenStartCharIndex = -1; 1023447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1033447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** The line on which the first character of the token resides */ 1043447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int tokenStartLine; 1053447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1063447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** The character position of first character within the line */ 1073447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int tokenStartCharPositionInLine; 1083447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1093447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** The channel number for the current token */ 1103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int channel; 1113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** The token type for the current token */ 1133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int type; 1143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** You can set the text for the current token to override what is in 1163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * the input char buffer. Use setText() or can set this instance var. 1173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 1183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public String text; 1193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public RecognizerSharedState() {;} 1213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public RecognizerSharedState(RecognizerSharedState state) { 1233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( this.following.length < state.following.length ) { 1243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.following = new BitSet[state.following.length]; 1253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein System.arraycopy(state.following, 0, this.following, 0, state.following.length); 1273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this._fsp = state._fsp; 1283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.errorRecovery = state.errorRecovery; 1293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.lastErrorIndex = state.lastErrorIndex; 1303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.failed = state.failed; 1313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.syntaxErrors = state.syntaxErrors; 1323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.backtracking = state.backtracking; 1333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( state.ruleMemo!=null ) { 1343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.ruleMemo = new Map[state.ruleMemo.length]; 1353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein System.arraycopy(state.ruleMemo, 0, this.ruleMemo, 0, state.ruleMemo.length); 1363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.token = state.token; 1383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.tokenStartCharIndex = state.tokenStartCharIndex; 1393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.tokenStartCharPositionInLine = state.tokenStartCharPositionInLine; 1403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.channel = state.channel; 1413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.type = state.type; 1423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.text = state.text; 1433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein} 145