Commit Graph

363 Commits

Author SHA1 Message Date
Rintaro Ishizaki
509db744f1 [Lexer] Disallow '$' as a start of identifier, special handle '$' (#6004)
Allowing 'let `$0` = 1', introduced in 6accc598 was not intentional.
2016-12-21 11:31:58 +09:00
Michael Gottesman
59c6a64f5a [gardening] 0 => nullptr. Fixed with clang-tidy. 2016-12-06 23:14:13 -08:00
David Farler
330c2d96e6 Make the lexer UTF-8 RFC 3629 correct re: prefix octets
RFC 2279 states that, in UTF-8:
"The octet values FE and FF never appear."

RFC 3629 states that, in UTF-8:
"The octet values C0, C1, F5 to FF never appear."

Generalize the check to advance past invalid starting bytes for
a UTF-8 sequence to fix a crash in the lexer.
2016-12-05 17:21:17 -08:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
David Farler
f450f0ccdf Revert "Preserve whitespace and comments during lexing as Trivia"
This reverts commit d6e2b58382.
2016-11-18 13:23:31 -08:00
David Farler
d6e2b58382 Preserve whitespace and comments during lexing as Trivia
Store leading a trailing "trivia" around a token, such as whitespace,
comments, doc comments, and escaping backticks. These are syntactically
important for preserving formatting when printing ASTs but don't
semantically affect the program.

Tokens take all trailing trivia up to, but not including, the next
newline. This is important to maintain checks that statements without
semicolon separators start on a new line, among other things.

Trivia are now data attached to the ends of tokens, not tokens
themselves.

Create a new Syntax sublibrary for upcoming immutable, persistent,
thread-safe ASTs, which will contain only the syntactic information
about source structure, as well as for generating new source code, and
structural editing. Proactively move swift::Token into there.

Since this patch is getting a bit large, a token fuzzer which checks
for round-trip equivlence with the workflow:

fuzzer => token stream => file1
  => Lexer => token stream => file 2 => diff(file1, file2)

Will arrive in a subsequent commit.

This patch does not change the grammar.
2016-11-15 16:11:57 -08:00
Xi Ge
ac3411234d [SourceKit] The initial implementation of range-info request.
Like cursor-info, range info (""source.request.cursorinfo"") answers some
questions clients have for a code snippet under selection, for instance, the type of a selected
expression. This commit implements this new quest kind and provides two
simple information about the selected code: (1) the kind of the
snippet, currently limited to single-statement and expression; and (2)
the type of the selected expression. Gradually, we will enrich the
response to provide more insight into the selected code snippet.
2016-11-03 16:07:04 -07:00
Robert Widmann
6accc5989e Disable the ability to use '$' as an identifier harder
When in Swift 3 Compatibility Mode we now acceptable a standalone
'$' as an identifier.  In all other cases this is now disallowed
and must be surrounded by backticks.
2016-10-27 16:51:18 -04:00
Rintaro Ishizaki
1df518c978 [Lexer] Simplify comment detection code in lexOperatorIdentifier 2016-10-24 11:17:12 +09:00
Rintaro Ishizaki
9d1a3fc62c [Lexer] Trim comments off operator-identifier before tokenize
Previously, builtin operators followed by comment block were tokenized as
normal operators (e.g. tok::oper_binary_spaced) instead of dedicated
token(e.g. tok::equal).
That used to cause strange parse errors:

  test.swift:1:3: error: use of unresolved operator '='
  _ =/* */2
    ^
2016-10-22 23:32:13 +09:00
Robert Widmann
389f779a27 Fixup tests 2016-07-31 19:28:45 -07:00
Wheerd
f008d03c73 Added a simple test for dollar identifier lexing. 2016-07-31 19:28:45 -07:00
Wheerd
110ec21802 Fixed a single '$' being accepted as a valid identifier by the lexer.
Now it becomes an unknown token instead.
2016-07-31 19:28:45 -07:00
Chris Lattner
45118037cc When we lex an invalid leftbound dot, instead of emitting an error and swallowing it
silently, have the lexer return it as an unknown token.  Enhance the expr parser to
detect these things and squash any expression in progress into an ErrorExpr.  This
allows us to silence really bad downstream errors.  For example, on:

struct S { func f() {} }
func f() {
  _ = S.
}

we formerly produced:

 x.swift:5:8: error: expected member name following '.'
 x.swift:5:7: error: expected member name or constructor call after type name
 x.swift:5:7: note: add arguments after the type to construct a value of the type
 x.swift:5:7: note: use '.self' to reference the type object

we now emit just the first one.  This fixes:
<rdar://problem/22290244> QoI: "UIColor." gives two issues, should only give one
2016-07-03 16:04:35 -07:00
Rintaro Ishizaki
a5eed3828a [Lexer][SR-1724] Handle hex letters after '.' on hex number literal
The following case used to emit an error:

  extension Int {
    var asUiColor: UIColor { ... }
  }

  0xfff.asUiColor
2016-06-22 13:07:41 +09:00
Robert Widmann
95b28cba98 [SR-1545] Implement lexing of source conflict markers. (#2924)
Lexing for conflict markers is inspired by the way clang handles them with a few
updates of our own.  Clang's lexer currently searches for the start of the
conflict marker, attempts to find the divider points, lexes that, then finds the
end.  We, unfortunately, cannot be so forgiving because of operator overloads.
Instead, we search for the start and end markers and ignore all text in between.
Even if what is found is not conflict markers, it certainly is not valid Swift either.
2016-06-09 17:15:12 -07:00
Ted Kremenek
b8bbed8c13 [WIP] Implement SE-0039 (Modernizing Playground Literals) (#2215)
* Implement the majority of parsing support for SE-0039.

* Parse old object literals names using new syntax and provide FixIt.

For example, parse "#Image(imageLiteral:...)" and provide a FixIt to
change it to "#imageLiteral(resourceName:...)".  Now we see something like:

test.swift:4:9: error: '#Image' has been renamed to '#imageLiteral
var y = #Image(imageLiteral: "image.jpg")
        ^~~~~~ ~~~~~~~~~~~~
        #imageLiteral resourceName

Handling the old syntax, and providing a FixIt for that, will be handled in a separate
commit.

Needs tests.  Will be provided in later commit once full parsing support is done.

* Add back pieces of syntax map for object literals.

* Add parsing support for old object literal syntax.

... and provide fixits to new syntax.

Full tests to come in later commit.

* Improve parsing of invalid object literals with old syntax.

* Do not include bracket in code completion results.

* Remove defunct code in SyntaxModel.

* Add tests for migration fixits.

* Add literals to code completion overload tests.

@akyrtzi told me this should be fine.

* Clean up response tests not to include full paths.

* Further adjust offsets.

* Mark initializer for _ColorLiteralConvertible in UIKit as @nonobjc.

* Put attribute in the correct place.
2016-04-25 07:19:26 -07:00
practicalswift
66183cdbf7 [gardening] Fix unjustified spacing 2016-04-07 10:10:24 +02:00
Jesse Rusak
51ded13bce [Lexer] Updates to operator-whitespace handling from code review 2016-04-02 17:38:42 -04:00
Jesse Rusak
83b4c47222 [Lexer] Treat comments as whitespace for operator arity rules.
Previously, comments were treated as non-whitespace. Operators
also checked for right-boundedness before detecting comments
which start in the middle of an operator.

This resolves both of these issues so that comments are
consistently treated as whitespace when determining whether
operators are left- or right-bound.

Fixes SR-186 and SR-960 (SE-0037)
2016-03-18 11:51:27 -04:00
Toni Suter
4f5a94e1a9 [Lexer] Remove redundant checks in lexNumber()
The if statements check whether a number literal consists
of just the 0b/0o/0x prefix which would be invalid. However, in all three cases there's a preceding check
that already addresses this issue.
2016-03-16 22:28:39 +01:00
Chris Lattner
1868856e26 update regex in comment, thanks to @schnippi for pointing this out. 2016-03-13 15:14:31 -07:00
Chris Lattner
4992474168 Add support for #sourceLocation in its ratified forms. Switch gyb to produce
the new form.  This keeps accepting #setline for now, but we should rip it out
at some point.
2016-03-11 22:21:42 -08:00
Slava Pestov
bbbe307980 SIL: Introduce SILDefaultWitnessTable and start plumbing
This will be used to help IRGen record protocol requirements
with resilient default implementations in protocol metadata.

To enable testing before all the Sema support is in place, this
patch adds SIL parser, printer and verifier support for default
witness tables.

For now, SILGen emits empty default witness tables for protocol
declarations in resilient modules, and IRGen ignores them when
emitting protocol metadata.
2016-02-05 20:57:11 -08:00
Chris Lattner
d522cd4270 Centralize the parsing logic for #identifiers and make it more similar to
the identifier parsing logic.
2016-02-03 22:37:28 -08:00
Ben Langmuir
7eaed61b6f [CodeCompletion] Fix completion after unspaced binary operator
We were miscalculating 'isRightBound' when the RHS was a code-completion
token leading to missing completions in unspaced binary expressions
  1+<here>
  1...<here>

rdar://problem/24278699
2016-02-03 18:46:15 -08:00
Doug Gregor
dccf3155f1 SE-0022: Implement parsing, AST, and semantic analysis for #selector. 2016-01-26 21:12:04 -08:00
Trent Nadeau
e983501bfd Don't error when lexing UTF-8 BOM 2016-01-26 05:49:39 +00:00
Anton Blanchard
baf0a4b004 [Parser] Missed signed cast of CurPtr
A char is unsigned by default on PowerPC. CurPtr is cast
to a signed char whenever a signed comparison is required,
but we are missing one place.

This fixes a testcase failure in Parse/BOM.swift
2016-01-18 05:52:14 +00:00
Chris Lattner
8d81349fe1 fix rdar://24029542 "Postfix '.' is reserved" error message" isn't helpful
This adds some heuristics so we can emit a fixit to remove extraneous
whitespace after a . and diagnose the case where a member just hasn't
been written yet better.  This also improves handling of tok::unknown
throughout the parser a bit.

This is a re-commit of ff4ea54 with an update for a SourceKit test.
2016-01-11 15:11:20 -08:00
Erik Eckstein
acc243a002 Revert "fix rdar://24029542 "Postfix '.' is reserved" error message" isn't helpful"
It's probably the cause for the fail of SourceKit/SyntaxMapData/syntaxmap-edit-del.swift

This reverts commit ff4ea54614.
2016-01-11 10:43:39 -08:00
Chris Lattner
ff4ea54614 fix rdar://24029542 "Postfix '.' is reserved" error message" isn't helpful
This adds some heuristics so we can emit a fixit to remove extraneous
whitespace after a . and diagnose the case where a member just hasn't
been written yet better.  This also improves handling of tok::unknown
throughout the parser a bit.
2016-01-10 15:28:03 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Chris Lattner
0bfacde242 Fix: <rdar://problem/16230507> Cannot use a negative constant as the second operator of ... operator
This is a case that the operator splitting code didn't handle because of
the bizarre lexer code for handling operators with periods in them.  We had
accreted some weird special case logic for what is valid in an operator
(but which even had a special case hack for ..<).  The policy is now very
simple: if an operator name starts with a dot, it is allowed to include other
dots in its name.  If it doesn't, it doesn't.  This allows us to get lexer
level operator splitting in cases like x=.Foo, allowing our existing operator
set that have dots in them without hacks, and provides a superior QoI
experience due to operator splitting.

This is technically a language change, but the TSPL operator grammar was
incorrect for it anyway.  I will file an internal radar to get TSPL updated
with the actual behavior (now that it is defensible).
2015-12-16 22:16:19 -08:00
Chris Lattner
a8b87f167f fix a regression that e28c2e2 introduced in IDE/coloring.swift. 2015-12-16 14:33:03 -08:00
Chris Lattner
d5934da6e3 QoI: improve the error message for when whitespace is inconsistent on either
side of the = to say that, instead of talking about prefix/postfix = operators,
which are "not a thing".
2015-12-16 14:27:17 -08:00
Chris Lattner
e28c2e2c6e Fix <rdar://14296004> [QoI] Poor diagnostic/recovery when two operators (e.g., == and -) are adjacted without spaces.
This is a frequently reported and surprising issue where lack of whitespace leads to
rejecting common code like "X*-4".  Fix this by diagnosing it specifically as a lack
of whitespace problem, including a fixit to insert the missing whitespace (to transform
it into "X * -4".  This even handles the cases where there are multiple valid (single)
splits possible by emitting a series of notes.
2015-12-16 13:20:28 -08:00
Chris Willmore
c99c02b5a6 Transform EditorPlaceholderExpr into trap if executed in playground
mode (take 2)

Allow untyped placeholder to take arbitrary type, but default to Void.
Add _undefined<T>() function, which is like fatalError() but has
arbitrary return type. In playground mode, merely warn about outstanding
placeholders instead of erroring out, and transform placeholders into
calls to _undefined(). This way, code with outstanding placeholders will
only crash when it attempts to evaluate such placeholders.

When generating constraints for an iterated sequence of type T, emit

    T convertible to $T1
    $T1 conforms to SequenceType

instead of

    T convertible to SequenceType

This ensures that an untyped placeholder in for-each sequence position
doesn't get inferred to have type SequenceType. (The conversion is still
necessary because the sequence may have IUO type.) The new constraint
system precipitates changes in CSSimplify and CSDiag, and ends up fixing
18741539 along the way.

(NOTE: There is a small regression in diagnosis of issues like the
following:

    class C {}
    class D: C {}
    func f(a: [C]!) { for _: D in a {} }

It complains that [C]! doesn't conform to SequenceType when it should be
complaining that C is not convertible to D.)

<rdar://problem/21167372>

(Originally Swift SVN r31481)
2015-12-10 22:05:16 -08:00
Frederick Kellison-Linn
0bfba2972e Fix lexer to properly handle single quote strings
Lexer now suggests escaping unescaped double quotes and unescaping escaped single quotes in single quote strings..
2015-12-09 20:09:04 -05:00
Dmitri Hrybenko
2e51d23875 Un-ifdef object literals
Swift SVN r32880
2015-10-25 07:50:53 +00:00
David Farler
7fceb124ba Look for EOF in addition to whitespace to lex the #endif token
In addition to just a whitespace char, also look ahead to a null
character to lex the #endif token.  This allows the token to be formed
when at the end of a file.

Fix endif-on-end-of-file.swift test file to not end in whitespace.

rdar://problem/21692337

Swift SVN r32224
2015-09-25 07:28:32 +00:00
Chris Lattner
92fc60f619 fix rdar://22387625 QoI: Common errors: 'let x= 5' and 'let x =5' could use Fix-its
Swift SVN r32004
2015-09-16 20:12:14 +00:00
Chris Willmore
4b8a5cf894 Lex single-quote string literals but emit an error if they're encountered.
Emit a fix-it replacing them with double-quote string literals.

<rdar://problem/21950709> QoI: Parse single-quoted literals like double-quoted literals

Swift SVN r31973
2015-09-15 22:24:18 +00:00
Chris Willmore
9c1f3e907a Revert "Transform EditorPlaceholderExpr into trap if executed in playground mode."
This reverts commit r31481, which apparently needed some parallel
changes to SourceKit and broke the build as a result.

Swift SVN r31483
2015-08-26 05:28:04 +00:00
Chris Willmore
0addd80bb3 Transform EditorPlaceholderExpr into trap if executed in playground mode.
Allow untyped placeholder to take arbitrary type, but default to Void.
Add _undefined<T>() function, which is like fatalError() but has
arbitrary return type. In playground mode, merely warn about outstanding
placeholders instead of erroring out, and transform placeholders into
calls to _undefined(). This way, code with outstanding placeholders will
only crash when it attempts to evaluate such placeholders.

<rdar://problem/21167372> transform EditorPlaceholderExpr into fatalError()

Swift SVN r31481
2015-08-26 04:50:55 +00:00
Chris Willmore
265f91fead Allow string literals in string literal interpolation.
Teach skipToEndOfInterpolatedExpression() to match quote marks as well
as parentheses in the interpolated expression. This makes expressions
like "hello \(names["bob"])" possible.

Swift SVN r31283
2015-08-18 00:41:01 +00:00
Doug Gregor
c02cd1a424 Factor omit-needless-words logic out of the Clang importer.
Sink the actual logic for omitting needless words way down into
Basic, so we can re-use it elsewhere. Tie the Clang importer into that
logic, mapping Clang types down to strings appropriately. NFC

Swift SVN r31233
2015-08-13 23:39:29 +00:00
Doug Gregor
f76bc108fc Don't omit words down to "get", "set", or a Swift keyword.
Swift SVN r31189
2015-08-12 21:24:42 +00:00
Jordan Rose
9757294002 [Lex] Use clang::isIdentifierBody instead of checking for '_' separately.
Also, use 'isHexDigit' instead of the built-in 'isxdigit' for consistency,
even though 'isxdigit' is locale-independent.

No intended functionality change.

Swift SVN r31139
2015-08-11 18:29:35 +00:00
Jordan Rose
8a9a238cc8 [SIL] Allow all ASCII identifier characters in SIL variable names.
For better test cases. The comment even made it look like this was
intended at one point.

('$' is not an identifier character.)

Swift SVN r31136
2015-08-11 17:56:55 +00:00