Fri, 17 Jan 2014 22:55:13 +0200
- parser now processes commands.def properly
src/commands.cc | file | annotate | diff | comparison | revisions | |
src/format.cc | file | annotate | diff | comparison | revisions | |
src/format.h | file | annotate | diff | comparison | revisions | |
src/lexer.cc | file | annotate | diff | comparison | revisions | |
src/lexer.h | file | annotate | diff | comparison | revisions | |
src/lexer_scanner.cc | file | annotate | diff | comparison | revisions | |
src/lexer_scanner.h | file | annotate | diff | comparison | revisions | |
src/main.cc | file | annotate | diff | comparison | revisions | |
src/str.cc | file | annotate | diff | comparison | revisions | |
src/tokens.h | file | annotate | diff | comparison | revisions |
--- a/src/commands.cc Fri Jan 17 21:42:02 2014 +0200 +++ b/src/commands.cc Fri Jan 17 22:55:13 2014 +0200 @@ -157,6 +157,10 @@ text += GetTypeName (comm->returnvalue); text += ' '; text += comm->name; + + if (comm->maxargs != 0) + text += ' '; + text += '('; bool hasoptionals = false; @@ -178,7 +182,7 @@ if (i >= comm->numargs) { - text += '='; + text += " = "; bool is_string = comm->args[i].type == TYPE_STRING;
--- a/src/format.cc Fri Jan 17 21:42:02 2014 +0200 +++ b/src/format.cc Fri Jan 17 22:55:13 2014 +0200 @@ -31,6 +31,7 @@ #include <cstdio> #include "main.h" #include "format.h" +#include "lexer.h" static void draw_pos (const string& fmt, int pos) { @@ -121,4 +122,18 @@ { string out = format_args (args); fprintf (fp, "%s", out.chars()); +} + +void do_error (string msg) +{ + lexer* lx = lexer::get_main_lexer(); + string fileinfo; + + if (lx != null && lx->has_valid_token()) + { + lexer::token* tk = lx->get_token(); + fileinfo = format ("%1:%2:%3: ", tk->file, tk->line, tk->column); + } + + throw script_error (fileinfo + msg); } \ No newline at end of file
--- a/src/format.h Fri Jan 17 21:42:02 2014 +0200 +++ b/src/format.h Fri Jan 17 22:55:13 2014 +0200 @@ -117,12 +117,13 @@ string format_args (const list<format_arg>& args); void print_args (FILE* fp, const list<format_arg>& args); void do_fatal (const list<format_arg>& args); +void do_error (string msg); #ifndef IN_IDE_PARSER # define format(...) format_args({ __VA_ARGS__ }) # define fprint(A, ...) print_args( A, { __VA_ARGS__ }) # define print(...) print_args( stdout, { __VA_ARGS__ }) -# define error(...) throw script_error (format (__VA_ARGS__)) +# define error(...) do_error (format (__VA_ARGS__)) #else string format (void, ...); void fprint (FILE* fp, ...);
--- a/src/lexer.cc Fri Jan 17 21:42:02 2014 +0200 +++ b/src/lexer.cc Fri Jan 17 22:55:13 2014 +0200 @@ -38,18 +38,15 @@ { assert (g_main_lexer == null); g_main_lexer = this; - devf ("Lexer initialized\n"); } lexer::~lexer() { g_main_lexer = null; - devf ("Lexer de-initialized\n"); } void lexer::process_file (string file_name) { - devf ("Lexer: processing %1\n", file_name); FILE* fp = fopen (file_name, "r"); if (fp == null) @@ -57,10 +54,8 @@ lexer_scanner sc (fp); - devf ("Processing tokens...\n"); while (sc.get_next_token()) { - devf (".\n"); // Preprocessor commands: if (sc.get_token_type() == tk_hash) { @@ -68,8 +63,6 @@ if (sc.get_token_text() == "include") { - devf ("Lexer: encountered #include\n"); - must_get_next_from_scanner (sc, tk_string); string file_name = sc.get_token_text(); @@ -90,12 +83,9 @@ tok.type = sc.get_token_type(); tok.text = sc.get_token_text(); m_tokens << tok; - devf ("Lexer: added %1 (%2)\n", describe_token_type (tok.type), - describe_token (&tok)); } } - devf ("Lexer: File %1 processed (%2 tokens).\n", file_name, m_tokens.size()); m_token_position = m_tokens.begin() - 1; } @@ -104,30 +94,21 @@ bool lexer::get_next (e_token req) { iterator pos = m_token_position; - devf ("Lexer: Requested next token, requirement: %1\n", describe_token_type (req)); if (m_tokens.is_empty()) - { - devf ("Lexer: no tokens! Failed.\n"); return false; - } - - if (is_at_end()) - { - devf ("Lexer: at end of tokens. Failed.\n"); - return false; - } m_token_position++; + if (is_at_end()) + return false; + if (req != tk_any && get_token_type() != req) { - devf ("Lexer: Token %1 does not meet the requirement\n", describe_token (get_token())); m_token_position = pos; return false; } - devf ("Lexer: Get successful: %1\n", describe_token (get_token())); return true; } @@ -158,8 +139,6 @@ // void lexer::must_get_any_of (const list<e_token>& toks) { - devf ("Lexer: need to get a token that is any of: %1\n", toks); - if (!get_next()) error ("unexpected EOF"); @@ -206,7 +185,6 @@ // void lexer::must_be (e_token tok) { - print ("pos: %1", m_token_position - m_tokens.begin()); if (get_token_type() != tok) error ("expected %1, got %2", describe_token_type (tok), describe_token (get_token())); @@ -216,7 +194,7 @@ // string lexer::describe_token_private (e_token tok_type, lexer::token* tok) { - if ( (int) tok_type < (int) last_named_token) + if (tok_type < tk_last_named_token) return "\"" + lexer_scanner::get_token_string (tok_type) + "\""; switch (tok_type)
--- a/src/lexer.h Fri Jan 17 21:42:02 2014 +0200 +++ b/src/lexer.h Fri Jan 17 22:55:13 2014 +0200 @@ -63,7 +63,7 @@ inline bool has_valid_token() const { - return (is_at_end() == false && m_token_position != m_tokens.begin()); + return (is_at_end() == false && m_token_position != m_tokens.begin() - 1); } inline token* get_token() const
--- a/src/lexer_scanner.cc Fri Jan 17 21:42:02 2014 +0200 +++ b/src/lexer_scanner.cc Fri Jan 17 22:55:13 2014 +0200 @@ -86,7 +86,7 @@ "onexit", "state", "switch", - "str" + "str", "void", "while", "enum", @@ -94,7 +94,7 @@ "return", }; -static_assert (countof (g_token_strings) == (int) last_named_token, +static_assert (countof (g_token_strings) == (int) tk_last_named_token + 1, "Count of g_token_strings is not the same as the amount of named token identifiers."); // ============================================================================= @@ -126,8 +126,8 @@ { bool r = strncmp (m_ptr, c, strlen (c)) == 0; - // There is to be whitespace after words - if (r && (flags & f_check_word) && !isspace (m_ptr[strlen (c)])) + // There is to be a non-symbol character after words + if (r && (flags & f_check_word) && is_symbol_char (m_ptr[strlen (c)], true)) r = false; // Advance the cursor unless we want to just peek @@ -143,41 +143,27 @@ { m_token_text = ""; - while (isspace (*m_ptr) == true) - { - if (*m_ptr == '\n') - { - m_line++; - m_line_break_pos = m_ptr; - } - - m_ptr++; - } + while (isspace (*m_ptr)) + skip(); // Check for comments if (strncmp (m_ptr, "//", 2) == 0) { m_ptr += 2; - while (*(++m_ptr) != '\n') - ; + while (*m_ptr != '\n') + skip(); return get_next_token(); } elif (strncmp (m_ptr, "/*", 2) == 0) { - m_ptr += 2; + skip (2); // skip the start symbols - while (strncmp (++m_ptr, "*/", 2) != 0) - { - if (*m_ptr == '\n') - { - m_line++; - m_line_break_pos = m_ptr; - } - } + while (strncmp (m_ptr, "*/", 2) != 0) + skip(); - m_ptr += 2; // skip the */ + skip (2); // skip the end symbols return get_next_token(); } @@ -185,9 +171,14 @@ return false; // Check tokens - for (int i = 0; i < (int) (sizeof g_token_strings / sizeof * g_token_strings); ++i) + for (int i = 0; i < countof (g_token_strings); ++i) { - if (check_string (g_token_strings[i], f_check_word)) + int flags = 0; + + if (i >= tk_first_named_token) + flags |= f_check_word; + + if (check_string (g_token_strings[i], flags)) { m_token_text = g_token_strings[i]; m_token_type = (e_token) i; @@ -229,8 +220,6 @@ return true; } - m_token_type = tk_symbol; - if (isdigit (*m_ptr)) { while (isdigit (*m_ptr)) @@ -240,25 +229,13 @@ return true; } - if (is_symbol_char (*m_ptr)) + if (is_symbol_char (*m_ptr, false)) { + m_token_type = tk_symbol; + while (m_ptr != '\0') { - if (!is_symbol_char (*m_ptr)) - break; - - bool stop_here = false; - - for (string i : g_token_strings) - { - if (check_string (i, f_check_peek | f_check_word)) - { - stop_here = true; - break; - } - } - - if (stop_here) + if (!is_symbol_char (*m_ptr, true)) break; m_token_text += *m_ptr++; @@ -273,8 +250,29 @@ // ============================================================================= // +void lexer_scanner::skip() +{ + if (*m_ptr == '\n') + { + m_line++; + m_line_break_pos = m_ptr; + } + + m_ptr++; +} + +// ============================================================================= +// +void lexer_scanner::skip (int chars) +{ + for (int i = 0; i < chars; ++i) + skip(); +} + +// ============================================================================= +// string lexer_scanner::get_token_string (e_token a) { - assert ((int) a <= last_named_token); + assert ((int) a <= tk_last_named_token); return g_token_strings[a]; }
--- a/src/lexer_scanner.h Fri Jan 17 21:42:02 2014 +0200 +++ b/src/lexer_scanner.h Fri Jan 17 22:55:13 2014 +0200 @@ -52,8 +52,11 @@ }; public: - static inline bool is_symbol_char (char c) + static inline bool is_symbol_char (char c, bool allow_numbers) { + if (allow_numbers && (c >= '0' && c <= '9')) + return true; + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c == '_'); @@ -101,6 +104,12 @@ // Sets the current position based on given data. void set_position (const position_info& a); + + // Skips one character + void skip(); + + // Skips many characters + void skip (int chars); }; #endif // IRIS_SCANNER_H
--- a/src/main.cc Fri Jan 17 21:42:02 2014 +0200 +++ b/src/main.cc Fri Jan 17 22:55:13 2014 +0200 @@ -188,16 +188,7 @@ } catch (script_error& e) { - lexer* lx = lexer::get_main_lexer(); - string fileinfo; - - if (lx != null && lx->has_valid_token()) - { - lexer::token* tk = lx->get_token(); - fileinfo = format ("%1:%2:%3: ", tk->file, tk->line, tk->column); - } - - fprint (stderr, "%1error: %2\n", fileinfo, e.what()); + fprint (stderr, "error: %1\n", e.what()); } }
--- a/src/str.cc Fri Jan 17 21:42:02 2014 +0200 +++ b/src/str.cc Fri Jan 17 22:55:13 2014 +0200 @@ -246,7 +246,10 @@ errno = 0; char* endptr; long i = strtol (m_string.c_str(), &endptr, base); - *ok = (errno == 0 && *endptr == '\0'); + + if (ok) + *ok = (errno == 0 && *endptr == '\0'); + return i; } @@ -257,7 +260,10 @@ errno = 0; char* endptr; float i = strtof (m_string.c_str(), &endptr); - *ok = (errno == 0 && *endptr == '\0'); + + if (ok) + *ok = (errno == 0 && *endptr == '\0'); + return i; } @@ -268,7 +274,10 @@ errno = 0; char* endptr; double i = strtod (m_string.c_str(), &endptr); - *ok = (errno == 0 && *endptr == '\0'); + + if (ok) + *ok = (errno == 0 && *endptr == '\0'); + return i; }
--- a/src/tokens.h Fri Jan 17 21:42:02 2014 +0200 +++ b/src/tokens.h Fri Jan 17 22:55:13 2014 +0200 @@ -104,8 +104,9 @@ tk_number, // ----- 55 tk_string, // - 56 - last_named_token = (int) tk_symbol - 1, - tk_any = INT_MAX + tk_first_named_token = tk_bool, + tk_last_named_token = (int) tk_symbol - 1, + tk_any = INT_MAX }; #endif \ No newline at end of file