-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcSug.peggyjs
171 lines (136 loc) · 6.01 KB
/
funcSug.peggyjs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
{
let indentationCount = 0
function toIdentifier(p_stringName_or_identifier) {
if (p_stringName_or_identifier instanceof Expression) return p_stringName_or_identifier
return new Expression('identifier', p_stringName_or_identifier, p_stringName_or_identifier)
}
function toExpression(ps_name, pArrayExpression) {
return new Expression(
'expression',
[toIdentifier(ps_name)].concat(pArrayExpression),
text()
)
}
function Expression(type, content, text, exprLabel, cancelExpression, multLabel) {
this.type = type
this.content = content
this.text = text
this.exprLabel = exprLabel
this.cancelExpression = cancelExpression
this.multLabel = multLabel
this.location = location()
}
input = input.replaceAll(/([A-Za-z_][0-9A-Za-z_]*)[:]([0-9A-Za-z_]+)[:]([0-9A-Za-z_]+) <-- /g, '%setToNamespace :getFromNamespace $$$1 $2 $3 ')
input = input.replaceAll(/([A-Za-z_][0-9A-Za-z_]*)[:]([0-9A-Za-z_]+) <-- /g, '%setToNamespace $$$1 $2 ')
input = input.replaceAll(/:await\s+([A-Za-z_][0-9A-Za-z_]*)[:]([0-9A-Za-z_]+)/g, '%awaitInNamespace $$$1 $2 ')
input = input.replaceAll(/[$]([A-Za-z_][0-9A-Za-z_]*)[:]([0-9A-Za-z_]+)[:]([0-9A-Za-z_]+)/g, ':getFromNamespace :getFromNamespace $$$1 $2 $3')
input = input.replaceAll(/[$]([A-Za-z_][0-9A-Za-z_]*)[:]([0-9A-Za-z_]+)/g, ':getFromNamespace $$$1 $2')
input = input.replaceAll(/([A-Za-z_][0-9A-Za-z_]*)[.]([0-9A-Za-z_]+)[.]([0-9A-Za-z_]+) <-- /g, '%setToObject :getFromObject $$$1 $2 $3 ')
input = input.replaceAll(/([A-Za-z_][0-9A-Za-z_]*)[.]([0-9A-Za-z_]+) <-- /g, '%setToObject $$$1 $2 ')
input = input.replaceAll(/[$]([A-Za-z_][0-9A-Za-z_]*)[.]([0-9A-Za-z_]+)[.]([0-9A-Za-z_]+)/g, ':getFromObject :getFromObject $$$1 $2 $3')
input = input.replaceAll(/[$]([A-Za-z_][0-9A-Za-z_]*)[.]([0-9A-Za-z_]+)/g, ':getFromObject $$$1 $2')
input = input.replaceAll(/.var\s+([A-Za-z_][0-9A-Za-z_]*) <-- /g, '.var $1 :set $1 ')
input = input.replaceAll(/\n(\s*)(#?)(.*)<--(.*)(?=\n)/g, '\n$1$2[ $3 <- $4 ]')
input = input.replaceAll(/([A-Za-z_][0-9A-Za-z_]*)\s*<-\s*\[\s*\+\s*/g, '$1 <- [ $$$1 + ')
input = input.replaceAll(/([A-Za-z_][0-9A-Za-z_]*)\s*<-\s*\[\s*\*\s*/g, '$1 <- [ $$$1 * ')
input = input.replaceAll(/([A-Za-z_][0-9A-Za-z_]*)\s*<-\s*\[\s*\-\s*/g, '$1 <- [ $$$1 - ')
input = input.replaceAll(/([A-Za-z_][0-9A-Za-z_]*)\s*<-\s*\[\s*\/\s*/g, '$1 <- [ $$$1 / ')
input = input.replaceAll('<-', ' set ')
}
start
= program
program
= __ expr:expression __ { return new Expression('program', expr, expr.text) }
// expression (or array of expression)
expression
= expressionDeffunc
/ "((" __ exprs:expressionStar __ "))" { return toExpression('par', exprs.flat()) }
/ "(&" n:name __ exprs:expressionStar __ ")" { return toExpression( 'call', [toExpression('get', [n])].concat(exprs.flat()) ) }
/ "(" __ exprs:expressionPlus __ ")" { return new Expression('expression', exprs.flat(), text() ) }
/ "[" __ exprs:expressionPlus __ "]" { return new Expression('expression', exprs.flat(), text() ) }
/ "{" __ startExpr:expression exprLabel:exprLabel? multLabel:multLabel? exprs:WSexpression* __ "}" cancelExpression:cancelExpression? { return new Expression('expression', [startExpr].concat(exprs.flat()), text(), exprLabel, cancelExpression, multLabel ) }
/ number / string / name
/ expression0 / expression1 / expression2 / expression3 / expression4 / expression5
/ expressionBlock1 / expressionEmpty
cancelExpression
= __ "~" __ expr:expression { return expr; }
expressionStar
= expression|..,spaces|
expressionPlus
= expression|1..,spaces|
WSexpression
= spaces expr:expression { return expr; }
expression0
= "!" n:name { return toExpression(n, []) }
expression1
= "." n:name spaces e1:expression { return toExpression(n, [e1]) }
expression2
= ":" n:name spaces e1:expression spaces e2:expression { return toExpression(n, [e1, e2]) }
expression3
= "%" n:name spaces e1:expression spaces e2:expression spaces e3:expression { return toExpression(n, [e1, e2, e3]) }
expression4
= "$" n:underscoreSimpleName { return toExpression('evalget', [toIdentifier(n)]) }
expression5
= "$" expr:expression { return toExpression('get', [expr]) }
expressionDeffunc
= "{" ("deffunc"/"defFunc") spaces n:name spaces exprs:expressionPlus __ "}"
{
return [
toExpression('var', [n]),
toExpression('set', [
n,
toExpression('lambda', exprs.flat())
])
]
}
expressionBlock1
= "|" e1:expression exprLabel:exprLabel? " "* newline INDENT multLabel:multLabel? startExpr:expression restExpr:(newline SAMEDENT expression " "*)* DEDENT { return new Expression('expression', [e1,startExpr].concat(restExpr), text(), exprLabel, null, multLabel ) }
expressionEmpty
= "(" spac:__ ")" { return new Expression('expression', [], text()) }
exprLabel
= spaces "@" n:$([A-Za-z_][0-9A-Za-z_]*) { return n }
multLabel
= spaces "*" n:$([A-Za-z_][0-9A-Za-z_]*) { return n }
name
= n:(simpleName / simpleOper) { return toIdentifier(n) }
simpleName
= nonUnderscoreSimpleName / underscoreSimpleName
nonUnderscoreSimpleName
= $([A-Za-z][0-9A-Za-z_]*)
underscoreSimpleName
= $("_" [A-Za-z_][0-9A-Za-z_]*)
simpleOper
= $([=+*/<>-][_=+*/<>-]*)
__ "optSpaces"
= (comment / singleSpace / newline)*
spaces "spaces"
= (comment / singleSpace / newline)+
comment "comment"
= "#" [^\n\r]* newline
singleSpace "singleSpace"
= [ \t]
whiteSpace "whiteSpace"
= [ \t\n\r]+
newline "newline"
= [\n\r]
number "number"
= digits:("-"?[0-9]+([.][0-9]+)?) {
return new Expression(
'number',
parseFloat( text() ),
text()
)
}
string "string"
= '"' content:[^"]* '"' { return new Expression('string', content.join(""), text() ) }
/ "'" content:[^']* "'" { return new Expression('string', content.join(""), text() ) }
/ '`' content:[^`]* '`' { return new Expression('string', content.join(""), text() ) }
// inspired by https://github.com/Kantouzin/python-pegjs
SAMEDENT
= tabs:tabs &{return tabs.length == indentationCount} {}
INDENT
= tabs:tabs &{return tabs.length == indentationCount + 1} {indentationCount += 1}
DEDENT
= tabs:tabs &{return tabs.length == indentationCount - 1} {indentationCount -= 1}
tabs "tabs"
= '\t'*