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

106 lines
2.9 KiB
Factor
Raw Normal View History

! Copyright (C) 2007, 2009 Slava Pestov.
2008-09-10 23:11:40 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors xmode.tokens xmode.rules xmode.keyword-map
2009-02-05 22:17:03 -05:00
xml.data xml.traversal xml assocs kernel combinators sequences
math.parser namespaces make parser lexer xmode.utilities
regexp io.files splitting arrays xml.syntax xml.syntax.private ;
2008-01-16 01:04:42 -05:00
IN: xmode.loader.syntax
! 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 scan-word [
2009-03-16 21:11:36 -04:00
[ parse-definition call( -- ) ] { } make
swap [ (parse-rule-tag) ] 2curry
] dip swap define-tag ; parsing
2008-01-16 01:04:42 -05:00
! 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 )
2009-01-29 00:56:47 -05:00
[ "NAME" attr ] [ "VALUE" attr ] bi ;
2008-01-16 01:04:42 -05:00
: parse-props-tag ( tag -- assoc )
children-tags
2008-01-16 01:04:42 -05:00
[ 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" }
2009-01-29 00:56:47 -05:00
[ attr string>boolean ] with map first3 ;
2008-01-16 01:04:42 -05:00
: parse-literal-matcher ( tag -- matcher )
dup children>string
rule-set get ignore-case?>> <string-matcher>
2008-01-16 01:04:42 -05:00
swap position-attrs <matcher> ;
: parse-regexp-matcher ( tag -- matcher )
dup children>string
rule-set get ignore-case?>> <?insensitive-regexp>
2008-01-16 01:04:42 -05:00
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
: parse-delegate ( string -- pair )
"::" split1 [ rule-set get swap ] unless* 2array ;
: 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
TAGS: parse-begin/end-tag ( rule tag -- )
2008-01-16 01:04:42 -05:00
TAG: BEGIN parse-begin/end-tag
2008-01-16 01:04:42 -05:00
! XXX
2008-08-30 21:53:59 -04:00
parse-literal-matcher >>start drop ;
2008-01-16 01:04:42 -05:00
TAG: END parse-begin/end-tag
2008-01-16 01:04:42 -05:00
! XXX
2008-08-30 21:53:59 -04:00
parse-literal-matcher >>end drop ;
2008-01-16 01:04:42 -05:00
: parse-begin/end-tags ( -- )
2008-01-16 01:04:42 -05:00
[
! XXX: handle position attrs on span tag itself
children-tags [ parse-begin/end-tag ] with each
2008-01-16 01:04:42 -05:00
] , ;
: 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 -- )
2008-12-17 20:17:37 -05:00
[ dup main>> string>token swap children>string ] dip set-at ;