Re: html parser

Sean Conner <[email protected]> Sat, 1 Aug 2026 15:52:25 -0400
Newsgroups gmane.comp.lang.lua.general
Message-ID <[email protected]>
It was thus said that the Great Marcus Rohrmoser once stated:
> 
> it's indeed beautiful and I immensely enjoy it[1], but while exploring
> convivial, malleable tools, I consider OCaml too heavy a toolchain -
> compared to the lua runtime.
> 
> I want tinkering with tools approachable for hobbyists, newcomers and
> brave non-techs.

  I do have an HTML parser [2] in LPeg, about 500 lines of code.  I wouldn't
consider LPeg all that approachable for hobbyists, newcomers or even brave
non-techs.  I like it, but it took me about a year of working with it to
become comfortable, and a few more before I understood all the oeprators and
captures one can make.  And even the simpler re module requires
understanding both BNF and the concepts from the lpeg module.

  Here's a sample of the LPeg parser:

	local parse_tags = P {
	  -- ... 
	  A   = tagi('a'   , A_attr   , (V'inline' - V'A')^0),
	  IMG = tagi('img' , IMG_attr , EMPTY,true),
	  -- ...
	}

  I later swapped the LPeg-based HTML parser for one written with peg (a C
tool [3]) that I modified [4] not because of speed, but because LPeg was
consuming more memory than I was comfortable with.  That peg was faster was
a benefit, but again, peg is a parser generator, something that might not be
approachable for hobbyists, newcomings or brave non-techs.

  Here's a sample of the C peg code:

	A   <- "<" _A   & ET { tagi(yy,"a");   } A_attr*   S* ">" (!A inline)*     "</" _A   ">"   { endtag(yy); }
	IMG <- "<" _IMG & ET { tagi(yy,"img"); } IMG_attr* S* ">"              (S* "</" _IMG ">")? { endtag(yy); }

	# ...

	# my customized syntax, using a backtick, not a single quote
	# for insensitive comparison.

	_A   <- `a`
	_IMG <- `img`

	# othewise, it was

	_A   <- [Aa]
	_IMG <- [Ii][Mm][Gg]

  I personally find the peg version to be easier to read than the LPeg
version, but I can't say the same for other people.

  -spc

[1]	Not my footnote.

[2]     HTML4 strict, which is what I needed.  I don't have an HTML5 parser 
        because the "living standard" changes too much, and the "grammar"
        (such as it is) is pages and pages of English text describing in
        minute detail how one should parse HTML5 character by character.
        Ick.

[3]	As tools go, it's fairly simple.  A single executable that takes PEG
	input and outputs C source which you need to compile.  It's similar
	in struture to lex and yacc.

[4]     I modified the C peg library to make case insensitive comparrisons  
        easier to write, and faster to parse.

-- 
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/20260801195225.GD6255%40brevard.conman.org.