Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Simplify template literals #33

Open
grebaldi opened this issue Aug 7, 2023 · 6 comments
Open

FEATURE: Simplify template literals #33

grebaldi opened this issue Aug 7, 2023 · 6 comments

Comments

@grebaldi
Copy link
Member

grebaldi commented Aug 7, 2023

As of right now, template literals are modeled after ECMAScript, e.g.:

export component Foo {
  bar: string

  return `
  Hello ${bar}
  `
}

Imho this causes two problems:

  1. The ${ sequence is a considerable complication for the parser (or better: The tokenizer)
  2. Template literals of this kind cause lots of unwanted space, that cannot be trimed away by the engine itself

I therefore suggest to change the template literal syntax to this:

export component Foo {
  bar: string

  return """
    Hello {bar}
    """
}

Rules are:

  1. Leading and trailing spaces are trimmed
  2. Indentation as per the closing delimiter is removed from every line (similar to PHP Heredoc syntax: https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)

This will still allow for possible future tagged template literals, like:

export component Foo {
  bar: string

  return markdown"""
    # A markdown document
    
    ## Some headline: {bar}
    Lorem ipsum...
    """
}
@mhsdesign
Copy link
Contributor

I think it’s like pythons f strings, except the f ^^ - so yes I like it ;)

also the ${ is less intuitive to write than a simple brace

@mhsdesign
Copy link
Contributor

Leading and trailing spaces are trimmed

i like phps behaviour of nowdocs better, there the indentation of the closing defines where each line starts

@grebaldi
Copy link
Member Author

grebaldi commented Aug 7, 2023

@mhsdesign

Like this?:

export component Foo {
  bar: string

  return """
      Hello {bar}
      """
}

@mhsdesign
Copy link
Contributor

yes.

taken from the php doc:

The closing identifier may be indented by space or tab, in which case the indentation will be stripped from all lines in the doc string. [...]

If the closing identifier is indented further than any lines of the body, then a ParseError will be thrown:

// no indentation
return """
      a
     b
    c
"""
// 4 spaces of indentation
return """
      a
     b
    c
    """

output:

      a
     b
    c
  a
 b
c

@grebaldi
Copy link
Member Author

grebaldi commented Aug 8, 2023

@mhsdesign:

i like phps behaviour of nowdocs better, there the indentation of the closing defines where each line starts

Agreed. I adjusted the issue description accordingly.

@grebaldi
Copy link
Member Author

grebaldi commented Aug 10, 2023

We'll need different delimiters, as """ cannot win against " in expressions.

I'm currently trying:

return ---
      Hello {bar}
      ---

but, dunno....

nvm... There needs to be an alternative to handling ambiguous tokens anyway, so """ can probably stay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants