diff options
Diffstat (limited to 'runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing')
9 files changed, 0 insertions, 1974 deletions
diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/ITokenSource`1.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/ITokenSource`1.cs deleted file mode 100644 index 569c05f..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/ITokenSource`1.cs +++ /dev/null @@ -1,41 +0,0 @@ -/* - * [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 ITokenSource<T> - : ITokenSource - where T : IToken - { - new T NextToken(); - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/ITokenStream`1.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/ITokenStream`1.cs deleted file mode 100644 index a8faca6..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/ITokenStream`1.cs +++ /dev/null @@ -1,46 +0,0 @@ -/* - * [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 ITokenStream<T> - where T : IToken - { - T LT(int k); - - T Get(int i); - - ITokenSource<T> TokenSource - { - get; - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/SlimLexer.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/SlimLexer.cs deleted file mode 100644 index 3b2ec7f..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/SlimLexer.cs +++ /dev/null @@ -1,445 +0,0 @@ -/* - * [The "BSD license"] - * Copyright (c) 2011 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2011 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 abstract class SlimLexer - : BaseRecognizer - , ITokenSource<SlimToken> - { - /** <summary>Where is the lexer drawing characters from?</summary> */ - protected SlimStringStream input; - SlimToken _token; - bool _emitted; - bool _skip; - - public SlimLexer() - { - } - - public SlimLexer( ICharStream input ) - { - this.input = (SlimStringStream)input; - } - - public SlimLexer( ICharStream input, RecognizerSharedState state ) - : base( state ) - { - this.input = (SlimStringStream)input; - } - - #region Properties - public string Text - { - /** <summary>Return the text matched so far for the current token or any text override.</summary> */ - get - { - if ( state.text != null ) - { - return state.text; - } - return input.Substring( state.tokenStartCharIndex, CharIndex - state.tokenStartCharIndex ); - } - /** <summary>Set the complete text of this token; it wipes any previous changes to the text.</summary> */ - set - { - state.text = value; - } - } - public int Line - { - get - { - return input.Line; - } - set - { - input.Line = value; - } - } - public int CharPositionInLine - { - get - { - return input.CharPositionInLine; - } - set - { - input.CharPositionInLine = value; - } - } - #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 - } - _token = default( SlimToken ); - _emitted = false; - _skip = false; - //state.token = null; - state.type = TokenTypes.Invalid; - state.channel = TokenChannels.Default; - state.tokenStartCharIndex = -1; -#if TRACK_POSITION - state.tokenStartCharPositionInLine = -1; - state.tokenStartLine = -1; -#endif - state.text = null; - } - - /** <summary>Return a token from this source; i.e., match a token on the char stream.</summary> */ - public virtual SlimToken NextToken() - { - for ( ; ; ) - { - _token = default( SlimToken ); - _emitted = false; - _skip = false; - //state.token = null; - state.channel = TokenChannels.Default; - state.tokenStartCharIndex = input.Index; -#if TRACK_POSITION - state.tokenStartCharPositionInLine = input.CharPositionInLine; - state.tokenStartLine = input.Line; -#endif - state.text = null; - if ( input.LA( 1 ) == CharStreamConstants.EndOfFile ) - { - return new SlimToken(TokenTypes.EndOfFile); - } - try - { - mTokens(); - if ( _skip ) - { - continue; - } - else if ( !_emitted ) - { - Emit(); - } - - return _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() - } - } - } - IToken ITokenSource.NextToken() - { - return NextToken(); - } - - /** <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() - { - _skip = true; - //state.token = Tokens.Skip; - } - - /** <summary>This is the lexer entry point that sets instance var 'token'</summary> */ - public abstract void mTokens(); - - public ICharStream CharStream - { - get - { - return input; - } - /** <summary>Set the char stream and reset the lexer</summary> */ - set - { - input = null; - Reset(); - input = (SlimStringStream)value; - } - } - - 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 void Emit( T token ) - //{ - // _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 void Emit() - { - _token = new SlimToken() - { - //InputStream = input, - Type = state.type, - Channel = state.channel, - //CharPositionInLine = state.tokenStartCharPositionInLine, - //Line = state.tokenStartLine, - //Text = state.text - }; - //Emit( t ); - //return t; - - //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 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); - Recover( mte ); - throw mte; - } - i++; - input.Consume(); - state.failed = false; - } - } - - public void MatchAny() - { - input.Consume(); - } - - public void Match( int c ) - { - if ( input.LA( 1 ) != c ) - { - if ( state.backtracking > 0 ) - { - state.failed = true; - return; - } - MismatchedTokenException mte = new MismatchedTokenException(c, input, TokenNames); - Recover( mte ); // don't really recover; just consume in lexer - throw mte; - } - input.Consume(); - state.failed = false; - } - - public 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 int CharIndex - { - get - { - return input.Index; - } - } - - 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.TokenNames, 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.Character ) + " 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.Character ); - } - 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.Character ); - } - else if ( e is MismatchedNotSetException ) - { - MismatchedNotSetException mse = (MismatchedNotSetException)e; - msg = "mismatched character " + GetCharErrorDisplay( e.Character ) + " expecting set " + mse.Expecting; - } - else if ( e is MismatchedSetException ) - { - MismatchedSetException mse = (MismatchedSetException)e; - msg = "mismatched character " + GetCharErrorDisplay( e.Character ) + " expecting set " + mse.Expecting; - } - else if ( e is MismatchedRangeException ) - { - MismatchedRangeException mre = (MismatchedRangeException)e; - msg = "mismatched character " + GetCharErrorDisplay( e.Character ) + " expecting set " + - GetCharErrorDisplay( mre.A ) + ".." + GetCharErrorDisplay( mre.B ); - } - else - { - msg = base.GetErrorMessage( e, tokenNames ); - } - return msg; - } - - public virtual string GetCharErrorDisplay( int c ) - { - string s = ( (char)c ).ToString(); - switch ( c ) - { - case TokenTypes.EndOfFile: - 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/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/SlimStringStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/SlimStringStream.cs deleted file mode 100644 index b2c5d54..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/SlimStringStream.cs +++ /dev/null @@ -1,334 +0,0 @@ -/* - * [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. - */ - -// #define TRACK_POSITION - -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 SlimStringStream : ICharStream - { - /** <summary>The data being scanned</summary> */ - protected string data; - //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; - -#if TRACK_POSITION - /** <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; -#endif - - /** <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 SlimStringStream() - { - } - - /** <summary>Copy data in string to a local char array</summary> */ - public SlimStringStream( string input ) - : this( input, null ) - { - } - - public SlimStringStream( string input, string sourceName ) - : this( input.ToCharArray(), input.Length, sourceName ) - { - } - - /** <summary>This is the preferred constructor as no data is copied</summary> */ - public SlimStringStream( char[] data, int numberOfActualCharsInArray ) - : this( data, numberOfActualCharsInArray, null ) - { - } - - public SlimStringStream( char[] data, int numberOfActualCharsInArray, string sourceName ) - : this() - { - //this.data = data; - this.data = new string( 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 int Index - { - get - { - return p; - } - } -#if TRACK_POSITION - public int Line - { - get - { - return line; - } - set - { - line = value; - } - } - public int CharPositionInLine - { - get - { - return charPositionInLine; - } - set - { - charPositionInLine = value; - } - } -#else - public int Line - { - get - { - return -1; - } - set - { - } - } - public int CharPositionInLine - { - get - { - return -1; - } - set - { - } - } -#endif - - /** <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 void Reset() - { - p = 0; -#if TRACK_POSITION - line = 1; - charPositionInLine = 0; -#endif - markDepth = 0; - } - - public void Consume() - { - //System.out.println("prev p="+p+", c="+(char)data[p]); - if ( p < n ) - { -#if TRACK_POSITION - charPositionInLine++; - if ( data[p] == '\n' ) - { - /* - System.out.println("newline char found on line: "+line+ - "@ pos="+charPositionInLine); - */ - line++; - charPositionInLine = 0; - } -#endif - p++; - //System.out.println("p moves to "+p+" (c='"+(char)data[p]+"')"); - } - } - - public 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.EndOfFile; // invalid; no char before first char - } - } - - if ( ( p + i - 1 ) >= n ) - { - //System.out.println("char LA("+i+")=EOF; p="+p); - return CharStreamConstants.EndOfFile; - } - //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 int LT( int i ) - { - return LA( i ); - } - - public int Count - { - get - { - return n; - } - } - - public 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; -#if TRACK_POSITION - state.line = line; - state.charPositionInLine = charPositionInLine; -#endif - lastMarker = markDepth; - return markDepth; - } - - public void Rewind( int m ) - { - CharStreamState state = markers[m]; - // restore stream state - Seek( state.p ); -#if TRACK_POSITION - line = state.line; - charPositionInLine = state.charPositionInLine; -#endif - Release( m ); - } - - public void Rewind() - { - Rewind( lastMarker ); - } - - public 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 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 string Substring( int start, int length ) - { - return data.Substring( start, length ); - //return new string( data, start, stop - start + 1 ); - } - - public string SourceName - { - get - { - return name; - } - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/SlimToken.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/SlimToken.cs deleted file mode 100644 index d850958..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/SlimToken.cs +++ /dev/null @@ -1,160 +0,0 @@ -/* - * [The "BSD license"] - * Copyright (c) 2011 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2011 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; - using System.Runtime.InteropServices; - - [StructLayout( LayoutKind.Sequential, Pack = 2 )] - public struct SlimToken - : IToken - { - private short _type; - private short _channel; - private short _startIndex; - private short _stopIndex; - - public SlimToken(int type) - : this() - { - _type = (short)type; - } - - #region IToken Members - - string IToken.Text - { - get - { - return string.Empty; - } - set - { - } - } - - public int Type - { - get - { - return _type; - } - set - { - _type = (short)value; - } - } - - int IToken.Line - { - get - { - return 0; - } - set - { - } - } - - int IToken.CharPositionInLine - { - get - { - return 0; - } - set - { - } - } - - public int Channel - { - get - { - return _channel; - } - set - { - _channel = (short)value; - } - } - - public int StartIndex - { - get - { - return _startIndex; - } - set - { - _startIndex = (short)value; - } - } - - public int StopIndex - { - get - { - return _stopIndex; - } - set - { - _stopIndex = (short)value; - } - } - - int IToken.TokenIndex - { - get - { - return 0; - } - set - { - } - } - - ICharStream IToken.InputStream - { - get - { - return null; - } - set - { - } - } - - #endregion - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/SlimTokenStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/SlimTokenStream.cs deleted file mode 100644 index 50da1ed..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/SlimTokenStream.cs +++ /dev/null @@ -1,438 +0,0 @@ -/* - * [The "BSD license"] - * Copyright (c) 2011 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2011 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 System; - using System.Text; - - /** <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 FastTokenStream - : ITokenStream<SlimToken> - { - [System.NonSerialized] - ITokenSource<SlimToken> _tokenSource; - - /** <summary> - * Record every single token pulled from the source so we can reproduce - * chunks of it later. - * </summary> - */ - protected List<SlimToken> tokens; - - /** <summary>Skip tokens on any channel but this one; this is how we skip whitespace...</summary> */ - protected int channel = TokenChannels.Default; - - /** <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 FastTokenStream() - { - tokens = new List<SlimToken>( 500 ); - } - - public FastTokenStream( ITokenSource<SlimToken> tokenSource ) - : this() - { - this._tokenSource = tokenSource; - } - - public FastTokenStream( ITokenSource<SlimToken> tokenSource, int channel ) - : this( tokenSource ) - { - this.channel = channel; - } - - public int Index - { - get - { - return p; - } - } - - /** <summary>Reset this token stream by setting its token source.</summary> */ - public void SetTokenSource( ITokenSource<SlimToken> tokenSource ) - { - this._tokenSource = tokenSource; - tokens.Clear(); - p = -1; - channel = TokenChannels.Default; - } - - /** <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> - */ - public void FillBuffer() - { - // fast return if the buffer is already full - if ( p != -1 ) - return; - - int index = 0; - SlimToken t = _tokenSource.NextToken(); - while ( t.Type != CharStreamConstants.EndOfFile ) - { - //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 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 int SkipOffTokenChannels( int i ) - { - int n = tokens.Count; - while ( i < n && tokens[i].Channel != channel ) - { - i++; - } - return i; - } - - protected int SkipOffTokenChannelsReverse( int i ) - { - while ( i >= 0 && tokens[i].Channel != channel ) - { - i--; - } - return i; - } - - public IList<SlimToken> GetTokens() - { - if ( p == -1 ) - { - FillBuffer(); - } - return tokens; - } - - public IList<SlimToken> 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 IList<SlimToken> 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} - List<SlimToken> filteredTokens = new List<SlimToken>(); - for ( int i = start; i <= stop; i++ ) - { - SlimToken t = tokens[i]; - if ( types == null || types.Member( t.Type ) ) - { - filteredTokens.Add( t ); - } - } - if ( filteredTokens.Count == 0 ) - { - filteredTokens = null; - } - return filteredTokens; - } - - public IList<SlimToken> GetTokens( int start, int stop, IList<int> types ) - { - return GetTokens( start, stop, new BitSet( types ) ); - } - - public IList<SlimToken> 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 SlimToken LT( int k ) - { - if ( p == -1 ) - { - FillBuffer(); - } - if ( k == 0 ) - { - return default( SlimToken ); - } - if ( k < 0 ) - { - return LB( -k ); - } - //System.out.print("LT(p="+p+","+k+")="); - if ( ( p + k - 1 ) >= tokens.Count ) - { - return new SlimToken(TokenTypes.EndOfFile); - } - //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 new SlimToken(TokenTypes.EndOfFile); - } - return tokens[i]; - } - - /** <summary>Look backwards k tokens on-channel tokens</summary> */ - protected SlimToken LB( int k ) - { - //System.out.print("LB(p="+p+","+k+") "); - if ( p == -1 ) - { - FillBuffer(); - } - if ( k == 0 ) - { - return default( SlimToken ); - } - if ( ( p - k ) < 0 ) - { - return default( SlimToken ); - } - - 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 default( SlimToken ); - } - return 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 SlimToken Get( int i ) - { - return tokens[i]; - } - - public int LA( int i ) - { - return LT( i ).Type; - } - - public int Mark() - { - if ( p == -1 ) - { - FillBuffer(); - } - lastMarker = Index; - return lastMarker; - } - - public void Release( int marker ) - { - // no resources to release - } - - public int Count - { - get - { - return tokens.Count; - } - } - - public void Rewind( int marker ) - { - Seek( marker ); - } - - public void Rewind() - { - Seek( lastMarker ); - } - - public void Reset() - { - p = 0; - lastMarker = 0; - } - - public void Seek( int index ) - { - p = index; - } - - public ITokenSource<SlimToken> TokenSource - { - get - { - return _tokenSource; - } - } - - public string SourceName - { - get - { - return TokenSource.SourceName; - } - } - - public override string ToString() - { - if ( p == -1 ) - { - throw new InvalidOperationException( "Buffer is not yet filled." ); - } - return ToString( 0, tokens.Count - 1 ); - } - - public virtual string ToString( int start, int stop ) - { - if ( start < 0 || stop < 0 ) - { - return null; - } - if ( p == -1 ) - { - throw new InvalidOperationException( "Buffer is not yet filled." ); - } - if ( stop >= tokens.Count ) - { - stop = tokens.Count - 1; - } - StringBuilder buf = new StringBuilder(); - for ( int i = start; i <= stop; i++ ) - { - SlimToken t = tokens[i]; - SlimLexer lexer = _tokenSource as SlimLexer; - if ( lexer != null ) - { - SlimStringStream input = lexer.CharStream as SlimStringStream; - if ( input != null ) - { - string text = input.Substring( t.StartIndex, t.StopIndex - t.StartIndex + 1 ); - buf.Append( 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/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/Tree/ITreeAdaptor`1.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/Tree/ITreeAdaptor`1.cs deleted file mode 100644 index dfb2ecf..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/Tree/ITreeAdaptor`1.cs +++ /dev/null @@ -1,314 +0,0 @@ -/* - * [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.Tree -{ - public interface ITreeAdaptor<T> - { - #region Construction - - /** <summary> - * 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. - * </summary> - * - * <remarks> - * Override if you want another kind of node to be built. - * </remarks> - */ - T Create(IToken payload); - - /** <summary>Duplicate a single tree node.</summary> - * <remarks>Override if you want another kind of node to be built.</remarks> - */ - T DupNode(T treeNode); - - /** <summary>Duplicate tree recursively, using dupNode() for each node</summary> */ - T DupTree(T 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> - */ - T Nil(); - - /** <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> - * 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. - * </remarks> - */ - T 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(T tree); - - /** <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. Do nothing if t or child is null. - * </summary> - */ - void AddChild(T t, T 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> - */ - T BecomeRoot(T newRoot, T 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. - * </summary> - * - * <remarks> - * 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(). - * </remarks> - */ - T RulePostProcessing(T root); - - /** <summary>For identifying trees.</summary> - * - * <remarks> - * How to identify nodes so we can say "add node to a prior node"? - * Even becomeRoot is an issue. Use System.identityHashCode(node) - * usually. - * </remarks> - */ - int GetUniqueID(T node); - - - // 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. - * </summary> - * - * <returns> - * Return node created for newRoot. - * </returns> - * - * <remarks> - * Be advised: when debugging ASTs, the DebugTreeAdaptor manually - * calls create(Token child) and then plain becomeRoot(node, node) - * because it needs to trap calls to create, but it can't since it delegates - * to not inherits from the TreeAdaptor. - * </remarks> - */ - T BecomeRoot(IToken newRoot, T 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]. - * </summary> - * - * <remarks> - * This should invoke createToken(Token). - * </remarks> - */ - T 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"]. - * </summary> - * - * <remarks> - * This should invoke createToken(Token). - * </remarks> - */ - T 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"]. - * </summary> - * - * <remarks> - * This should invoke createToken(int,String). - * </remarks> - */ - T Create(int tokenType, string text); - - #endregion - - - #region Content - - /** <summary>For tree parsing, I need to know the token type of a node</summary> */ - int GetType(T t); - - /** <summary>Node constructors can set the type of a node</summary> */ - void SetType(T t, int type); - - string GetText(T t); - - /** <summary>Node constructors can set the text of a node</summary> */ - void SetText(T t, string text); - - /** <summary> - * 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(). - * </summary> - */ - IToken GetToken(T t); - - /** <summary> - * 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. - * </summary> - */ - void SetTokenBoundaries(T t, IToken startToken, IToken stopToken); - - /** <summary>Get the token start index for this subtree; return -1 if no such index</summary> */ - int GetTokenStartIndex(T t); - - /** <summary>Get the token stop index for this subtree; return -1 if no such index</summary> */ - int GetTokenStopIndex(T t); - - #endregion - - - #region Navigation / Tree Parsing - - /** <summary>Get a child 0..n-1 node</summary> */ - T GetChild(T t, int i); - - /** <summary>Set ith child (0..n-1) to t; t must be non-null and non-nil node</summary> */ - void SetChild(T t, int i, T child); - - /** <summary>Remove ith child and shift children down from right.</summary> */ - T DeleteChild(T t, int i); - - /** <summary>How many children? If 0, then this is a leaf node</summary> */ - int GetChildCount(T t); - - /** <summary> - * 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. - * </summary> - */ - T GetParent(T t); - void SetParent(T t, T parent); - - /** <summary> - * 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. - * </summary> - */ - int GetChildIndex(T t); - void SetChildIndex(T 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(T parent, int startChildIndex, int stopChildIndex, T t); - - #endregion - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/Tree/ITreeFactory.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/Tree/ITreeFactory.cs deleted file mode 100644 index 4b17634..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/Tree/ITreeFactory.cs +++ /dev/null @@ -1,54 +0,0 @@ -/* - * [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.Tree -{ - public interface ITreeFactory<TToken, TTree> - where TToken : IToken - where TTree : ITree - { - TTree Create( TToken payload ); - - TTree CloneTree( TTree tree ); - - TTree CloneNode( TTree node ); - - TTree Nil(); - - TTree CreateErrorNode( ITokenStream<TToken> input, TToken start, TToken stop, RecognitionException e ); - } - - public interface ITreeFactory - : ITreeFactory<IToken, ITree> - { - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/Tree/ITreeNodeStream`1.cs b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/Tree/ITreeNodeStream`1.cs deleted file mode 100644 index f23183f..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/SlimParsing/Tree/ITreeNodeStream`1.cs +++ /dev/null @@ -1,142 +0,0 @@ -/* - * [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 -{ - public interface ITreeNodeStream<T> - { - /** <summary> - * 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. - * </summary> - */ - T this[int i] - { - get; - } - - /** <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. - * </summary> - * - * <remarks> - * 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. :) - * </remarks> - */ - T LT(int k); - - /** <summary> - * Where is this stream pulling nodes from? This is not the name, but - * the object that provides node objects. - * </summary> - */ - object TreeSource - { - get; - } - - /** <summary> - * 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. - * </summary> - */ - 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<T> 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 UniqueNavigationNodes - { - get; - set; - } - - /** <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(T start, T 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. 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. - * </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(T parent, int startChildIndex, int stopChildIndex, T t); - - #endregion - } -} |