MCPcopy Create free account
hub / github.com/numpy/numpy / parse

Function parse

tools/npy_tempita/__init__.py:777–843  ·  view source on GitHub ↗

r""" Parses a string into a kind of AST >>> parse('{{x}}') [('expr', (1, 3), 'x')] >>> parse('foo') ['foo'] >>> parse('{{if x}}test{{endif}}') [('cond', (1, 3), ('if', (1, 3), 'x', ['test']))] >>> parse( ... 'series->{{for x in

(s, name=None, line_offset=0, delimiters=None)

Source from the content-addressed store, hash-verified

775
776
777def parse(s, name=None, line_offset=0, delimiters=None):
778 r"""
779 Parses a string into a kind of AST
780
781 >>> parse('{{x}}')
782 [('expr', (1, 3), 'x')]
783 >>> parse('foo')
784 ['foo']
785 >>> parse('{{if x}}test{{endif}}')
786 [('cond', (1, 3), ('if', (1, 3), 'x', ['test']))]
787 >>> parse(
788 ... 'series->{{for x in y}}x={{x}}{{endfor}}'
789 ... ) #doctest: +NORMALIZE_WHITESPACE
790 ['series->',
791 ('for', (1, 11), ('x',), 'y', ['x=', ('expr', (1, 27), 'x')])]
792 >>> parse('{{for x, y in z:}}{{continue}}{{endfor}}')
793 [('for', (1, 3), ('x', 'y'), 'z', [('continue', (1, 21))])]
794 >>> parse('{{py:x=1}}')
795 [('py', (1, 3), 'x=1')]
796 >>> parse(
797 ... '{{if x}}a{{elif y}}b{{else}}c{{endif}}'
798 ... ) #doctest: +NORMALIZE_WHITESPACE
799 [('cond', (1, 3), ('if', (1, 3), 'x', ['a']),
800 ('elif', (1, 12), 'y', ['b']), ('else', (1, 23), None, ['c']))]
801
802 Some exceptions::
803
804 >>> parse('{{continue}}')
805 Traceback (most recent call last):
806 ...
807 tempita.TemplateError: continue outside of for loop at line 1 column 3
808 >>> parse('{{if x}}foo')
809 Traceback (most recent call last):
810 ...
811 tempita.TemplateError: No {{endif}} at line 1 column 3
812 >>> parse('{{else}}')
813 Traceback (most recent call last):
814 ...
815 tempita.TemplateError: else outside of an if block at line 1 column 3
816 >>> parse('{{if x}}{{for x in y}}{{endif}}{{endfor}}')
817 Traceback (most recent call last):
818 ...
819 tempita.TemplateError: Unexpected endif at line 1 column 25
820 >>> parse('{{if}}{{endif}}')
821 Traceback (most recent call last):
822 ...
823 tempita.TemplateError: if with no expression at line 1 column 3
824 >>> parse('{{for x y}}{{endfor}}')
825 Traceback (most recent call last):
826 ...
827 tempita.TemplateError: Bad for (no "in") in 'x y' at line 1 column 3
828 >>> parse('{{py:x=1\ny=2}}') #doctest: +NORMALIZE_WHITESPACE
829 Traceback (most recent call last):
830 ...
831 tempita.TemplateError: Multi-line py blocks must start
832 with a newline at line 1 column 3
833 """
834

Callers 1

__init__Method · 0.70

Calls 2

lexFunction · 0.85
parse_exprFunction · 0.85

Tested by

no test coverage detected