diff options
Diffstat (limited to 'test/liftattrs.lm')
-rw-r--r-- | test/liftattrs.lm | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/test/liftattrs.lm b/test/liftattrs.lm new file mode 100644 index 0000000..574ea35 --- /dev/null +++ b/test/liftattrs.lm @@ -0,0 +1,83 @@ +##### LM ##### + +# +# Regular Definitions +# +rl rl_ws /[ \t\n\r\v]+/ +rl rl_id /[a-zA-Z_][a-zA-Z0-9_]*/ + +# +# Tokens +# + +lex + literal `= `< `> `/ + + # Ignore whitespace. + ignore /rl_ws/ + + # Open and close id + token id /rl_id/ +end + +# +# Productions +# + +def attr [id `= id] + +def attr_list + [attr_list attr] +| [] + +def open_tag + [`< id attr_list `>] + +def close_tag + [`< `/ id `>] + +def tag + [open_tag item_list close_tag] + +def item_list + [item_list tag] +| [] + +parse ILP: item_list[stdin] +IL: item_list = ILP + +# Get the item list +match IL [RootItemList: item_list] + +# List for collecting the attrs we pull out. +CollectedAttrs: attr_list = construct attr_list [] + +# Iterate through all attributes +for AttrListIter:attr_list in RootItemList { + # If the name of the attr is foo, remove it. + if match AttrListIter + [SubAttrList:attr_list "foo=" Val:id] + { + # Remove the attribute + AttrListIter = construct attr_list + [SubAttrList] + + # Add it to the colection + CollectedAttrs = construct attr_list + [CollectedAttrs " foo=" ^Val] + } +} + +# Reconstruct the left hand side with the +IL = construct item_list + ["<wrapper " ^CollectedAttrs ">" ^RootItemList "</wrapper>"] + +print( ^IL '\n' ) +##### IN ##### +<t1 a=b foo=bar1 c=d> + <t2 foo=bar2 e=f></t2> +</t1> +##### EXP ##### +<wrapper foo=bar1 foo=bar2><t1 a=b c=d> + <t2 e=f></t2> +</t1></wrapper> |