-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCNSFileToAST.cpp
More file actions
100 lines (82 loc) · 2.06 KB
/
Copy pathCNSFileToAST.cpp
File metadata and controls
100 lines (82 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// CNSFileToAST.cpp — Pipeline stage that reads a CNS source file and produces an AST.
//
// Part of Sourcy — CNS -> Lua compiler for MUGEN-style characters.
// Licensed under the MIT License. See LICENSE in the project root.
//
// Author: Miguel Angel Exposito Sanchez (radexx), 2012.
#include "CNSFileToAST.h"
#include "antlr3customtokenstream.h"
#include <iostream>
CNSFileToAST::CNSFileToAST(Pipeline * p) : PipelineStage(p, ED_FILE, ED_AST)
{
m_input = nullptr;
m_lxr = nullptr;
m_tstream = nullptr;
m_psr = nullptr;
}
CNSFileToAST::~CNSFileToAST()
{
CNSFileToAST::reset();
}
void CNSFileToAST::reset()
{
PipelineStage::reset();
if(m_psr)
{
m_psr->free(m_psr); m_psr = nullptr;
}
if(m_tstream)
{
m_tstream->free(m_tstream); m_tstream = nullptr;
}
if(m_lxr)
{
m_lxr->free(m_lxr); m_lxr = nullptr;
}
if(m_input)
{
m_input->close(m_input); m_input = nullptr;
}
}
void CNSFileToAST::run()
{
reset();
m_input = antlr3FileStreamNew((pANTLR3_UINT8)m_filename.c_str(), ANTLR3_ENC_UTF8);
if (m_input == nullptr)
{
std::cerr << "Unable to open file " << m_filename << std::endl;
++m_nErrors;
return;
}
// Case insensitivity
m_input->setUcaseLA(m_input, ANTLR3_TRUE);
m_lxr = cnsLexerNew(m_input); // xxxLexerNew is generated by ANTLR
if ( m_lxr == nullptr )
{
std::cerr << "Unable to create the lexer due to malloc() failure!" << std::endl;
++m_nErrors;
return;
}
m_tstream = antlr3CustomTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(m_lxr));
if (m_tstream == nullptr)
{
std::cerr << "Out of memory trying to allocate token stream" << std::endl;
++m_nErrors;
return;
}
m_psr = cnsParserNew(reinterpret_cast<pANTLR3_COMMON_TOKEN_STREAM>(m_tstream)); // xxxParserNew is generated by ANTLR3
if (m_psr == nullptr)
{
std::cerr << "Out of memory trying to allocate parser" << std::endl;
++m_nErrors;
return;
}
// Here is where the parsing takes place
m_cnsAST = m_psr->cns(m_psr);
////
m_nErrors = m_psr->pParser->rec->state->errorCount;
if(m_nErrors > 0)
{
return;
}
}