Skip to content
This repository was archived by the owner on Oct 5, 2024. It is now read-only.

implement char escapes #20

Merged
merged 9 commits into from
Jul 4, 2019
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
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ Oh God please yes! :heart: Feel free to look around the [<kbd>help wanted</kbd>]
| ----------------- | -------------------- | -------------------- | ------------------ | ------------------ | -------------------- | ------------------ | ------------------ |
| integers | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :warning: [[2]](#f2) | :heavy_check_mark: | :heavy_check_mark: |
| floats | :x: [[3]](#f3) | :x: [[4]](#f4) | :x: [[3]](#f3) | :x: [[3]](#f3) | :x: [[5]](#f5) | :x: [[3]](#f3) | :x: [[3]](#f3) |
| characters | :warning: [[6]](#f6) | :warning: [[7]](#f7) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| strings | :warning: [[8]](#f8) | :warning: [[9]](#f9) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| characters | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| strings | :warning: [[6]](#f6) | :warning: [[7]](#f7) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| booleans | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| variables | :warning: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :warning: | :heavy_check_mark: | :heavy_check_mark: |
| lists | :x: | :x: | :x: | :x: | :x: | :x: | :x: |
Expand All @@ -98,10 +98,8 @@ Oh God please yes! :heart: Feel free to look around the [<kbd>help wanted</kbd>]
3. <span id="f3"></span> Not implemented; tracked in [#17](https://github.com/elm-in-elm/compiler/issues/17)
4. <span id="f4"></span> Not implemented; not tracked yet
5. <span id="f5"></span> To be optimized the same way Ints are; not tracked yet
6. <span id="f6"></span> Comprehensive tests missing; will be fixed in [#15](https://github.com/elm-in-elm/compiler/pull/15)
7. <span id="f7"></span> Escape sequences not implemented; not tracked yet
8. <span id="f8"></span> Comprehensive tests missing; tracked in [#21](https://github.com/elm-in-elm/compiler/issues/21)
9. <span id="f9"></span> Multiline strings (and maybe more) missing; not tracked yet
6. <span id="f6"></span> Comprehensive tests missing; not tracked yet
7. <span id="f7"></span> Multiline strings (and maybe more) missing; not tracked yet
Copy link
Contributor

Choose a reason for hiding this comment

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

Next time please don't bother shifting the numbers, life's too short for that :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Phew 👍


## Prerequisites

Expand Down Expand Up @@ -234,6 +232,12 @@ Make sure to format code before submitting a pull request!
</br>
<a href="https://github.com/Warry">Maxime Dantec</a>
</td>
<td align="center">
<img width="150" height="150"
src="https://avatars0.githubusercontent.com/u/16829510">
</br>
<a href="https://github.com/aaronjanse">Aaron Janse</a>
</td>
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

</tr>
<tbody>
</table>
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/Error.elm
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ type ParseProblem
| ExpectingInt
| ExpectingSingleQuote
| ExpectingChar
| ExpectingEscapeBackslash
| ExpectingEscapeCharacter Char
| ExpectingUnicodeEscapeLeftBrace
| ExpectingUnicodeEscapeRightBrace
| InvalidUnicodeCodePoint
| ExpectingDoubleQuote
| ExpectingPlusOperator
| ExpectingModuleDot -- `import Foo>.<Bar`
Expand Down
63 changes: 54 additions & 9 deletions src/compiler/Stage/Parse/Parser.elm
Original file line number Diff line number Diff line change
Expand Up @@ -428,22 +428,67 @@ literalInt =
|> P.inContext InLiteralInt


{-| TODO escapes
TODO Unicode escapes
-}

-- for literalChar and, in the future, literalString


character =
P.oneOf
[ P.succeed identity
|. P.token (P.Token "\\" ExpectingEscapeBackslash)
|= P.oneOf
[ P.map (\_ -> '"') (P.token (P.Token "\"" (ExpectingEscapeCharacter '"'))) -- " (elm-vscode workaround)
, P.map (\_ -> '\'') (P.token (P.Token "'" (ExpectingEscapeCharacter '\'')))
, P.map (\_ -> '\n') (P.token (P.Token "n" (ExpectingEscapeCharacter 'n')))
, P.map (\_ -> '\t') (P.token (P.Token "t" (ExpectingEscapeCharacter 't')))
, P.map (\_ -> '\u{000D}') (P.token (P.Token "r" (ExpectingEscapeCharacter 'r')))
, P.succeed identity
|. P.token (P.Token "u" (ExpectingEscapeCharacter 'u'))
|. P.token (P.Token "{" ExpectingUnicodeEscapeLeftBrace)
|= unicode
|. P.token (P.Token "}" ExpectingUnicodeEscapeRightBrace)
]
, P.succeed identity
|= P.getChompedString (P.chompIf (always True) ExpectingChar)
|> P.andThen
(\string ->
string
|> String.uncons
|> Maybe.map (Tuple.first >> P.succeed)
|> Maybe.withDefault (P.problem (CompilerBug "Multiple characters chomped in `character`"))
)
]


literalChar : Parser_ Literal
literalChar =
(P.succeed identity
|. P.symbol (P.Token "'" ExpectingSingleQuote)
|= P.getChompedString (P.chompIf (always True) ExpectingChar)
|= character
|. P.symbol (P.Token "'" ExpectingSingleQuote)
)
|> P.map Char


unicode : Parser_ Char
unicode =
P.getChompedString (P.chompWhile Char.isHexDigit)
|> P.andThen
(\string ->
string
|> String.uncons
|> Maybe.map (Tuple.first >> Char >> P.succeed)
|> Maybe.withDefault (P.problem (CompilerBug "Multiple characters chomped in `literalChar`"))
(\str ->
let
len =
String.length str
in
if len < 4 || len > 6 then
P.problem InvalidUnicodeCodePoint

else
str
|> String.toLower
|> Hex.fromString
|> Result.map Char.fromCode
|> Result.map P.succeed
|> Result.withDefault (P.problem InvalidUnicodeCodePoint)
)


Expand Down
31 changes: 31 additions & 0 deletions tests/ParserTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,37 @@ expr =
, "'A'"
, Ok (Literal (Char 'A'))
)

-- https://github.com/elm/compiler/blob/dcbe51fa22879f83b5d94642e117440cb5249bb1/compiler/src/Parse/String.hs#L279-L285
, ( "escape n"
, "'\\n'"
, Ok (Literal (Char '\n'))
)
, ( "escape r"
, "'\\r'"
, Ok (Literal (Char '\u{000D}'))
)
, ( "escape t"
, "'\\t'"
, Ok (Literal (Char '\t'))
)
, ( "double quote"
, "'\\\"'"
, Ok (Literal (Char '"'))
-- " (for vscode-elm bug)
)
, ( "single quote"
, "'\\''"
, Ok (Literal (Char '\''))
)
, ( "emoji"
, "'😃'"
, Ok (Literal (Char '😃'))
)
, ( "escaped unicode code point"
, "'\\u{1F648}'"
, Ok (Literal (Char '🙈'))
)
]
)
, ( "literal string"
Expand Down