Commit cf2c2df7 authored by Stefan Behnel's avatar Stefan Behnel

allow useless trailing semicolons in some places where people tend to put...

allow useless trailing semicolons in some places where people tend to put them, but warn about them (trac ticket 832)
parent 7a47dfda
...@@ -1226,7 +1226,7 @@ def p_pass_statement(s, with_newline = 0): ...@@ -1226,7 +1226,7 @@ def p_pass_statement(s, with_newline = 0):
pos = s.position() pos = s.position()
s.expect('pass') s.expect('pass')
if with_newline: if with_newline:
s.expect_newline("Expected a newline") s.expect_newline("Expected a newline", ignore_semicolon=True)
return Nodes.PassStatNode(pos) return Nodes.PassStatNode(pos)
def p_break_statement(s): def p_break_statement(s):
...@@ -1793,7 +1793,7 @@ def p_DEF_statement(s): ...@@ -1793,7 +1793,7 @@ def p_DEF_statement(s):
value = expr.compile_time_value(denv) value = expr.compile_time_value(denv)
#print "p_DEF_statement: %s = %r" % (name, value) ### #print "p_DEF_statement: %s = %r" % (name, value) ###
denv.declare(name, value) denv.declare(name, value)
s.expect_newline() s.expect_newline("Expected a newline", ignore_semicolon=True)
return Nodes.PassStatNode(pos) return Nodes.PassStatNode(pos)
def p_IF_statement(s, ctx): def p_IF_statement(s, ctx):
...@@ -1950,7 +1950,7 @@ def p_suite_with_docstring(s, ctx, with_doc_only=False): ...@@ -1950,7 +1950,7 @@ def p_suite_with_docstring(s, ctx, with_doc_only=False):
body = p_simple_statement_list(s, ctx) body = p_simple_statement_list(s, ctx)
else: else:
body = p_pass_statement(s) body = p_pass_statement(s)
s.expect_newline("Syntax error in declarations") s.expect_newline("Syntax error in declarations", ignore_semicolon=True)
if not with_doc_only: if not with_doc_only:
doc, body = _extract_docstring(body) doc, body = _extract_docstring(body)
return doc, body return doc, body
...@@ -2842,7 +2842,7 @@ def p_c_func_or_var_declaration(s, pos, ctx): ...@@ -2842,7 +2842,7 @@ def p_c_func_or_var_declaration(s, pos, ctx):
assignable = 1, nonempty = 1) assignable = 1, nonempty = 1)
declarators.append(declarator) declarators.append(declarator)
doc_line = s.start_line + 1 doc_line = s.start_line + 1
s.expect_newline("Syntax error in C variable declaration") s.expect_newline("Syntax error in C variable declaration", ignore_semicolon=True)
if ctx.level in ('c_class', 'c_class_pxd') and s.start_line == doc_line: if ctx.level in ('c_class', 'c_class_pxd') and s.start_line == doc_line:
doc = p_doc_string(s) doc = p_doc_string(s)
else: else:
...@@ -2876,7 +2876,7 @@ def p_ctypedef_statement(s, ctx): ...@@ -2876,7 +2876,7 @@ def p_ctypedef_statement(s, ctx):
else: else:
base_type = p_c_base_type(s, nonempty = 1) base_type = p_c_base_type(s, nonempty = 1)
declarator = p_c_declarator(s, ctx, is_type = 1, nonempty = 1) declarator = p_c_declarator(s, ctx, is_type = 1, nonempty = 1)
s.expect_newline("Syntax error in ctypedef statement") s.expect_newline("Syntax error in ctypedef statement", ignore_semicolon=True)
return Nodes.CTypeDefNode( return Nodes.CTypeDefNode(
pos, base_type = base_type, pos, base_type = base_type,
declarator = declarator, declarator = declarator,
...@@ -3090,8 +3090,7 @@ def p_ignorable_statement(s): ...@@ -3090,8 +3090,7 @@ def p_ignorable_statement(s):
if s.sy == 'BEGIN_STRING': if s.sy == 'BEGIN_STRING':
pos = s.position() pos = s.position()
string_node = p_atom(s) string_node = p_atom(s)
if s.sy != 'EOF': s.expect_newline("Syntax error in string", ignore_semicolon=True)
s.expect_newline("Syntax error in string")
return Nodes.ExprStatNode(pos, expr=string_node) return Nodes.ExprStatNode(pos, expr=string_node)
return None return None
...@@ -3100,8 +3099,7 @@ def p_doc_string(s): ...@@ -3100,8 +3099,7 @@ def p_doc_string(s):
if s.sy == 'BEGIN_STRING': if s.sy == 'BEGIN_STRING':
pos = s.position() pos = s.position()
kind, bytes_result, unicode_result = p_cat_string_literal(s) kind, bytes_result, unicode_result = p_cat_string_literal(s)
if s.sy != 'EOF': s.expect_newline("Syntax error in doc string", ignore_semicolon=True)
s.expect_newline("Syntax error in doc string")
if kind in ('u', ''): if kind in ('u', ''):
return unicode_result return unicode_result
warning(pos, "Python 3 requires docstrings to be unicode strings") warning(pos, "Python 3 requires docstrings to be unicode strings")
......
...@@ -51,4 +51,4 @@ cdef class PyrexScanner(Scanner): ...@@ -51,4 +51,4 @@ cdef class PyrexScanner(Scanner):
cdef expected(self, what, message = *) cdef expected(self, what, message = *)
cdef expect_indent(self) cdef expect_indent(self)
cdef expect_dedent(self) cdef expect_dedent(self)
cdef expect_newline(self, message = *) cdef expect_newline(self, message=*, bint ignore_semicolon=*)
...@@ -10,12 +10,12 @@ import platform ...@@ -10,12 +10,12 @@ import platform
import cython import cython
cython.declare(EncodedString=object, any_string_prefix=unicode, IDENT=unicode, cython.declare(EncodedString=object, any_string_prefix=unicode, IDENT=unicode,
print_function=object) print_function=object, error=object, warning=object)
from .. import Utils from .. import Utils
from ..Plex.Scanners import Scanner from ..Plex.Scanners import Scanner
from ..Plex.Errors import UnrecognizedInput from ..Plex.Errors import UnrecognizedInput
from .Errors import error from .Errors import error, warning
from .Lexicon import any_string_prefix, make_lexicon, IDENT from .Lexicon import any_string_prefix, make_lexicon, IDENT
from .Future import print_function from .Future import print_function
...@@ -479,7 +479,13 @@ class PyrexScanner(Scanner): ...@@ -479,7 +479,13 @@ class PyrexScanner(Scanner):
self.expect('DEDENT', self.expect('DEDENT',
"Expected a decrease in indentation level") "Expected a decrease in indentation level")
def expect_newline(self, message = "Expected a newline"): def expect_newline(self, message="Expected a newline", ignore_semicolon=False):
# Expect either a newline or end of file # Expect either a newline or end of file
useless_trailing_semicolon = None
if ignore_semicolon and self.sy == ';':
useless_trailing_semicolon = self.position()
self.next()
if self.sy != 'EOF': if self.sy != 'EOF':
self.expect('NEWLINE', message) self.expect('NEWLINE', message)
if useless_trailing_semicolon is not None:
warning(useless_trailing_semicolon, "useless trailing semicolon")
# mode: compile
# the following are valid syntax constructs and should not produce errors
ctypedef int x;
cdef no_semi():
cdef int i
cdef with_semi():
cdef int i;
# mode: error
# tag: werror
cdef useless_semicolon():
cdef int i;
pass;
ctypedef int x;
_ERRORS="""
5:14: useless trailing semicolon
6:8: useless trailing semicolon
8:14: useless trailing semicolon
"""
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment