Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/cbhaley/calibre
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Nov 11, 2021
2 parents 90aac28 + 89ce945 commit 0c3245e
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/calibre/utils/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Node:
NODE_CONTINUE = 23
NODE_RETURN = 24
NODE_CHARACTER = 25
NODE_STRCAT = 26

def __init__(self, line_number, name):
self.my_line_number = line_number
Expand Down Expand Up @@ -255,6 +256,13 @@ def __init__(self, line_number, expression):
self.expression = expression


class StrcatNode(Node):
def __init__(self, line_number, expression_list):
Node.__init__(self, line_number, 'strcat()')
self.node_type = self.NODE_STRCAT
self.expression_list = expression_list


class _Parser:
LEX_OP = 1
LEX_ID = 2
Expand Down Expand Up @@ -535,9 +543,9 @@ def call_expression(self, name, arguments):
lambda ln, args: FieldNode(ln, args[0])),
'raw_field': (lambda args: len(args) == 1,
lambda ln, args: RawFieldNode(ln, *args)),
'test': (lambda args: len(args) == 1,
'test': (lambda args: len(args) == 3,
lambda ln, args: IfNode(ln, args[0], (args[1],), (args[2],))),
'first_non_empty': (lambda args: len(args) == 1,
'first_non_empty': (lambda args: len(args) >= 1,
lambda ln, args: FirstNonEmptyNode(ln, args)),
'assign': (lambda args: len(args) == 2 and len(args[0]) == 1 and args[0][0].node_type == Node.NODE_RVALUE,
lambda ln, args: AssignNode(ln, args[0][0].name, args[1])),
Expand All @@ -547,6 +555,8 @@ def call_expression(self, name, arguments):
lambda ln, args: CharacterNode(ln, args[0])),
'print': (lambda _: True,
lambda ln, args: PrintNode(ln, args)),
'strcat': (lambda _: True,
lambda ln, args: StrcatNode(ln, args))
}

def expr(self):
Expand Down Expand Up @@ -935,6 +945,12 @@ def do_node_first_non_empty(self, prog):
self.break_reporter(prog.node_name, '', prog.line_number)
return ''

def do_node_strcat(self, prog):
res = ''.join([self.expr(expr) for expr in prog.expression_list])
if self.break_reporter:
self.break_reporter(prog.node_name, res, prog.line_number)
return res

def do_node_for(self, prog):
line_number = prog.line_number
try:
Expand Down Expand Up @@ -1125,6 +1141,7 @@ def do_node_print(self, prog):
Node.NODE_CONTINUE: do_node_continue,
Node.NODE_RETURN: do_node_return,
Node.NODE_CHARACTER: do_node_character,
Node.NODE_STRCAT: do_node_strcat,
}

def expr(self, prog):
Expand Down

0 comments on commit 0c3245e

Please sign in to comment.