def parse_first_assignment(cls, assignment): c_index, c = lineparse.find_first( lambda current_char: current_char in (',', '='), assignment) if c_index == -1: # If assignment / next variable not found c_index = len(assignment) variable_name = assignment[:c_index].strip() if not variable_name.startswith('$'): raise SyntaxError( "Variable name must start in '$'! Variable: {variable}".format( variable=variable_name)) # Initialized with an expression if c == '=': # Parse the expression if len(assignment) == c_index: raise SyntaxError("Empty Assignment Value") exp_str = assignment[c_index + 1:] exp_tree, exp_end = expressions.parse_expression( exp_str, end_options=(',', ), allow_more=True) variable_value = exp_tree else: variable_value = expnodes.ValueNode(value='') exp_end = len(assignment) return variable_name, variable_value, c_index + 1 + exp_end + 1
def get_operator(exp): op_index, op = lineparse.find_first(lambda cur_op: exp.startswith(cur_op), all_operators) if op_index == -1: return None, op_index if op in needs_space and exp[len(op):len(op)+1] != ' ': raise SyntaxError("Operator {} needs space", op) return op, len(op)
def get_name_end(exp): if not exp: raise SyntaxError("Empty variable name") if exp[0] not in name_first_letter: raise SyntaxError("First letter is invalid: {0}".format(exp[0])) char_index, _ = lineparse.find_first(lambda char: char not in name_chars, exp) if char_index == -1: char_index = len(exp) return char_index
def parse(cls, raw_lines, current_line): start_line = current_line + 1 end_line, _ = lineparse.find_first( func=lambda line: line.content in CommentsSectionDirective. end_keywords, seq=raw_lines, start=start_line) if end_line == -1: raise SyntaxError("Missing #comments-end") comment_section = '\n'.join(line.content for line in raw_lines[start_line:end_line]) return CommentsSectionDirective(comment_section), end_line + 1
def get_precedence_level(operator): index, _ = lineparse.find_first(lambda ops: operator in ops, operators_by_precedence) return index