Loading...
Searching...
No Matches
stelaParser.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include "stela/ast.hpp"
9
10namespace by {
11 class stelaTokenScan;
12
39 class _nout stelaParser: public stelaTokenScanable {
40 BY(ME(stelaParser))
41
42 public:
44
45 public:
51 tstr<stela> parse(const std::string& codes);
63 tstr<stela> parseFromFile(const std::string& path);
70
71 stelaTokenDispatcher& getDispatcher();
72 std::vector<ncnt>& getIndents();
73
74 nbool isInit() const;
75
80 template <typename T> void setScan() { _mode = &T::instance; }
81
82 void rel();
83
94 int popState();
95
96 // events:
97 // scan:
98 using stelaTokenScanable::onScan;
108 nint onScan(stelaParser& ps, ZZSTYPE* val, ZZLTYPE* loc, zzscan_t scanner, nbool& isBypass) override;
162 void onComment(const nchar* text);
168 void onCommentBegin(const nchar* text, ncnt col);
181
182 // keyword:
183 stela* onDefBlock(stela* stmt);
184 stela* onDefBlock(stela* blk, stela* stmt);
185 stela* onDefBlock();
186
187 // expr:
188 // def:
189 // var:
196 template <typename T> stela* onPrimitive(const T& arg) { return _bornVal(new numStela(arg)); }
197
198 verStela* onVer(const std::string& version);
204 stela* onInt(const std::string& repr);
208 stela* onFlt(const std::string& repr);
209
210 stela* onDefProp(const std::string& name, stela& rhs);
211 stela* onDefAssign(const std::string& name, stela* rhs);
212 // obj:
213 stela* onDefOrigin(const std::string& name, stela& blk);
214 // container:
215 stela* onDefArray();
216 stela* onDefArray(stela& elem);
217 stela* onDefArray(stela& as, stela& elem);
218 // file:
219 stela* onCompilationUnit(stela* blk);
220
226 void onParseErr(const std::string& msg, const nchar* symbolName);
231 void report(const std::string& msg);
232
233 private:
234 void _prepare();
235 std::string _removeCRLF(const std::string& codes);
236 void* _scanString(const nchar* src, void* scanner);
237 nint _onTokenEndOfInlineBlock(nint tok);
238 nint _onScan(ZZSTYPE* val, ZZLTYPE* loc, zzscan_t scanner);
239 tstr<stela> _finalize();
240 void _addElem(stela& arr, stela& elem);
241 stela* _bornVal(stela* val);
242 stela* _bornNum(numStela* num, const std::string& repr);
243 static nbool _isLineContent(nint tok);
244
245 private:
246 stelaTokenScan* _mode;
247 nbool _isIgnoreWhitespace;
248 stelaTokenDispatcher _dispatcher;
249 std::vector<ncnt> _indents;
250 tstr<stela> _root;
251 std::vector<nint> _states;
252 std::string _prefix;
253 std::string _postfix;
254 nbool _isTokenInLine;
255 nbool _isDefHeader;
256 std::vector<std::pair<std::string, std::string>> _defComments;
257 std::vector<std::string> _assignComments;
258 ncnt _commentCol;
259 stelaSmartDedent _dedent;
260 std::vector<std::string> _errs;
261 };
262
263 template <> inline stela* stelaParser::onPrimitive(const nbool& arg) { return _bornVal(new boolStela(arg)); }
264
265 template <> inline stela* stelaParser::onPrimitive(const std::string& arg) { return _bornVal(new strStela(arg)); }
266} // namespace by
Boolean literal — the BOOLVAL form of stela source.
Definition boolStela.hpp:13
Numeric literal — the INTVAL and FLTVAL forms of stela source.
Definition numStela.hpp:18
Definition stela.hpp:63
Parser for stela configuration language.
Definition stelaParser.hpp:39
void onCommentBegin(const nchar *text, ncnt col)
Collects the # or ## that opens a comment.
void setScan()
Sets the scanning mode for the lexer.
Definition stelaParser.hpp:80
stela * onInt(const std::string &repr)
Creates a number node from an integer literal, keeping its spelling.
nint onScan(stelaParser &ps, ZZSTYPE *val, ZZLTYPE *loc, zzscan_t scanner, nbool &isBypass) override
Handles scanning for the lexer, overriding the base stelaTokenScanable behavior.
nint onIndent(ncnt col, nint tok)
Handles an increase in indentation.
tstr< stela > parseFromFile(const std::string &path)
Parses stela code from the specified file path.
int popState()
Pops a Flex scanner state from the state stack.
int pushState(int newState)
Pushes a new Flex scanner state onto the state stack.
nint onTokenComma(nint tok)
Handles the comma token.
void onParseErr(const std::string &msg, const nchar *symbolName)
Handles a parse error, reporting the message and the symbol where the error occurred.
nint onTokenColon(nint tok)
Handles the colon token.
tstr< stela > parseFromFile(const nchar *path)
Parses stela code from the specified C-style file path.
tstr< stela > parse(const nchar *codes)
Parses the given C-style string of stela code.
stela * onFlt(const std::string &repr)
Creates a number node from a float literal, keeping its spelling.
nint onTokenDefAssign(nint tok)
Holds aside the comments above an assignment until its value is complete.
tstr< stela > parse(const std::string &codes)
Parses the given string of stela code.
nchar onScanUnexpected(const nchar *token)
Handles an unexpected token encountered during scanning.
nint onIgnoreIndent(nint tok)
Handles ignoring indentation (e.g., for inline blocks).
stela * onPrimitive(const T &arg)
Creates a value stela node from a primitive value.
Definition stelaParser.hpp:196
void report(const std::string &msg)
Reports a message, typically an error or warning, encountered during parsing.
nint onTokenDef(nint tok)
Marks the start of a def header so its comments can be held aside.
void onComment(const nchar *text)
Collects a piece of comment text the scanner would otherwise drop.
nint onTokenNewLine(nint tok)
Handles the newline token.
nint onTokenEndOfFile()
Handles the end-of-file token.
nint onDedent(ncnt col, nint tok)
Handles a decrease in indentation.
Smart dedent handler for indentation-based parsing.
Definition stelaSmartDedent.hpp:25
Token dispatcher for managing token queue during parsing.
Definition stelaTokenDispatcher.hpp:14
Base class for token scanning strategies.
Definition stelaTokenScan.hpp:30
Interface for token scanning during stela parsing.
Definition stelaTokenScanable.hpp:19
Version stela node for semantic version handling.
Definition verStela.hpp:26
Rich logging support with polymorphic type conversion.
Definition richLog.hpp:34