src/lexer.cpp

Tue, 22 Jul 2014 12:57:46 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 22 Jul 2014 12:57:46 +0300
changeset 138
a426c1039655
parent 137
73d057b030d0
child 140
04a6eb68f226
permissions
-rw-r--r--

- even more rework

119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
133
dbbdb870c835 - changed all source files to use my correct legal name instead of my calling name
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
2 Copyright 2012-2014 Teemu Piippo
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 All rights reserved.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 modification, are permitted provided that the following conditions
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 are met:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 1. Redistributions of source code must retain the above copyright
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 notice, this list of conditions and the following disclaimer.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 notice, this list of conditions and the following disclaimer in the
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 documentation and/or other materials provided with the distribution.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 3. The name of the author may not be used to endorse or promote products
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 derived from this software without specific prior written permission.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 */
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 #include <cstring>
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 #include "lexer.h"
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
32 static StringList FileNameStack;
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
33 static Lexer* MainLexer = null;
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
35 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 Lexer::Lexer()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 {
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
39 ASSERT_EQ (MainLexer, null);
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
40 MainLexer = this;
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
43 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 Lexer::~Lexer()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 {
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
47 MainLexer = null;
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
50 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 void Lexer::processFile (String fileName)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 {
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
54 FileNameStack << fileName;
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 FILE* fp = fopen (fileName, "r");
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 if (fp == null)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 error ("couldn't open %1 for reading: %2", fileName, strerror (errno));
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 LexerScanner sc (fp);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 checkFileHeader (sc);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 while (sc.getNextToken())
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 // Preprocessor commands:
135
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
66 if (sc.getTokenType() == Token::Hash)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
68 mustGetFromScanner (sc,Token::Symbol);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 if (sc.getTokenText() == "include")
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
72 mustGetFromScanner (sc,Token::String);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 String fileName = sc.getTokenText();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
75 if (FileNameStack.contains (fileName))
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 error ("attempted to #include %1 recursively", sc.getTokenText());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 processFile (fileName);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 else
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 error ("unknown preprocessor directive \"#%1\"", sc.getTokenText());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 else
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 TokenInfo tok;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 tok.file = fileName;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 tok.line = sc.getLine();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 tok.column = sc.getColumn();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 tok.type = sc.getTokenType();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 tok.text = sc.getTokenText();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 // devf ("Token #%1: %2:%3:%4: %5 (%6)\n", mTokens.size(),
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 // tok.file, tok.line, tok.column, DescribeToken (&tok),
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 // GetTokenTypeString (tok.type));
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 m_tokens << tok;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 m_tokenPosition = m_tokens.begin() - 1;
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
101 FileNameStack.removeOne (fileName);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 // ============================================================================
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 static bool isValidHeader (String header)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 if (header.endsWith ("\n"))
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 header.removeFromEnd (1);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 StringList tokens = header.split (" ");
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
113 if (tokens.size() != 2 or tokens[0] != "#!botc" or tokens[1].isEmpty())
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 return false;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 StringList nums = tokens[1].split (".");
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 if (nums.size() == 2)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 nums << "0";
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 elif (nums.size() != 3)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 return false;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 bool okA, okB, okC;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 long major = nums[0].toLong (&okA);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 long minor = nums[1].toLong (&okB);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 long patch = nums[2].toLong (&okC);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
128 if (!okA or !okB or !okC)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 return false;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 if (VERSION_NUMBER < MAKE_VERSION_NUMBER (major, minor, patch))
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 error ("The script file requires " APPNAME " v%1, this is v%2",
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 makeVersionString (major, minor, patch), versionString (false));
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 return true;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 // ============================================================================
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 void Lexer::checkFileHeader (LexerScanner& sc)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 if (!isValidHeader (sc.readLine()))
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 error ("Not a valid botscript file! File must start with '#!botc <version>'");
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
146 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 //
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
148 bool Lexer::next (Token req)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 Iterator pos = m_tokenPosition;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 if (m_tokens.isEmpty())
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153 return false;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 m_tokenPosition++;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
157 if (isAtEnd() or (req !=Token::Any and tokenType() != req))
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 m_tokenPosition = pos;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160 return false;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163 return true;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
166 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167 //
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
168 void Lexer::mustGetNext (Token tok)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 if (!next())
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 error ("unexpected EOF");
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
173 if (tok !=Token::Any)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 tokenMustBe (tok);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
177 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 // eugh..
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 //
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
180 void Lexer::mustGetFromScanner (LexerScanner& sc, Token tt)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 if (sc.getNextToken() == false)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183 error ("unexpected EOF");
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
185 if (tt != Token::Any and sc.getTokenType() != tt)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 // TODO
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188 TokenInfo tok;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 tok.type = sc.getTokenType();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 tok.text = sc.getTokenText();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 error ("at %1:%2: expected %3, got %4",
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
193 FileNameStack.last(),
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 sc.getLine(),
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
195 DescribeTokenType (tt),
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
196 DescribeToken (&tok));
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
200 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 //
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
202 void Lexer::mustGetAnyOf (const List<Token>& toks)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 if (!next())
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
205 error ("unexpected EOF");
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
207 for (Token tok : toks)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208 if (tokenType() == tok)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209 return;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211 String toknames;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
213 for (const Token& tokType : toks)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
214 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215 if (&tokType == &toks.last())
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
216 toknames += " or ";
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217 elif (toknames.isEmpty() == false)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218 toknames += ", ";
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
220 toknames += DescribeTokenType (tokType);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
221 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
222
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
223 error ("expected %1, got %2", toknames, DescribeToken (token()));
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
225
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
226 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
227 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
228 int Lexer::getOneSymbol (const StringList& syms)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
229 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
230 if (!next())
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
231 error ("unexpected EOF");
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
232
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
233 if (tokenType() ==Token::Symbol)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
234 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
235 for (int i = 0; i < syms.size(); ++i)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
236 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
237 if (syms[i] == token()->text)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
238 return i;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
242 error ("expected one of %1, got %2", syms, DescribeToken (token()));
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243 return -1;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
245
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
246 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
247 //
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
248 void Lexer::tokenMustBe (Token tok)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
249 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
250 if (tokenType() != tok)
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
251 error ("expected %1, got %2", DescribeTokenType (tok),
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
252 DescribeToken (token()));
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
253 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
254
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
255 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
256 //
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
257 String Lexer::DescribeTokenPrivate (Token tokType, Lexer::TokenInfo* tok)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
258 {
136
1c40bb4f8221 - major refactoring pretty much done, compiles now
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
259 if (tokType < LastNamedToken)
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
260 return "\"" + LexerScanner::GetTokenString (tokType) + "\"";
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
261
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
262 switch (tokType)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
263 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
264 case Token::Symbol: return tok ? tok->text : "a symbol";
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
265 case Token::Number: return tok ? tok->text : "a number";
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
266 case Token::String: return tok ? ("\"" + tok->text + "\"") : "a string";
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
267 case Token::Any: return tok ? tok->text : "any token";
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
268 default: break;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
269 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
270
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
271 return "";
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
272 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
273
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
274 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
275 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
276 bool Lexer::peekNext (Lexer::TokenInfo* tk)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
277 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
278 Iterator pos = m_tokenPosition;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
279 bool r = next();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
280
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
281 if (r and tk != null)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
282 *tk = *m_tokenPosition;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
283
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
284 m_tokenPosition = pos;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
285 return r;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
286 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
287
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
288 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
289 //
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
290 bool Lexer::peekNextType (Token req)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
291 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
292 Iterator pos = m_tokenPosition;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
293 bool result = false;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
294
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
295 if (next() and tokenType() == req)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
296 result = true;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
297
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
298 m_tokenPosition = pos;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
299 return result;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
300 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
301
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
302 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
303 //
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
304 Lexer* Lexer::GetCurrentLexer()
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
305 {
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
306 return MainLexer;
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
307 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
308
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
309 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
310 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
311 String Lexer::peekNextString (int a)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
312 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
313 if (m_tokenPosition + a >= m_tokens.end())
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
314 return "";
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
315
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
316 Iterator oldpos = m_tokenPosition;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
317 m_tokenPosition += a;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
318 String result = token()->text;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
319 m_tokenPosition = oldpos;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
320 return result;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
321 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
322
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
323 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
324 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
325 String Lexer::describeCurrentPosition()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
326 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
327 return token()->file + ":" + token()->line;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
328 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
329
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
330 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
331 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
332 String Lexer::describeTokenPosition()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
333 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
334 return format ("%1 / %2", m_tokenPosition - m_tokens.begin(), m_tokens.size());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
335 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
336
138
a426c1039655 - even more rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 137
diff changeset
337 // _________________________________________________________________________________________________
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
338 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
339 void Lexer::mustGetSymbol (const String& a)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
340 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
341 mustGetNext (Token::Any);
137
73d057b030d0 - a bit more refactoring, adjusted the main commandline interface
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
342
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
343 if (token()->text != a)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
344 error ("expected \"%1\", got \"%2\"", a, token()->text);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
345 }

mercurial