Functional-lang  0.0
An example functional programming language
lexer.hpp
Go to the documentation of this file.
1 #ifndef FUNC_LEXER_HPP
2 #define FUNC_LEXER_HPP
3 
8 #include "func/global.hpp"
9 
13 enum Token : int {
14  tok_id = 256,
18 
21 
27 
31 
34 
36 
38 
40 };
41 
45 enum Operator {
51 
54 
60 
62 
64 };
65 
66 class TokenPos {
67  size_t start, end, lineStart, lineEnd;
68 public:
69  TokenPos(size_t start, size_t end, size_t lineStart, size_t lineEnd)
70  : start{start}, end{end}, lineStart{lineStart}, lineEnd{lineEnd} {}
71  TokenPos(const TokenPos &pos0, const TokenPos &pos1)
72  : start{pos0.start < pos1.start ? pos0.start : pos1.start},
73  end{pos0.end > pos1.end ? pos0.end : pos1.end},
74  lineStart{pos0.lineStart < pos1.lineStart ? pos0.lineStart : pos1.lineStart},
75  lineEnd{pos0.lineEnd > pos1.lineEnd ? pos0.lineEnd : pos1.lineEnd} {}
76  virtual ~TokenPos() {}
77 
78  size_t getStart() const noexcept { return start; }
79  size_t getEnd() const noexcept { return end; }
80  size_t getLineStart() const noexcept { return lineStart; }
81  size_t getLineEnd() const noexcept { return lineEnd; }
82 
83  TokenPos min() const noexcept {
84  return TokenPos(start, end - 1, lineStart, lineEnd);
85  }
86 };
87 
88 namespace std {
89  std::string to_string(Operator op) noexcept;
90 };
91 
95 
96 class Lexer {
97  std::size_t line;
98  std::size_t column;
99 
100  std::string lineStr;
101 
102  Token curtok;
103  Operator curop;
104  double curnum;
105  int64_t curint;
106  std::string curid;
107 
108  int curchar;
109 
110  std::istream *input;
111 
112  size_t token_start, token_end;
113  std::vector<std::string>& lines;
114 public:
115  Lexer(std::istream &input, std::vector<std::string> &lines);
116  virtual ~Lexer();
117 
124  int nextChar();
125 
129  int currentChar() const noexcept ;
130 
134  Token nextToken();
135 
139  Token currentToken() const noexcept;
140 
144  Operator currentOperator() const noexcept;
145 
149  double currentNumber() const noexcept;
150 
154  std::int64_t currentInteger() const noexcept;
155 
156  size_t currentLine() const noexcept
157  { return curchar == -2 || curchar == '\n' || curchar == EOF || curtok == tok_eof ?
158  (line == 0 ? 0 : line - 1) : line; }
159  size_t getTokenStartPos() const noexcept
160  { return curchar < 0 || curchar == '\n' ? token_start :
161  (token_start == 0 ? 0 : token_start - 1); }
162  size_t getTokenEndPos() const noexcept
163  { return curchar < 0 || curchar == '\n' ? token_end :
164  (token_end == 0 ? 0 : token_end - 1); }
165  const std::vector<std::string> &getLines() { return lines; }
166 
167  TokenPos getTokenPos() const noexcept {
168  return TokenPos(getTokenStartPos(), getTokenEndPos(),
169  currentLine(), currentLine());
170  }
171 
175  const std::string &currentIdentifier() const noexcept;
176 
179  int currentPrecedence();
180 
184  Token reportError(const std::string &msg) noexcept;
185 
186  Token reportError(const std::string &msg, const TokenPos &pos) noexcept;
187  Token reportError(const std::string &msg, size_t start, size_t end) noexcept;
188 
192  bool skipNewLine = false;
193 
197  std::string skippedNewLinePrefix = "";
198 };
199 
200 #endif /* FUNC_LEXER_HPP */
Floating-point Number.
Definition: lexer.hpp:15
'in'
Definition: lexer.hpp:33
Definition: lexer.hpp:24
'else'
Definition: lexer.hpp:30
(
Definition: lexer.hpp:22
'let'
Definition: lexer.hpp:32
Definition: lexer.hpp:96
const std::string & currentIdentifier() const noexcept
||
Definition: lexer.hpp:53
std::int64_t currentInteger() const noexcept
Definition: lexer.hpp:57
Binary-Operator.
Definition: lexer.hpp:17
Identifier.
Definition: lexer.hpp:14
/
Definition: lexer.hpp:58
Definition: lexer.hpp:25
Definition: lexer.hpp:66
bool skipNewLine
True if lines should be ignored/skipped by nextToken.
Definition: lexer.hpp:192
double currentNumber() const noexcept
Token
Tokens for Tokenizer/Lexical Analysis.
Definition: lexer.hpp:13
int currentPrecedence()
Returns precedence of current token.
<=
Definition: lexer.hpp:47
Not a real operator (functional substition)
Definition: lexer.hpp:63
std::string skippedNewLinePrefix
Prefix to print if line was ignored/skipped by nextToken.
Definition: lexer.hpp:197
$
Definition: lexer.hpp:26
End of file.
Definition: lexer.hpp:20
'then'
Definition: lexer.hpp:29
Token nextToken()
Operator
Operator type.
Definition: lexer.hpp:45
<
Definition: lexer.hpp:49
End of line (new-line character)
Definition: lexer.hpp:19
Definition: lexer.hpp:55
Error.
Definition: lexer.hpp:39
==
Definition: lexer.hpp:46
>
Definition: lexer.hpp:50
int getOperatorPrecedence(Operator op)
>=
Definition: lexer.hpp:48
'_'
Definition: lexer.hpp:37
int nextChar()
Aquire next char.
)
Definition: lexer.hpp:23
&&
Definition: lexer.hpp:52
File for managing external headers.
Definition: lexer.hpp:56
=
Definition: lexer.hpp:61
Operator currentOperator() const noexcept
int currentChar() const noexcept
^
Definition: lexer.hpp:59
Token currentToken() const noexcept
Integer number.
Definition: lexer.hpp:16
Token reportError(const std::string &msg) noexcept
Prints error to console (std::cerr).
'if'
Definition: lexer.hpp:28
';'
Definition: lexer.hpp:35