Page Menu
Home
Xreate
Search
Configure Global Search
Log In
Docs
Questions
Repository
Issues
Patches
Internal API
Files
F4820277
Parser.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Aug 24, 6:54 AM
Size
16 KB
Mime Type
text/x-c
Expires
Wed, Aug 26, 6:54 AM (1 d, 2 h)
Engine
blob
Format
Raw Data
Handle
286705
Attached To
rXR Xreate
Parser.cpp
View Options
/*----------------------------------------------------------------------
Compiler Generator Coco/R,
Copyright (c) 1990, 2004 Hanspeter Moessenboeck, University of Linz
extended by M. Loeberbauer & A. Woess, Univ. of Linz
ported to C++ by Csaba Balazs, University of Szeged
with improvements by Pat Terry, Rhodes University
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
As an exception, it is allowed to write an extension of Coco/R that is
used as a plugin in non-free software.
If not otherwise stated, any source code generated by Coco/R (other than
Coco/R itself) does not fall under the GNU General Public License.
-----------------------------------------------------------------------*/
#include <wchar.h>
#include "Parser.h"
#include "Scanner.h"
void Parser::SynErr(int n) {
if (errDist >= minErrDist) errors->SynErr(la->line, la->col, n);
errDist = 0;
}
void Parser::SemErr(const wchar_t* msg) {
if (errDist >= minErrDist) errors->Error(t->line, t->col, msg);
errDist = 0;
}
void Parser::Get() {
for (;;) {
t = la;
la = scanner->Scan();
if (la->kind <= maxT) { ++errDist; break; }
if (dummyToken != t) {
dummyToken->kind = t->kind;
dummyToken->pos = t->pos;
dummyToken->col = t->col;
dummyToken->line = t->line;
dummyToken->next = NULL;
coco_string_delete(dummyToken->val);
dummyToken->val = coco_string_create(t->val);
t = dummyToken;
}
la = t;
}
}
void Parser::Expect(int n) {
if (la->kind==n) Get(); else { SynErr(n); }
}
void Parser::ExpectWeak(int n, int follow) {
if (la->kind == n) Get();
else {
SynErr(n);
while (!StartOf(follow)) Get();
}
}
bool Parser::WeakSeparator(int n, int syFol, int repFol) {
if (la->kind == n) {Get(); return true;}
else if (StartOf(repFol)) {return false;}
else {
SynErr(n);
while (!(StartOf(syFol) || StartOf(repFol) || StartOf(0))) {
Get();
}
return StartOf(syFol);
}
}
void Parser::Xreate() {
while (la->kind == _ident || la->kind == 24 /* "rule" */) {
if (la->kind == _ident) {
FDecl();
} else {
RuleDecl();
}
}
}
void Parser::FDecl() {
std::wstring fname; Expression fbody; std::wstring varname; TypeAnnotation typIn; TypeAnnotation typOut;
Ident(fname);
Expect(6 /* "=" */);
Expect(7 /* "function" */);
Expect(8 /* ":" */);
Function f = Function(fname);
if (StartOf(1)) {
Type(typOut);
f.setReturnType(typOut);
} else if (la->kind == _lparen) {
Get();
Ident(varname);
Expect(8 /* ":" */);
Type(typIn);
f.addArg(varname, typIn);
while (la->kind == 9 /* "," */) {
Get();
Ident(varname);
Expect(8 /* ":" */);
Type(typIn);
f.addArg(varname, typIn);
}
Expect(_rparen);
Expect(10 /* "->" */);
Type(typOut);
f.setReturnType(typOut);
while (la->kind == 9 /* "," */) {
Get();
FnTag(f);
}
} else SynErr(35);
Expect(11 /* "{" */);
while (la->kind == _ident) {
VDecl(f);
Expect(12 /* ";" */);
}
Expr(fbody);
Expect(12 /* ";" */);
Expect(13 /* "}" */);
f.setBody(fbody); root.add(f);
}
void Parser::RuleDecl() {
Expect(24 /* "rule" */);
Expect(8 /* ":" */);
RuleArguments args; RuleGuards guards; RuleBody body; DomainAnnotation typ;
Expect(_lparen);
Ident(arg);
Expect(8 /* ":" */);
Domain(typ);
args.add(arg, typ);
while (la->kind == 9 /* "," */) {
Get();
Ident(arg);
Expect(8 /* ":" */);
Domain(typ);
args.add(arg, typ);
}
Expect(_rparen);
if (la->kind == 25 /* "case" */) {
Get();
RGuard(guards);
while (la->kind == 9 /* "," */) {
Get();
RGuard(guards);
}
}
Expect(11 /* "{" */);
RBody(args, guards);
Expect(13 /* "}" */);
}
void Parser::Ident(std::wstring& name) {
Expect(_ident);
name = t->val;
}
void Parser::Type(TypeAnnotation& typ) {
TypeAnnotation typ2; TypeAtom typ3;
if (la->kind == 19 /* "[" */) {
Get();
Type(typ2);
Expect(20 /* "]" */);
typ = TypeAnnotation(TypeOperator::LIST, typ2);
} else if (StartOf(2)) {
TypeTerm(typ3);
typ = TypeAnnotation(typ3);
} else SynErr(36);
}
void Parser::FnTag(Function& f) {
std::wstring tag; TagModifier mod = TagModifier::NONE;
Ident(tag);
if (la->kind == 21 /* "-" */) {
Get();
TagMod(mod);
}
f.addTag(tag, mod);
}
void Parser::VDecl(Function& f) {
std::wstring vname; Expression e; TypeAnnotation typ;
Ident(vname);
Expect(6 /* "=" */);
if (StartOf(3)) {
Expr(e);
Expect(8 /* ":" */);
Type(typ);
f.addDeclaration(vname, typ, e);
} else if (la->kind == 19 /* "[" */) {
ListLiteral(e);
Expect(8 /* ":" */);
Type(typ);
f.addListDeclaration(vname, typ, e);
} else SynErr(37);
}
void Parser::Expr(Expression& e) {
Operator op; Expression e2;
SimExpr(e);
if (la->kind == 6 /* "=" */ || la->kind == 29 /* ">" */ || la->kind == 33 /* "<" */) {
RelOp(op);
SimExpr(e2);
e = Expression(op, e); e.addArg(e2);
}
}
void Parser::TypeTerm(TypeAtom& typ) {
if (la->kind == 14 /* "string" */) {
Get();
} else if (la->kind == 15 /* "int" */) {
Get();
} else if (la->kind == 16 /* "num" */) {
Get();
} else if (la->kind == 17 /* "float" */) {
Get();
} else if (la->kind == 18 /* "bool" */) {
Get();
} else SynErr(38);
typ = Atom<Type_t>(t->val);
}
void Parser::ListLiteral(Expression& e) {
Expression e2;
Expect(19 /* "[" */);
e = Expression(Operator::LIST, Expression());
if (StartOf(3)) {
Expr(e2);
e.addArg(e2);
while (la->kind == 9 /* "," */) {
Get();
Expr(e2);
e.addArg(e2);
}
}
Expect(20 /* "]" */);
}
void Parser::TagMod(TagModifier& mod) {
if (la->kind == 22 /* "assert" */) {
Get();
mod = TagModifier::ASSERT;
} else if (la->kind == 23 /* "require" */) {
Get();
mod = TagModifier::REQUIRE;
} else SynErr(39);
}
void Parser::Domain(DomainAnnotation& dom) {
if (la->kind == 7 /* "function" */) {
Get();
dom = DomainAnnotation::FUNCTION;
} else if (la->kind == 26 /* "variable" */) {
Get();
dom = DomainAnnotation::VARIABLE;
} else SynErr(40);
}
void Parser::RGuard(RuleGuards& guards) {
MetaExpression e;
MetaExpr(e);
guards.add(e);
}
void Parser::RBody(RuleBody& body) {
MetaExpr e;
Expect(27 /* "warning" */);
MetaExpr(e);
if (la->kind == 28 /* "message" */) {
Get();
Expect(_string);
}
}
void Parser::MetaExpr(MetaExpression& e) {
MetaExpr2(e);
if (la->kind == 21 /* "-" */) {
MetaOp(op);
MetaExpr2(e2);
MetaParameters params;
params.add(e);
params.add(e2);
e = MetaExpression(op, params);
}
}
void Parser::MetaExpr2(MetaExpression& e) {
std::wstring i1, i2, infix; MetaParameters params;
if (checkParametersList()) {
Ident(i1);
Expect(_lparen);
if (la->kind == _ident || la->kind == _lparen) {
MetaParams(params);
}
Expect(_rparen);
e = MetaExpression(Operator::CALL, i1, params);
} else if (checkInfix()) {
Ident(i1);
Ident(infix);
Ident(i2);
params.add(Expression(Atom<Identifier_t>(i1->val)));
params.add(Expression(Atom<Identifier_t>(i2->val)));
e = MetaExpression(Operator::CALL, Atom<Identifier_t>(infix->val), params);
} else if (la->kind == _ident) {
Ident(i1);
e = MetaExpression(Atom<Identifier_t>(i1->val));
} else if (la->kind == _lparen) {
Get();
MetaExpr(e);
Expect(_rparen);
} else SynErr(41);
}
void Parser::MetaOp(Operator& op) {
Expect(21 /* "-" */);
Expect(29 /* ">" */);
op = OPERATOR::IMPL;
}
void Parser::MetaParams(MetaParameters& params) {
MetaExpr e;
MetaExpr(e);
params.add(e);
while (la->kind == 9 /* "," */) {
Get();
MetaExpr(e);
params.add(e)
}
}
void Parser::SimExpr(Expression& e) {
Operator op; Expression e2;
Term(e);
while (la->kind == 21 /* "-" */ || la->kind == 30 /* "+" */) {
AddOp(op);
Term(e2);
e = Expression(op, e); e.addArg(e2);
}
}
void Parser::RelOp(Operator& op) {
op = Operator::EQU;
if (la->kind == 6 /* "=" */) {
Get();
Expect(6 /* "=" */);
} else if (la->kind == 33 /* "<" */) {
Get();
op = Operator::LSS;
} else if (la->kind == 29 /* ">" */) {
Get();
op = Operator::GTR;
} else SynErr(42);
}
void Parser::Term(Expression& e) {
Operator op; Expression e2;
Factor(e);
while (la->kind == 31 /* "*" */ || la->kind == 32 /* "/" */) {
MulOp(op);
Factor(e2);
e = Expression(op, e); e.addArg(e2);
}
}
void Parser::AddOp(Operator& op) {
op = Operator::ADD;
if (la->kind == 30 /* "+" */) {
Get();
} else if (la->kind == 21 /* "-" */) {
Get();
op = Operator::SUB;
} else SynErr(43);
}
void Parser::Factor(Expression& e) {
std::wstring name;
if (checkParametersList()) {
Ident(name);
e = Expression(Operator::CALL, Atom<Identifier_t>(name));
Expect(_lparen);
if (StartOf(3)) {
CalleeParams(e);
}
Expect(_rparen);
} else if (la->kind == _ident) {
Ident(name);
e = Expression(Atom<Identifier_t>(name));
} else if (la->kind == _number) {
Get();
e = Expression(Atom<Number_t>(t->val));
} else if (la->kind == 21 /* "-" */) {
Get();
Factor(e);
e = Expression(Operator::NEG, e);
} else if (la->kind == _lparen) {
Get();
Expr(e);
Expect(_rparen);
} else SynErr(44);
}
void Parser::MulOp(Operator& op) {
op = Operator::MUL;
if (la->kind == 31 /* "*" */) {
Get();
} else if (la->kind == 32 /* "/" */) {
Get();
op = Operator::DIV;
} else SynErr(45);
}
void Parser::CalleeParams(Expression& e) {
Expression e2;
Expr(e2);
e.addArg(e2);
while (la->kind == 9 /* "," */) {
Get();
Expr(e2);
e.addArg(e2);
}
}
// If the user declared a method Init and a mehtod Destroy they should
// be called in the contructur and the destructor respctively.
//
// The following templates are used to recognize if the user declared
// the methods Init and Destroy.
template<typename T>
struct ParserInitExistsRecognizer {
template<typename U, void (U::*)() = &U::Init>
struct ExistsIfInitIsDefinedMarker{};
struct InitIsMissingType {
char dummy1;
};
struct InitExistsType {
char dummy1; char dummy2;
};
// exists always
template<typename U>
static InitIsMissingType is_here(...);
// exist only if ExistsIfInitIsDefinedMarker is defined
template<typename U>
static InitExistsType is_here(ExistsIfInitIsDefinedMarker<U>*);
enum { InitExists = (sizeof(is_here<T>(NULL)) == sizeof(InitExistsType)) };
};
template<typename T>
struct ParserDestroyExistsRecognizer {
template<typename U, void (U::*)() = &U::Destroy>
struct ExistsIfDestroyIsDefinedMarker{};
struct DestroyIsMissingType {
char dummy1;
};
struct DestroyExistsType {
char dummy1; char dummy2;
};
// exists always
template<typename U>
static DestroyIsMissingType is_here(...);
// exist only if ExistsIfDestroyIsDefinedMarker is defined
template<typename U>
static DestroyExistsType is_here(ExistsIfDestroyIsDefinedMarker<U>*);
enum { DestroyExists = (sizeof(is_here<T>(NULL)) == sizeof(DestroyExistsType)) };
};
// The folloing templates are used to call the Init and Destroy methods if they exist.
// Generic case of the ParserInitCaller, gets used if the Init method is missing
template<typename T, bool = ParserInitExistsRecognizer<T>::InitExists>
struct ParserInitCaller {
static void CallInit(T *t) {
// nothing to do
}
};
// True case of the ParserInitCaller, gets used if the Init method exists
template<typename T>
struct ParserInitCaller<T, true> {
static void CallInit(T *t) {
t->Init();
}
};
// Generic case of the ParserDestroyCaller, gets used if the Destroy method is missing
template<typename T, bool = ParserDestroyExistsRecognizer<T>::DestroyExists>
struct ParserDestroyCaller {
static void CallDestroy(T *t) {
// nothing to do
}
};
// True case of the ParserDestroyCaller, gets used if the Destroy method exists
template<typename T>
struct ParserDestroyCaller<T, true> {
static void CallDestroy(T *t) {
t->Destroy();
}
};
void Parser::Parse() {
t = NULL;
la = dummyToken = new Token();
la->val = coco_string_create(L"Dummy Token");
Get();
Xreate();
Expect(0);
}
Parser::Parser(Scanner *scanner) {
maxT = 34;
ParserInitCaller<Parser>::CallInit(this);
dummyToken = NULL;
t = la = NULL;
minErrDist = 2;
errDist = minErrDist;
this->scanner = scanner;
errors = new Errors();
}
bool Parser::StartOf(int s) {
const bool T = true;
const bool x = false;
static bool set[4][36] = {
{T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,T,T,T, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,T,T,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,T,T,x, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x}
};
return set[s][la->kind];
}
Parser::~Parser() {
ParserDestroyCaller<Parser>::CallDestroy(this);
delete errors;
delete dummyToken;
}
Errors::Errors() {
count = 0;
}
void Errors::SynErr(int line, int col, int n) {
wchar_t* s;
switch (n) {
case 0: s = coco_string_create(L"EOF expected"); break;
case 1: s = coco_string_create(L"ident expected"); break;
case 2: s = coco_string_create(L"number expected"); break;
case 3: s = coco_string_create(L"string expected"); break;
case 4: s = coco_string_create(L"lparen expected"); break;
case 5: s = coco_string_create(L"rparen expected"); break;
case 6: s = coco_string_create(L"\"=\" expected"); break;
case 7: s = coco_string_create(L"\"function\" expected"); break;
case 8: s = coco_string_create(L"\":\" expected"); break;
case 9: s = coco_string_create(L"\",\" expected"); break;
case 10: s = coco_string_create(L"\"->\" expected"); break;
case 11: s = coco_string_create(L"\"{\" expected"); break;
case 12: s = coco_string_create(L"\";\" expected"); break;
case 13: s = coco_string_create(L"\"}\" expected"); break;
case 14: s = coco_string_create(L"\"string\" expected"); break;
case 15: s = coco_string_create(L"\"int\" expected"); break;
case 16: s = coco_string_create(L"\"num\" expected"); break;
case 17: s = coco_string_create(L"\"float\" expected"); break;
case 18: s = coco_string_create(L"\"bool\" expected"); break;
case 19: s = coco_string_create(L"\"[\" expected"); break;
case 20: s = coco_string_create(L"\"]\" expected"); break;
case 21: s = coco_string_create(L"\"-\" expected"); break;
case 22: s = coco_string_create(L"\"assert\" expected"); break;
case 23: s = coco_string_create(L"\"require\" expected"); break;
case 24: s = coco_string_create(L"\"rule\" expected"); break;
case 25: s = coco_string_create(L"\"case\" expected"); break;
case 26: s = coco_string_create(L"\"variable\" expected"); break;
case 27: s = coco_string_create(L"\"warning\" expected"); break;
case 28: s = coco_string_create(L"\"message\" expected"); break;
case 29: s = coco_string_create(L"\">\" expected"); break;
case 30: s = coco_string_create(L"\"+\" expected"); break;
case 31: s = coco_string_create(L"\"*\" expected"); break;
case 32: s = coco_string_create(L"\"/\" expected"); break;
case 33: s = coco_string_create(L"\"<\" expected"); break;
case 34: s = coco_string_create(L"??? expected"); break;
case 35: s = coco_string_create(L"invalid FDecl"); break;
case 36: s = coco_string_create(L"invalid Type"); break;
case 37: s = coco_string_create(L"invalid VDecl"); break;
case 38: s = coco_string_create(L"invalid TypeTerm"); break;
case 39: s = coco_string_create(L"invalid TagMod"); break;
case 40: s = coco_string_create(L"invalid Domain"); break;
case 41: s = coco_string_create(L"invalid MetaExpr2"); break;
case 42: s = coco_string_create(L"invalid RelOp"); break;
case 43: s = coco_string_create(L"invalid AddOp"); break;
case 44: s = coco_string_create(L"invalid Factor"); break;
case 45: s = coco_string_create(L"invalid MulOp"); break;
default:
{
wchar_t format[20];
coco_swprintf(format, 20, L"error %d", n);
s = coco_string_create(format);
}
break;
}
wprintf(L"-- line %d col %d: %ls\n", line, col, s);
coco_string_delete(s);
count++;
}
void Errors::Error(int line, int col, const wchar_t *s) {
wprintf(L"-- line %d col %d: %ls\n", line, col, s);
count++;
}
void Errors::Warning(int line, int col, const wchar_t *s) {
wprintf(L"-- line %d col %d: %ls\n", line, col, s);
}
void Errors::Warning(const wchar_t *s) {
wprintf(L"%ls\n", s);
}
void Errors::Exception(const wchar_t* s) {
wprintf(L"%ls", s);
exit(1);
}
Event Timeline
Log In to Comment