# Parser and AST

`Braces.Handlebars.parse/2` is a port of Handlebars.js's
[handlebars-parser](https://github.com/handlebars-lang/handlebars-parser) (2.2.4, commit
`c3c048f`). It gives `Braces.Handlebars.AST` structs, the same AST as handlebars-parser, with each
node's `loc` (`line` from 1, `column` from 0, in UTF-16 code units as Handlebars.js counts
them).

```elixir
{:ok, ast} = Braces.Handlebars.parse("Hi {{name}}")
ast.body
#=> [%Braces.Handlebars.AST.ContentStatement{value: "Hi ", …}, %Braces.Handlebars.AST.MustacheStatement{…}]
```

- `Braces.Handlebars.parse/2` applies Handlebars' whitespace control: `~` strips, and block, comment
  and partial tags alone on a line take the line with them. `ignore_standalone: true` keeps
  those lines, as Handlebars.js's `ignoreStandalone` does.
- `Braces.Handlebars.parse_without_processing/1` leaves the text as written.
- `Braces.Handlebars.to_string/1` prints an AST as Handlebars source. Print what
  `parse_without_processing/1` gives, or an AST you built: printing what `parse/2` gives and
  parsing the result would apply whitespace control twice.
- `Braces.Handlebars.variables/2` lists the data a template reads, as paths such as `items[].name`.
- `Braces.Handlebars.Printer.print/1` prints an AST as handlebars-parser's `PrintVisitor` does, to
  compare it with Handlebars.js.

## Parse errors

A template that doesn't parse is a `Braces.Handlebars.ParseError`. Its `description` is
Handlebars.js's message, and its `message` adds the template's line with a caret under the
error, as does the `CompileError` from `~HBS`:

```
if doesn't match each - 1:3

    {{#if a}}{{/each}}
       ^
```

A syntax error has its own description instead, on the line Handlebars.js reports, with the
caret at the start of the tag that doesn't parse, even inside blocks (Handlebars.js points at
the token inside the tag). That includes an `{{else}}`, `{{^}}` or `{{else if}}` its block can't
have, and a `{{!--` comment with no `--}}`. When a block closes with another block's name, or
has an invalid path, before the syntax error, that error is reported instead, as Handlebars.js
does. The caret counts a wide character, such as an emoji or a CJK character, as two columns:

```
Parse error on line 3, column 2: unexpected "{{foo bar=}}\n{{/each"

      {{foo bar=}}
      ^
```
