#include "ParsingAutomaton.h"
namespace
{
namespace
{
using
namespace
collections
;
using
namespace
definitions
;
using
namespace
tabling
;
namespace
{
GetTypeNameForCreateInstruction
(
*
)
{
*
=
->
();
if
(
parent
->
()==
::
)
{
return
GetTypeNameForCreateInstruction
(
parent
)+
L"."
+
->
();
}
else
{
return
->
();
}
}
void
(
<
<
::
>>&
,
<
<
::
>>&
,
const
<
<
::
>>&
)
{
(
,
(
)
.Where([&](
<
::
>
)
{
return
(
).
([&](
<
::
>
)
{
return
::
::
(
lai
,
lai2
)==
::
::
;
});
}),
true
);
}
void
(
<
<
::
>>&
,
const
<
<
::
>>&
)
{
for
(
=
.
()-
1
;
i
>=
0
;
i
--)
{
if
(
.
(
i
].
()))
{
.
(
i
);
}
}
}
bool
(
<
>
,
<
<
::
>>&
,
)
{
=
.
();
for
(
=
0
;
i
<
count
;
i
++)
{
<
::
>
=
i
];
if
(
lai
.
()==
)
{
return
false
;
}
::
::
(
,
lai
,
lai
,
);
}
return
true
;
}
void
(
<
::
>
,
<
<
::
>>&
)
{
(
,
,
true
);
(
,
(
)
.Where([&](
<
::
>
)
{
return
(
).All([&](
<
::
>
)
{
if
(
lai
lai2
)
return
true
;
::
::
=
::
::
(
lai
,
lai2
);
switch
(
result
)
{
case
::
::
:
return
false
;
case
::
::
:
return
.
(
lai
.
()) <
.
(
lai2
.
());
default
:
return
true
;
}
});
}));
}
bool
(
<
>
,
<
::
>
,
<
::
>
,
)
{
<
<
::
>>
,
,
,
;
::
::
(
,
0
,
,
la1
);
::
::
(
,
0
,
,
la2
);
do
{
(
la1
,
sla1
,
la2
);
(
la2
,
sla2
,
la1
);
(
la1
,
sla1
);
(
la2
,
sla2
);
FOREACH(Ptr<ParsingTable::LookAheadInfo>, lai1, la1)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
::
>>&
= ::
vl
::
collections
::
(
la1
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
::
>
;
__foreach_iterator__
.
(
lai1
);)
{
FOREACH(Ptr<ParsingTable::LookAheadInfo>, lai2, la2)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
::
>>&
= ::
vl
::
collections
::
(
la2
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
::
>
;
__foreach_iterator__
.
(
lai2
);)
{
if
(
lai1
==
lai2
)
{
if
(
::
::
(
lai1
,
lai2
) !=
::
::
)
{
return
false
;
}
if
(
::
::
(
lai2
,
lai1
) !=
::
::
)
{
return
false
;
}
}
}
}
if
(!
(
,
la1
,
) || !
(
,
la2
,
))
{
return
false
;
}
}
while
(
la1
.
()>
0
||
la2
.
()>
0
);
(
,
sla1
);
(
,
sla2
);
return
true
;
}
void
(
*
,
<
*>&
)
{
if
(
->
()==
::
)
{
.
(
);
}
=
->
();
for
(
=
0
;
i
<
count
;
i
++)
{
(
->
(
i
),
);
}
}
void
(
<
::
>
,
<
<
definitions
::
ParsingDefinitionAttribute
>>&
)
{
FOREACH(Ptr<definitions::ParsingDefinitionAttribute>, datt, atts)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
definitions
::
ParsingDefinitionAttribute
>>&
= ::
vl
::
collections
::
(
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
definitions
::
ParsingDefinitionAttribute
>
;
__foreach_iterator__
.
(
datt
);)
{
<
::
>
=
new
::
(
datt
);
(
tatt
,
datt
);
.
(
tatt
);
}
}
<
::
>
(
<
<
definitions
::
ParsingDefinitionAttribute
>>&
)
{
<
::
>
=
new
::
;
(
att
,
);
return
att
;
}
LookAheadConflictPriority
(
)
{
switch
(
)
{
case
::
:
return
0
;
case
::
:
return
1
;
default
:
return
2
;
}
}
void
(
<
>
,
<
*>&
,
,
,
<
::
>
,
<
::
>
,
bool
,
collections
::
<
<
>>&
)
{
if
(
::
::
(
,
,
::
::
)==
::
::
)
{
if
(
|| !
(
,
,
,
16
))
{
if
(
LookAheadConflictPriority
(
) !=
LookAheadConflictPriority
(
))
{
return
;
}
=
(
)+
L"["
+
(
).
+
L"]"
;
=
==
::
?
(
L"$TokenBegin"
):
==
::
?
(
L"$TokenFinish"
):
==
::
?
(
L"$NormalReduce"
):
==
::
?
(
L"$LeftRecursiveReduce"
):
(
).
;
switch
(
)
{
case
::
:
.
(
new
(
]->
,
L"Conflict happened with normal reduce in transition of \""
+
tokenName
+
L"\" of state \""
+
stateName
+
L"\"."
));
break
;
case
::
:
.
(
new
(
]->
,
L"Conflict happened with left recursive reduce in transition of \""
+
tokenName
+
L"\" of state \""
+
stateName
+
L"\"."
));
break
;
default
:
.
(
new
(
]->
,
L"Conflict happened in transition of \""
+
tokenName
+
L"\" of state \""
+
stateName
+
L"\"."
));
break
;
}
}
}
}
<
tabling
::
>
(
<
definitions
::
>
,
*
,
<
>
,
bool
,
collections
::
<
<
>>&
)
{
<
<
::
>>
;
<
*>
;
<
,
>
;
<
<
,
>,
>
;
<
*>
;
<
*,
<
<
*>>>
;
(
->
(),
types
);
FOREACH(ParsingSymbol*, type, types)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
types
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
type
);)
{
<
::
>
=
new
::
;
*
=
type
;
while
(
parent
)
{
ParsingDefinitionClassDefinition
*
=
->
(
parent
);
(
typeAtt
,
classDef
->
);
<
<
*>>
;
=
childTypeValues
.
().
(
parent
);
if
(
index
== -
1
)
{
children
new
<
*>;
orderedChildTypeKeys
.
(
parent
);
childTypeValues
.
(
parent
,
children
);
}
else
{
children
childTypeValues
.
().
(
index
);
}
children
(
type
);
parent
=
parent
->
();
}
if
(
typeAtt
.
() >
0
)
{
typeAtts
.
(
(
type
),
atts
.
());
atts
.
(
typeAtt
);
}
else
{
typeAtts
.
(
(
type
), -
1
);
}
}
FOREACH(ParsingSymbol*, type, orderedChildTypeKeys)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
orderedChildTypeKeys
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
type
);)
{
<
*>&
= *
childTypeValues
type
].
();
ParsingDefinitionClassDefinition
*
=
->
(
type
);
<
>
;
FOREACH_INDEXER(Ptr<ParsingDefinitionClassMemberDefinition>, field, index, classDef->members)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
ParsingDefinitionClassMemberDefinition
>>&
= ::
vl
::
collections
::
(
classDef
->
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
if
(
bool
=
true
)
for
(
=
0
;
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
ParsingDefinitionClassMemberDefinition
>
;
__foreach_iterator__
.
(
field
);
index
++)
{
if
(
field
.
() >
0
)
{
fieldAtts
.
(
atts
.
());
atts
.
(
(
field
));
}
else
{
fieldAtts
.
(-
1
);
}
}
FOREACH(ParsingSymbol*, child, children)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
children
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
child
);)
{
=
(
child
);
FOREACH_INDEXER(Ptr<ParsingDefinitionClassMemberDefinition>, field, index, classDef->members)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
ParsingDefinitionClassMemberDefinition
>>&
= ::
vl
::
collections
::
(
classDef
->
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
if
(
bool
=
true
)
for
(
=
0
;
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
ParsingDefinitionClassMemberDefinition
>
;
__foreach_iterator__
.
(
field
);
index
++)
{
treeFieldAtts
.
(
<
,
>(
type
,
field
),
fieldAtts
index
]);
}
}
}
=
0
;
=
0
;
<
*,
>
;
<
>
;
<
,
>
;
<
,
>
;
FOREACH(Ptr<ParsingDefinitionTokenDefinition>, token, definition->tokens)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
ParsingDefinitionTokenDefinition
>>&
= ::
vl
::
collections
::
(
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
ParsingDefinitionTokenDefinition
>
;
__foreach_iterator__
.
(
token
);)
{
if
(
token
.
() >
0
)
{
tokenAtts
.
(
token
,
atts
.
());
atts
.
(
(
token
));
}
else
{
tokenAtts
.
(
token
, -
1
);
}
if
(
token
)
{
discardTokens
.
(
token
);
discardTokenCount
++;
}
else
{
*
=
->
()->
(
token
);
tokenIds
.
(
tokenSymbol
,
tokenIds
.
() +
::
);
tokenCount
++;
}
}
FOREACH(Ptr<ParsingDefinitionRuleDefinition>, rule, definition->rules)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
ParsingDefinitionRuleDefinition
>>&
= ::
vl
::
collections
::
(
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
ParsingDefinitionRuleDefinition
>
;
__foreach_iterator__
.
(
rule
);)
{
if
(
rule
.
() >
0
)
{
ruleAtts
.
(
rule
,
atts
.
());
atts
.
(
(
rule
));
}
else
{
ruleAtts
.
(
rule
, -
1
);
}
}
<
*>
;
=
0
;
{
=
0
;
<
*>
;
FOREACH(Ptr<RuleInfo>, ruleInfo, jointPDA->ruleInfos)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
>>&
= ::
vl
::
collections
::
(
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
>
;
__foreach_iterator__
.
(
ruleInfo
);)
{
if
(!
scanningStates
.
(
ruleInfo
))
{
scanningStates
.
(
ruleInfo
);
}
while
(
currentState
<
scanningStates
.
())
{
*
=
scanningStates
currentState
++];
stateIds
.
(
state
);
FOREACH(Transition*, transition, state->transitions)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
state
->
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
transition
);)
{
if
(!
scanningStates
.
(
transition
->
))
{
scanningStates
.
(
transition
->
);
}
}
}
}
availableStateCount
=
scanningStates
.
();
}
FOREACH(Ptr<State>, state, jointPDA->states)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
>>&
= ::
vl
::
collections
::
(
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
>
;
__foreach_iterator__
.
(
state
);)
{
if
(!
stateIds
.
(
state
.
()))
{
stateIds
.
(
state
.
());
}
}
=
stateIds
.
();
<
>
=
new
(
atts
.
(),
typeAtts
.
(),
treeFieldAtts
.
(),
tokenCount
,
discardTokenCount
,
stateCount
,
.
());
FOREACH_INDEXER(Ptr<ParsingTable::AttributeInfoList>, att, index, atts)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
::
>>&
= ::
vl
::
collections
::
(
atts
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
if
(
bool
=
true
)
for
(
=
0
;
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
::
>
;
__foreach_iterator__
.
(
att
);
index
++)
{
table
(
index
,
att
);
}
typedef
<
,
>
;
FOREACH_INDEXER(TreeTypeAttsPair, type, index, typeAtts)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
>&
= ::
vl
::
collections
::
(
typeAtts
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
if
(
bool
=
true
)
for
(
=
0
;
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
;
__foreach_iterator__
.
(
type
);
index
++)
{
table
(
index
,
::
(
type
.
,
type
.
));
}
typedef
<
<
,
>,
>
;
FOREACH_INDEXER(TreeFieldAttsPair, field, index, treeFieldAtts)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
>&
= ::
vl
::
collections
::
(
treeFieldAtts
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
if
(
bool
=
true
)
for
(
=
0
;
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
;
__foreach_iterator__
.
(
field
);
index
++)
{
table
(
index
,
::
(
field
.
.
,
field
.
.
,
field
.
));
}
FOREACH(ParsingSymbol*, symbol, tokenIds.Keys())
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
tokenIds
.
());
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
symbol
);)
{
::
;
info
.
symbol
->
();
info
.
regex
symbol
->
();
info
.
=
tokenAtts
info
.
];
=
tokenIds
symbol
];
table
(
id
,
info
);
}
FOREACH_INDEXER(WString, name, i, discardTokens)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
>&
= ::
vl
::
collections
::
(
discardTokens
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
if
(
bool
=
true
)
for
(
=
0
;
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
;
__foreach_iterator__
.
(
name
);
i
++)
{
*
=
->
()->
(
name
);
::
;
info
.
symbol
->
();
info
.
regex
symbol
->
();
info
.
=
tokenAtts
info
.
];
table
(
i
,
info
);
}
FOREACH_INDEXER(ParsingDefinitionRuleDefinition*, rule, i, jointPDA->orderedRulesDefs)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
ParsingDefinitionRuleDefinition
*>&
= ::
vl
::
collections
::
(
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
if
(
bool
=
true
)
for
(
=
0
;
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
ParsingDefinitionRuleDefinition
*
;
__foreach_iterator__
.
(
rule
);
i
++)
{
<
>
=
rule
];
::
;
info
.
rule
->
;
info
.
(
rule
->
.
());
info
.
=
stateIds
.
(
pdaRuleInfo
);
info
.
=
ruleAtts
info
.
];
if
(
<
ParsingDefinitionPrimitiveType
>
=
rule
->
.
<
ParsingDefinitionPrimitiveType
>())
{
*
=
->
()->
(
rule
->
);
*
=
ruleSymbol
->
();
ParsingDefinitionClassDefinition
*
=
->
(
ruleType
);
if
(
ruleTypeDef
&&
ruleTypeDef
->
)
{
*
=
->
(
ruleTypeDef
->
.
(),
ruleType
->
());
info
.
(
ambiguousType
);
}
}
table
(
i
,
info
);
}
FOREACH_INDEXER(State*, state, i, stateIds)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
stateIds
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
if
(
bool
=
true
)
for
(
=
0
;
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
state
);
i
++)
{
::
;
info
.
state
->
->
;
info
.
state
->
;
info
.
state
->
;
table
(
i
,
info
);
}
FOREACH_INDEXER(State*, state, stateIndex, stateIds)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
stateIds
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
if
(
bool
=
true
)
for
(
=
0
;
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
state
);
stateIndex
++)
{
if
(
stateIndex
>=
availableStateCount
)
break
;
FOREACH(Transition*, transition, state->transitions)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
state
->
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
transition
);)
{
= -
1
;
switch
(
transition
->
)
{
case
::
:
tokenIndex
=
::
;
break
;
case
::
:
tokenIndex
=
::
;
break
;
case
::
:
tokenIndex
=
::
;
break
;
case
::
:
tokenIndex
=
::
;
break
;
case
::
:
tokenIndex
=
tokenIds
transition
->
];
break
;
default
:;
}
<
::
>
=
table
(
stateIndex
,
tokenIndex
);
if
(!
bag
)
{
bag
new
::
;
table
(
stateIndex
,
tokenIndex
,
bag
);
}
<
::
>
=
new
::
;
item
=
tokenIndex
;
item
=
stateIds
.
(
transition
->
);
bag
.
(
item
);
FOREACH(Ptr<Action>, action, transition->actions)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
>>&
= ::
vl
::
collections
::
(
transition
->
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
>
;
__foreach_iterator__
.
(
action
);)
{
::
;
switch
(
action
)
{
case
::
:
{
ins
.
=
::
::
;
ins
.
GetTypeNameForCreateInstruction
(
action
);
}
break
;
case
::
:
{
ins
.
=
::
::
;
}
break
;
case
::
:
{
if
(
action
->
()->
() ==
::
)
{
ins
.
=
::
::
;
}
else
{
ins
.
=
::
::
;
}
ins
.
action
->
();
}
break
;
case
::
:
{
ins
.
=
::
::
;
ins
.
action
->
();
ins
.
action
->
();
}
break
;
case
::
:
{
ins
.
=
::
::
;
ins
.
=
stateIds
.
(
action
);
}
break
;
case
::
:
{
ins
.
=
::
::
;
ins
.
=
stateIds
.
(
action
);
item
.
(
ins
.
);
}
break
;
case
::
:
{
ins
.
=
::
::
;
ins
.
=
stateIds
.
(
action
);
}
break
;
}
ins
.
action
->
;
item
.
(
ins
);
}
}
}
for
(
=
0
;
i
<
table
();
i
++)
{
for
(
=
0
;
j
<
table
();
j
++)
{
<
::
>
=
table
(
i
,
j
);
if
(
bag
)
{
(
bag
,
(
bag
)
.OrderBy([&](
<
::
>
,
<
::
>
)
{
=
bag
.
(
t1
.
());
=
bag
.
(
t2
.
());
auto
=
i1
<
i2
?
::
::
:
i1
>
i2
?
::
::
:
::
::
;
return
::
::
(
t1
,
t2
,
defaultOrder
);
})
);
for
(
=
0
;
k1
<
bag
.
() -
1
;
k1
++)
{
for
(
=
k1
+
1
;
k2
<
bag
.
();
k2
++)
{
<
::
>
=
bag
k1
];
<
::
>
=
bag
k2
];
(
table
,
stateIds
,
i
,
j
,
t1
,
t2
,
,
);
}
}
for
(
=
::
;
t
<=
::
&&
t
<
j
;
t
++)
{
if
(
<
::
>
=
table
(
i
,
t
))
{
for
(
=
0
;
k1
<
reduceBag
.
();
k1
++)
{
for
(
=
0
;
k2
<
bag
.
();
k2
++)
{
<
::
>
=
reduceBag
k1
];
<
::
>
=
bag
k2
];
(
table
,
stateIds
,
i
,
j
,
t1
,
t2
,
,
);
}
}
}
}
}
}
}
if
(
.
() >
0
)
{
table
(
true
);
}
table
();
return
table
;
}
<
tabling
::
>
(
<
definitions
::
>
,
bool
,
collections
::
<
<
>>&
)
{
.
();
;
(
, &
symbolManager
,
);
if
(
.
()==
0
)
{
<
>
=
(
, &
symbolManager
);
<
>
=
CreateNondeterministicPDAFromEpsilonPDA
(
epsilonPDA
);
<
>
=
CreateJointPDAFromNondeterministicPDA
(
nondeterministicPDA
);
(
jointPDA
);
MarkLeftRecursiveInJointPDA
(
jointPDA
,
);
if
(
.
()==
0
)
{
<
>
=
(
, &
symbolManager
,
jointPDA
,
,
);
if
(
||
.
()==
0
)
{
return
table
;
}
}
}
return
0
;
}
}
}
}