Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions functional_tests/SemiColonTerminatedStatementTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import Foundation;

println("Hello, World!")

enum CompassPoint {
case North;
case South;
case East;
case West;
};

protocol SomeProtocol {
var fullName: String { get };
func demo();
func dummy(f: Int);
func foo(bar: String, baz: Double);
};

extension SomeType {
// new functionality to add to SomeType goes here
};

struct DemoStruct {
var x: String;

func test() {
// while loop test
while (true)
{
};

// for loop
for ; ; {

};

// do while
do {

} while (true);
};
};

// Class examples

class lowerCamelCase {
var x: String = "hello";
let b = 2;

func demo()
{
for var x = 0; ; {
print(x);
};

if temperatureInFahrenheit <= 32 {
println("It's very cold. Consider wearing a scarf.");
} else if temperatureInFahrenheit >= 86 {
println("It's really warm. Don't forget to wear sunscreen.");
} else {
println("It's not that cold. Wear a t-shirt.");
}
};
};

44 changes: 22 additions & 22 deletions grammar/Swift.g4
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,20 @@ genericArgument : sType ;
// GRAMMAR OF A DECLARATION

declaration
: importDeclaration
| constantDeclaration
| variableDeclaration
| typealiasDeclaration
| functionDeclaration
| enumDeclaration
| structDeclaration
| classDeclaration
| protocolDeclaration
| initializerDeclaration
| deinitializerDeclaration
| extensionDeclaration
| subscriptDeclaration
| operatorDeclaration
: importDeclaration ';'?
| constantDeclaration ';'?
| variableDeclaration ';'?
| typealiasDeclaration ';'?
| functionDeclaration ';'?
| enumDeclaration ';'?
| structDeclaration ';'?
| classDeclaration ';'?
| protocolDeclaration ';'?
| initializerDeclaration ';'?
| deinitializerDeclaration ';'?
| extensionDeclaration ';'?
| subscriptDeclaration ';'?
| operatorDeclaration ';'?
;

declarations : declaration declarations? ;
Expand Down Expand Up @@ -223,7 +223,7 @@ typealiasAssignment : '=' sType ;

// GRAMMAR OF A FUNCTION DECLARATION

functionDeclaration : functionHead functionName genericParameterClause? functionSignature functionBody ;
functionDeclaration : functionHead functionName genericParameterClause? functionSignature functionBody ;
functionHead : attributes? declarationSpecifiers? 'func' ;
functionName : identifier | operator ;
functionSignature : parameterClauses functionResult? ;
Expand All @@ -246,7 +246,7 @@ enumDeclaration : attributes? 'enum' enumDef ;
enumDef: unionStyleEnum | rawValueStyleEnum ;
unionStyleEnum : enumName genericParameterClause?'{' unionStyleEnumMembers?'}' ;
unionStyleEnumMembers : unionStyleEnumMember unionStyleEnumMembers? ;
unionStyleEnumMember : declaration | unionStyleEnumCaseClause ;
unionStyleEnumMember : declaration | unionStyleEnumCaseClause ';'? ;
unionStyleEnumCaseClause : attributes? 'case' unionStyleEnumCaseList ;
unionStyleEnumCaseList : unionStyleEnumCase | unionStyleEnumCase ',' unionStyleEnumCaseList ;
unionStyleEnumCase : enumCaseName tupleType? ;
Expand Down Expand Up @@ -277,11 +277,11 @@ classBody : '{' declarations? '}' ;
protocolDeclaration : attributes? 'protocol' protocolName typeInheritanceClause? protocolBody ;
protocolName : identifier ;
protocolBody : '{' protocolMemberDeclarations?'}' ;
protocolMemberDeclaration : protocolPropertyDeclaration
| protocolMethodDeclaration
| protocolInitializerDeclaration
| protocolSubscriptDeclaration
| protocolAssociatedTypeDeclaration
protocolMemberDeclaration : protocolPropertyDeclaration ';'?
| protocolMethodDeclaration ';'?
| protocolInitializerDeclaration ';'?
| protocolSubscriptDeclaration ';'?
| protocolAssociatedTypeDeclaration ';'?
;
protocolMemberDeclarations : protocolMemberDeclaration protocolMemberDeclarations? ;

Expand Down Expand Up @@ -417,7 +417,7 @@ expressionList : expression (',' expression)* ;
expression : prefixExpression binaryExpression* ;

prefixExpression
: prefixOperator? postfixExpression
: prefixOperator? postfixExpression ';'?
| inOutExpression
;

Expand Down
2 changes: 1 addition & 1 deletion pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# no Warning level messages displayed, use"--disable=all --enable=classes
# --disable=W"
# TODO: Remove once "#26 Add docstrings to all significant modules, classes, methods, and functions" is resolved
disable=missing-docstring
disable=fixme, missing-docstring


[BASIC]
Expand Down
34 changes: 33 additions & 1 deletion tailor/listeners/mainlistener.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from tailor.swift.swiftlistener import SwiftListener
from tailor.types.location import Location
from tailor.utils.charformat import is_upper_camel_case


Expand Down Expand Up @@ -27,7 +28,38 @@ def enterProtocolName(self, ctx):
self.__verify_upper_camel_case(
ctx, 'Protocol names should be in UpperCamelCase')

def enterStatement(self, ctx):
self.__verify_not_semicolon_terminated(ctx)

def enterDeclaration(self, ctx):
self.__verify_not_semicolon_terminated(ctx)

def enterLoopStatement(self, ctx):
self.__verify_not_semicolon_terminated(ctx)

def enterBranchStatement(self, ctx):
self.__verify_not_semicolon_terminated(ctx)

def enterLabeledStatement(self, ctx):
self.__verify_not_semicolon_terminated(ctx)

def enterControlTransferStatement(self, ctx):
self.__verify_not_semicolon_terminated(ctx)

def enterUnionStyleEnumMember(self, ctx):
self.__verify_not_semicolon_terminated(ctx)

def enterProtocolMemberDeclaration(self, ctx):
self.__verify_not_semicolon_terminated(ctx)

def __verify_upper_camel_case(self, ctx, err_msg):
construct_name = ctx.getText()
if not is_upper_camel_case(construct_name):
self.__printer.error(ctx, err_msg)
self.__printer.error(err_msg, ctx)

def __verify_not_semicolon_terminated(self, ctx):
line = ctx.getText()
if line.endswith(';'):
self.__printer.error(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently error messages display the ctx.start.line and ctx.start.column, but for this type of rule (that checks for statement-terminating punctuation), the messages should use the ctx.stop.line and ctx.stop.column. A parameter (that potentially defaults to ctx.start) could be used to modify error() for this use case.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

'Statement should not terminate with a semicolon',
loc=Location(ctx.stop.line, ctx.stop.column + 1))
20 changes: 13 additions & 7 deletions tailor/output/printer.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import os

from tailor.types.location import Location


class Printer:

def __init__(self, filepath):
self.__filepath = os.path.abspath(filepath)

def warn(self, ctx, warn_msg):
self.__print(ctx, 'warning', warn_msg)
def warn(self, warn_msg, ctx=None, loc=Location(1, 1)):
self.__print('warning', warn_msg, ctx, loc)

def error(self, ctx, err_msg):
self.__print(ctx, 'error', err_msg)
def error(self, err_msg, ctx=None, loc=Location(1, 1)):
self.__print('error', err_msg, ctx, loc)

def __print(self, ctx, classification, message):
print(self.__filepath + ':' + str(ctx.start.line) + ':' +
str(ctx.start.column) + ': ' + classification + ': ' + message)
def __print(self, classification, msg, ctx, loc):
if ctx is not None:
print(self.__filepath + ':' + str(ctx.start.line) + ':' +
str(ctx.start.column) + ': ' + classification + ': ' + msg)
else:
print(self.__filepath + ':' + str(loc.line) + ':' +
str(loc.column) + ': ' + classification + ': ' + msg)
Loading