• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * [The "BSD licence"]
3  * Copyright (c) 2005-2008 Terence Parr
4  * All rights reserved.
5  *
6  * Conversion to C#:
7  * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 namespace Antlr.Runtime.Tree {
34     using StringBuilder = System.Text.StringBuilder;
35 
36     public class TreePatternLexer {
37         public const int EOF = -1;
38         public const int BEGIN = 1;
39         public const int END = 2;
40         public const int ID = 3;
41         public const int ARG = 4;
42         public const int PERCENT = 5;
43         public const int COLON = 6;
44         public const int DOT = 7;
45 
46         /** <summary>The tree pattern to lex like "(A B C)"</summary> */
47         protected string pattern;
48 
49         /** <summary>Index into input string</summary> */
50         protected int p = -1;
51 
52         /** <summary>Current char</summary> */
53         protected int c;
54 
55         /** <summary>How long is the pattern in char?</summary> */
56         protected int n;
57 
58         /** <summary>Set when token type is ID or ARG (name mimics Java's StreamTokenizer)</summary> */
59         public StringBuilder sval = new StringBuilder();
60 
61         public bool error = false;
62 
TreePatternLexer(string pattern)63         public TreePatternLexer(string pattern) {
64             this.pattern = pattern;
65             this.n = pattern.Length;
66             Consume();
67         }
68 
NextToken()69         public virtual int NextToken() {
70             sval.Length = 0; // reset, but reuse buffer
71             while (c != EOF) {
72                 if (c == ' ' || c == '\n' || c == '\r' || c == '\t') {
73                     Consume();
74                     continue;
75                 }
76                 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_') {
77                     sval.Append((char)c);
78                     Consume();
79                     while ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
80                             (c >= '0' && c <= '9') || c == '_') {
81                         sval.Append((char)c);
82                         Consume();
83                     }
84                     return ID;
85                 }
86                 if (c == '(') {
87                     Consume();
88                     return BEGIN;
89                 }
90                 if (c == ')') {
91                     Consume();
92                     return END;
93                 }
94                 if (c == '%') {
95                     Consume();
96                     return PERCENT;
97                 }
98                 if (c == ':') {
99                     Consume();
100                     return COLON;
101                 }
102                 if (c == '.') {
103                     Consume();
104                     return DOT;
105                 }
106                 if (c == '[') { // grab [x] as a string, returning x
107                     Consume();
108                     while (c != ']') {
109                         if (c == '\\') {
110                             Consume();
111                             if (c != ']') {
112                                 sval.Append('\\');
113                             }
114                             sval.Append((char)c);
115                         } else {
116                             sval.Append((char)c);
117                         }
118                         Consume();
119                     }
120                     Consume();
121                     return ARG;
122                 }
123                 Consume();
124                 error = true;
125                 return EOF;
126             }
127             return EOF;
128         }
129 
Consume()130         protected virtual void Consume() {
131             p++;
132             if (p >= n) {
133                 c = EOF;
134             } else {
135                 c = pattern[p];
136             }
137         }
138     }
139 }
140