Merge branch 'master' of https://github.com/cbhaley/calibre into master

This commit is contained in:
Kovid Goyal 2020-10-12 17:11:11 +05:30
commit be6ce66ed1
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 13 additions and 3 deletions

View File

@ -195,7 +195,8 @@ The syntax of the language is shown by the following grammar. For a discussion o
function ::= identifier '(' expression [ ',' expression ]* ')'
compare ::= expression compare_op expression
compare_op ::= '==' | '!=' | '>=' | '>' | '<=' | '<' | '==#' | '!=#' | '>=#' | '>#' | '<=#' | '<#'
if_expression ::= 'if' expression 'then' expression_list ['else' statement] 'fi'
if_expression ::= 'if' expression 'then' expression_list [elif_expression] ['else' expression_list] 'fi'
elif_expression ::= 'elif' expression 'then' expression_list elif_expression | ''
assignment ::= identifier '=' expression
constant ::= " string " | ' string ' | number
identifier ::= sequence of letters or ``_`` characters

View File

@ -44,7 +44,7 @@ class TemplateHighlighter(QSyntaxHighlighter):
Formats = {}
BN_FACTOR = 1000
KEYWORDS = ["program", 'if', 'then', 'else', 'fi']
KEYWORDS = ["program", 'if', 'then', 'else', 'elif', 'fi']
def __init__(self, parent=None):
super(TemplateHighlighter, self).__init__(parent)

View File

@ -233,6 +233,13 @@ class _Parser(object):
except:
return False
def token_is_elif(self):
try:
token = self.prog[self.lex_pos]
return token[1] == 'elif' and token[0] == self.LEX_KEYWORD
except:
return False
def token_is_fi(self):
try:
token = self.prog[self.lex_pos]
@ -282,6 +289,8 @@ class _Parser(object):
self.error(_("Missing 'then' in if statement"))
self.consume()
then_part = self.expression_list()
if self.token_is_elif():
return IfNode(condition, then_part, [self.if_expression(),])
if self.token_is_else():
self.consume()
else_part = self.expression_list()
@ -609,7 +618,7 @@ class TemplateFormatter(string.Formatter):
lex_scanner = re.Scanner([
(r'(==#|!=#|<=#|<#|>=#|>#)', lambda x,t: (_Parser.LEX_NUMERIC_INFIX, t)),
(r'(==|!=|<=|<|>=|>)', lambda x,t: (_Parser.LEX_STRING_INFIX, t)), # noqa
(r'(if|then|else|fi)\b', lambda x,t: (_Parser.LEX_KEYWORD, t)), # noqa
(r'(if|then|else|elif|fi)\b',lambda x,t: (_Parser.LEX_KEYWORD, t)), # noqa
(r'[(),=;]', lambda x,t: (_Parser.LEX_OP, t)), # noqa
(r'-?[\d\.]+', lambda x,t: (_Parser.LEX_CONST, t)), # noqa
(r'\$', lambda x,t: (_Parser.LEX_ID, t)), # noqa