cliparser.y 3.02 KB
Newer Older
1
2
3
4
5
6
7
/**
 * command line parser
 * @author Tobias Weber <tweber@ill.fr>
 * @date 28-may-18
 * @license see 'LICENSE' file
 */

8
9
%define parser_class_name { CliParser }
//%define api.parser.class { CliParser }
10
11
%define api.value.type variant
%define api.token.constructor
Tobias WEBER's avatar
Tobias WEBER committed
12
13
//%error-verbose
%define parse.error verbose
14
15
16
17
18
19

%code requires { #include "cliparser_types.h" }
%code { #include "cliparser.h" }
%param { CliParserContext &context }


Tobias WEBER's avatar
Tobias WEBER committed
20
// terminals
21
22
23
%token<t_real_cli> TOK_REAL
%token<std::string> TOK_STRING TOK_IDENT
%token TOK_BRACKET_OPEN TOK_BRACKET_CLOSE
Tobias WEBER's avatar
Tobias WEBER committed
24
%token TOK_SQBRACKET_OPEN TOK_SQBRACKET_CLOSE
25
%token TOK_CURLBRACKET_OPEN TOK_CURLBRACKET_CLOSE
26
27
%token TOK_PLUS TOK_MINUS TOK_MULT TOK_DIV TOK_MOD TOK_POW
%token TOK_ASSIGN
Tobias WEBER's avatar
Tobias WEBER committed
28
%token TOK_MEMBER_ACCESS
Tobias WEBER's avatar
Tobias WEBER committed
29
%token TOK_COMMA
30
%token TOK_NEWLINE
Tobias WEBER's avatar
Tobias WEBER committed
31
//%token TOK_EOF
32

Tobias WEBER's avatar
Tobias WEBER committed
33
34

// non-terminals
35
%type<std::shared_ptr<CliAST>> command commands
Tobias WEBER's avatar
Tobias WEBER committed
36
37
%type<std::shared_ptr<CliAST>> expression expressions
%type<std::shared_ptr<CliAST>> ident
38

Tobias WEBER's avatar
Tobias WEBER committed
39
40

// operator precedence
41
42
43
%right TOK_ASSIGN
%left TOK_PLUS TOK_MINUS
%left TOK_MULT TOK_DIV TOK_MOD
Tobias WEBER's avatar
Tobias WEBER committed
44
%right PREC_UNARY_PLUSMINUS
45
%nonassoc TOK_POW
Tobias WEBER's avatar
Tobias WEBER committed
46
%nonassoc TOK_MEMBER_ACCESS
47
%left TOK_SQBRACKET_OPEN TOK_CURLBRACKET_OPEN
Tobias WEBER's avatar
Tobias WEBER committed
48

49
50
51
52
53

%%

commands
	: commands command
54
	    { context.AddAST($2); }
55
56
57
58
59
60
	| /* eps */
	    { $$ = nullptr; }
	;

command
	: expression TOK_NEWLINE
61
	    { $$ = $1; }
62
63
64
65
66
67
68
69
70
	| TOK_NEWLINE
	    { $$ = nullptr; }
	;

ident
	: TOK_IDENT
	    { $$ = std::make_shared<CliASTIdent>($1); }
	;

Tobias WEBER's avatar
Tobias WEBER committed
71
72
73
74
75
76
77
78
79
expressions
	: expressions TOK_COMMA expression
		{ $$ = std::make_shared<CliASTExprList>($1, $3); }
	| expression
		{ $$ = $1; }
	| /* eps */
	    { $$ = nullptr; }
	;

80
81
82
expression
	: ident TOK_ASSIGN expression
		{ $$ = std::make_shared<CliASTAssign>($1, $3); }
Tobias WEBER's avatar
Tobias WEBER committed
83
84
	| ident TOK_BRACKET_OPEN expressions TOK_BRACKET_CLOSE
		{ $$ = std::make_shared<CliASTCall>($1, $3); }
Tobias WEBER's avatar
Tobias WEBER committed
85
86
87
	| expression TOK_MEMBER_ACCESS ident TOK_BRACKET_OPEN expressions TOK_BRACKET_CLOSE
		{ $$ = std::make_shared<CliASTCall>($3, std::make_shared<CliASTExprList>($1, $5)); }

Tobias WEBER's avatar
Tobias WEBER committed
88
89
	| TOK_SQBRACKET_OPEN expressions TOK_SQBRACKET_CLOSE
		{ $$ = std::make_shared<CliASTArray>($2); }
90
91
	| expression TOK_SQBRACKET_OPEN expression TOK_SQBRACKET_CLOSE
		{ $$ = std::make_shared<CliASTArrayAccess>($1, $3); }
92
93
94

	| TOK_BRACKET_OPEN expression TOK_BRACKET_CLOSE
		{ $$ = $2; }
Tobias WEBER's avatar
Tobias WEBER committed
95
	| TOK_PLUS expression %prec PREC_UNARY_PLUSMINUS
96
		{ $$ = $2; }
Tobias WEBER's avatar
Tobias WEBER committed
97
	| TOK_MINUS expression %prec PREC_UNARY_PLUSMINUS
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
		{ $$ = std::make_shared<CliASTMinus>(nullptr, $2); }

	| expression TOK_PLUS expression
		{ $$ = std::make_shared<CliASTPlus>($1, $3); }
	| expression TOK_MINUS expression
		{ $$ = std::make_shared<CliASTMinus>($1, $3); }
	| expression TOK_MULT expression
		{ $$ = std::make_shared<CliASTMult>($1, $3); }
	| expression TOK_DIV expression
		{ $$ = std::make_shared<CliASTDiv>($1, $3); }
	| expression TOK_MOD expression
		{ $$ = std::make_shared<CliASTMod>($1, $3); }
	| expression TOK_POW expression
		{ $$ = std::make_shared<CliASTPow>($1, $3); }

	| ident
		{ $$ = $1; }
	| TOK_REAL
		{ $$ = std::make_shared<CliASTReal>($1); }
	| TOK_STRING
		{ $$ = std::make_shared<CliASTString>($1); }
	;
%%