1 //
2 //  rbbitblb.h
3 //
4 
5 /*
6 **********************************************************************
7 *   Copyright (c) 2002-2016, International Business Machines
8 *   Corporation and others.  All Rights Reserved.
9 **********************************************************************
10 */
11 
12 #ifndef RBBITBLB_H
13 #define RBBITBLB_H
14 
15 #include "unicode/utypes.h"
16 #include "unicode/uobject.h"
17 #include "unicode/rbbi.h"
18 #include "rbbinode.h"
19 
20 
21 U_NAMESPACE_BEGIN
22 
23 class RBBIRuleScanner;
24 class RBBIRuleBuilder;
25 
26 //
27 //  class RBBITableBuilder is part of the RBBI rule compiler.
28 //                         It builds the state transition table used by the RBBI runtime
29 //                         from the expression syntax tree generated by the rule scanner.
30 //
31 //                         This class is part of the RBBI implementation only.
32 //                         There is no user-visible public API here.
33 //
34 
35 class RBBITableBuilder : public UMemory {
36 public:
37     RBBITableBuilder(RBBIRuleBuilder *rb, RBBINode **rootNode);
38     ~RBBITableBuilder();
39 
40     void     build();
41     int32_t  getTableSize() const;      // Return the runtime size in bytes of
42                                         //     the built state table
43     void     exportTable(void *where);  // fill in the runtime state table.
44                                         //     Sufficient memory must exist at
45                                         //     the specified location.
46 
47 
48 private:
49     void     calcNullable(RBBINode *n);
50     void     calcFirstPos(RBBINode *n);
51     void     calcLastPos(RBBINode  *n);
52     void     calcFollowPos(RBBINode *n);
53     void     calcChainedFollowPos(RBBINode *n);
54     void     bofFixup();
55     void     buildStateTable();
56     void     flagAcceptingStates();
57     void     flagLookAheadStates();
58     void     flagTaggedStates();
59     void     mergeRuleStatusVals();
60 
61     void     addRuleRootNodes(UVector *dest, RBBINode *node);
62 
63     // Set functions for UVector.
64     //   TODO:  make a USet subclass of UVector
65 
66     void     setAdd(UVector *dest, UVector *source);
67     UBool    setEquals(UVector *a, UVector *b);
68 
69     void     sortedAdd(UVector **dest, int32_t val);
70 
71 public:
72 #ifdef RBBI_DEBUG
73     void     printSet(UVector *s);
74     void     printPosSets(RBBINode *n /* = NULL*/);
75     void     printStates();
76     void     printRuleStatusTable();
77 #else
78     #define  printSet(s)
79     #define  printPosSets(n)
80     #define  printStates()
81     #define  printRuleStatusTable()
82 #endif
83 
84 private:
85     RBBIRuleBuilder  *fRB;
86     RBBINode         *&fTree;              // The root node of the parse tree to build a
87                                            //   table for.
88     UErrorCode       *fStatus;
89 
90     UVector          *fDStates;            //  D states (Aho's terminology)
91                                            //  Index is state number
92                                            //  Contents are RBBIStateDescriptor pointers.
93 
94 
95     RBBITableBuilder(const RBBITableBuilder &other); // forbid copying of this class
96     RBBITableBuilder &operator=(const RBBITableBuilder &other); // forbid copying of this class
97 };
98 
99 //
100 //  RBBIStateDescriptor - The DFA is constructed as a set of these descriptors,
101 //                        one for each state.
102 class RBBIStateDescriptor : public UMemory {
103 public:
104     UBool            fMarked;
105     int32_t          fAccepting;
106     int32_t          fLookAhead;
107     UVector          *fTagVals;
108     int32_t          fTagsIdx;
109     UVector          *fPositions;          // Set of parse tree positions associated
110                                            //   with this state.  Unordered (it's a set).
111                                            //   UVector contents are RBBINode *
112 
113     UVector          *fDtran;              // Transitions out of this state.
114                                            //   indexed by input character
115                                            //   contents is int index of dest state
116                                            //   in RBBITableBuilder.fDStates
117 
118     RBBIStateDescriptor(int maxInputSymbol,  UErrorCode *fStatus);
119     ~RBBIStateDescriptor();
120 
121 private:
122     RBBIStateDescriptor(const RBBIStateDescriptor &other); // forbid copying of this class
123     RBBIStateDescriptor &operator=(const RBBIStateDescriptor &other); // forbid copying of this class
124 };
125 
126 
127 
128 U_NAMESPACE_END
129 #endif
130