OpenClonk
C4AulParse Class Reference

#include <C4AulParse.h>

Public Member Functions

 C4AulParse (class C4ScriptHost *host)
 
 C4AulParse (C4AulScriptFunc *Fn, C4AulScriptContext *context, C4AulScriptEngine *Engine)
 
 ~C4AulParse ()
 
std::unique_ptr<::aul::ast::FunctionDeclParse_DirectExec (const char *code, bool whole_function)
 
std::unique_ptr<::aul::ast::ScriptParse_Script (C4ScriptHost *)
 

Protected Member Functions

std::unique_ptr<::aul::ast::FunctionDeclParse_ToplevelFunctionDecl ()
 
std::unique_ptr<::aul::ast::StmtParse_Statement ()
 
std::unique_ptr<::aul::ast::BlockParse_Block ()
 
std::unique_ptr<::aul::ast::ArrayLitParse_Array ()
 
std::unique_ptr<::aul::ast::ProplistLitParse_PropList ()
 
std::unique_ptr<::aul::ast::DoLoopParse_DoWhile ()
 
std::unique_ptr<::aul::ast::WhileLoopParse_While ()
 
std::unique_ptr<::aul::ast::IfParse_If ()
 
std::unique_ptr<::aul::ast::ForLoopParse_For ()
 
std::unique_ptr<::aul::ast::RangeLoopParse_ForEach ()
 
std::unique_ptr<::aul::ast::ExprParse_Expression (int iParentPrio=-1)
 
std::unique_ptr<::aul::ast::VarDeclParse_Var ()
 
void Shift ()
 

Friends

class C4AulParseError
 

Detailed Description

Definition at line 45 of file C4AulParse.h.

Constructor & Destructor Documentation

◆ C4AulParse() [1/2]

C4AulParse::C4AulParse ( class C4ScriptHost host)

Definition at line 105 of file C4AulParse.cpp.

105  :
106  Fn(nullptr), Host(a), pOrgScript(a), Engine(a->Engine),
107  SPos(a->Script.getData()), TokenSPos(SPos),
108  TokenType(ATT_INVALID),
109  ContextToExecIn(nullptr)
110 { }
@ ATT_INVALID
Definition: C4AulParse.cpp:82
#define a

◆ C4AulParse() [2/2]

C4AulParse::C4AulParse ( C4AulScriptFunc Fn,
C4AulScriptContext context,
C4AulScriptEngine Engine 
)

Definition at line 112 of file C4AulParse.cpp.

112  :
113  Fn(Fn), Host(nullptr), pOrgScript(nullptr), Engine(Engine),
114  SPos(Fn->Script), TokenSPos(SPos),
115  TokenType(ATT_INVALID),
116  ContextToExecIn(context)
117 { }
const char * Script

◆ ~C4AulParse()

C4AulParse::~C4AulParse ( )

Definition at line 119 of file C4AulParse.cpp.

120 {
121  ClearToken();
122 }

Member Function Documentation

◆ Parse_Array()

std::unique_ptr<::aul::ast::ArrayLit > C4AulParse::Parse_Array ( )
protected

Definition at line 1211 of file C4AulParse.cpp.

1212 {
1213  auto arr = ::aul::ast::ArrayLit::New(TokenSPos);
1214  // force "["
1215  Match(ATT_BOPEN2);
1216  // Create an array
1217  while (TokenType != ATT_BCLOSE2)
1218  {
1219  // got no parameter before a ","? then push nil
1220  if (TokenType == ATT_COMMA)
1221  {
1222  Warn(C4AulWarningId::empty_parameter_in_array, (unsigned)arr->values.size());
1223  arr->values.emplace_back(::aul::ast::NilLit::New(TokenSPos));
1224  }
1225  else
1226  arr->values.emplace_back(Parse_Expression());
1227  if (TokenType == ATT_BCLOSE2)
1228  break;
1229  Match(ATT_COMMA, "',' or ']'");
1230  // [] -> size 0, [*,] -> size 2, [*,*,] -> size 3
1231  if (TokenType == ATT_BCLOSE2)
1232  {
1233  Warn(C4AulWarningId::empty_parameter_in_array, (unsigned)arr->values.size());
1234  arr->values.emplace_back(::aul::ast::NilLit::New(TokenSPos));
1235  }
1236  }
1237  Shift();
1238  return arr;
1239 }
@ ATT_BCLOSE2
Definition: C4AulParse.cpp:94
@ ATT_COMMA
Definition: C4AulParse.cpp:88
@ ATT_BOPEN2
Definition: C4AulParse.cpp:93
void Shift()
Definition: C4AulParse.cpp:760
std::unique_ptr<::aul::ast::Expr > Parse_Expression(int iParentPrio=-1)

References ATT_BCLOSE2, ATT_BOPEN2, ATT_COMMA, Parse_Expression(), and Shift().

Referenced by Parse_Expression().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Parse_Block()

std::unique_ptr<::aul::ast::Block > C4AulParse::Parse_Block ( )
protected

Definition at line 1050 of file C4AulParse.cpp.

1051 {
1052  auto block = ::aul::ast::Block::New(TokenSPos);
1053  Match(ATT_BLOPEN);
1054  while (TokenType != ATT_BLCLOSE)
1055  {
1056  block->children.push_back(Parse_Statement());
1057  }
1058  Shift();
1059  return block;
1060 }
@ ATT_BLCLOSE
Definition: C4AulParse.cpp:96
@ ATT_BLOPEN
Definition: C4AulParse.cpp:95
std::unique_ptr<::aul::ast::Stmt > Parse_Statement()

References ATT_BLCLOSE, ATT_BLOPEN, Parse_Statement(), and Shift().

Referenced by Parse_Statement().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Parse_DirectExec()

std::unique_ptr<::aul::ast::FunctionDecl > C4AulParse::Parse_DirectExec ( const char *  code,
bool  whole_function 
)

Definition at line 857 of file C4AulParse.cpp.

858 {
859  // get first token
860  Shift();
861  // Synthesize a wrapping function which we can call
862  std::unique_ptr<::aul::ast::FunctionDecl> func;
863  if (whole_function)
864  {
866  }
867  else
868  {
869  auto expr = Parse_Expression();
870  func = std::make_unique<::aul::ast::FunctionDecl>("$internal$eval");
871  func->body = std::make_unique<::aul::ast::Block>();
872  func->body->children.push_back(std::make_unique<::aul::ast::Return>(std::move(expr)));
873  }
874  Match(ATT_EOF);
875  return func;
876 }
@ ATT_EOF
Definition: C4AulParse.cpp:102
std::unique_ptr<::aul::ast::FunctionDecl > Parse_ToplevelFunctionDecl()
Definition: C4AulParse.cpp:964

References ATT_EOF, Parse_Expression(), Parse_ToplevelFunctionDecl(), and Shift().

Referenced by C4AulScriptFunc::ParseDirectExecFunc(), and C4AulScriptFunc::ParseDirectExecStatement().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Parse_DoWhile()

std::unique_ptr<::aul::ast::DoLoop > C4AulParse::Parse_DoWhile ( )
protected

Definition at line 1277 of file C4AulParse.cpp.

1278 {
1279  auto loop = ::aul::ast::DoLoop::New(TokenSPos);
1280  Shift();
1281  loop->body = Parse_Statement();
1282  // Execute condition
1283  if (TokenType != ATT_IDTF || !SEqual(Idtf, C4AUL_While))
1284  UnexpectedToken("'while'");
1285  Shift();
1286  Match(ATT_BOPEN);
1287  loop->cond = Parse_Expression();
1288  Match(ATT_BCLOSE);
1289  return loop;
1290 }
#define C4AUL_While
Definition: C4AulParse.cpp:48
@ ATT_IDTF
Definition: C4AulParse.cpp:84
@ ATT_BOPEN
Definition: C4AulParse.cpp:91
@ ATT_BCLOSE
Definition: C4AulParse.cpp:92
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93

References ATT_BCLOSE, ATT_BOPEN, ATT_IDTF, C4AUL_While, Parse_Expression(), Parse_Statement(), SEqual(), and Shift().

Referenced by Parse_Statement().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Parse_Expression()

std::unique_ptr<::aul::ast::Expr > C4AulParse::Parse_Expression ( int  iParentPrio = -1)
protected

Definition at line 1380 of file C4AulParse.cpp.

1381 {
1382  const char *NodeStart = TokenSPos;
1383  std::unique_ptr<::aul::ast::Expr> expr;
1384  const C4ScriptOpDef * op;
1385  C4Value val;
1386  switch (TokenType)
1387  {
1388  case ATT_IDTF:
1389  // XXX: Resolving literals here means that you can't create a variable
1390  // with the names "true", "false", "nil" or "this" anymore. I don't
1391  // consider this too much of a problem, because there is never a reason
1392  // to do this and it makes my job a lot easier
1393  if (SEqual(Idtf, C4AUL_True))
1394  {
1395  Shift();
1396  expr = ::aul::ast::BoolLit::New(NodeStart, true);
1397  }
1398  else if (SEqual(Idtf, C4AUL_False))
1399  {
1400  Shift();
1401  expr = ::aul::ast::BoolLit::New(NodeStart, false);
1402  }
1403  else if (SEqual(Idtf, C4AUL_Nil))
1404  {
1405  Shift();
1406  expr = ::aul::ast::NilLit::New(NodeStart);
1407  }
1408  else if (SEqual(Idtf, C4AUL_this))
1409  {
1410  Shift();
1411  if (TokenType == ATT_BOPEN)
1412  {
1413  Shift();
1414  Match(ATT_BCLOSE);
1415  // TODO: maybe warn about "this" with parentheses?
1416  }
1417  expr = ::aul::ast::ThisLit::New(NodeStart);
1418  }
1419  // XXX: Other things that people aren't allowed to do anymore: name their variables or functions any of:
1420  // "if", "else", "for", "while", "do", "return", or "Par".
1421  // We could allow variables with these names and disambiguate based on the syntax, but no.
1422  else if (SEqual(Idtf, C4AUL_If) || SEqual(Idtf, C4AUL_Else) || SEqual(Idtf, C4AUL_For) || SEqual(Idtf, C4AUL_While) || SEqual(Idtf, C4AUL_Do) || SEqual(Idtf, C4AUL_Return))
1423  {
1424  Error("reserved identifier not allowed in expressions: %s", Idtf);
1425  }
1426  else if (SEqual(Idtf, C4AUL_Par))
1427  {
1428  Shift();
1429  // "Par" is special in that it isn't a function and thus doesn't accept an arbitrary number of parameters
1430  Match(ATT_BOPEN);
1431  expr = ::aul::ast::ParExpr::New(NodeStart, Parse_Expression());
1432  Match(ATT_BCLOSE);
1433  }
1434  else if (SEqual(Idtf, C4AUL_New))
1435  {
1436  // Because people might call a variables or functions "new", we need to look ahead and guess whether it's a proplist constructor.
1437  PushParsePos();
1438  Shift();
1439  if (TokenType == ATT_IDTF)
1440  {
1441  // this must be a proplist because two identifiers can't immediately follow each other
1442  PopParsePos();
1443  expr = Parse_PropList();
1444  }
1445  else
1446  {
1447  // Some non-identifier means this is either a variable, a function, or a syntax error. Which one exactly is something we'll figure out later.
1448  PopParsePos();
1449  }
1450  }
1451  else if (SEqual(Idtf, C4AUL_Func))
1452  {
1453  PushParsePos();
1454  Shift();
1455  if (TokenType == ATT_BOPEN)
1456  {
1457  auto func = ::aul::ast::FunctionExpr::New(NodeStart);
1458  Parse_Function(func.get());
1459  expr = std::move(func);
1460  DiscardParsePos();
1461  }
1462  else
1463  {
1464  PopParsePos();
1465  }
1466  }
1467  if (!expr)
1468  {
1469  // If we end up here, it must be a proper identifier (or a reserved word that's used as an identifier).
1470  // Time to look ahead and see whether it's a function call.
1471  std::string identifier = Idtf;
1472  Shift();
1473  if (TokenType == ATT_BOPEN)
1474  {
1475  // Well, it looks like one, at least
1476  auto func = ::aul::ast::CallExpr::New(NodeStart);
1477  func->callee = identifier;
1478  Parse_CallParams(func.get());
1479  expr = std::move(func);
1480  }
1481  else
1482  {
1483  // It's most certainly not a function call.
1484  expr = ::aul::ast::VarExpr::New(NodeStart, identifier);
1485  }
1486  }
1487  break;
1488  case ATT_INT: // constant in cInt
1489  expr = ::aul::ast::IntLit::New(NodeStart, cInt);
1490  Shift();
1491  break;
1492  case ATT_STRING: // reference in cStr
1493  expr = ::aul::ast::StringLit::New(NodeStart, cStr->GetCStr());
1494  Shift();
1495  break;
1496  case ATT_OPERATOR:
1497  // -> must be a prefix operator
1498  op = &C4ScriptOpMap[cInt];
1499  // postfix?
1500  if (op->Postfix)
1501  // oops. that's wrong
1502  throw C4AulParseError(this, "postfix operator without first expression");
1503  Shift();
1504  // generate code for the following expression
1505  expr = Parse_Expression(op->Priority);
1506  if (SEqual(op->Identifier, "+"))
1507  {
1508  // This is a no-op.
1509  }
1510  else
1511  {
1512  expr = ::aul::ast::UnOpExpr::New(NodeStart, op - C4ScriptOpMap, std::move(expr));
1513  }
1514  break;
1515  case ATT_BOPEN:
1516  Shift();
1517  expr = Parse_Expression();
1518  Match(ATT_BCLOSE);
1519  break;
1520  case ATT_BOPEN2:
1521  expr = Parse_Array();
1522  break;
1523  case ATT_BLOPEN:
1524  expr = Parse_PropList();
1525  break;
1526  default:
1527  UnexpectedToken("expression");
1528  }
1529 
1530  assert(expr);
1531 
1532  while (true)
1533  {
1534  NodeStart = TokenSPos;
1535  switch (TokenType)
1536  {
1537  case ATT_SET:
1538  {
1539  // back out of any kind of parent operator
1540  // (except other setters, as those are right-associative)
1541  if (iParentPrio > 1)
1542  return expr;
1543  Shift();
1544  expr = ::aul::ast::AssignmentExpr::New(NodeStart, std::move(expr), Parse_Expression(1));
1545  break;
1546  }
1547  case ATT_OPERATOR:
1548  {
1549  // expect postfix operator
1550  const C4ScriptOpDef * op = &C4ScriptOpMap[cInt];
1551  if (!op->Postfix)
1552  {
1553  // does an operator with the same name exist?
1554  // when it's a postfix-operator, it can be used instead.
1555  const C4ScriptOpDef * postfixop;
1556  for (postfixop = op + 1; postfixop->Identifier; ++postfixop)
1557  if (SEqual(op->Identifier, postfixop->Identifier))
1558  if (postfixop->Postfix)
1559  break;
1560  // not found?
1561  if (!postfixop->Identifier)
1562  {
1563  Error("unexpected prefix operator: %s", op->Identifier);
1564  }
1565  // otherwise use the new-found correct postfix operator
1566  op = postfixop;
1567  }
1568 
1569  if (iParentPrio + !op->Changer > op->Priority)
1570  return expr;
1571 
1572  Shift();
1573  if (op->NoSecondStatement)
1574  {
1575  // Postfix unary op
1576  expr = ::aul::ast::UnOpExpr::New(NodeStart, op - C4ScriptOpMap, std::move(expr));
1577  }
1578  else
1579  {
1580  expr = ::aul::ast::BinOpExpr::New(NodeStart, op - C4ScriptOpMap, std::move(expr), Parse_Expression(op->Priority));
1581  }
1582  break;
1583  }
1584  case ATT_BOPEN2:
1585  {
1586  // parse either [index], or [start:end] in which case either index is optional
1587  Shift();
1588  ::aul::ast::ExprPtr start;
1589  if (TokenType == ATT_COLON)
1590  start = ::aul::ast::IntLit::New(TokenSPos, 0); // slice with first index missing -> implicit start index zero
1591  else
1592  start = Parse_Expression();
1593 
1594  if (TokenType == ATT_BCLOSE2)
1595  {
1596  expr = ::aul::ast::SubscriptExpr::New(NodeStart, std::move(expr), std::move(start));
1597  }
1598  else if (TokenType == ATT_COLON)
1599  {
1600  Shift();
1601  ::aul::ast::ExprPtr end;
1602  if (TokenType == ATT_BCLOSE2)
1603  {
1604  end = ::aul::ast::IntLit::New(TokenSPos, std::numeric_limits<int32_t>::max());
1605  }
1606  else
1607  {
1608  end = Parse_Expression();
1609  }
1610  expr = ::aul::ast::SliceExpr::New(NodeStart, std::move(expr), std::move(start), std::move(end));
1611  }
1612  else
1613  {
1614  UnexpectedToken("']' or ':'");
1615  }
1616  Match(ATT_BCLOSE2);
1617  break;
1618  }
1619  case ATT_DOT:
1620  Shift();
1621  Check(ATT_IDTF, "property name");
1622  expr = ::aul::ast::SubscriptExpr::New(NodeStart, std::move(expr), ::aul::ast::StringLit::New(TokenSPos, Idtf));
1623  Shift();
1624  break;
1625  case ATT_CALL: case ATT_CALLFS:
1626  {
1627  auto call = ::aul::ast::CallExpr::New(NodeStart);
1628  call->context = std::move(expr);
1629  call->safe_call = TokenType == ATT_CALLFS;
1630  Shift();
1631  Check(ATT_IDTF, "function name after '->'");
1632  call->callee = Idtf;
1633  Shift();
1634  Parse_CallParams(call.get());
1635  expr = std::move(call);
1636  break;
1637  }
1638  default:
1639  return expr;
1640  }
1641  }
1642 }
#define C4AUL_Par
Definition: C4AulParse.cpp:53
#define C4AUL_Else
Definition: C4AulParse.cpp:46
#define C4AUL_Do
Definition: C4AulParse.cpp:47
#define C4AUL_If
Definition: C4AulParse.cpp:45
#define C4AUL_this
Definition: C4AulParse.cpp:56
@ ATT_CALL
Definition: C4AulParse.cpp:97
@ ATT_INT
Definition: C4AulParse.cpp:85
@ ATT_SET
Definition: C4AulParse.cpp:100
@ ATT_DOT
Definition: C4AulParse.cpp:87
@ ATT_COLON
Definition: C4AulParse.cpp:89
@ ATT_CALLFS
Definition: C4AulParse.cpp:98
@ ATT_OPERATOR
Definition: C4AulParse.cpp:101
@ ATT_STRING
Definition: C4AulParse.cpp:86
#define C4AUL_For
Definition: C4AulParse.cpp:49
#define C4AUL_Return
Definition: C4AulParse.cpp:51
#define C4AUL_Func
Definition: C4AulParse.cpp:37
const C4ScriptOpDef C4ScriptOpMap[]
Definition: C4AulParse.cpp:275
#define C4AUL_New
Definition: C4AulParse.cpp:77
#define C4AUL_True
Definition: C4AulParse.cpp:74
#define C4AUL_False
Definition: C4AulParse.cpp:75
#define C4AUL_Nil
Definition: C4AulParse.cpp:76
const char * Identifier
Definition: C4AulParse.h:33
bool NoSecondStatement
Definition: C4AulParse.h:37
unsigned short Priority
Definition: C4AulParse.h:32
friend class C4AulParseError
Definition: C4AulParse.h:102
std::unique_ptr<::aul::ast::ProplistLit > Parse_PropList()
std::unique_ptr<::aul::ast::ArrayLit > Parse_Array()
const char * GetCStr() const
Definition: C4StringTable.h:49
std::unique_ptr< Expr > ExprPtr
Definition: C4AulAST.h:147

References ATT_BCLOSE, ATT_BCLOSE2, ATT_BLOPEN, ATT_BOPEN, ATT_BOPEN2, ATT_CALL, ATT_CALLFS, ATT_COLON, ATT_DOT, ATT_IDTF, ATT_INT, ATT_OPERATOR, ATT_SET, ATT_STRING, C4AUL_Do, C4AUL_Else, C4AUL_False, C4AUL_For, C4AUL_Func, C4AUL_If, C4AUL_New, C4AUL_Nil, C4AUL_Par, C4AUL_Return, C4AUL_this, C4AUL_True, C4AUL_While, C4AulParseError, C4ScriptOpMap, aul::ast::CallExpr::callee, C4ScriptOpDef::Changer, aul::ast::CallExpr::context, C4String::GetCStr(), C4ScriptOpDef::Identifier, C4ScriptOpDef::NoSecondStatement, Parse_Array(), Parse_PropList(), C4ScriptOpDef::Postfix, C4ScriptOpDef::Priority, aul::ast::CallExpr::safe_call, SEqual(), and Shift().

Referenced by Parse_Array(), Parse_DirectExec(), Parse_DoWhile(), Parse_For(), Parse_ForEach(), Parse_If(), Parse_PropList(), Parse_Statement(), Parse_Var(), and Parse_While().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Parse_For()

std::unique_ptr<::aul::ast::ForLoop > C4AulParse::Parse_For ( )
protected

Definition at line 1323 of file C4AulParse.cpp.

1324 {
1325  auto loop = ::aul::ast::ForLoop::New(TokenSPos);
1326  Match(ATT_IDTF); Match(ATT_BOPEN);
1327  // Initialization
1328  if (TokenType == ATT_IDTF && SEqual(Idtf, C4AUL_VarNamed))
1329  {
1330  loop->init = Parse_Var();
1331  }
1332  else if (TokenType != ATT_SCOLON)
1333  {
1334  loop->init = Parse_Expression();
1335  }
1336  // Consume first semicolon
1337  Match(ATT_SCOLON);
1338  // Condition
1339  if (TokenType != ATT_SCOLON)
1340  {
1341  loop->cond = Parse_Expression();
1342  }
1343  // Consume second semicolon
1344  Match(ATT_SCOLON);
1345  // Incrementor
1346  if (TokenType != ATT_BCLOSE)
1347  {
1348  loop->incr = Parse_Expression();
1349  }
1350  // Consume closing bracket
1351  Match(ATT_BCLOSE);
1352  loop->body = Parse_Statement();
1353  return loop;
1354 }
@ ATT_SCOLON
Definition: C4AulParse.cpp:90
#define C4AUL_VarNamed
Definition: C4AulParse.cpp:60
std::unique_ptr<::aul::ast::VarDecl > Parse_Var()

References ATT_BCLOSE, ATT_BOPEN, ATT_IDTF, ATT_SCOLON, C4AUL_VarNamed, Parse_Expression(), Parse_Statement(), Parse_Var(), and SEqual().

Referenced by Parse_Statement().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Parse_ForEach()

std::unique_ptr<::aul::ast::RangeLoop > C4AulParse::Parse_ForEach ( )
protected

Definition at line 1356 of file C4AulParse.cpp.

1357 {
1358  auto loop = ::aul::ast::RangeLoop::New(TokenSPos);
1359  Match(ATT_IDTF); Match(ATT_BOPEN);
1360  if (TokenType == ATT_IDTF && SEqual(Idtf, C4AUL_VarNamed))
1361  {
1362  loop->scoped_var = true;
1363  Shift();
1364  }
1365  // get variable name
1366  Check(ATT_IDTF, "variable name");
1367  loop->var = Idtf;
1368  Shift();
1369  if (TokenType != ATT_IDTF || !SEqual(Idtf, C4AUL_In))
1370  UnexpectedToken("'in'");
1371  Shift();
1372  // get expression for array
1373  loop->cond = Parse_Expression();
1374  Match(ATT_BCLOSE);
1375  // loop body
1376  loop->body = Parse_Statement();
1377  return loop;
1378 }
#define C4AUL_In
Definition: C4AulParse.cpp:50

References ATT_BCLOSE, ATT_BOPEN, ATT_IDTF, C4AUL_In, C4AUL_VarNamed, Parse_Expression(), Parse_Statement(), SEqual(), and Shift().

Referenced by Parse_Statement().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Parse_If()

std::unique_ptr<::aul::ast::If > C4AulParse::Parse_If ( )
protected

Definition at line 1305 of file C4AulParse.cpp.

1306 {
1307  auto stmt = ::aul::ast::If::New(TokenSPos);
1308  Shift();
1309  Match(ATT_BOPEN);
1310  stmt->cond = Parse_Expression();
1311  Match(ATT_BCLOSE);
1312  // parse controlled statement
1313  stmt->iftrue = Parse_Statement();
1314  if (TokenType == ATT_IDTF && SEqual(Idtf, C4AUL_Else))
1315  {
1316  Shift();
1317  // expect a command now
1318  stmt->iffalse = Parse_Statement();
1319  }
1320  return stmt;
1321 }

References ATT_BCLOSE, ATT_BOPEN, ATT_IDTF, C4AUL_Else, Parse_Expression(), Parse_Statement(), SEqual(), and Shift().

Referenced by Parse_Statement().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Parse_PropList()

std::unique_ptr<::aul::ast::ProplistLit > C4AulParse::Parse_PropList ( )
protected

Definition at line 1241 of file C4AulParse.cpp.

1242 {
1243  auto proplist = ::aul::ast::ProplistLit::New(TokenSPos);
1244  if (TokenType == ATT_IDTF && SEqual(Idtf, C4AUL_New))
1245  {
1246  Shift();
1247  proplist->values.emplace_back(Strings.P[P_Prototype].GetCStr(), Parse_Expression());
1248  }
1249  Match(ATT_BLOPEN);
1250  while (TokenType != ATT_BLCLOSE)
1251  {
1252  std::string key;
1253  if (TokenType == ATT_IDTF)
1254  {
1255  key = Idtf;
1256  Shift();
1257  }
1258  else if (TokenType == ATT_STRING)
1259  {
1260  key = cStr->GetCStr();
1261  Shift();
1262  }
1263  else UnexpectedToken("string or identifier");
1264  if (TokenType != ATT_COLON && TokenType != ATT_SET)
1265  UnexpectedToken("':' or '='");
1266  Shift();
1267  proplist->values.emplace_back(key, Parse_Expression());
1268  if (TokenType == ATT_COMMA)
1269  Shift();
1270  else if (TokenType != ATT_BLCLOSE)
1271  UnexpectedToken("'}' or ','");
1272  }
1273  Shift();
1274  return proplist;
1275 }
C4StringTable Strings
Definition: C4Globals.cpp:42
@ P_Prototype
C4String P[P_LAST]

References ATT_BLCLOSE, ATT_BLOPEN, ATT_COLON, ATT_COMMA, ATT_IDTF, ATT_SET, ATT_STRING, C4AUL_New, C4String::GetCStr(), C4StringTable::P, P_Prototype, Parse_Expression(), SEqual(), Shift(), and Strings.

Referenced by Parse_Expression().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Parse_Script()

std::unique_ptr<::aul::ast::Script > C4AulParse::Parse_Script ( C4ScriptHost scripthost)

Definition at line 878 of file C4AulParse.cpp.

879 {
880  pOrgScript = scripthost;
881  SPos = pOrgScript->Script.getData();
882  const char * SPos0 = SPos;
883  bool first_error = true;
884  auto script = ::aul::ast::Script::New(SPos0);
885  while (true) try
886  {
887  // Go to the next token if the current token could not be processed or no token has yet been parsed
888  if (SPos == SPos0)
889  {
890  Shift();
891  }
892  SPos0 = SPos;
893  switch (TokenType)
894  {
895  case ATT_DIR:
896  // check for include statement
897  if (SEqual(Idtf, C4AUL_Include))
898  {
899  Shift();
900  // get id of script to include
901  Check(ATT_IDTF, "script name");
902  script->declarations.push_back(::aul::ast::IncludePragma::New(TokenSPos, Idtf));
903  Shift();
904  }
905  else if (SEqual(Idtf, C4AUL_Append))
906  {
907  if (pOrgScript->GetPropList()->GetDef())
908  throw C4AulParseError(this, "#appendto in a Definition");
909  Shift();
910  // get id of script to include/append
911  switch (TokenType)
912  {
913  case ATT_IDTF:
914  script->declarations.push_back(::aul::ast::AppendtoPragma::New(TokenSPos, Idtf));
915  break;
916  case ATT_OPERATOR:
917  if (SEqual(C4ScriptOpMap[cInt].Identifier, "*"))
918  {
919  script->declarations.push_back(::aul::ast::AppendtoPragma::New(TokenSPos));
920  break;
921  }
922  //fallthrough
923  default:
924  // -> ID expected
925  UnexpectedToken("identifier or '*'");
926  }
927  Shift();
928  }
929  else
930  // -> unknown directive
931  Error("unknown directive: %s", Idtf);
932  break;
933  case ATT_IDTF:
934  // need a keyword here to avoid parsing random function contents
935  // after a syntax error in a function
936  // check for object-local variable definition (local)
937  if (SEqual(Idtf, C4AUL_LocalNamed) || SEqual(Idtf, C4AUL_GlobalNamed))
938  {
939  script->declarations.push_back(Parse_Var());
940  Match(ATT_SCOLON);
941  }
942  // check for variable definition (static)
943  else
944  script->declarations.push_back(Parse_ToplevelFunctionDecl());
945  break;
946  case ATT_EOF:
947  return script;
948  default:
949  UnexpectedToken("declaration");
950  }
951  first_error = true;
952  }
953  catch (C4AulError &err)
954  {
955  if (first_error)
956  {
957  ++Engine->errCnt;
959  }
960  first_error = false;
961  }
962 }
#define C4AUL_Include
Definition: C4AulParse.cpp:30
#define C4AUL_Append
Definition: C4AulParse.cpp:31
#define C4AUL_LocalNamed
Definition: C4AulParse.cpp:59
#define C4AUL_GlobalNamed
Definition: C4AulParse.cpp:58
@ ATT_DIR
Definition: C4AulParse.cpp:83
C4AulScriptEngine ScriptEngine
Definition: C4Globals.cpp:43
virtual void OnError(const char *msg)=0
const char * what() const noexcept override
Definition: C4Aul.cpp:59
C4AulErrorHandler * ErrorHandler
Definition: C4Aul.h:128
virtual C4Def const * GetDef() const
Definition: C4PropList.cpp:654
StdStrBuf Script
Definition: C4ScriptHost.h:91
virtual C4PropListStatic * GetPropList()
Definition: C4ScriptHost.h:51
const char * getData() const
Definition: StdBuf.h:442

References ATT_DIR, ATT_EOF, ATT_IDTF, ATT_OPERATOR, ATT_SCOLON, C4AUL_Append, C4AUL_GlobalNamed, C4AUL_Include, C4AUL_LocalNamed, C4AulParseError, C4ScriptOpMap, C4AulScriptEngine::errCnt, C4AulScriptEngine::ErrorHandler, StdStrBuf::getData(), C4PropList::GetDef(), C4ScriptHost::GetPropList(), C4AulErrorHandler::OnError(), Parse_ToplevelFunctionDecl(), Parse_Var(), C4ScriptHost::Script, ScriptEngine, SEqual(), Shift(), and C4AulError::what().

Referenced by C4ScriptHost::Preparse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Parse_Statement()

std::unique_ptr<::aul::ast::Stmt > C4AulParse::Parse_Statement ( )
protected

Definition at line 1062 of file C4AulParse.cpp.

1063 {
1064  const char *NodeStart = TokenSPos;
1065  std::unique_ptr<::aul::ast::Stmt> stmt;
1066  switch (TokenType)
1067  {
1068  // do we have a block start?
1069  case ATT_BLOPEN:
1070  return Parse_Block();
1071  case ATT_BOPEN:
1072  case ATT_BOPEN2:
1073  case ATT_SET:
1074  case ATT_OPERATOR:
1075  case ATT_INT:
1076  case ATT_STRING:
1077  {
1078  stmt = Parse_Expression();
1079  Match(ATT_SCOLON);
1080  return stmt;
1081  }
1082  // additional function separator
1083  case ATT_SCOLON:
1084  Shift();
1085  return ::aul::ast::Noop::New(NodeStart);
1086  case ATT_IDTF:
1087  // check for variable definition
1088  if (SEqual(Idtf, C4AUL_VarNamed) || SEqual(Idtf, C4AUL_LocalNamed) || SEqual(Idtf, C4AUL_GlobalNamed))
1089  stmt = Parse_Var();
1090  // check new-form func begin
1091  else if (SEqual(Idtf, C4AUL_Func) ||
1092  SEqual(Idtf, C4AUL_Private) ||
1093  SEqual(Idtf, C4AUL_Protected) ||
1094  SEqual(Idtf, C4AUL_Public) ||
1095  SEqual(Idtf, C4AUL_Global))
1096  {
1097  throw C4AulParseError(this, "unexpected end of function");
1098  }
1099  // get function by identifier: first check special functions
1100  else if (SEqual(Idtf, C4AUL_If)) // if
1101  {
1102  return Parse_If();
1103  }
1104  else if (SEqual(Idtf, C4AUL_Else)) // else
1105  {
1106  throw C4AulParseError(this, "misplaced 'else'");
1107  }
1108  else if (SEqual(Idtf, C4AUL_Do)) // while
1109  {
1110  stmt = Parse_DoWhile();
1111  }
1112  else if (SEqual(Idtf, C4AUL_While)) // while
1113  {
1114  return Parse_While();
1115  }
1116  else if (SEqual(Idtf, C4AUL_For)) // for
1117  {
1118  PushParsePos();
1119  Shift();
1120  // Look if it's the for([var] foo in array)-form
1121  // must be followed by a bracket
1122  Match(ATT_BOPEN);
1123  // optional var
1124  if (TokenType == ATT_IDTF && SEqual(Idtf, C4AUL_VarNamed))
1125  Shift();
1126  // variable and "in"
1127  if (TokenType == ATT_IDTF
1128  && GetNextToken() == ATT_IDTF
1129  && SEqual(Idtf, C4AUL_In))
1130  {
1131  // reparse the stuff in the brackets like normal statements
1132  PopParsePos();
1133  return Parse_ForEach();
1134  }
1135  else
1136  {
1137  // reparse the stuff in the brackets like normal statements
1138  PopParsePos();
1139  return Parse_For();
1140  }
1141  }
1142  else if (SEqual(Idtf, C4AUL_Return)) // return
1143  {
1144  Shift();
1145  if (TokenType == ATT_SCOLON)
1146  {
1147  // allow return; without return value (implies nil)
1148  stmt = ::aul::ast::Return::New(NodeStart, ::aul::ast::NilLit::New(NodeStart));
1149  }
1150  else
1151  {
1152  // return retval;
1153  stmt = ::aul::ast::Return::New(NodeStart, Parse_Expression());
1154  }
1155  }
1156  else if (SEqual(Idtf, C4AUL_Break)) // break
1157  {
1158  Shift();
1159  stmt = ::aul::ast::Break::New(NodeStart);
1160  }
1161  else if (SEqual(Idtf, C4AUL_Continue)) // continue
1162  {
1163  Shift();
1164  stmt = ::aul::ast::Continue::New(NodeStart);
1165  }
1166  else
1167  {
1168  stmt = Parse_Expression();
1169  }
1170  Match(ATT_SCOLON);
1171  assert(stmt);
1172  return stmt;
1173  default:
1174  UnexpectedToken("statement");
1175  }
1176 }
#define C4AUL_Public
Definition: C4AulParse.cpp:41
#define C4AUL_Continue
Definition: C4AulParse.cpp:55
#define C4AUL_Protected
Definition: C4AulParse.cpp:40
#define C4AUL_Break
Definition: C4AulParse.cpp:54
#define C4AUL_Global
Definition: C4AulParse.cpp:42
#define C4AUL_Private
Definition: C4AulParse.cpp:39
std::unique_ptr<::aul::ast::If > Parse_If()
std::unique_ptr<::aul::ast::ForLoop > Parse_For()
std::unique_ptr<::aul::ast::WhileLoop > Parse_While()
std::unique_ptr<::aul::ast::Block > Parse_Block()
std::unique_ptr<::aul::ast::RangeLoop > Parse_ForEach()
std::unique_ptr<::aul::ast::DoLoop > Parse_DoWhile()

References ATT_BLOPEN, ATT_BOPEN, ATT_BOPEN2, ATT_IDTF, ATT_INT, ATT_OPERATOR, ATT_SCOLON, ATT_SET, ATT_STRING, C4AUL_Break, C4AUL_Continue, C4AUL_Do, C4AUL_Else, C4AUL_For, C4AUL_Func, C4AUL_Global, C4AUL_GlobalNamed, C4AUL_If, C4AUL_In, C4AUL_LocalNamed, C4AUL_Private, C4AUL_Protected, C4AUL_Public, C4AUL_Return, C4AUL_VarNamed, C4AUL_While, C4AulParseError, Parse_Block(), Parse_DoWhile(), Parse_Expression(), Parse_For(), Parse_ForEach(), Parse_If(), Parse_Var(), Parse_While(), SEqual(), and Shift().

Referenced by Parse_Block(), Parse_DoWhile(), Parse_For(), Parse_ForEach(), Parse_If(), and Parse_While().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Parse_ToplevelFunctionDecl()

std::unique_ptr<::aul::ast::FunctionDecl > C4AulParse::Parse_ToplevelFunctionDecl ( )
protected

Definition at line 964 of file C4AulParse.cpp.

965 {
966  const char *NodeStart = TokenSPos;
967  bool is_global = SEqual(Idtf, C4AUL_Global);
968  // skip access modifier
969  if (SEqual(Idtf, C4AUL_Private) ||
970  SEqual(Idtf, C4AUL_Protected) ||
971  SEqual(Idtf, C4AUL_Public) ||
972  SEqual(Idtf, C4AUL_Global))
973  {
974  Shift();
975  }
976 
977  // check for func declaration
978  if (!SEqual(Idtf, C4AUL_Func))
979  Error("Declaration expected, but found identifier: %s", Idtf);
980  Shift();
981  // get next token, must be func name
982  Check(ATT_IDTF, "function name");
983 
984  auto func = ::aul::ast::FunctionDecl::New(NodeStart, Idtf);
985  func->is_global = is_global;
986  Shift();
987  Parse_Function(func.get());
988  return func;
989 }

References ATT_IDTF, C4AUL_Func, C4AUL_Global, C4AUL_Private, C4AUL_Protected, C4AUL_Public, SEqual(), and Shift().

Referenced by Parse_DirectExec(), and Parse_Script().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Parse_Var()

std::unique_ptr<::aul::ast::VarDecl > C4AulParse::Parse_Var ( )
protected

Definition at line 1644 of file C4AulParse.cpp.

1645 {
1646  auto decl = ::aul::ast::VarDecl::New(TokenSPos);
1647  if (SEqual(Idtf, C4AUL_VarNamed))
1648  {
1649  decl->scope = ::aul::ast::VarDecl::Scope::Func;
1650  }
1651  else if (SEqual(Idtf, C4AUL_LocalNamed))
1652  {
1653  decl->scope = ::aul::ast::VarDecl::Scope::Object;
1654  }
1655  else if (SEqual(Idtf, C4AUL_GlobalNamed))
1656  {
1657  decl->scope = ::aul::ast::VarDecl::Scope::Global;
1658  }
1659  else
1660  {
1661  assert(0 && "C4AulParse::Parse_Var called with invalid parse state (current token should be scope of variable)");
1662  // Uh this shouldn't happen, ever
1663  Error("internal error: C4AulParse::Parse_Var called with invalid parse state (current token should be scope of variable, but is '%s')", Idtf);
1664  }
1665  Shift();
1666  if (TokenType == ATT_IDTF && SEqual(Idtf, C4AUL_Const))
1667  {
1668  decl->constant = true;
1669  Shift();
1670  }
1671  while (true)
1672  {
1673  // get desired variable name
1674  Check(ATT_IDTF, "variable name");
1675  std::string identifier = Idtf;
1676  Shift();
1677  ::aul::ast::ExprPtr init;
1678  if (TokenType == ATT_SET)
1679  {
1680  Shift();
1681  init = Parse_Expression();
1682  }
1683  decl->decls.push_back({ identifier, std::move(init) });
1684  if (TokenType == ATT_SCOLON)
1685  return decl;
1686  Match(ATT_COMMA, "',' or ';'");
1687  }
1688 }
#define C4AUL_Const
Definition: C4AulParse.cpp:43

References ATT_COMMA, ATT_IDTF, ATT_SCOLON, ATT_SET, C4AUL_Const, C4AUL_GlobalNamed, C4AUL_LocalNamed, C4AUL_VarNamed, aul::ast::VarDecl::Func, aul::ast::VarDecl::Global, aul::ast::VarDecl::Object, Parse_Expression(), SEqual(), and Shift().

Referenced by Parse_For(), Parse_Script(), and Parse_Statement().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Parse_While()

std::unique_ptr<::aul::ast::WhileLoop > C4AulParse::Parse_While ( )
protected

Definition at line 1292 of file C4AulParse.cpp.

1293 {
1294  auto loop = ::aul::ast::WhileLoop::New(TokenSPos);
1295  Shift();
1296  // Execute condition
1297  Match(ATT_BOPEN);
1298  loop->cond = Parse_Expression();
1299  Match(ATT_BCLOSE);
1300  // Execute body
1301  loop->body = Parse_Statement();
1302  return loop;
1303 }

References ATT_BCLOSE, ATT_BOPEN, Parse_Expression(), Parse_Statement(), and Shift().

Referenced by Parse_Statement().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Shift()

void C4AulParse::Shift ( )
protected

Definition at line 760 of file C4AulParse.cpp.

761 {
762  TokenType = GetNextToken();
763 }

Referenced by Parse_Array(), Parse_Block(), Parse_DirectExec(), Parse_DoWhile(), Parse_Expression(), Parse_ForEach(), Parse_If(), Parse_PropList(), Parse_Script(), Parse_Statement(), Parse_ToplevelFunctionDecl(), Parse_Var(), and Parse_While().

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ C4AulParseError

friend class C4AulParseError
friend

Definition at line 102 of file C4AulParse.h.

Referenced by Parse_Expression(), Parse_Script(), and Parse_Statement().


The documentation for this class was generated from the following files: