-
Notifications
You must be signed in to change notification settings - Fork 0
/
TerminalExpressions.cpp
106 lines (104 loc) · 2.23 KB
/
TerminalExpressions.cpp
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
101
102
103
104
105
106
#include"TerminalExpressions.h"
#include"Token.h"
LiteralExpression::LiteralExpression(AsciiRange *ar)
{
_characterRange = ar;
_literal = NULL;
_terminatingCharacter = "";
}
LiteralExpression::~LiteralExpression()
{
if(_characterRange != NULL)
{
delete _characterRange;
_characterRange = NULL;
}
}
LiteralExpression::LiteralExpression(std::string *strng):TerminalExpression(strng)
{
_characterRange = NULL;
_literalInstance = "";
}
void LiteralExpression::Interpret(Context context)
{
Expression::IsValidContext(context);
_literalInstance = context.GetStringFromContext();
if(_characterRange == NULL && _literal != NULL)
InterpretWithString(context);
else
InterpretWithAsciiRange(context);
}
void LiteralExpression::InterpretWithString(Context context)
{
if(*context.GetBegin()==*context.GetEnd())
{
AddNode(context.GetASTreeBuilder());
}
else
{
bool found = false;
for(unsigned i=0;i<_literal->length() && found==false;i++)
{
if(*(context.GetBegin())==(*_literal)[i])
{
found = true;
}
}
if(found)
{
context.AdvanceOneCharacter();
InterpretWithString(context);
}
else if(!found)
{
throw std::invalid_argument("syntax error in string");
}
}
}
void LiteralExpression::InterpretWithAsciiRange(Context context)
{
if(*context.GetBegin()==*context.GetEnd())
{
AddNode(context.GetASTreeBuilder());
}
else if(_characterRange->IsWithinRange(*context.GetBegin()))
{
context.AdvanceOneCharacter();
InterpretWithAsciiRange(context);
}
else
{
throw std::invalid_argument("syntax error in string");
}
}
void LiteralExpression::AddNode(Token *ast_builder)
{
ast_builder->AddLeaf(_literalInstance);
}
ConstLiteralExpression::~ConstLiteralExpression()
{
}
void ConstLiteralExpression::Interpret(Context context)
{
Expression::IsValidContext(context);
bool match = true;
for(unsigned i=0;context.GetBegin()+i != context.GetEnd() && match==true;i++)
{
if(i>=_literal->length())
match = false;
else if(*(context.GetBegin()+i)!=(*_literal)[i])
match = false;
}
if(!match)
{
throw std::invalid_argument("syntax error in string literal");
}
else
{
AddNode(context.GetASTreeBuilder());
}
}
void ConstLiteralExpression::AddNode(Token *ast_builder)
{
ast_builder->AddInternalNode(*_literal);
}