Paste: parser for markup language
Author: | hsuh |
Mode: | factor |
Date: | Wed, 28 Oct 2009 21:55:21 |
Plain Text |
USING: accessors arrays assocs combinators fry io
io.encodings.utf8 io.files io.streams.string kernel lists
locals macros math math.order namespaces sequences splitting
strings unicode.categories urls.encoding vectors
xml.data xml.syntax xml.writer xmode.code2html
;
IN: t2t
: strip ( line -- line' )
[ "\t " member? ] trim ;
: heading? ( string -- ? )
[ f ]
[
[ first "=+" member? ]
[ last "=+" member? ] bi and
] if-empty ;
: list? ( string -- ? )
[ f ]
[
[ first "-+" member? ]
[ heading? not ] bi and
] if-empty ;
: comment? ( line -- ? )
first CHAR: % = ;
: table-row? ( line -- ? )
strip
[ f ]
[
[ first CHAR: | = ]
[ last CHAR: | = ] bi and
] if-empty ;
SYMBOL: lines
: eof? ( -- ? )
lines get empty? ;
: skip-line ( -- )
lines get
[ "skip-line found EOF" throw ]
[ 1 tail-slice lines set ] if-empty ;
: peek-line ( -- line )
lines get
[ "peek-line found EOF" throw ]
[ first ] if-empty ;
SYMBOL: ast
: add-node ( x -- )
ast get swap suffix ast set ;
: parse-heading ( -- )
"heading" add-node skip-line ;
: parse-list ( -- )
"list" add-node skip-line
peek-line list? [ skip-line ] when ;
: parse-text ( -- )
"text" add-node skip-line ;
: parse-lines ( -- )
[ eof? not ]
[
{
{ [ peek-line empty? ] [ "empty" add-node skip-line ] }
{ [ peek-line comment? ] [ skip-line ] }
{ [ peek-line heading? ] [ parse-heading ] }
{ [ peek-line list? ] [ parse-list ] }
[ parse-text ]
} cond
] while ;
: parse ( filename -- ast )
{ } ast set
utf8 file-lines lines set
parse-lines
ast get ;
Author: | hsuh |
Mode: | factor |
Date: | Thu, 29 Oct 2009 00:49:32 |
Plain Text |
USING: combinators io.encodings.utf8 io.files kernel make
namespaces sequences unicode.categories ;
IN: t2t
: strip ( line -- line' )
[ blank? ] trim ;
: heading? ( string -- ? )
[ f ]
[
[ first "=+" member? ]
[ last "=+" member? ] bi and
] if-empty ;
: list? ( string -- ? )
[ f ]
[
[ first "-+" member? ]
[ heading? not ] bi and
] if-empty ;
: comment? ( line -- ? )
first CHAR: % = ;
: table-row? ( line -- ? )
strip
[ f ]
[
[ first CHAR: | = ]
[ last CHAR: | = ] bi and
] if-empty ;
SYMBOL: lines
: eof? ( -- ? )
lines get empty? ;
: skip-line ( -- )
lines get
[ "skip-line found EOF" throw ]
[ 1 tail-slice lines set ] if-empty ;
: peek-line ( -- line )
lines get
[ "peek-line found EOF" throw ]
[ first ] if-empty ;
: parse-heading ( -- )
"heading" , skip-line ;
: parse-list ( -- )
"list" , skip-line
peek-line list? [ skip-line ] when ;
: parse-text ( -- )
"text" , skip-line ;
: parse-lines ( -- )
[ eof? not ]
[
{
{ [ peek-line empty? ] [ "empty" , skip-line ] }
{ [ peek-line comment? ] [ skip-line ] }
{ [ peek-line heading? ] [ parse-heading ] }
{ [ peek-line list? ] [ parse-list ] }
[ parse-text ]
} cond
] while ;
: parse ( filename -- ast )
utf8 file-lines lines set
[ parse-lines ] { } make ;
New Annotation