src/script/parser.h

branch
scripting
changeset 921
e2e9f594de66
child 922
81887a77baa0
equal deleted inserted replaced
920:147497785496 921:e2e9f594de66
1 #pragma once
2 #include <QString>
3 #include "ast.h"
4
5 namespace Script
6 {
7 enum TokenType
8 {
9 TOK_Dollar,
10 TOK_Semicolon,
11 TOK_BraceLeft,
12 TOK_BraceRight,
13 TOK_BracketLeft,
14 TOK_BracketRight,
15 TOK_ParenLeft,
16 TOK_ParenRight,
17 TOK_String,
18 TOK_Symbol,
19 TOK_Number,
20 TOK_Any
21 };
22
23 enum Type
24 {
25 TYPE_Var, // mixed
26 TYPE_Int,
27 TYPE_Real,
28 TYPE_String,
29 TYPE_Type, // heh
30 TYPE_Vertex,
31 TYPE_Object,
32 TYPE_Line,
33 TYPE_OptLine,
34 TYPE_Triangle,
35 TYPE_Quad,
36 TYPE_Reference,
37 };
38
39 enum Function
40 {
41 FUNC_Abs,
42 FUNC_Print,
43 FUNC_Typeof,
44 };
45
46 struct Token
47 {
48 TokenType type;
49 QString text;
50 };
51
52 struct SavedPosition
53 {
54 int position;
55 int lineNumber;
56
57 void reset()
58 {
59 position = 0;
60 lineNumber = 1;
61 }
62 };
63
64 class ParseError : public std::exception
65 {
66 public:
67 ParseError(QString text) : m_text (text) {}
68 const char* what() const { return m_text; }
69
70 private:
71 QString m_text;
72 };
73
74 class Parser
75 {
76 public:
77 Parser(QString text);
78 ~Parser();
79
80 void parse();
81 void scriptError(QString text) { throw ParseError(text); }
82 bool next(TokenType desiredType = TOK_Any);
83 void mustGetNext(TokenType desiredType = TOK_Any);
84 bool peekNext(Token& tok);
85 const SavedPosition& position() const;
86 void setPosition(const SavedPosition& pos);
87
88 private:
89 QString m_data;
90 SavedPosition m_position;
91 Token m_token;
92 AstNode* m_astRoot;
93 };
94 }

mercurial