Implemented the one-liner operators ` ( )
[fridhskrift.git] / fridh / lexer.hpp
blobeff92d5b4e03db4104fc8ac18dcb7b8b3983d24b
1 #pragma once
3 #include <string>
4 #include <vector>
5 #include <ail/types.hpp>
6 #include <fridh/symbol.hpp>
7 #include <fridh/construction.hpp>
8 #include <boost/thread/mutex.hpp>
10 namespace fridh
12 namespace lexeme_type
14 enum type
16 uninitialised,
18 non_terminating_placeholder,
20 name,
22 nil,
23 boolean,
24 signed_integer,
25 unsigned_integer,
26 floating_point_value,
27 string,
29 addition,
30 subtraction,
31 multiplication,
32 division,
33 modulo,
34 exponentiation,
36 negation,
38 assignment,
39 addition_assignment,
40 subtraction_assignment,
41 multiplication_assignment,
42 division_assignment,
43 modulo_assignment,
44 exponentiation_assignment,
46 increment,
47 decrement,
49 less_than,
50 less_than_or_equal,
51 greater_than,
52 greater_than_or_equal,
53 not_equal,
54 equal,
56 logical_not,
58 logical_and,
59 logical_or,
61 shift_left,
62 shift_right,
64 binary_and,
65 binary_or,
66 binary_xor,
68 binary_not,
70 bracket_start,
71 bracket_end,
73 array_start,
74 array_end,
77 scope_start,
78 scope_end,
81 iteration,
82 iterator,
84 while_operator,
86 function_declaration,
87 anonymous_function_declaration,
89 scope_operator,
90 class_operator,
92 dot,
94 call_operator,
95 spaced_call_operator,
99 namespace lexeme_group
101 enum type
103 argument,
104 unary_operator,
105 binary_operator,
107 call_operator,
109 post_fix_operator,
113 struct lexeme;
114 struct line_of_code;
116 typedef std::vector<lexeme> lexeme_container;
117 typedef std::vector<line_of_code> lines_of_code;
119 struct lexeme
121 lexeme_type::type type;
122 union
124 bool boolean;
125 types::signed_integer signed_integer;
126 types::unsigned_integer unsigned_integer;
127 types::floating_point_value floating_point_value;
128 std::string * string;
131 lexeme();
132 lexeme(lexeme const & other);
133 lexeme(lexeme_type::type type);
134 explicit lexeme(types::boolean boolean);
135 explicit lexeme(types::signed_integer signed_integer);
136 explicit lexeme(types::unsigned_integer unsigned_integer);
137 explicit lexeme(types::floating_point_value floating_point_value);
138 explicit lexeme(std::string const & string);
140 ~lexeme();
142 std::string to_string() const;
144 lexeme & operator=(lexeme const & other);
146 void copy(lexeme const & other);
147 void destroy();
149 bool is_string() const;
152 struct line_of_code
154 uword line;
155 uword indentation_level;
156 lexeme_container lexemes;
158 line_of_code();
161 struct operator_lexeme
163 lexeme_type::type lexeme;
164 std::string string;
166 operator_lexeme(lexeme_type::type lexeme, std::string const & string);
167 bool operator<(operator_lexeme const & other) const;
170 class lexer
172 public:
173 lexer(std::string const & input, lines_of_code & lines);
174 bool parse(std::string & error);
176 private:
177 std::string const & input;
178 lines_of_code & lines;
180 uword line;
181 std::size_t
183 end,
184 line_offset;
185 line_of_code current_line;
187 bool parse_operator(line_of_code & output);
188 void parse_string(line_of_code & output);
189 bool parse_number(line_of_code & output);
190 void parse_name(line_of_code & output);
191 void parse_comment();
193 void lexer_error(std::string const & message, uword error_line = 0);
194 void number_parsing_error(std::string const & message);
196 bool is_name_char(char input);
197 bool string_match(std::string const & target);
198 void process_newline(bool next_line = true);
200 void parse_lexemes();
202 void process_one_liner(word summand);
205 std::string visualise_lexemes(lines_of_code & lines);
207 void initialise_tables();
209 bool get_lexeme_group(lexeme_type::type input, lexeme_group::type & output);
211 extern boost::mutex table_mutex;
212 extern std::vector<operator_lexeme> operator_lexeme_data;