factor/basis/xmode/loader/syntax/syntax.factor

105 lines
2.7 KiB
Factor
Raw Normal View History

2008-09-10 23:11:40 -04:00
! Copyright (C) 2007, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors xmode.tokens xmode.rules xmode.keyword-map
xml.data xml.utilities xml assocs kernel combinators sequences
math.parser namespaces make parser lexer xmode.utilities
parser-combinators.regexp io.files ;
2008-01-16 01:04:42 -05:00
IN: xmode.loader.syntax
SYMBOL: ignore-case?
! Rule tag parsing utilities
: (parse-rule-tag) ( rule-set tag specs class -- )
new swap init-from-tag swap add-rule ; inline
2008-01-16 01:04:42 -05:00
: RULE:
scan scan-word
parse-definition { } make
swap [ (parse-rule-tag) ] 2curry (TAG:) ; parsing
! Attribute utilities
: string>boolean ( string -- ? ) "TRUE" = ;
: string>match-type ( string -- obj )
{
{ "RULE" [ f ] }
{ "CONTEXT" [ t ] }
[ string>token ]
} case ;
: string>rule-set-name ( string -- name ) "MAIN" or ;
2008-01-16 01:04:42 -05:00
! PROP, PROPS
: parse-prop-tag ( tag -- key value )
"NAME" over at "VALUE" rot at ;
: parse-props-tag ( tag -- assoc )
child-tags
[ parse-prop-tag ] H{ } map>assoc ;
: position-attrs ( tag -- at-line-start? at-whitespace-end? at-word-start? )
! XXX Wrong logic!
{ "AT_LINE_START" "AT_WHITESPACE_END" "AT_WORD_START" }
swap [ at string>boolean ] curry map first3 ;
: parse-literal-matcher ( tag -- matcher )
dup children>string
ignore-case? get <string-matcher>
swap position-attrs <matcher> ;
: parse-regexp-matcher ( tag -- matcher )
dup children>string ignore-case? get <regexp>
swap position-attrs <matcher> ;
: shared-tag-attrs ( -- )
2008-08-30 21:53:59 -04:00
{ "TYPE" string>token (>>body-token) } , ; inline
2008-01-16 01:04:42 -05:00
: delegate-attr ( -- )
2008-08-30 21:53:59 -04:00
{ "DELEGATE" f (>>delegate) } , ;
2008-01-16 01:04:42 -05:00
: regexp-attr ( -- )
2008-08-30 21:53:59 -04:00
{ "HASH_CHAR" f (>>chars) } , ;
2008-01-16 01:04:42 -05:00
: match-type-attr ( -- )
2008-08-30 21:53:59 -04:00
{ "MATCH_TYPE" string>match-type (>>match-token) } , ;
2008-01-16 01:04:42 -05:00
: span-attrs ( -- )
2008-08-30 21:53:59 -04:00
{ "NO_LINE_BREAK" string>boolean (>>no-line-break?) } ,
{ "NO_WORD_BREAK" string>boolean (>>no-word-break?) } ,
{ "NO_ESCAPE" string>boolean (>>no-escape?) } , ;
2008-01-16 01:04:42 -05:00
: literal-start ( -- )
2008-08-30 21:53:59 -04:00
[ parse-literal-matcher >>start drop ] , ;
2008-01-16 01:04:42 -05:00
: regexp-start ( -- )
2008-08-30 21:53:59 -04:00
[ parse-regexp-matcher >>start drop ] , ;
2008-01-16 01:04:42 -05:00
: literal-end ( -- )
2008-08-30 21:53:59 -04:00
[ parse-literal-matcher >>end drop ] , ;
2008-01-16 01:04:42 -05:00
! SPAN's children
2008-06-15 04:25:41 -04:00
<TAGS: parse-begin/end-tag ( rule tag -- )
2008-01-16 01:04:42 -05:00
TAG: BEGIN
! XXX
2008-08-30 21:53:59 -04:00
parse-literal-matcher >>start drop ;
2008-01-16 01:04:42 -05:00
TAG: END
! XXX
2008-08-30 21:53:59 -04:00
parse-literal-matcher >>end drop ;
2008-01-16 01:04:42 -05:00
TAGS>
: parse-begin/end-tags ( -- )
2008-01-16 01:04:42 -05:00
[
! XXX: handle position attrs on span tag itself
child-tags [ parse-begin/end-tag ] with each
] , ;
: init-span-tag ( -- ) [ drop init-span ] , ;
2008-01-16 01:04:42 -05:00
: init-eol-span-tag ( -- ) [ drop init-eol-span ] , ;
2008-01-16 01:04:42 -05:00
: parse-keyword-tag ( tag keyword-map -- )
>r dup main>> string>token swap children>string r> set-at ;