Skip to content

Instantly share code, notes, and snippets.

@clone1018
Last active March 27, 2017 12:01
Show Gist options
  • Save clone1018/b17ef51ca1fd2e84e32754d35dd3956c to your computer and use it in GitHub Desktop.
Save clone1018/b17ef51ca1fd2e84e32754d35dd3956c to your computer and use it in GitHub Desktop.
<template>
Everything inside of here would be tag_contents
<template> <-- this is where the parser fails with a syntax error
everything inside of here would still be tag_contents
</template>
</template>
<script>
everything in here would be tag_contents
</script>
Definitions.
% Helper Regexes (non-token)
ValidInTagName = [A-Za-z0-9]
ValidTagAttributeName = [A-Za-z\-]
ValidTagAttributeValue = [A-Za-z0-9\s="\-/:;,\.?\(\)@]
ValidTagAttribute = {ValidTagAttributeName}+=("|')?{ValidTagAttributeValue}+("|')?
ValidTags = (\s{ValidTagAttribute})*
ValidInsideTag = (.)
% Core HTML tokens
ROOT_TAG = \<root{ValidTags}\>
TEMPLATE_TAG = \<template{ValidTags}\>
SCRIPT_TAG = \<script{ValidTags}\>
START_TAG = \<{ValidInTagName}+{ValidTags}\>
END_TAG = \<\/{ValidInTagName}+\>
CHARACTER = {ValidInsideTag}
% Useful tokens
NEW_LINE = [\r\n]\s*
SELF_CLOSING_TAG = \<{ValidInTagName}+{ValidTags}\s\/\>
Rules.
{START_TAG} : {token, {start_tag, TokenLine, TokenChars}}.
{END_TAG} : {token, {end_tag, TokenLine, TokenChars}}.
{CHARACTER} : {token, {char, TokenLine, TokenChars}}.
{NEW_LINE} : skip_token.
Erlang code.
Terminals start_tag end_tag char.
Nonterminals tag tag_contents chars.
Rootsymbol tag.
tag -> start_tag end_tag : {unwrap('$1'), []}.
tag -> start_tag tag_contents end_tag : {unwrap('$1'), '$2'}.
tag_contents -> chars : ['$1'].
chars -> char chars : unicode:characters_to_binary([unwrap('$1')|'$2']).
chars -> char : unwrap('$1').
Erlang code.
unwrap({_,_,V}) -> V.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment