1 ! Copyright (C) 2007, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors xmode.tokens xmode.rules xmode.keyword-map
4 xml.data xml.utilities xml assocs kernel combinators sequences
5 math.parser namespaces make parser lexer xmode.utilities
6 parser-combinators.regexp io.files ;
7 IN: xmode.loader.syntax
11 ! Rule tag parsing utilities
12 : (parse-rule-tag) ( rule-set tag specs class -- )
13 new swap init-from-tag swap add-rule ; inline
17 parse-definition { } make
18 swap [ (parse-rule-tag) ] 2curry (TAG:) ; parsing
21 : string>boolean ( string -- ? ) "TRUE" = ;
23 : string>match-type ( string -- obj )
30 : string>rule-set-name ( string -- name ) "MAIN" or ;
33 : parse-prop-tag ( tag -- key value )
34 "NAME" over at "VALUE" rot at ;
36 : parse-props-tag ( tag -- assoc )
38 [ parse-prop-tag ] H{ } map>assoc ;
40 : position-attrs ( tag -- at-line-start? at-whitespace-end? at-word-start? )
42 { "AT_LINE_START" "AT_WHITESPACE_END" "AT_WORD_START" }
43 swap [ at string>boolean ] curry map first3 ;
45 : parse-literal-matcher ( tag -- matcher )
47 ignore-case? get <string-matcher>
48 swap position-attrs <matcher> ;
50 : parse-regexp-matcher ( tag -- matcher )
51 dup children>string ignore-case? get <regexp>
52 swap position-attrs <matcher> ;
54 : shared-tag-attrs ( -- )
55 { "TYPE" string>token (>>body-token) } , ; inline
57 : delegate-attr ( -- )
58 { "DELEGATE" f (>>delegate) } , ;
61 { "HASH_CHAR" f (>>chars) } , ;
63 : match-type-attr ( -- )
64 { "MATCH_TYPE" string>match-type (>>match-token) } , ;
67 { "NO_LINE_BREAK" string>boolean (>>no-line-break?) } ,
68 { "NO_WORD_BREAK" string>boolean (>>no-word-break?) } ,
69 { "NO_ESCAPE" string>boolean (>>no-escape?) } , ;
71 : literal-start ( -- )
72 [ parse-literal-matcher >>start drop ] , ;
75 [ parse-regexp-matcher >>start drop ] , ;
78 [ parse-literal-matcher >>end drop ] , ;
81 <TAGS: parse-begin/end-tag ( rule tag -- )
85 parse-literal-matcher >>start drop ;
89 parse-literal-matcher >>end drop ;
93 : parse-begin/end-tags ( -- )
95 ! XXX: handle position attrs on span tag itself
96 child-tags [ parse-begin/end-tag ] with each
99 : init-span-tag ( -- ) [ drop init-span ] , ;
101 : init-eol-span-tag ( -- ) [ drop init-eol-span ] , ;
103 : parse-keyword-tag ( tag keyword-map -- )
104 >r dup main>> string>token swap children>string r> set-at ;