source: proto/Xerces/parabix_xerces.py @ 1262

Last change on this file since 1262 was 1223, checked in by cameron, 8 years ago

Elim tag_Callouts.LAngleFollow, simd_and

File size: 11.3 KB
Line 
1#
2# Parallel XML Parsing with Bitstream Addition
3#
4# Version for Xerces XML Reader
5#   - assumes:  input transcoded to UTF-16,
6#               line break normalization applied,
7#               pseudo-ASCII bit streams
8#
9# Robert D. Cameron
10
11class Lex():
12        RefStart = 0
13        Semicolon = 0 
14        Colon = 0
15        LAngle = 0
16        RAngle = 0
17        LBracket = 0
18        RBracket = 0
19        Exclam = 0
20        QMark = 0
21        Hyphen = 0
22        Equals = 0
23        SQuote = 0
24        DQuote = 0
25        Slash = 0
26        Hash = 0
27        x = 0
28        ASCII_name_start = 0
29        ASCII_name_char = 0
30        NameScan = 0
31        Digit = 0
32        Hex = 0
33        WS = 0
34
35class Control():
36# The delmask marks bit stream positions that should be
37# considered deleted, i.e., positions at no corresponding
38# character data should be produced.
39        delmask = 0
40        XML_error = 0  # 0x0-0x1F except HT, LF CR, 0xFFFE, 0xFFFF
41        ch_error = 0   # Bad UTF8 characters
42        normalized_LF = 0
43        HT = 0
44        pseudo_bit0 = 0
45
46class Scope1 ():
47        RefStart = 0
48        LAngle = 0
49        Hyphen = 0
50        QMark = 0
51        RBracket = 0
52
53class CtCDPI_Callouts():
54        CD_end = 0
55        Ct_starts = 0
56        Ct_ends = 0
57        CD_starts = 0
58        CD_ends = 0
59        PI_starts = 0
60        PI_name_starts = 0
61        PI_name_ends = 0
62        PI_ends = 0
63        CtCDPI_mask = 0
64        error = 0
65
66class Ref_Callouts():
67        GenRef_starts = 0
68        GenRef_ends = 0
69        DecRef_starts = 0
70        DecRef_ends = 0
71        HexRef_starts = 0
72        HexRef_ends = 0
73        error = 0
74
75class Tag_Callouts():
76        ElemName_starts = 0
77        ElemName_ends = 0
78        AttName_starts = 0
79        AttName_ends = 0
80        AttVal_starts = 0
81        AttVal_ends = 0
82        AttVal_spans = 0
83        EmptyTag_marks = 0
84        EndTag_marks = 0
85        error = 0
86               
87class Check_streams():
88        non_ascii_name_starts = 0
89        non_ascii_names = 0
90        tag_marks = 0 
91        name_follows = 0 
92        att_refs = 0 
93        error_mask = 0
94
95class Xml_names():
96        namespace_error = 0
97
98       
99def Add_scope_streams(lex, scope1):
100        #scope1.LAngle = bitutil.Advance(lex.LAngle)
101        #scope1.Hyphen = bitutil.Advance(lex.Hyphen)
102        #scope1.QMark = bitutil.Advance(lex.QMark)
103        v = lex.LAngle | lex.Hyphen
104        w = lex.Hyphen | lex.QMark
105        v1 = bitutil.Advance(v)
106        w1 = bitutil.Advance(w)
107        scope1.LAngle = v1 &~ w1
108        scope1.Hyphen = v1 & w1
109        scope1.QMark = w1 &~ v1
110
111def Parse_CtCDPI(ctCDPI_Callouts, lex, scope1, check_streams):
112        ctCDPI_Callouts.CD_end = 0
113        ctCDPI_Callouts.Ct_starts = 0
114        ctCDPI_Callouts.Ct_ends = 0
115        ctCDPI_Callouts.CD_starts = 0
116        ctCDPI_Callouts.CD_ends = 0
117        ctCDPI_Callouts.PI_starts = 0
118        ctCDPI_Callouts.PI_name_starts = 0
119        ctCDPI_Callouts.PI_name_ends = 0
120        ctCDPI_Callouts.PI_ends = 0
121        ctCDPI_Callouts.CtCDPI_mask = 0
122        ctCDPI_Callouts.error = 0
123        CtCDPI_starts = 0
124        Ct_errors = 0
125
126        if lex.RBracket:
127                scope1.RBracket = bitutil.Advance(lex.RBracket)
128                ctCDPI_Callouts.CD_end = bitutil.Advance(scope1.RBracket & lex.RBracket) & lex.RAngle
129        PI_start = scope1.LAngle & lex.QMark
130        CtCD_start = scope1.LAngle & lex.Exclam
131        CtCDPI_start = PI_start | CtCD_start
132
133        DoubleHyphen = scope1.Hyphen & lex.Hyphen
134        PI_end = scope1.QMark & lex.RAngle
135
136        #
137        # Initiate the scan
138        CtCDPI_Cursor = bitutil.ScanToFirst(CtCDPI_start)
139        while CtCDPI_Cursor:
140                CtCDPI_starts |= CtCDPI_Cursor
141                PI_Cursor = CtCDPI_Cursor & PI_start
142                CD_Ct_Cursor = bitutil.Advance(CtCDPI_Cursor & ~PI_Cursor)
143                CD_Cursor = CD_Ct_Cursor & lex.LBracket
144                Ct_Cursor = CD_Ct_Cursor & lex.Hyphen
145                ctCDPI_Callouts.PI_starts |= PI_Cursor
146                ctCDPI_Callouts.CD_starts |= CD_Cursor
147                ctCDPI_Callouts.Ct_starts |= Ct_Cursor
148                Ct_Cursor = bitutil.Advance(Ct_Cursor) 
149                Ct_errors |= Ct_Cursor & ~ lex.Hyphen
150                # Advance twice past <!--, so that we don't treat <!---
151                # as being a terminated comment.
152                Ct_Cursor = bitutil.Advance(bitutil.Advance(Ct_Cursor))
153                PI_Cursor = bitutil.Advance(PI_Cursor)
154                ctCDPI_Callouts.PI_name_starts |= PI_Cursor
155                PI_name_end = bitutil.ScanThru(PI_Cursor, lex.NameScan)
156                ctCDPI_Callouts.PI_name_ends |= PI_name_end
157                PI_Cursor = bitutil.ScanTo(PI_name_end, PI_end)
158                CD_Cursor = bitutil.ScanTo(CD_Cursor, ctCDPI_Callouts.CD_end)
159                Ct_Cursor = bitutil.Advance(bitutil.ScanTo(Ct_Cursor, DoubleHyphen))
160                ctCDPI_Callouts.PI_ends |= PI_Cursor
161                ctCDPI_Callouts.CD_ends |= CD_Cursor
162                ctCDPI_Callouts.Ct_ends |= Ct_Cursor
163                CtCDPI_Cursor = PI_Cursor | CD_Cursor | Ct_Cursor
164                CtCDPI_Cursor = bitutil.ScanTo(CtCDPI_Cursor, CtCDPI_start)
165       
166                ctCDPI_Callouts.CtCDPI_mask = bitutil.Advance(ctCDPI_Callouts.CD_ends | ctCDPI_Callouts.Ct_ends | ctCDPI_Callouts.PI_ends) - CtCDPI_starts             
167                #ctCDPI_Callouts.error = Ct_ends & ~lex.RAngle | Ct_starts & ~ lex.Hyphen
168                ctCDPI_Callouts.error = Ct_errors | ctCDPI_Callouts.Ct_ends & ~lex.RAngle
169                ctCDPI_Callouts.error |= bitutil.Advance(ctCDPI_Callouts.PI_name_ends & ~ lex.WS) & ~ PI_end
170                ctCDPI_Callouts.error |= ctCDPI_Callouts.PI_name_starts & ctCDPI_Callouts.PI_name_ends
171                # If any of the Comment, CDATA or PI markups are unterminated, it is an error.
172                ctCDPI_Callouts.error |= ctCDPI_Callouts.CtCDPI_mask &~ EOF_mask
173               
174
175def Parse_tags(lex, scope1, ctCDPI_Callouts, tag_Callouts):
176
177       
178        # Delimiters for scans.
179        DQuoteDelim = lex.DQuote | lex.LAngle
180        SQuoteDelim = lex.SQuote | lex.LAngle
181        AttListDelim = lex.Slash | lex.RAngle
182       
183        # Start the parallel parsing by inspecting the character
184        # after the opening "<" of a tag.
185        LAngleFollow = scope1.LAngle &~ ctCDPI_Callouts.CtCDPI_mask
186        tag_Callouts.ElemName_starts = LAngleFollow & ~lex.Slash
187        tag_Callouts.EndTag_marks = LAngleFollow & lex.Slash
188       
189        # Start Tag/Empty Element Tag Parsing
190
191        # Advance all cursors by scanning through the tag name.
192        tag_Callouts.ElemName_ends = bitutil.ScanThru(tag_Callouts.ElemName_starts, lex.NameScan)
193        # Must have at least one name character for a legal start tag.
194        # Mark any occurrences of null names as errors.
195        ParseError = tag_Callouts.ElemName_starts & tag_Callouts.ElemName_ends
196       
197        # Initialize the accumulators for attribute name and value positions.
198        tag_Callouts.AttName_starts = 0 
199        tag_Callouts.AttName_ends = 0
200        EqToCheck = 0
201        tag_Callouts.AttVal_starts = 0
202        AttValEnds = 0
203        tag_Callouts.AttVal_ends = 0
204
205        # After the element name, there may or may not be an attlist.
206        AfterWS = bitutil.ScanThru(tag_Callouts.ElemName_ends, lex.WS)
207        AttListEnd = AfterWS & AttListDelim
208        AttNameStart = AfterWS & ~AttListDelim
209        # At least one WS character is required between ElemNames and AttNames.
210        ParseError |= tag_Callouts.ElemName_ends & AttNameStart
211
212        #
213        # The following loop iterates through attributes within a start tag.
214        # Because all start tags are processed in parallel, the number of
215        # iterations is the maximum number of attributes found in any one
216        # start tag, plus one.
217        while AttNameStart:
218                tag_Callouts.AttName_starts |= AttNameStart
219                AttNameFollow = bitutil.ScanThru(AttNameStart, lex.NameScan)
220                tag_Callouts.AttName_ends |= AttNameFollow
221                # Scan through WS to the expected '=' delimiter.
222                # EqExpected = bitutil.ScanThru(AttNameFollow, lex.WS)
223                # But use if test to optimize.
224                if AttNameFollow & lex.WS: 
225                        EqExpected = bitutil.ScanThru(AttNameFollow, lex.WS)
226                else: EqExpected = AttNameFollow
227                EqToCheck |= EqExpected
228                AttValPos = bitutil.ScanThru(EqExpected, EqExpected | lex.WS)
229                tag_Callouts.AttVal_starts |= AttValPos
230                DQuoteAttVal = AttValPos & lex.DQuote
231                SQuoteAttVal = AttValPos & lex.SQuote
232                DQuoteAttEnd = bitutil.ScanTo(DQuoteAttVal, DQuoteDelim &~ DQuoteAttVal)
233                SQuoteAttEnd = bitutil.ScanTo(SQuoteAttVal, SQuoteDelim &~ SQuoteAttVal)
234                AttValEnd = DQuoteAttEnd | SQuoteAttEnd
235                AttValEnds |= AttValEnd
236                AttValFollow = bitutil.Advance(AttValEnd)
237                tag_Callouts.AttVal_ends |= AttValFollow
238                #  AfterWS = bitutil.ScanThru(AttValFollow, lex.WS)
239                if AttValFollow & lex.WS: 
240                        AfterWS = bitutil.ScanThru(AttValFollow, lex.WS)
241                else: AfterWS = AttValFollow
242                AfterWS = bitutil.ScanThru(AttValFollow, lex.WS)
243                AttListEnd |= AfterWS & AttListDelim
244                AttNameStart = AfterWS & ~AttListDelim
245
246        # No more attribute values to process when AttNameStart == 0.
247        STagEnds = AttListEnd & lex.RAngle
248        # Mark any "/" characters found as the ends of empty element tags.
249        tag_Callouts.EmptyTag_marks = bitutil.Advance(AttListEnd & lex.Slash)
250       
251        # Check for errors.
252        ParseError |= tag_Callouts.AttVal_ends & tag_Callouts.AttName_starts # No intervening WS.
253        ParseError |= tag_Callouts.AttName_starts & tag_Callouts.AttName_ends # Null AttName
254        ParseError |= EqToCheck & ~lex.Equals # = not found where expected.
255        ParseError |= tag_Callouts.AttVal_starts & ~ (lex.DQuote | lex.SQuote)
256        ParseError |= AttValEnds & ~ (lex.DQuote | lex.SQuote)
257        ParseError |= tag_Callouts.EmptyTag_marks & ~lex.RAngle
258
259        # End Tag Parsing
260
261        EndTagEnds = bitutil.ScanThru(tag_Callouts.EndTag_marks, tag_Callouts.EndTag_marks | lex.NameScan)
262        if EndTagEnds & lex.WS:
263                EndTagEnds = bitutil.ScanThru(EndTagEnds, lex.WS)
264        ParseError |= EndTagEnds & ~lex.RAngle
265        tag_Callouts.error = ParseError
266               
267        # Attribute value spans
268        tag_Callouts.AttVal_spans = tag_Callouts.AttVal_ends - tag_Callouts.AttVal_starts
269                       
270def Parse_refs(lex, scope1, ctCDPI_Callouts, ref_Callouts):
271        ref_Callouts.GenRef_starts = 0
272        ref_Callouts.GenRef_ends = 0
273        ref_Callouts.DecRef_starts = 0
274        ref_Callouts.DecRef_ends = 0
275        ref_Callouts.HexRef_starts = 0
276        ref_Callouts.HexRef_ends = 0
277        ref_Callouts.error = 0
278
279        Ref1 = lex.RefStart &~ ctCDPI_Callouts.CtCDPI_mask
280        # All remaining "&" must be reference start characters; parse them.
281        if Ref1:
282                scope1.RefStart = bitutil.Advance(Ref1)
283                NumRef2 = scope1.RefStart & lex.Hash
284                ref_Callouts.GenRef_starts = scope1.RefStart &~ lex.Hash
285                NumRef3 = bitutil.Advance(NumRef2)
286                HexRef3 = NumRef3 & lex.x
287                ref_Callouts.DecRef_starts = NumRef3 &~ lex.x
288                ref_Callouts.HexRef_starts = bitutil.Advance(HexRef3) 
289                ref_Callouts.GenRef_ends = bitutil.ScanThru(ref_Callouts.GenRef_starts, lex.NameScan)
290                ref_Callouts.DecRef_ends = bitutil.ScanThru(ref_Callouts.DecRef_starts, lex.Digit)
291                ref_Callouts.HexRef_ends = bitutil.ScanThru(ref_Callouts.HexRef_starts, lex.Hex)
292                # Error checks
293                # At least one digit required for DecRef, one hex digit for HexRef.
294                ref_error1 = ref_Callouts.DecRef_starts &~ lex.Digit
295                ref_error2 = ref_Callouts.HexRef_starts &~ lex.Hex
296                # Semicolon terminator required (also covers unterminated at EOF).
297                ref_ends = ref_Callouts.GenRef_ends | ref_Callouts.DecRef_ends | ref_Callouts.HexRef_ends
298                ref_error3 = ref_ends &~ lex.Semicolon
299                ref_Callouts.error = ref_error1 | ref_error2 | ref_error3
300   
301def Do_check_streams(ctCDPI_Callouts, tag_Callouts, lex, u8, scope1, ref_Callouts, xml_names, check_streams):
302    # Ensure that no occurrence of ]]> occurs outside of markup.
303    CD_end_error = ctCDPI_Callouts.CD_end & ~(ctCDPI_Callouts.CtCDPI_mask | tag_Callouts.AttVal_spans)
304           
305    # Consolidate and check for errors
306    check_streams.error_mask = lex.error & EOF_mask | ctCDPI_Callouts.error | tag_Callouts.error | CD_end_error | ref_Callouts.error
307
308    check_streams.tag_marks = tag_Callouts.EmptyTag_marks | tag_Callouts.ElemName_starts | tag_Callouts.EndTag_marks | tag_Callouts.AttName_starts
309    check_streams.name_follows = tag_Callouts.ElemName_ends | tag_Callouts.AttName_ends
310    check_streams.att_refs = tag_Callouts.AttVal_spans & scope1.RefStart
311   
312   
313def Main(lex, scope1, ctCDPI_Callouts, ref_Callouts, tag_Callouts, masks, xml_names, check_streams):   
314       
315        Add_scope_streams(lex, scope1)
316   
317        # Parse all comments, CDATA sections and processing instructions.
318        Parse_CtCDPI(ctCDPI_Callouts, lex, scope1, check_streams)
319               
320        # All remaining '<' must be tag start characters; parse tags.
321        Parse_tags(lex, scope1, ctCDPI_Callouts, tag_Callouts) 
322
323        # All remaining '&' must be reference start characters; parse them.
324        Parse_refs(lex, scope1, ctCDPI_Callouts, ref_Callouts)   
325   
326        Do_check_streams(ctCDPI_Callouts, tag_Callouts, lex, scope1, ref_Callouts, check_streams)
327
328
Note: See TracBrowser for help on using the repository browser.