Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the pattern AST creation function #344

Merged
merged 5 commits into from
Mar 4, 2020

Conversation

chisholm
Copy link
Contributor

Fixes #326

It seems like stix2.pattern_visitor.create_pattern_object() was written based on a copy-paste of a function from the pattern validator. But it wasn't changed enough. I fixed it up:

  • The docstring still mentioned validation. That's not what the function does. It was totally wrong. I gave it a better one.
  • That pattern[:2] junk was from the validator checking whether the pattern starts with brackets. It doesn't belong in the AST builder. I removed it.
  • An error listener was used which produced messages with "FAIL:". That makes sense in the context of validation, but not here. There is no success/fail test happening. I switched to a different error listener which produces a more appropriate message.
  • I did an ANTLR parser setup similar to the pattern matcher, with BailErrorStrategy and the appropriate error handling, so that parse errors result in exceptions with sensible error messages.

If we want to make visitor use more convenient, maybe we should give the Pattern class(es) from the pattern validator a visit() method analogous to the walk() method?

An example of error output now (with Python 3):

Traceback (most recent call last):
  File "c:\programming\python\stix\pattern-validator\stix2patterns\v20\grammars\STIXPatternParser.py", line 2090, in primitiveLiteral
    raise NoViableAltException(self)
antlr4.error.Errors.NoViableAltException: None

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "testx_pattern_parse.py", line 3, in <module>
    errs = create_pattern_object(pattern="[ file: name = 'weirdname]")
  File "C:\Programming\python\stix\cti-python-stix2\stix2\pattern_visitor.py", line 363, in create_pattern_object
    six.raise_from(
  File "<string>", line 3, in raise_from
stix2patterns.exceptions.ParseException: 1:15: no viable alternative at input '''

documentation at least isn't wrong, and it behaves better.
@codecov-io
Copy link

codecov-io commented Feb 18, 2020

Codecov Report

Merging #344 into master will increase coverage by 0.05%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #344      +/-   ##
==========================================
+ Coverage    98.1%   98.16%   +0.05%     
==========================================
  Files         124      124              
  Lines       14185    14158      -27     
==========================================
- Hits        13916    13898      -18     
+ Misses        269      260       -9
Impacted Files Coverage Δ
stix2/pattern_visitor.py 72.81% <100%> (-1.75%) ⬇️
stix2/test/v21/test_indicator.py 100% <100%> (ø) ⬆️
stix2/test/v21/test_pattern_expressions.py 100% <100%> (ø) ⬆️
stix2/v21/observables.py 96.93% <0%> (-0.21%) ⬇️
stix2/test/v21/test_observed_data.py 100% <0%> (ø) ⬆️
stix2/base.py 97.8% <0%> (+0.09%) ⬆️
stix2/properties.py 98.36% <0%> (+0.54%) ⬆️
stix2/utils.py 97.72% <0%> (+1.13%) ⬆️
stix2/canonicalization/Canonicalize.py 61.44% <0%> (+1.2%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8aca39a...4f00c7c. Read the comment docs.

@clenk
Copy link
Contributor

clenk commented Feb 19, 2020

Looks great! I think we should have a test case to make sure the right exception class is being thrown.

I wouldn't be opposed to also adding a visit() method.

@chisholm
Copy link
Contributor Author

I added the visit() method and changed create_pattern_object() to take advantage of it. Of course, travis tests won't pass until a new version of the pattern validator is released which has the change. (And we too are suffering from the ANTLR 4.8 issue.)

@emmanvg emmanvg added this to the 1.3.1 milestone Mar 3, 2020
Copy link
Contributor

@clenk clenk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@clenk clenk merged commit 30a59ad into oasis-open:master Mar 4, 2020
@chisholm chisholm deleted the fix_ast_builder branch March 4, 2020 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect Pattern Error Propagation
4 participants