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

Function parse_expr

tools/npy_tempita/__init__.py:846–895  ·  view source on GitHub ↗
(tokens, name, context=())

Source from the content-addressed store, hash-verified

844
845
846def parse_expr(tokens, name, context=()):
847 if isinstance(tokens[0], basestring_):
848 return tokens[0], tokens[1:]
849 expr, pos = tokens[0]
850 expr = expr.strip()
851 if expr.startswith('py:'):
852 expr = expr[3:].lstrip(' \t')
853 if expr.startswith('\n') or expr.startswith('\r'):
854 expr = expr.lstrip('\r\n')
855 if '\r' in expr:
856 expr = expr.replace('\r\n', '\n')
857 expr = expr.replace('\r', '')
858 expr += '\n'
859 else:
860 if '\n' in expr:
861 raise TemplateError(
862 'Multi-line py blocks must start with a newline',
863 position=pos, name=name)
864 return ('py', pos, expr), tokens[1:]
865 elif expr in ('continue', 'break'):
866 if 'for' not in context:
867 raise TemplateError(
868 'continue outside of for loop',
869 position=pos, name=name)
870 return (expr, pos), tokens[1:]
871 elif expr.startswith('if '):
872 return parse_cond(tokens, name, context)
873 elif (expr.startswith('elif ') or expr == 'else'):
874 raise TemplateError(
875 '%s outside of an if block' % expr.split()[0],
876 position=pos, name=name)
877 elif expr in ('if', 'elif', 'for'):
878 raise TemplateError(
879 '%s with no expression' % expr,
880 position=pos, name=name)
881 elif expr in ('endif', 'endfor', 'enddef'):
882 raise TemplateError(
883 'Unexpected %s' % expr,
884 position=pos, name=name)
885 elif expr.startswith('for '):
886 return parse_for(tokens, name, context)
887 elif expr.startswith('default '):
888 return parse_default(tokens, name, context)
889 elif expr.startswith('inherit '):
890 return parse_inherit(tokens, name, context)
891 elif expr.startswith('def '):
892 return parse_def(tokens, name, context)
893 elif expr.startswith('#'):
894 return ('comment', pos, tokens[0][0]), tokens[1:]
895 return ('expr', pos, tokens[0][0]), tokens[1:]
896
897
898def parse_cond(tokens, name, context):

Callers 4

parseFunction · 0.85
parse_one_condFunction · 0.85
parse_forFunction · 0.85
parse_defFunction · 0.85

Calls 11

TemplateErrorClass · 0.85
parse_condFunction · 0.85
parse_forFunction · 0.85
parse_defaultFunction · 0.85
parse_inheritFunction · 0.85
parse_defFunction · 0.85
stripMethod · 0.80
startswithMethod · 0.80
lstripMethod · 0.80
replaceMethod · 0.80
splitMethod · 0.45

Tested by

no test coverage detected