Bridging from NSString to String in Swift 5 is a transcoding operation, so
it is expensive. Use String(describing:) instead of NSStringFromClass
to avoid this cost.
This switches from using the deprecated hashValue to hash(into:).
It also switches from using index to firstIndex (matching the change in
the standard library).
In the test template, we switch to using String directly instead of
String.characters.
This also switches all the Travis macOS tests to use the Xcode 10.2 / Mojave
image and changes the Linux Swift tests to download Swift 5.0.1.
Converted back all TO_DO items to TODO (they were renamed ANTLR4 TODO items, but I had some for C++ which I all wanted to fix).
Also removed some default copy assignment operators which clang warned about.
When an exception is thrown, it calls getText to get the text of the tokens invloved but the current C++ and Java implemention first fetches the rest of the tokens instead of just using the tokens it already has. C#'s version is correct (fill() is called only when asking for the entire stream's text).
Importing module is quite expensive operation for using it
in a lexer during normal operations.
The patch avoids it by caching the required properties
in LexerATNSimulator object.
Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
The revently added clang directive cause a new warning for Visual Studio build. Need to include the new code with a directive checking for visual studio
<!--
Before submitting an issue to ANTLR, please check off these boxes:
- [x] I am not submitting a question on how to use ANTLR; instead, go to [antlr4-discussion google group](https://groups.google.com/forum/#!forum/antlr-discussion) or ask at [stackoverflow](http://stackoverflow.com/questions/tagged/antlr4)
- [ x] I have done a search of the existing issues to make sure I'm not sending in a duplicate
Please include information about the expected behavior, actual behavior, and the smallest grammar or code that reproduces the behavior. If appropriate, please indicate the code generation targets such as Java, C#, ... Pointers into offending code regions are also very welcome.
-->
As documented previousTokenOnChannel, should "return -1 if there are no tokens on channel between i and 0." but the C++ version of the runtime returns 0 not -1 as can be seen below:
```
while (true) {
Token *token = _tokens[i].get();
if (token->getType() == Token::EOF || token->getChannel() == channel) {
return i;
}
if (i == 0)
break;
i--;
}
```
Looking at the Java implementation, it would seem the C++ code should instead be:
```
while (true) {
Token *token = _tokens[i].get();
if (token->getType() == Token::EOF || token->getChannel() == channel) {
return i;
}
if (i == 0)
return -1;
i--;
}
```
This bug causes getHiddenTokensToLeft() to miss hidden tokens that come before the first non-hidden token. There would also be a potential bug in CommonTokenStream::LB as the "< 0" case would never happen.
In the orignal PredictionContext::mergeArrays there was a bug on Line 281 where the logic differs from java: It currently is:
bool both$ = payload == EMPTY_RETURN_STATE && a_parent && b_parent;
and should instead match java as:
bool both$ = payload == EMPTY_RETURN_STATE && !a_parent && !b_parent;
This was brought over from the Java runtime in the initial port, but there
it was used as an array capacity hint. We're not using it in Swift so
this is useless.
This is a subclass that only exists to have a different constructor. There
is no need for this construction in Swift, since we have named parameters,
so we can remove the entire subclass and make ATNConfigSet final instead.
Change all the subprocess calls in boot.py to check whether they succeeded,
and set the script status appropriately.
In particular, when our unit tests fail, we need the script to exit
with a failure code so that we actually notice on Travis.
This was always clearly a possibility, looking at the body of the method.
The newly-enabled performance tests expose this bug (and I don't know how
we've gotten away with it otherwise for so long).
The Java runtime also returns null at this point.
The ATNState hashValue and == override are just using the stateNumber field, so
using the Int directly is equivalent, and saves bouncing through those methods.
This also seems to be a correctness issue with the new Hashable protocol changes in
Swift 4.2 (though I admit that I don't know why).
Remove PredictionMode.getStateToAltMap, which was just a stub onto
ATNConfigSet.getStateToAltMap and didn't seem to be doing anything useful.
Avoid adding to closureBusy before all ATNConfig properties are set.
This fixes#2372.
This is a port of c8805ab from the Java runtime. That was PR #1955.
Suppress "Optional" in the output when printing a value in the tests and
some debugging messages.
This is a change in behavior in Swift 4.2 (SE-0054) that implicitly
unwrapped optionals are now seen as plain Optional at runtime, and so
print doesn't implicitly unwrap them any more.
This was working before because calling hashValue on a boxed UInt32 gave
back the value itself. This is apparently no longer true.
It's not something we should have been doing anyway. We were needlessly
boxing the intermediate hash values, and passing them into a generic
method, just to unbox them again.
Fix this by creating a helper method, and calling that directly when
updating intermediate hash values.
This is three instances of flatMap changing to compactMap, and
one instance of UnsafeMutablePointer.deallocate(capacity:) changing
to UnsafeMutablePointer.deallocate().
The antlrcpp::Any::is function should not throw a std::bad_cast
exception if the contained type can't be cast to the requested type,
but should instead just return a boolean result. Add a boolean
parameter to the private getDerived helper function to allow callers
to specify whether or not they want the cast results checked. In the
is() function, pass false for this parameter; in the as() functions,
pass true.
To make it easier to work with the const antlrcpp::Any arguments to
the AbstractParseTreeVisitor class aggregateResult and
shouldVisitNextChild functions, add a public const overload for the
antlrcpp::Any::as member function to return a const StorageType&, and
add a public const overloaded conversion operator returning a const
instance of the type contained within the Any.
Add the private antlrcpp::Any::getDerived function to avoid
duplicating the dynamic_cast of the internal pointer, and call it from
the overloaded Any::as functions and also the Any::is function.
This is to have appropriate logs during debug and avoid crashing in some occasions when startIndex=stopIndex
range(startIndex, stopIndex) is equivalent to an array of values from startIndex to stopIndex-1 thus when startIndex=stopIndex range(startIndex, stopIndex+1) = [startIndex] which is not exactly an interval and would break getText that is expecting an interval as a Tuple.
Previously Visual C++ users were forced to link CRT statically,
i.e. use /MT flag whenever they want to use the static library.
Linker have an error if user tries to link a /MT static library
to a /MD executable.
This commit defaults the build to statically link with CRT, but
may be turned off if needed.
This commit fixes:
- In MingGW
warning: declaration 'class std::exception' does not declare anything
- In Clang (I tested with clang-tidy on Windows specifically)
error: forward declaration of class cannot have a nested name
specifier [clang-diagnostic-error]
This adds an Automatic-Module-Name entry to the runtime jar in order to
provide a stable name upon which other modules can depend. The module
name chosen was "org.antlr.antlr4.runtime". This closely matches the
Maven artifact name with the obviously change that the module name
doesn't contain a hyphen (hyphens can't be used in module names at
the language level in Java).
Fix#2163
'antlr4-runtime' which imports the required runtime targets and
'antlr4-generator' which provides a convenient function to use the generator.
Only the latter one has a dependency to the Java package.
The shared s0 state in a DFA was deleted when setting a new one (from the DFA entries possibly). That could led to a situation where a valid reference was deleted, even though it's still needed.
Additionally fixed some formattings and updated the XCode projects in the demo.
Parser::exitRule() in the C++ runtime is a virtual function which is not
reimplemented anywhere. OTOH, it is invoked during the execution of
every rule, which can cause a noticeable performance hit. This commit
removes its virtual qualifier. It should make a difference particularly
for large grammars, because the number of rules corresponds to the
number of the Parser object's virtual functions, and, consequently, its
vtable lookup time.
Tested with a VHDL grammar of 436 rules, where it brings down parsing
time from 75 to 44 seconds on unoptimized compilation, i.e. a 40% speed
gain. Still a lot slower than an equivalent java parser, though, which
takes 2.64 seconds for the same input.
Add missing override markers to the following functions of the C++
runtime:
- TokensStartState::getStateType()
- TagChunk::toString()
- TextChunk::toString()
The missing markers made builds against the API with -Wsuggest-override
choke.
Remove HashMap, and replace all uses of it with dictionaries. There's
no need for us to have a custom HashMap implementation (mirroring the Java
standard library) when Swift's standard dictionaries work just fine.
Fix Parser.bypassAltsAtnCache. This was declared as a Parser instance
variable, when in the Java runtime it is static (and therefore the cache
outlives the Parser instances). It was also being handled in a
thread-unsafe manner, because the cache was being read outside of the
mutex that was supposed to be protecting it. Fix both issues by moving
the cache and the mutex so that they are static to the Parser module and
rewriting getATNWithBypassAlts.
Remove Parser.decisionToDFAMutex. The Java code uses a synchronized block
on ParserATNSimulator.decisionToDFA, but the translation to Swift had put
a mutex in Parser. The decisionToDFA value is shared between Parser,
ParserATNSimulator, and the generated parser, so a mutex in
ParserATNSimulator isn't blocking all possible accesses, so it's useless.
Since this is only code for debugging anyway, just remove the useless mutex
and simplify getDFAStrings and dumpDFA.