Nothing Special   »   [go: up one dir, main page]

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

Java Backend: Add line number support. #217

Merged
merged 1 commit into from
Dec 18, 2017

Conversation

guardbotmk3
Copy link

Use complex symbol for automatic context with syntax errors.

@guardbotmk3
Copy link
Author

Consider this a first pass. If there's something that doesn't match the project's style, or you'd like to see re-worked, let me know.

@andreasabel
Copy link
Member

Thanks for the PR and your contribution to BNFC.

I took your PR as opportunity to put out some contribution guidelines, see README at https://github.com/BNFC/bnfc, section Contribute.

I am a bit confused about the title Java Backend: Add line number support. Looking at the existing code, there seems already to be line number support for the java backend. Maybe you meant column number?

  • Please add tests to the https://github.com/BNFC/bnfc/tree/master/testing infrastructure that document the new behavior.
  • Please document your changes in the code and the changelog.
  • From the style guide: Document function arguments, avoid Bool.

Looking forward to the updated PR!

Copy link
Member
@andreasabel andreasabel left a comment

Choose a reason for hiding this comment

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

Mostly request for documentation. Avoid magic numbers.

parserLexerSelector :: String -> JavaLexerParser -> ParserLexerSpecification
parserLexerSelector _ JLexCup = ParseLexSpec
{ lexer = cf2JLex
parserLexerSelector :: String -> JavaLexerParser -> Bool -> ParserLexerSpecification
Copy link
Member

Choose a reason for hiding this comment

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

Avoid Bool (or at least document function argument).

Copy link
Author

Choose a reason for hiding this comment

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

Done

then if jflex then ""
else "int yycolumn = -2;"
else vcat
[ "int yyline = -2;"
Copy link
Member

Choose a reason for hiding this comment

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

Magic numbers like -2 should be avoided.
If unavoidable, document properly. (Why -2, why doesn't it work without the magic?)

Copy link
Author

Choose a reason for hiding this comment

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

Done.

, "%public"
, "%{"
, nest 2 $ vcat
[ "String pstring = new String();"
, "ComplexSymbolFactory.Location left = new ComplexSymbolFactory.Location(-1, -1);"
Copy link
Member

Choose a reason for hiding this comment

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

Document magic numbers (-1).

Copy link
Author

Choose a reason for hiding this comment

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

Done

then "right.move(0, yylength(), yylength());"
else "")
, " return right;"
, "}"
, "public String buff()" <+> braces
(if jflex
then "return new String(zzBuffer,zzCurrentPos,10).trim();"
Copy link
Member

Choose a reason for hiding this comment

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

Magic number 10.

Copy link
Author

Choose a reason for hiding this comment

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

Magic number 10 was there before.

@guardbotmk3
Copy link
Author

I am a bit confused about the title Java Backend: Add line number support. Looking at the existing code, there seems already to be line number support for the java backend. Maybe you meant column number?

No. The line number support currently extends only as far the lexer. The symbols created for the parser have no line information, so syntax errors have no context info to find them.

ComplexSymbol is necessary because the standard JLex Symbol class only keeps track of character position, oddly enough.

Expect an eventual followup to record the line info in the generated abstract syntax tree.

@guardbotmk3
Copy link
Author

Added tests to the testsuite, and verified they pass for jlex and jflex, both with and without line numbers.

@andreasabel
Copy link
Member

Thanks for the quick response!

Will you update this PR or make a new one, with the changes you outlined above?

@guardbotmk3
Copy link
Author
guardbotmk3 commented Dec 13, 2017 via email

@andreasabel andreasabel self-assigned this Dec 13, 2017
@andreasabel andreasabel added this to the 2.8.2 milestone Dec 13, 2017
@andreasabel
Copy link
Member

Thanks for the update. Almost there, can we make the title a bit more precise?
"Java Backend: Add line number support." suggests that there was no line number support even though the help text says

Special options for the Java backend
  -l                        Add and set line_number field for all syntax classes

The new text should make clear

  • what did not work about the existing -l option
  • what works now
  • what does not work yet.

Please write this up in a paragraph or so and put it into Java.hs, and then update the title for more precision.

Thanks for your contribution so far!

Currently the help message includes the following for Java:
  Special options for the Java backend
    -l                        Add and set line_number field for all syntax classes
The only place this support existed was in the help documentation. A quick
glance through the Java backend files will show that the Option.linenumbers was
never inspected. This change adds that support for parsing.

Cup's "Symbol" class only supports character offsets, so we use its
"ComplexSymbol" class, which supports line, column, and offset. JLex does not
provide column info, but JFlex does. ComplexSymbol tracks the left and right
extents of a production, and so for variable length tokens we capture this info.

The abstract syntax classes still do not record line information, but the
necessary information is now available during parsing, and a follow-up commit
will handle adding the info to the Absyn classes and code to record it to the
generated cup file.
@guardbotmk3
Copy link
Author

I added the additional documentation to the commit message. Java.hs didn't seem like the right place for it.
The short answer as to what didn't work is "any of it". If you look at the existing code, nothing in the Java Backend even checks Options.linenumbers. The commit also clarifies what is working post commit, and what still needs to be done.

@andreasabel andreasabel merged commit 0e92612 into BNFC:master Dec 18, 2017
@andreasabel
Copy link
Member

Thanks for the good work!

@andreasabel
Copy link
Member

Er, the testsuite does not pass. I am getting errors like:

C4/Yylex.java:299: error: incompatible types: int cannot be converted to String
    return new ComplexSymbolFactory.Location(yyline+1, yycolumn+1, yychar);
                                                   ^
C4/Yylex.java:303: error: cannot find symbol
  right.move(0, yylength(), yylength());
       ^

According to http://czt.sourceforge.net/dev/java-cup-runtime/apidocs/java_cup/runtime/ComplexSymbolFactory.Location.html there is no Location constructor of the type you use.

Could be that your patch requires a different version of cup?

ubuntu xenial comes with version 0.11a.

apt list -v cup
cup/xenial,xenial,now 0.11a+20060608-6 all [installed]
  LALR parser generator for Java(tm)

What is the minimal version of cup you require?

@andreasabel
Copy link
Member

It seems that with the latest version of java-cup, there are no problems with the testsuite.

How can we ensure that or test whether the necessary version is installed?

@guardbotmk3
Copy link
Author

With the move in there, it requires a fairly new cup, 2015 or so. Without it, any cup 0.11b will do.

I can also adjust the code to work with 0.11a. I have that working already. I'll create a new PR that does that.

andreasabel added a commit that referenced this pull request Jan 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants