]> gitweb.factorcode.org Git - factor.git/blob - basis/xmode/loader/syntax/syntax.factor
Updating code for make and fry changes
[factor.git] / basis / xmode / loader / syntax / syntax.factor
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 regexp
6 io.files ;
7 IN: xmode.loader.syntax
8
9 SYMBOL: ignore-case?
10
11 ! Rule tag parsing utilities
12 : (parse-rule-tag) ( rule-set tag specs class -- )
13     new swap init-from-tag swap add-rule ; inline
14
15 : RULE:
16     scan scan-word
17     parse-definition { } make
18     swap [ (parse-rule-tag) ] 2curry (TAG:) ; parsing
19
20 ! Attribute utilities
21 : string>boolean ( string -- ? ) "TRUE" = ;
22
23 : string>match-type ( string -- obj )
24     {
25         { "RULE" [ f ] }
26         { "CONTEXT" [ t ] }
27         [ string>token ]
28     } case ;
29
30 : string>rule-set-name ( string -- name ) "MAIN" or ;
31
32 ! PROP, PROPS
33 : parse-prop-tag ( tag -- key value )
34     "NAME" over at "VALUE" rot at ;
35
36 : parse-props-tag ( tag -- assoc )
37     child-tags
38     [ parse-prop-tag ] H{ } map>assoc ;
39
40 : position-attrs ( tag -- at-line-start? at-whitespace-end? at-word-start? )
41     ! XXX Wrong logic!
42     { "AT_LINE_START" "AT_WHITESPACE_END" "AT_WORD_START" }
43     swap [ at string>boolean ] curry map first3 ;
44
45 : parse-literal-matcher ( tag -- matcher )
46     dup children>string
47     ignore-case? get <string-matcher>
48     swap position-attrs <matcher> ;
49
50 : parse-regexp-matcher ( tag -- matcher )
51     dup children>string ignore-case? get <regexp>
52     swap position-attrs <matcher> ;
53
54 : shared-tag-attrs ( -- )
55     { "TYPE" string>token (>>body-token) } , ; inline
56
57 : delegate-attr ( -- )
58     { "DELEGATE" f (>>delegate) } , ;
59
60 : regexp-attr ( -- )
61     { "HASH_CHAR" f (>>chars) } , ;
62
63 : match-type-attr ( -- )
64     { "MATCH_TYPE" string>match-type (>>match-token) } , ;
65
66 : span-attrs ( -- )
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?) } , ;
70
71 : literal-start ( -- )
72     [ parse-literal-matcher >>start drop ] , ;
73
74 : regexp-start ( -- )
75     [ parse-regexp-matcher >>start drop ] , ;
76
77 : literal-end ( -- )
78     [ parse-literal-matcher >>end drop ] , ;
79
80 ! SPAN's children
81 <TAGS: parse-begin/end-tag ( rule tag -- )
82
83 TAG: BEGIN
84     ! XXX
85     parse-literal-matcher >>start drop ;
86
87 TAG: END
88     ! XXX
89     parse-literal-matcher >>end drop ;
90
91 TAGS>
92
93 : parse-begin/end-tags ( -- )
94     [
95         ! XXX: handle position attrs on span tag itself
96         child-tags [ parse-begin/end-tag ] with each
97     ] , ;
98
99 : init-span-tag ( -- ) [ drop init-span ] , ;
100
101 : init-eol-span-tag ( -- ) [ drop init-eol-span ] , ;
102
103 : parse-keyword-tag ( tag keyword-map -- )
104     >r dup main>> string>token swap children>string r> set-at ;