llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
lexer.h
1 // This file if part of the llir-opt project.
2 // Licensing information can be found in the LICENSE file.
3 // (C) 2018 Nandor Licker. All rights reserved.
4 
5 #pragma once
6 
7 #include <llvm/ADT/StringRef.h>
8 
9 #include <string>
10 #include <fstream>
11 
12 #include "core/adt/sexp.h"
13 #include "core/constant.h"
14 
15 class Prog;
16 class Func;
17 class Block;
18 
19 
20 
24 class Lexer final {
25 public:
27  enum class Token {
28  // '\n'
29  NEWLINE,
30  // End of stream
31  END,
32  // '['
33  LBRACKET,
34  // ']'
35  RBRACKET,
36  // '(',
37  LPAREN,
38  // ')',
39  RPAREN,
40  // ','
41  COMMA,
42  // '$[a-z]+'
43  REG,
44  // '$[0-9]+'
45  VREG,
46  // [a-zA-Z_.][a-zA-Z_0-9.]*
47  IDENT,
48  // [IDENT]:
49  COLON,
50  // [0-9]+
51  NUMBER,
52  // @[a-zA-Z0-9_]+
53  ANNOT,
54  // Quoted string
55  STRING,
56  // Plus sign.
57  PLUS,
58  // Minus sign.
59  MINUS,
60  };
61 
62 public:
64  Lexer(llvm::StringRef buf);
65 
67  ~Lexer();
68 
70  Token GetToken() const { return tk_; }
72  Token NextToken();
74  void Expect(Token type);
76  void Check(Token type);
78  bool AtEnd() const { return tk_ == Token::END; }
79 
81  std::string_view String() const;
83  int64_t Int() const;
85  Register Reg() const;
87  uint64_t VReg() const;
88 
90  SExp ParseSExp();
91 
93  [[noreturn]] void Error(const std::string &msg);
94  [[noreturn]] void Error(Func *f, const std::string &msg);
95  [[noreturn]] void Error(Func *f, Block *b, const std::string &msg);
96 
97 private:
99  char NextChar();
100 
101 private:
103  llvm::StringRef buf_;
105  const char *ptr_;
107  char char_;
109  Token tk_;
111  unsigned row_;
113  unsigned col_;
115  std::string str_;
117  Register reg_;
119  uint64_t vreg_;
121  int64_t int_;
122 };
SExp
Definition: sexp.h:17
Lexer::Check
void Check(Token type)
Checks if the current token is of a specific type.
Definition: lexer.cpp:319
Func
Definition: func.h:30
Lexer::Lexer
Lexer(llvm::StringRef buf)
Creates a lexer for a stream.
Definition: lexer.cpp:93
Lexer::VReg
uint64_t VReg() const
Returns the current virtual register.
Definition: lexer.cpp:366
Lexer::Error
void Error(const std::string &msg)
Error reporting.
Definition: lexer.cpp:406
Lexer::~Lexer
~Lexer()
Cleanup.
Definition: lexer.cpp:106
Lexer::AtEnd
bool AtEnd() const
Checks whether the end of stream was reached.
Definition: lexer.h:78
Lexer::Token
Token
Enumeration of tokens extracted from the stream.
Definition: lexer.h:27
Lexer::NextToken
Token NextToken()
Fetches the next token.
Definition: lexer.cpp:111
Lexer
Definition: lexer.h:24
Lexer::Reg
Register Reg() const
Returns the current register.
Definition: lexer.cpp:360
Lexer::Int
int64_t Int() const
Returns the current integer.
Definition: lexer.cpp:354
Lexer::GetToken
Token GetToken() const
Returns the current token.
Definition: lexer.h:70
Lexer::String
std::string_view String() const
Returns the current string.
Definition: lexer.cpp:348
Prog
Definition: prog.h:33
Lexer::ParseSExp
SExp ParseSExp()
Parses an S-Expression.
Definition: lexer.cpp:372
Lexer::Expect
void Expect(Token type)
Checks if the next character is of a specific type.
Definition: lexer.cpp:312
Block
Definition: block.h:29