parser.hpp
Go to the documentation of this file.
1
2#pragma once
3
6#include "core/ast/node.hpp"
7#include "core/ast/origin.hpp"
8#include "core/ast/params.hpp"
9#include "core/ast/slot.hpp"
10#include "core/ast/func.hpp"
18
19namespace by {
20
21 class expr;
22 class ctor;
23 class blockExpr;
24 class runExpr;
25 class asExpr;
26 class isExpr;
27 class addExpr;
28 class getExpr;
29 class retExpr;
30 class breakExpr;
31 class nextExpr;
32 class ifExpr;
33 class FUOExpr;
34 class defPropExpr;
35 class genericOrigin;
36 class defNestedFuncExpr;
37 class endExpr;
38
43 class _nout parser: public worker<str, slot>, public tokenScanable {
45 BY(CLASS(parser, __super5))
46 friend class srcSupply;
47
48 public:
49 parser();
50
51 public:
52 node* getSubPack();
53 const node* getSubPack() const BY_CONST_FUNC(getSubPack())
54
55 srcSupplies& getSrcSupplies();
56 const srcSupplies& getSrcSupplies() const BY_CONST_FUNC(getSrcSupplies())
57 me& addSupply(const srcSupply& new1);
58 me& relSupplies();
59
60 tokenDispatcher& getDispatcher();
61 std::vector<ncnt>& getIndents();
62
63 nbool isInit() const;
64
65 template <typename T> void setScan() {
66 BY_DI("change scanmode(%s -> %s)", _mode ? _mode->getType().getName() : "null", *T::_instance);
67 _mode = T::_instance;
68 }
69
70 void rel() override;
71
72 int pushState(int newState);
73 int popState();
74
75 // events:
76 // scan:
77 using tokenScanable::onScan;
78 nint onScan(parser& ps, YYSTYPE* val, YYLTYPE* loc, yyscan_t scanner, nbool& isBypass) override;
79 nint onTokenEndOfFile();
80 nint onTokenColon(nint tok);
81 nint onTokenNewLine(nint tok);
82 nint onTokenLParan(nint tok);
83 nint onTokenRParan(nint tok);
84 nint onTokenComma(nint tok);
85 nbool onTokenEndOfBraces();
86 void onTokenStartOfStrTemplateBrace();
87 nint onIndent(ncnt col, nint tok);
88 nint onDedent(ncnt col, nint tok);
89 nint onIgnoreIndent(nint tok);
90 nchar onScanUnexpected(const nchar* token);
91 void onSrcArea(const area& area);
92
93 // err:
94 // operator:
95 // dot:
96 node* onGet(const std::string& name);
97 node* onGet(const std::string& name, const narr& args);
98 node* onGet(node& from, const std::string& name);
99 node* onGet(node& from, node& it);
100 node* onGet(const std::string& name, const std::string& name2);
101 node* onCallAccess(node& it, const narr& args);
102
103 // unary operation:
104 FBOExpr* onUnaryMinus(const node& it);
105 FBOExpr* onUnaryNot(const node& it);
106 FUOExpr* onUnaryBitwiseNot(const node& it);
107 node* onUnaryDoublePlus(node& it);
108 node* onUnaryDoubleMinus(node& it);
109 FUOExpr* onUnaryPostfixDoublePlus(const node& it);
110 FUOExpr* onUnaryPostfixDoubleMinus(const node& it);
111 // binary operation:
112 // arithmetic:
113 FBOExpr* onAdd(const node& lhs, const node& rhs);
114 FBOExpr* onSub(const node& lhs, const node& rhs);
115 FBOExpr* onMul(const node& lhs, const node& rhs);
116 FBOExpr* onDiv(const node& lhs, const node& rhs);
117 FBOExpr* onMod(const node& lhs, const node& rhs);
118 // bitwise:
119 FBOExpr* onBitwiseAnd(const node& lhs, const node& rhs);
120 FBOExpr* onBitwiseXor(const node& lhs, const node& rhs);
121 FBOExpr* onBitwiseOr(const node& lhs, const node& rhs);
122 FBOExpr* onLShift(const node& lhs, const node& rhs);
123 FBOExpr* onRShift(const node& lhs, const node& rhs);
124 // assign:
125 node* onAssign(node& lhs, node& rhs);
126 node* onAddAssign(node& lhs, node& rhs);
127 node* onSubAssign(node& lhs, node& rhs);
128 node* onMulAssign(node& lhs, node& rhs);
129 node* onDivAssign(node& lhs, node& rhs);
130 node* onModAssign(node& lhs, node& rhs);
131 // logical:
132 FBOExpr* onGt(const node& lhs, const node& rhs);
133 FBOExpr* onGe(const node& lhs, const node& rhs);
134 FBOExpr* onLt(const node& lhs, const node& rhs);
135 FBOExpr* onLe(const node& lhs, const node& rhs);
136 FBOExpr* onEq(const node& lhs, const node& rhs);
137 FBOExpr* onNe(const node& lhs, const node& rhs);
138 FBOExpr* onAnd(const node& lhs, const node& rhs);
139 FBOExpr* onOr(const node& lhs, const node& rhs);
140
141 // generic:
142 node* onGetGeneric(const std::string& orgName, const args& typeParams);
143 node* onGetElem(const node& arr, const node& idx);
144 // container:
145 node* onGetArray(node& elemType);
146
147 // keyword:
148 obj* onPack(const node& path);
149 obj* onPack(const std::string& path);
150 obj* onPack();
151 obj* onSubPack(obj& subpack);
152 endExpr* onEnd(const blockExpr& blk);
153 blockExpr* onBlock(const node* stmt);
154 blockExpr* onBlock(blockExpr* blk, const node* stmt);
155 blockExpr* onBlock();
156 defBlock* onDefBlock(node* stmt);
157 defBlock* onDefBlock(defBlock* blk, node* stmt);
158 defBlock* onDefBlock();
159 node* onFor(const std::string& iterName, const node& expr, const blockExpr& blk);
160 node* onWhile(const node& condition, const blockExpr& blk);
161
162 // expr:
163 // def:
164 // list:
165 node* onParanthesisAsTuple(narr& tuple);
166 args* onTuple();
167 args* onTuple(const node& elem);
168 args* onTuple(args& as, const node& elem);
169 args* onFuncCallTuple();
170 args* onFuncCallTuple(const node& elem);
171 args* onFuncCallTuple(args& as, const node& elem);
172 // typenames:
173 args* onTypeNames(const node& param);
174 args* onTypeNames(args& params, const node& param);
175
176 // var:
177 template <typename T, typename Arg> T* onPrimitive(const Arg& arg) {
178 BY_DI("on%s(...)", ttype<T>::get());
179 return new T(arg);
180 }
181
182 template <typename T> T* onPrimitive() {
183 BY_DI("on%s()", ttype<T>::get());
184 return new T();
185 }
186
187 node* onDefProp(const modifier& mod, const std::string& name, const node& rhs);
188 node* onDefProp(const std::string& name, const node& rhs);
189 node* onDefProp(const node& rhs);
190 node* onDefProp(const modifier& mod, const node& rhs);
191 node* onDefAssign(const modifier& mod, const std::string& name, const node* rhs);
192 node* onDefAssign(const defPropExpr* prop, const node* rhs);
193 node* onDefAssign(const std::string& name, const node* rhs);
194 // obj:
195 obj* onDefOrigin(const std::string& name, defBlock& blk);
196 obj* onDefOrigin(const std::string& name, const narr& args, defBlock& blk);
197 genericOrigin* onDefObjGeneric(const std::string& name, const args& typeParams, defBlock& blk);
198 genericOrigin* onDefObjGeneric(const std::string& name, const args& typeParams, const narr& args,
199 defBlock& blk);
200 // container:
201 node* onDefArray(const narr& items);
202 node* onDefSeq(const node& start, const node& end);
203 // file:
204 void onCompilationUnit(obj* subpack);
205 void onCompilationUnit(obj* subpack, defBlock* blk);
206 // func:
207 func* onFuncSignature(const modifier& mod, const getExpr& access, const node* retType);
208 func* onFuncSignature(const getExpr& access, const node* retType);
209 func* onFuncSignature(const modifier& mod, node& it, const node* retType);
210 func* onFuncSignature(node& it, const node* retType);
211 func* onAbstractFunc(func& f);
212 node* onFunc(func& func, const blockExpr& blk);
213 defNestedFuncExpr* onLambda(const narr& params, const node& retType, const blockExpr& blk);
214 ctor* onCtor(const modifier& mod, const narr& args, const blockExpr& blk);
215 ctor* onCtor(const narr& args, const blockExpr& blk);
216 ctor* onCtor(const modifier& mod, const blockExpr& blk);
217 ctor* onCtor(const blockExpr& blk);
218 // params:
219 narr* onParams();
220 narr* onParams(const defPropExpr* elem);
221 narr* onParams(narr& it, const defPropExpr* elem);
222 // modifier:
223 modifier* onModifier(nbool isPublic, nbool isOverriden);
224
225 // stmt:
226 node* onDeclStmt(const narr& dotnames) {
227 // TODO:
228 return nullptr;
229 }
230
231 // return:
232 retExpr* onRet();
233 retExpr* onRet(node& exp);
234 breakExpr* onBreak();
235 nextExpr* onNext();
236 // run:
237 runExpr* onFillFromOfFuncCall(const node& me, runExpr& to);
238 runExpr* onRunExpr(node& type, const narr& a);
239 runExpr* onRunExpr(node& type, const args& a);
240 // cast:
241 isExpr* onIs(const node& me, const node& to);
242 asExpr* onAs(const node& me, const node& as);
243 // if:
244 ifExpr* onIf(const node& condition, const blockExpr& then);
245 ifExpr* onIf(const node& condition, const blockExpr& then, const blockExpr& elseBlk);
246 ifExpr* onIf(const node& condition, const blockExpr& then, const ifExpr& elseIf);
247 // in:
248 runExpr* onIn(const node& it, const node& container);
249
250 void onEndFunc();
251 void onParseErr(const std::string& msg, const nchar* symbolName);
252
253 protected:
254 void* _scanString(const std::string& src, void* scanner);
255 exprMaker& _getMaker();
256 void _prepare() override;
257 void _onEndWork() override;
258
262 str _onWork() override;
263
264 private:
265 node* _onDefAssign(const modifier& mod, const node* type, const std::string& name, const node* rhs);
266 nint _onTokenEndOfInlineBlock(nint tok);
267 node* _onSetElem(runExpr& lhs, const node& rhs);
268 node* _onAssignElem(FBOExpr::symbol type, node& lhs, node& rhs);
269 node* _onConvertAssignElem(runExpr& lhs, node& rhs);
270 runExpr* _onRunExpr(node* me, node& type, const args& a);
271
272 nint _onScan(YYSTYPE* val, YYLTYPE* loc, yyscan_t scanner);
273 void _report(baseErr* new1);
274 params _asParams(const args& exprs);
275
276 nbool _onInjectObjSubs(obj& it, defBlock& blk);
277 nbool _onInjectCtor(obj& it, defBlock& blk);
278 std::vector<std::string> _extractParamTypeNames(const args& types);
279 std::string _joinVectorString(const std::vector<std::string>& container) const;
280 const node& _onDefArrayType(const narr& items);
281 std::vector<std::string> _toDotnames(const node& path);
282 void _onCompilationUnit(obj* subpack, defBlock* blk);
283 tstr<modifier> _makeDefaultModifier();
284
285 private:
286 tokenScan* _mode;
287 nbool _isIgnoreWhitespace;
288 tokenDispatcher _dispatcher;
289 std::vector<ncnt> _indents;
290 str _subpack;
291 tstr<scope> _filescope;
292 std::vector<nint> _states;
293 exprMaker _maker;
294 smartDedent _dedent;
295 srcSupplies _supplies;
296 std::vector<func*> _funcs;
297 ncnt _strTemplateCnt;
298 };
299} // namespace by
Fundamental Binary Operation expression.
Definition: FBOExpr.hpp:17
Fundamental Unary Operation expression.
Definition: FUOExpr.hpp:17
Function call arguments container.
Definition: args.hpp:13
Managed array container for byeol language.
Definition: arr.hpp:14
Type casting expression.
Definition: asExpr.hpp:16
Base class for all error types.
Definition: baseErr.hpp:12
Block expression containing multiple statements.
Definition: blockExpr.hpp:15
Break statement expression.
Definition: breakExpr.hpp:19
Constructor function for byeol objects.
Definition: ctor.hpp:11
Definition block for organizing object definitions.
Definition: defBlock.hpp:15
Nested func definition expression (lambda)
Definition: defNestedFuncExpr.hpp:13
Property definition expression.
Definition: defPropExpr.hpp:11
End blockExpr for function cleanup.
Definition: endExpr.hpp:11
Deferred execution utility like defer keyword in other languages.
Definition: end.hpp:14
Base class for all expressions.
Definition: expr.hpp:15
Factory for creating expressions with source location information.
Definition: exprMaker.hpp:17
Function definition in byeol language.
Definition: func.hpp:20
Generic type origin with type parameters.
Definition: genericOrigin.hpp:15
Expression for property and method access.
Definition: getExpr.hpp:17
If conditional expression.
Definition: ifExpr.hpp:13
ntype checking expression
Definition: isExpr.hpp:11
Access modifier for class members and func.
Definition: modifier.hpp:12
Continue statement expression.
Definition: nextExpr.hpp:19
Base class for all AST nodes in byeol language.
Definition: node.hpp:30
Managed object in byeol programming environment.
Definition: obj.hpp:19
Function parameter definition.
Definition: param.hpp:13
Function parameter list container.
Definition: params.hpp:15
Parser for byeol language source code.
Definition: parser.hpp:43
str _onWork() override
Return statement expression.
Definition: retExpr.hpp:14
Function/method call expression.
Definition: runExpr.hpp:14
Smart indentation management for code parsing.
Definition: smartDedent.hpp:10
Source location information.
Definition: src.hpp:11
Source code supply interface.
Definition: srcSupply.hpp:15
Token queue management for parser.
Definition: tokenDispatcher.hpp:13
Base class for token scanning strategies.
Definition: tokenScan.hpp:19
Interface for token scanning functionality.
Definition: tokenScanable.hpp:18
Template type wrapper for type metadata management.
Definition: ttype.hpp:16
Base class for runtime type information in byeol language.
Definition: type.hpp:12
Base worker template for processing tasks.
Definition: worker.hpp:28
Source code area tracking structure.
Definition: point.hpp:27