-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsparse.h
102 lines (84 loc) · 2.53 KB
/
jsparse.h
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
// jsparse.h - Javascript parser
#include "jslex.h"
#include <stdio.h>
#include "errcodes.h"
#include "AST.h"
namespace js2cpp
{
class AST;
class GlobalText : public SourceText
{
public:
GlobalText(const char* s);
virtual const char *Title();
virtual bool ReadLine(char *buffer, int buflen);
private:
const char* source;
};
class Parser
{
public:
Parser(Lexer* plex, ErrorSink* perr);
~Parser();
AST* Parse(void);
// Parse the lexical stream and return an
// abstract syntax tree.
private:
Lexer& lex; // pointer to current token stream
GlobalText srcGlob; // source text, global defs
ErrorSink& err;
Token token; // current token
bool bNewline; // current token starts a new line.
Token peeked[6]; // token look-ahead.
int nPeeked; // number of peeked tokens
int nLitFuncs; // counts literal functions
bool Advance(void);
// Read the next token
bool Advance(TokenType tt);
// Advance over token of specified type,
// or report error.
const Token& Peek(int i);
// token i positions ahead of current token
// Peek(0) returns the current token.
// Check for a semicolon (or '}')
// Interpolates one if there is a line break!
bool SoftSemicolon(void);
AST* Statements(void);
AST* Statement(void);
AST* Function(void);
AST* FunctionLiteral(void);
AST* FormalParams(void);
AST* Block(void);
AST* VarDecl(void);
AST* VarExpr(void);
AST* Expression(void);
AST* ExprList(void);
AST* AssignmentExpr(void);
AST* ConditionalExpr(void);
AST* Term(void);
AST* ObjectLiteral(void);
AST* ArrayLiteral(void);
AST* ElementList(void);
// Scoping of variables - done during parse
//
aScope* scope[8]; // scopes
int nesting; // scope nesting depth (0 in global scope)
void InitGlobalScope(void);
void StartScope(const char *name); // enter a new scope
void EndScope(AST* node);
// close off and clean up a scope
// attach it to the specified node
// Export any refs to the surrounding scope
void DeclareExternal(const char* extname);
void DeclareVariable(const char* varname, AST* init);
void DeclareFunction(const char* fname, AST* fun);
void DeclareLiteralFunction(AST* def);
void Reference(const char* id);
// Reporting syntax errors and warnings
//
void Warning(ErrorCode w);
// Issue a warning at the current token
void Error(ErrorCode e, const char *note = "");
// Report an error at the current token
}; // Parser
} // namespace