-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathParser.txt
132 lines (102 loc) · 4.48 KB
/
Parser.txt
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
*vital/Text/Parser.txt* parser library.
Maintainer: rbtnn <[email protected]>
==============================================================================
CONTENTS *Vital.Text.Parser-contents*
INTRODUCTION |Vital.Text.Parser-introduction|
INTERFACE |Vital.Text.Parser-interface|
FUNCTIONS |Vital.Text.Parser-functions|
OBJECTS |Vital.Text.Parser-objects|
Parser Object |Vital.Text.Parser-Parser|
EXAMPLES |Vital.Text.Parser-examples|
==============================================================================
INTRODUCTION *Vital.Text.Parser-introduction*
*Vital.Text.Parser* is parser library.
==============================================================================
INTERFACE *Vital.Text.Parser-interface*
------------------------------------------------------------------------------
FUNCTIONS *Vital.Text.Parser-functions*
parser() *Vital.Text.Parser.parser()*
Creates a new Parser object.
A Parser object has exec({expr}).
==============================================================================
OBJECTS *Vital.Text.Parser-objects*
------------------------------------------------------------------------------
Parser Object *Vital.Text.Parser-Parser*
Parser.exec({expr}) *Vital.Text.Parser-Parser.exec()*
Begins parsing {expr}.
Parser.end() *Vital.Text.Parser-Parser.end()*
Returns 1 if next() is the end of {lexed_tokens}, otherwise zero.
Parser.next() *Vital.Text.Parser-Parser.next()*
Returns the next token. This function does not consume.
Parser.next_is({labels}) *Vital.Text.Parser-Parser.next_is()*
Returns 1 if next().label is contained {labels}, otherwise zero.
Parser.consume() *Vital.Text.Parser-Parser.consume()*
Consumes the next token from {lexed_tokens}. Return this token.
Parser.ignore() *Vital.Text.Parser-Parser.ignore()*
Consumes it while the next token is contained 'ignore_labels'.
Parser.config({dict}) *Vital.Text.Parser-Parser.config()*
{dict} is options of this Parser object.
{dict} can have following keys.
'ignore_labels' : It is used Parser.ignore().
==============================================================================
EXAMPLES *Vital.Text.Parser-examples*
>
let s:V = vital#{plugin-name}#new()
let s:L = s:V.import('Text.Lexer')
let s:P = s:V.import('Text.Parser')
let s:rules = [
\ [ 'NUMBER', '[0-9]\+' ],
\ [ 'PLUS', '+' ],
\ [ 'MUL', '*' ],
\ [ 'MINUS', '-' ],
\ [ 'DIV', '/' ],
\ [ 'WS', '\s\+' ],
\ ]
let s:lexed_obj = s:L.lexer(s:rules).exec('12 + 2 - 9 * 3 - 18 / 3 * 2')
let s:parser_obj = s:P.parser().exec(s:lexed_obj)
call s:parser_obj.config({ 'ignore_labels' : ['WS'] })
function! s:parser_obj.number() dict
call self.ignore()
if self.next_is('NUMBER')
return eval(self.consume().matched_text)
else
throw 'syntax error'
endif
endfunction
function! s:parser_obj.term() dict
let lhs = self.number()
while ! self.end()
call self.ignore()
if self.next_is(['MUL'])
call self.consume()
let lhs = lhs * self.number()
elseif self.next_is(['DIV'])
call self.consume()
let lhs = lhs / self.number()
else
break
endif
endwhile
return lhs
endfunction
function! s:parser_obj.expression() dict
let lhs = self.term()
while ! self.end()
call self.ignore()
if self.next_is(['PLUS'])
call self.consume()
let lhs = lhs + self.term()
elseif self.next_is(['MINUS'])
call self.consume()
let lhs = lhs - self.term()
else
break
endif
endwhile
return lhs
endfunction
echo s:parser_obj.expression()
" -25
<
==============================================================================
vim:tw=78:fo=tcq2mM:ts=8:ft=help:norl