Skip to content

Commit c9a4c70

Browse files
committed
simplify parser
1 parent 97ba743 commit c9a4c70

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

SHELL_REFERENCE.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,36 @@ a *b = list_natural_numbers
219219
# pure functions can be executed in parallel, using a thread pool
220220
range 10 >>= math 10 * $ + 1 |> reduce sum
221221
```
222+
223+
### Queries
224+
225+
#### Show tabular data
226+
227+
*"Show the users"*
228+
229+
```diff
230+
- SELECT * FROM users
231+
+ show users
232+
| | email | name |
233+
|-----:|:-------------------|:-------|
234+
| 1000 | name.0@company.com | name_0 |
235+
| 1001 | name.1@company.com | name_1 |
236+
| 1002 | name.2@company.com | name_2 |
237+
```
238+
239+
#### Inner join
240+
241+
*"Find users that own a at least one document"*
242+
243+
```diff
244+
- SELECT name FROM users INNER JOIN documents ON users.id == document.owner
245+
+ {users | users.id in {documents.owner}} >>= show $1.name
246+
```
247+
248+
*"Show documents of each user"*
249+
250+
```diff
251+
- SELECT users.name, documents.name FROM users LEFT JOIN documents ON users.id == document.owner
252+
+ { users documents | users.id = documents.owner } >>= show $1.email $2.name
253+
```
254+

src/mash/shell/grammer/parser.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
1919
"""
2020
from logging import getLogger
21-
import logging
2221
import ply.yacc as yacc
2322
from mash.shell.ast import Assign, BashPipe, BinaryExpression, Else, ElseIf, ElseIfThen, FunctionDefinition, If, IfThen, IfThenElse, Indent, InlineFunctionDefinition, Lines, LogicExpression, Map, Math, Method, Pipe, Quoted, Return, Shell, Terms, Then, Variable, Word
2423
from mash.shell.grammer.tokenizer import main, tokens
@@ -29,8 +28,8 @@
2928

3029

3130
def parse(text, init=True):
32-
# TODO use Node/Tree classes rather than tuples
33-
# e.g. classes with a .run() method (extends Runnable<>)
31+
"""Implement ply methods
32+
"""
3433

3534
precedence = (
3635
('left', 'BREAK'),
@@ -210,7 +209,9 @@ def p_expression_full_conditional(p):
210209
p[0] = p[1]
211210

212211
def p_expression(p):
213-
'expression : basic_expression'
212+
"""expression : join
213+
| logic_expression
214+
"""
214215
p[0] = p[1]
215216

216217
def p_shell(p):
@@ -225,13 +226,6 @@ def p_math(p):
225226
'expression : MATH expression'
226227
p[0] = Math(p[2])
227228

228-
def p_basic_expression(p):
229-
"""basic_expression : join
230-
| logic_expression
231-
| terms
232-
"""
233-
p[0] = p[1]
234-
235229
def p_logic_binary(p):
236230
"""join : logic_expression AND join
237231
| logic_expression AND logic_expression
@@ -342,12 +336,11 @@ def p_error(p):
342336
tokenizer = main()
343337
else:
344338
tokenizer.clone()
345-
339+
346340
log = getLogger()
347341
parser = yacc.yacc(debug=log)
348342
if not isinstance(text, str):
349-
text
350-
raise
343+
raise ValueError(text)
351344

352345
# insert a newline to allow empty strings to be matched
353346
return parser.parse('\n' + text)

0 commit comments

Comments
 (0)