Use new operators in the XML parser

This allows the removal of quite a bit of recursion based code.
This commit is contained in:
Yorick Peterse 2015-03-19 00:22:29 +01:00
parent 02da47c1f0
commit 2bbb7d2b10
1 changed files with 23 additions and 46 deletions

View File

@ -31,26 +31,20 @@
document document
= expressions { on_document(val[0]) } = expressions { on_document(val[0]) }
| _ { on_document }
; ;
expressions expressions
= expression expressions = expression*
{
val[1].unshift(val[0])
val[1]
}
| _
; ;
expression expression
= doctype { val[0] } = doctype
| cdata { val[0] } | cdata
| comment { val[0] } | comment
| proc_ins { val[0] } | proc_ins
| text { val[0] } | text
| element { val[0] } | element
| xml_decl { val[0] } | xml_decl
; ;
# Doctypes # Doctypes
@ -88,12 +82,7 @@ doctype_follow
; ;
doctype_inline doctype_inline
= T_DOCTYPE_INLINE doctype_inline_follow { val[0] + val[1] } = T_DOCTYPE_INLINE+ { val[0].inject(:+) }
;
doctype_inline_follow
= doctype_inline { val[0] }
| _ { '' }
; ;
doctype_types doctype_types
@ -108,11 +97,15 @@ doctype_types_follow
# CDATA tags # CDATA tags
cdata = T_CDATA { on_cdata(val[0]) }; cdata
= T_CDATA { on_cdata(val[0]) }
;
# Comments # Comments
comment = T_COMMENT { on_comment(val[0]) }; comment
= T_COMMENT { on_comment(val[0]) }
;
# Processing Instructions # Processing Instructions
@ -155,11 +148,8 @@ element
# Attributes # Attributes
attributes = attributes_ { on_attributes(val[0]) }; attributes
= attribute* { on_attributes(val[0]) }
attributes_
= attribute attributes_ { val[0] ? [val[0], *val[1]] : val[1] }
| _
; ;
attribute attribute
@ -183,34 +173,21 @@ xml_decl
# Plain text # Plain text
text = T_TEXT { on_text(val[0]) }; text
= T_TEXT { on_text(val[0]) }
;
# Strings # Strings
# #
# This parses both (empty) single and double quoted strings. # This parses both (empty) single and double quoted strings.
string string
= T_STRING_DQUOTE string_dquote_follow { val[1] } = T_STRING_DQUOTE string_body T_STRING_DQUOTE { val[1] }
| T_STRING_SQUOTE string_squote_follow { val[1] } | T_STRING_SQUOTE string_body T_STRING_SQUOTE { val[1] }
;
string_dquote_follow
= T_STRING_DQUOTE { '' }
| string_body T_STRING_DQUOTE { val[0] }
;
string_squote_follow
= T_STRING_SQUOTE { '' }
| string_body T_STRING_SQUOTE { val[0] }
; ;
string_body string_body
= T_STRING_BODY string_body_follow { val[1] ? val[0] + val[1] : val[0] } = T_STRING_BODY* { val[0].inject(:+) || '' }
;
string_body_follow
= T_STRING_BODY string_body_follow { val[1] ? val[0] + val[1] : val[0] }
| _ { nil }
; ;
%inner %inner