Mercurial > python-cmd2
annotate lineend.py @ 61:82891de09dbf
before splicing new terminator parsing in
author | catherine@Elli.myhome.westell.com |
---|---|
date | Fri, 20 Jun 2008 20:41:19 -0400 |
parents | 682588392eaf |
children | 95e239c87010 |
rev | line source |
---|---|
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
1 import pyparsing, sys, doctest |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
2 |
61
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
3 terminators = (';', 'EOF') |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
4 |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
5 #pattern = pyparsing.Optional(pyparsing.CharsNotIn(terminators)) + pyparsing.oneOf(';') |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
6 complete_pattern = pyparsing.SkipTo(pyparsing.oneOf(terminators)) |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
7 complete_pattern.ignore(pyparsing.sglQuotedString) |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
8 complete_pattern.ignore(pyparsing.sglQuotedString) |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
9 |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
10 redirect_pattern = pyparsing.Optional(pyparsing.CharsNotIn('<>')) + pyparsing.oneOf('< >') + pyparsing.Word(pyparsing.printables) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
11 redirect_pattern.ignore(pyparsing.sglQuotedString) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
12 redirect_pattern.ignore(pyparsing.sglQuotedString) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
13 |
61
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
14 pipe_pattern = pyparsing.Optional(pyparsing.CharsNotIn('|')) + '|' + pyparsing.SkipTo(pyparsing.StringEnd()) |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
15 pipe_pattern.ignore(pyparsing.sglQuotedString) |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
16 pipe_pattern.ignore(pyparsing.sglQuotedString) |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
17 |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
18 def parse(txt, mustBeTerminated=False): |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
19 """ |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
20 >>> sorted(parse('select * from dual;', True).items()) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
21 [('complete', True), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)] |
61
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
22 >>> sorted(parse('select * from dual E', True).items()) |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
23 [('complete', False), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)] |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
24 >>> sorted(parse('select * from', True).items()) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
25 [('complete', False), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)] |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
26 >>> sorted(parse('select * from dual; > result.txt', True).items()) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
27 [('complete', True), ('inputFrom', None), ('outputTo', 'result.txt'), ('pipeTo', None)] |
61
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
28 >>> sorted(parse("select * from dual where val = 'x > y'", True).items()) |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
29 [('complete', False), ('inputFrom', None), ('outputTo', None), ('pipeTo', None)] |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
30 >>> sorted(parse('select * from dual; | wc -c', True).items()) |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
31 [('complete', True), ('inputFrom', None), ('outputTo', None), ('pipeTo', 'wc -c')] |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
32 """ |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
33 result = {'complete': not mustBeTerminated, 'inputFrom': None, 'outputTo': None, 'pipeTo': None} |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
34 if mustBeTerminated: |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
35 try: |
61
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
36 complete_pattern.parseString(txt) |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
37 result['complete'] = True |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
38 except pyparsing.ParseException: |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
39 pass |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
40 |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
41 if result['complete']: |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
42 try: |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
43 parsed = redirect_pattern.parseString(txt) |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
44 if parsed[1] == '>': |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
45 result['outputTo'] = parsed[2] |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
46 else: |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
47 result['inputFrom'] = parsed[2] |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
48 except pyparsing.ParseException: |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
49 pass |
61
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
50 try: |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
51 parsed = pipe_pattern.parseString(txt) |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
52 result['pipeTo'] = parsed[2] |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
53 except pyparsing.ParseException: |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
54 pass |
82891de09dbf
before splicing new terminator parsing in
catherine@Elli.myhome.westell.com
parents:
60
diff
changeset
|
55 |
60
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
56 return result |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
57 |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
58 if __name__ == '__main__': |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
59 doctest.testmod() |
682588392eaf
experiments with parsing in lineend
catherine@Elli.myhome.westell.com
parents:
diff
changeset
|
60 |