Added assignment lexemes
[fridhskrift.git] / frith / lexer.hpp
blob2b982b657c2cf3f731efb4c7172e6c0ca44db0bf
1 #pragma once
3 #include <string>
4 #include <vector>
5 #include <ail/types.hpp>
6 #include <frith/symbol.hpp>
7 #include <boost/thread/mutex.hpp>
9 namespace frith
11 namespace lexeme_type
13 enum type
15 name,
17 boolean,
18 signed_integer,
19 unsigned_integer,
20 floating_point_value,
21 string,
23 addition,
24 subtraction,
25 multiplication,
26 division,
27 modulo,
28 exponentiation,
30 assignment,
31 addition_assignment,
32 subtraction_assignment,
33 multiplication_assignment,
34 division_assignment,
35 modulo_assignment,
36 exponentiation_assignment,
38 increment,
39 decrement,
41 less_than,
42 less_than_or_equal,
43 greater_than,
44 greater_than_or_equal,
45 unequal,
46 equal,
48 logical_not,
50 logical_and,
51 logical_or,
53 shift_left,
54 shift_right,
56 binary_and,
57 binary_or,
58 binary_xor,
60 binary_not,
62 bracket_start,
63 bracket_end,
65 array_start,
66 array_end,
68 scope_start,
69 scope_end,
71 iteration,
72 iterator,
73 function_declaration,
74 anonymous_function_declaration,
75 class_operator,
76 dot,
77 scope_operator,
81 struct lexeme
83 lexeme_type::type type;
84 union
86 bool boolean;
87 types::signed_integer signed_integer;
88 types::unsigned_integer unsigned_integer;
89 types::floating_point_value floating_point_value;
90 std::string * string;
93 lexeme();
94 lexeme(lexeme_type::type type);
95 explicit lexeme(types::boolean boolean);
96 explicit lexeme(types::signed_integer signed_integer);
97 explicit lexeme(types::unsigned_integer unsigned_integer);
98 explicit lexeme(types::floating_point_value floating_point_value);
99 explicit lexeme(lexeme_type::type type, std::string const & string);
100 std::string to_string() const;
103 struct line_of_code
105 uword line;
106 uword indentation_level;
107 std::vector<lexeme> lexemes;
109 line_of_code();
112 struct operator_lexeme
114 lexeme_type::type lexeme;
115 std::string string;
117 operator_lexeme(lexeme_type::type lexeme, std::string const & string);
118 bool operator<(operator_lexeme const & other) const;
121 class lexer
123 public:
124 lexer(std::string const & input, std::vector<line_of_code> & lines, std::string & error);
126 bool parse();
128 private:
129 std::string const & input;
130 std::vector<line_of_code> & lines;
131 std::string & error;
133 uword line;
134 std::size_t
136 end,
137 line_offset;
138 line_of_code current_line;
140 bool parse_operator(line_of_code & output);
141 bool parse_string(line_of_code & output, std::string & error_message, std::string error_prefix = "");
142 bool parse_number(line_of_code & output, bool & error_occured);
143 void parse_name(line_of_code & output);
144 bool parse_comment(std::string & error_message);
146 std::string lexer_error(std::string const & message, uword error_line = 0);
147 std::string number_parsing_error(std::string const & message, bool & error_occured);
149 bool is_name_char(char input);
150 bool string_match(std::string const & target);
151 void process_newline();
154 std::string visualise_lexemes(std::vector<line_of_code> & lines);
156 void initialise_tables();
158 extern boost::mutex table_mutex;
159 extern std::vector<operator_lexeme> operator_lexeme_data;