Joos1W Compiler Framework
All Classes Functions Typedefs Pages
Joos1WGrammar.h
1 #pragma once
2 
3 #include <FlexLexer.h>
4 #define INCLUDED_FLEXLEXER_H
5 #include "grammar/joos1w_lexer_internal.h"
6 #undef INCLUDED_FLEXLEXER_H
7 
8 #include <iostream>
9 #include <memory_resource>
10 #include <sstream>
11 #include <vector>
12 
13 #include "diagnostics/Diagnostics.h"
14 #include "diagnostics/SourceManager.h"
15 #include "utils/BumpAllocator.h"
16 
17 class Joos1WParser final {
18 public:
19  Joos1WParser(std::string const& in, BumpAllocator& alloc,
20  diagnostics::DiagnosticEngine* diag = nullptr)
21  : lexer{alloc, diag}, iss{in} {
22  buffer = lexer.yy_create_buffer(iss, in.size());
23  lexer.yy_switch_to_buffer(buffer);
24  }
25 
26  Joos1WParser(std::string const& in,
27  diagnostics::DiagnosticEngine* diag = nullptr)
28  : mbr{}, alloc{&mbr}, lexer{alloc, diag}, iss{in} {
29  buffer = lexer.yy_create_buffer(iss, in.size());
30  lexer.yy_switch_to_buffer(buffer);
31  }
32 
33  Joos1WParser(SourceFile file, BumpAllocator& alloc,
34  diagnostics::DiagnosticEngine* diag = nullptr)
35  : mbr{},
36  alloc{&mbr},
37  lexer{alloc, diag, file},
39  buffer = lexer.yy_create_buffer(iss, SourceManager::getBuffer(file).size());
40  lexer.yy_switch_to_buffer(buffer);
41  }
42 
43  int yylex() { return lexer.yylex(); }
44 
45  int parse(parsetree::Node*& ret) {
46  ret = nullptr;
47  return yyparse(&ret, lexer);
48  }
49 
50  ~Joos1WParser() {
51  if(buffer) lexer.yy_delete_buffer(buffer);
52  }
53 
54 private:
55  std::pmr::monotonic_buffer_resource mbr;
56  BumpAllocator alloc;
57  Joos1WLexer lexer;
58  yy_buffer_state* buffer;
59  std::istringstream iss;
60 };