PackageFactory / PackageFactory.ComponentEngine

GNU General Public License v3.0
4 stars 1 forks source link

FEATURE: Simplify template literals #33

Open grebaldi opened 1 year ago

grebaldi commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@mhsdesign

Like this?:

export component Foo {
  bar: string

  return """
      Hello {bar}
      """
}
mhsdesign commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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.