Skip to content

Commit

Permalink
Merge pull request #1522 from DanielXMoore/string-literal
Browse files Browse the repository at this point in the history
`:"symbol name"` symbol shorthand with quotes
  • Loading branch information
edemaine authored Oct 27, 2024
2 parents 5bb7acd + 4994fc0 commit 590b444
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
6 changes: 6 additions & 0 deletions civet.dev/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,12 @@ magicSymbol := :magic
iteratorSymbol := :iterator
</Playground>
Symbol names that aren't valid identifiers can be wrapped in quotes:
<Playground>
magicSymbol := :"magic-symbol"
</Playground>
## Operators
### All JavaScript/TypeScript Operators
Expand Down
34 changes: 25 additions & 9 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -2838,24 +2838,40 @@ CoffeeScriptBooleanLiteral

# NOTE: Added :symbol shorthand for Symbol.symbol or Symbol.for("symbol")
SymbolLiteral
Colon:colon IdentifierName:id ->
const { name, children: [ token ] } = id
Colon:colon ( IdentifierName / StringLiteral ):id ->
let name, token
if (id.type === "Identifier") {
({ name, children: [ token ] } = id)
} else {
name = literalValue({
type: "Literal",
subtype: "StringLiteral",
raw: id.token,
children: [id],
})
token = id
}
if (config.symbols.includes(name)) { // well-known symbol
return {
type: "SymbolLiteral",
children: [
{ ...colon, token: "Symbol." },
token,
],
children:
id.type === "Identifier" ? [
{ ...colon, token: "Symbol." },
token,
] : [
{ ...colon, token: "Symbol[" },
token,
"]",
],
name,
}
} else { // use global symbol registry
return {
type: "SymbolLiteral",
children: [
{ ...colon, token: 'Symbol.for("' },
token,
'")'
{ ...colon, token: 'Symbol.for(' },
id.type === "Identifier" ? [ '"', token, '"' ] : token,
')'
],
name,
}
Expand Down
10 changes: 10 additions & 0 deletions test/symbol.civet
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ describe ":symbol literals", ->
() => Symbol.for("foo")
"""

testCase """
string
---
=> :"foo-bar"
=> :"iterator"
---
() => Symbol.for("foo-bar");
() => Symbol["iterator"]
"""

testCase """
well-known
---
Expand Down

0 comments on commit 590b444

Please sign in to comment.