Commit Graph

784 Commits

Author SHA1 Message Date
Dmitri Hrybenko
02084efab7 Implement code completion for some function calls and member variable accesses
in expr-dot and expr-postfix that can be typechecked without typechecking the
beginning of the function body.


Swift SVN r6198
2013-07-12 02:00:41 +00:00
Doug Gregor
a40fdbfd8d Make 'in' a real keyword.
Swift SVN r6129
2013-07-10 21:37:50 +00:00
Doug Gregor
85231a5d16 Remove || closures.
Swift SVN r6119
2013-07-10 17:40:52 +00:00
Joe Groff
f7ad75fc7d AST: Remove unneeded implicit subject var from SwitchStmts.
Our implementation no longer requires an implicit representation of the entire subject value of switches.

Swift SVN r6087
2013-07-09 03:23:49 +00:00
Dmitri Hrybenko
1c0233efb1 Move lib/Parse/{Parser.h, Scope.h} -> include/swift/Parse/
Swift SVN r6062
2013-07-08 20:36:40 +00:00
Joe Groff
ce5eb80c33 Parse: Case blocks matching multiple patterns can't bind variables.
Similarly, "fallthrough" cannot transfer control into a case block that binds variables.

Swift SVN r5907
2013-06-30 19:10:11 +00:00
Joe Groff
f6d1999569 Parse: Introduce pattern vars into case scopes.
Create a scope for each case block to contain bindings from its patterns, and invoke addVarsToScope after parsing case label patterns to introduce vars into that scope. Refactor addVarsToScope to use an ASTWalker so it finds pattern vars embedded in expr patterns.

Swift SVN r5899
2013-06-29 16:41:57 +00:00
Joe Groff
675de63208 Parse matching pattern specific productions at top level.
Parse 'var', '_', and 'is' pattern forms at the top level of a matching pattern context. Keep track of VarPatternDepth in the parser state and raise an error if 'var' appears in a 'var'.

Swift SVN r5839
2013-06-27 18:13:01 +00:00
Joe Groff
45a69154bd Parse: Parse switch statements (again).
Reimplement 'switch' parsing for our new AST representation, where cases contain patterns and 'where' guards, case blocks can have multiple cases, and 'default' is constrained to being the lone label of the last block if present. No type-checking or parsing of actual pattern productions yet.

Swift SVN r5834
2013-06-27 05:13:41 +00:00
Joe Groff
10f136f7d3 Parse: Remove 'OtherThanAssignment' from 'parseStmt'.
Assignment isn't a Stmt anymore.

Swift SVN r5832
2013-06-27 05:13:33 +00:00
Dmitri Hrybenko
efffe5c065 Don't add names to non-resolvable scopes
Currently not only we insert names in non-resolvable scopes, but every
overloaded name gets stored only once (the last one wins).  Everything just
happens to work, because we never do name lookup in these scopes.

I also added a ScopeKind to every Scope (instead of just adding the bit --
isResolvableScope), because this provides a better debugging experience, and
centralizes knowledge about what scope kind is resolvable in the function
isResolvableScope(ScopeKind).


Swift SVN r5822
2013-06-26 21:44:11 +00:00
Dmitri Hrybenko
5bc7242fb9 Followup to 5815: rename ParserState to ParserPosition. Missed a function and
a variable that used the word 'State'.


Swift SVN r5818
2013-06-26 18:20:16 +00:00
Dmitri Hrybenko
6eedf59e32 Rename ParserState to ParserPosition. A facility to restore parser *state*
(for example, the scope stack) will be added soon.


Swift SVN r5817
2013-06-26 18:04:58 +00:00
Joe Groff
a7db082ee9 Parse: Re-allow '_' in foreach statement patterns.
Missed a spot. Add a test for 'for _ in seq'.

Swift SVN r5814
2013-06-26 17:20:06 +00:00
Chris Lattner
71be5c9c60 fallthrough is a control flow transfer too.
Swift SVN r5810
2013-06-26 04:37:59 +00:00
Chris Lattner
1259e32913 - Update LangRef to mention stmt-break/continue/fallthrough. Factor the stmt
grammar description in LangRef and the parser to expose a new stmt-control-transfer.
- remove obsolete comment in ParseStmt.cpp talking about stmt-brace.

No behavior change.


Swift SVN r5809
2013-06-26 04:31:28 +00:00
Joe Groff
8deec52b17 Rework AST representation of CaseStmts.
A single case block can have one or more 'case ...:' labels. 'case' labels contain patterns instead of exprs. 'default:' is a funny spelling for 'case _:'. Change the CaseStmt representation and rip out all the parsing, type-checking, and SILGen built off the old representation.

Swift SVN r5795
2013-06-25 00:31:42 +00:00
Dmitri Hrybenko
a2ddd4e488 Refactor lexer backtracking. Introduce opaque types that encapsulate lexer and
parser state.  Backtracking will be used a lot when we implement delayed
parsing for function bodies, and we don't want to leak lexer and parser state
details to AST classes when we store the state for the first and last token for
the function body.


Swift SVN r5759
2013-06-21 22:26:41 +00:00
Doug Gregor
fad5c78484 Remove expr-func from the syntax of the language.
FuncExpr still lives on as the implementation mechanism for FuncDecl,
but it's not long for this world.


Swift SVN r5752
2013-06-21 18:54:18 +00:00
Joe Groff
6dcf8ae206 Parse assignments as part of expr-sequence.
Parse '=' as a binary operator with fixed precedence, parsing it into a temporary UnsequencedAssignExpr that gets matched to operands and turned into an AssignExpr during sequence expr folding. This makes '=' behave like library-defined assignment-like binary operators.

This temporarily puts '=' at the wrong precedence relative to 'as' and 'is', until 'as' and 'is' can be integrated into sequence parsing as well.

Swift SVN r5508
2013-06-07 16:15:40 +00:00
Joe Groff
cb1f81db84 Make assignment an expression.
Change AssignStmt into AssignExpr; this will make assignment behave more consistently with assignment-like operators, and is a first step toward integrating '=' parsing with SequenceExpr resolution so that '=' can obey precedence rules. This also nicely simplifies the AST representation of c-style ForStmts; the initializer and increment need only be Expr* instead of awkward Expr*/AssignStmt* unions.

This doesn't actually change any user-visible behavior yet; AssignExpr is still only parsed at statement scope, and typeCheckAssignment is still segregrated from the constraint checker at large. (In particular, a PipeClosureExpr containing a single assign expr in its body still doesn't use the assign expr to resolve its own type.) The parsing issue will be addressed by handling '=' during SequenceExpr resolution. typeCheckAssignment can hopefully be reworked to work within the constraint checker too.

Swift SVN r5500
2013-06-06 22:18:54 +00:00
Jordan Rose
8f845b6bc7 Fix doc comment typo.
Swift SVN r5327
2013-05-25 01:35:00 +00:00
Doug Gregor
c10b1cef65 Allow trailing closures without pipes wherever we don't expect curly braces.
This moves trailing closures from expr-postfix up to the level of
expr, and introduces an intermediate level (expr-basic) for places
that need to parse expressions followed by curly braces, such as
if/while/switch/for. Trailing closures are still restricted to occur
after expr-postfix, although the parser itself parses a slightly more
general and then complains if it got more than an expr-postfix.


Swift SVN r5256
2013-05-21 22:30:25 +00:00
Chris Lattner
b4eee19287 Switch the SIL parse to parse the file in a model similar to "immediate"
mode for normal .swift files.  We basically parse batches of non-sil function
decls, type check them as a batch, then process any SIL functions.  This allows
us to have mutually recursive types and other things that are fully sema'd and
that are referenced by SIL functions, without involving SIL functions too
intimately with type checking.

This does mean that SIL functions can't forward reference types, oh well.



Swift SVN r5243
2013-05-21 03:27:27 +00:00
Doug Gregor
6e64ca66f0 Treat '|' as a delimiter while parsing the signature of a closure.
'|' is part of the character set for operators, but within the
signature of a closure we need to treat the first non-nested '|' as
the closing delimiter for the closure parameter list. For example,

  { |x = 1| 2 + x}

parses with the default value of '1' for x, with the body 2 + x. If
the '|' operator is needed in the default value, it can be wrapped in
parentheses:

  { |x = (1|2)| x }

Note that we have problems with both name binding and type checking
for default values in closures (<rdar://problem/13372694>), so they
aren't actually enabled. However, this allows us to parse them and
recover better in their presence.



Swift SVN r5202
2013-05-17 16:02:44 +00:00
Chris Lattner
bde8f753da wire up minimal sil function parsing (still not validating or creating
a SIL function).  This introduces contextual lexing state for SIL function 
bodies for SIL-specific lexing rules.


Swift SVN r5179
2013-05-16 18:46:46 +00:00
Chris Lattner
15f40068d4 remove stmt-brace. If you need something like it for scoping purposes, you can always use "if true {}".
Swift SVN r5034
2013-05-03 05:59:27 +00:00
Doug Gregor
e27deb1494 Remove the least liked of the message-send syntaxes, e.g.,
foo.bar(1) wibble(true)

because it is ridiculously ambiguous.


Swift SVN r4996
2013-04-30 17:05:33 +00:00
Jordan Rose
790248d8b4 Diagnostics: use builder pattern instead of streaming for ranges/fix-its.
Per Chris's feedback and suggestions on the verbose fix-it API, convert
diagnostics over to using the builder pattern instead of Clang's streaming
pattern (<<) for fix-its and ranges. Ranges are included because
otherwise it's syntactically difficult to add a fix-it after a range.

New syntax:

  diagnose(Loc, diag::warn_problem)
    .highlight(E->getRange())
    .fixItRemove(E->getLHS()->getRange())
    .fixItInsert(E->getRHS()->getLoc(), "&")
    .fixItReplace(E->getOp()->getRange(), "++");

These builder functions only exist on InFlightDiagnostic; while you can
still modify a plain Diagnostic, you have to do it with plain accessors
and a raw DiagnosticInfo::FixIt.

Swift SVN r4894
2013-04-24 23:15:53 +00:00
Jordan Rose
b6c1ff4483 All the rest of the easy fix-its.
Also, turn a diagnostic into an assertion (string_interpolation_extra),

Swift SVN r4816
2013-04-18 23:34:27 +00:00
Jordan Rose
f7cd3a6ec6 Add fix-its for missing and spurious separators.
Swift SVN r4789
2013-04-18 00:42:59 +00:00
Chris Lattner
af3b55c1f0 Further reinforce TopLevelCodeDecl as the container for top level code.
This nests top level PatternBindingDecls (in "main modules") under TopLevelCodeDecls,
instead of having them live in a translation unit.  They contain code that is executed,
so they should be in a TLCD.


Swift SVN r4668
2013-04-10 23:13:22 +00:00
Chris Lattner
1ee0bed38f Clean up Parser::parseBraceItemList even more:
Extend the existing "isTerminatorForBraceItemListKind" logic to
handle the special case for top level code, instead of having
weird logic dumped in the middle of parseBraceItemList with no comments.

This logic is still seriously dubious, but at least it is out of the way
instead of dump into already really complex logic.


Swift SVN r4665
2013-04-10 22:24:08 +00:00
Chris Lattner
a00464dde1 refactor the logic that determines whether newly parsed top-level decls need to
be immediately run by the REPL to live in one simple place, out of the braceitem
parsing loop.


Swift SVN r4663
2013-04-10 22:06:07 +00:00
Joe Groff
bfd2f85b5c Parse 'fallthrough' statements.
Create a new FallthroughStmt, which transfers control from a 'case' or 'default' block to the next 'case' or 'default' block within a switch. Implement parsing and sema for FallthroughStmt, which syntactically consists of a single 'fallthrough' keyword. Sema verifies that 'fallthrough' actually appears inside a switch statement and that there is a following case or default block to pass control to.

SILGen/IRGen support forthcoming.

Swift SVN r4653
2013-04-10 17:30:42 +00:00
Chris Lattner
28774bf9fc Come full circle on TopLevelCodeDecl, making top level stmts and exprs each get their
own TLCD.  This is important to preserve the ordering of stmt and expr w.r.t. 
PatternBindingDecls that initialize the decls.

We keep the BraceStmt wrapping it to make it more similar to other decls
though.


Swift SVN r4626
2013-04-06 21:58:26 +00:00
Chris Lattner
b4fd6dd04a Change TopLevelCodeDecl to allow it to hold a sequence of different exprs and statements in one unit, wrapping them into a BraceStmt. This makes it more similar to other decls (e.g. funcdecl, ctor decls, etc) and will be useful for future sil work.
Unfortunately, this regresses the repl when expressions like (1,2) are entered. This is because the repl is violating some invariants (forming dags out of ASTs, making ASDAG's which upset the type checker).  I'm going to fix this next, but can't bring myself to do it in the same commit.



Swift SVN r4617
2013-04-05 22:33:14 +00:00
Joe Groff
aeeda4ee12 Parser: Parse operator decls.
At the top level, if 'operator' is followed by 'infix', 'prefix', or 'postfix', consider it a contextual keyword, and parse an operator decl following it that looks like:

  operator {infix|postfix|prefix} <+> {
    attributes…
  }

Prefix and postfix operator decls currently admit no attributes. Infix operators have 'associativity {left|right|none}' and 'precedence <int>' attributes.

This patch implements parsing for operator declarations but does not yet attach the declared attributes to func decls for the operators.

Swift SVN r4596
2013-04-03 23:30:50 +00:00
Joe Groff
88d4284178 Parser: Relax whitespace rules for ( and [.
Now that we enforce semicolon or newline separation between statements, we can relax the whitespace requirements on '(' and '[' tokens. A "following" token is now just a token that isn't at the start of a line, and any token can be a "starting" token. This allows for:

  a(b)
  a (b)
  a[b]
  a [b]

to parse as applications and subscripts, and:

  a
  (b)
  a
  [b]

to parse as an expr followed by a tuple or an expr followed by a container literal.

Swift SVN r4573
2013-04-02 16:43:20 +00:00
Joe Groff
89e1b7e773 Parser: Give IfExpr traditional ternary syntax.
Swift SVN r4489
2013-03-26 01:17:34 +00:00
Joe Groff
4c09ef61e3 Add conditional expressions.
Implement the syntax 'if x then y else z', which evaluates to 'y' if 'x' is true or 'z' if 'x' is false. 'x' must be a valid logic value, and 'y' and 'z' must be implicitly convertible to a common type.

Swift SVN r4407
2013-03-16 20:28:58 +00:00
Doug Gregor
b11b655fcc Introduce another calling syntax for selectors.
This new syntax aims to be closer to the declaration syntax. For
example, to call this method:

  func performSelector(_ : SEL) withObject(obj1 : id)  { }

one would use

  target.performSelector("doThis:") withObject(object)

The additional selector pieces (e.g., withObject(object)) occur on the
same line; otherwise, they are taken as a separate statement. However,
one can use ':' as a continuation character at the beginning of the
next line to continue the message send, e.g.,

  target.performSelector("doThis:")
        :withObject(object)

For the 3-argument version, one could use, e.g.,

  target.performSelector("doThis:") withObject(object1) withObject(object2)

or

  target.performSelector("doThis:")
        :withObject(object1) withObject(object2)

or

  target.performSelector("doThis:")
        :withObject(object1) 
        :withObject(object2)

depending on the width of your screen.

Note that I've tweaked the parsing of case statements slightly to
accommodate this change, by requiring that the ':' that follows a case
statement not start a new line. Thus,

  case foo:

is okay, but

  case foo
    :

is not. This is mostly paranoia, so that

  case target.performSelector("sel"):

is "obviously" a simple method invocation in the case, while

  case target.performSelector("sel")
             :withObject(object):

is "obviously" a two-argument method invocation in the case.

This syntax has some positives, such as similarity with the function
declaration syntax and being a fairly clean extension of the "normal"
Swift method call syntax. It also has some negatives: we have our
first continuation character (':'), the syntax for constructors is
(again) a bit unfortunate

  new NSURL(initWithString="http://www.apple.com")

and it's not clear how to invoke a variadic method with this syntax
without, say, burying the additional arguments in the last argument
(which is currently not permitted), e.g.,

  NSString.alloc().initWithFormat("blah") locale(locale, arg1, arg2)





Swift SVN r4366
2013-03-13 04:19:00 +00:00
Doug Gregor
bd61ca5927 Diagnose multiple statements/declarations on the same line that are not separated by a semicolon.
Swift SVN r4364
2013-03-13 01:32:30 +00:00
Joe Groff
062ad267c4 Value-only switch statements.
Implement switch statements with simple value comparison to get the drudge work of parsing and generating switches in place. Cases are checked using a '=~' operator to compare the subject of the switch to the value in the case. Unlike a C switch, cases each have their own scope and don't fall through. 'break' and 'continue' apply to an outer loop rather to the switch itself. Multiple case values can be specified in a comma-separated list, as in 'case 1, 2, 3, 4:'. Currently no effort is made to check for duplicate cases or to rank cases by match strength; cases are just checked in source order, and the first one wins (aside from 'default', which is branched to if all cases fail).

Swift SVN r4359
2013-03-12 04:43:01 +00:00
Dave Zarzycki
7db15ab4c3 Use backtrackToToken() to remove the CForLoopHack
...and also use the API to forgive tok::period_prefix if it is provably
part of a builder API design pattern.

Swift SVN r4355
2013-03-11 23:30:17 +00:00
Dave Zarzycki
d2ba334949 Simplify parseMatchingToken()
Swift SVN r3966
2013-02-06 06:19:25 +00:00
Dave Zarzycki
b36678214a Rename l_(paren|square)_(call|subscript)
Thanks Chris and John for the feedback.

Swift SVN r3893
2013-01-29 21:13:39 +00:00
Dave Zarzycki
d7cc4b4a91 Reclaim "in" as an identifier
In Swift the "in" keyword is really a form of punctuation, and highly
context specific punctuation at that. It never begins a statement, nor
does the grammar require it be statement keyword. The grammar also
doesn't use it outside of for-each loops, and its use within a for-each
loop is highly unambiguous.

Thanks to Chris for the performance related feedback. This improves the
performance of getter/setter parsing as well.

Swift SVN r3880
2013-01-26 01:49:18 +00:00
Dave Zarzycki
eb5bb20bf9 Remove unnecessary use of isAnyLParen()
Swift SVN r3857
2013-01-24 17:36:36 +00:00
Joe Groff
8af835edcc Lexer: '[' and '(' after a keyword is non-literal.
Opening brackets after a keyword have to lex as l_paren_call or l_square_subscript in order for expressions like 'super.constructor()' or 'super[i]' to parse. While we're here, let's move the keyword and punctuator list to a metaprogrammable Tokens.def header too. Update decl and stmt parsers to use 'isAnyLParen' so that, e.g., 'constructor(' and 'constructor (' both work as before.

Swift SVN r3846
2013-01-23 21:24:26 +00:00