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

Print parse tree from CLI to terminal #2638

Closed
romani opened this issue Nov 21, 2015 · 16 comments
Closed

Print parse tree from CLI to terminal #2638

romani opened this issue Nov 21, 2015 · 16 comments
Assignees
Milestone

Comments

@romani
Copy link
Member

romani commented Nov 21, 2015

this this good task for new contributors.

in connection with Javadoc tree #652 we need a option to main CLI to print parse tree to output.
That will help a lot .

right now we are forced to use Swing gui for this.

it is not clear for now in what format to print a tree, need to be discussed.

@Vladlis
Copy link
Contributor

Vladlis commented Jan 24, 2016

I suggest to use the same format which is used in JavadocTreePrinter.
Example file:

package pack.test;

class A extends B {
    final static String CONSTANT = "This is constant";

    private void foo(String x) { // comment
        foo(x);
    }
}

Result:

PACKAGE_DEF : [package]
|--ANNOTATIONS : [ANNOTATIONS]
|--DOT : [.]
    |--IDENT : [pack]
    |--IDENT : [test]
|--SEMI : [;]
CLASS_DEF : [CLASS_DEF]
|--MODIFIERS : [MODIFIERS]
|--LITERAL_CLASS : [class]
|--IDENT : [A]
|--EXTENDS_CLAUSE : [extends]
    |--IDENT : [B]
|--OBJBLOCK : [OBJBLOCK]
    |--LCURLY : [{]
    |--VARIABLE_DEF : [VARIABLE_DEF]
        |--MODIFIERS : [MODIFIERS]
            |--FINAL : [final]
            |--LITERAL_STATIC : [static]
        |--TYPE : [TYPE]
            |--IDENT : [String]
        |--IDENT : [CONSTANT]
        |--ASSIGN : [=]
            |--EXPR : [EXPR]
                |--STRING_LITERAL : ["This is constant"]
        |--SEMI : [;]
    |--METHOD_DEF : [METHOD_DEF]
        |--MODIFIERS : [MODIFIERS]
            |--LITERAL_PRIVATE : [private]
        |--TYPE : [TYPE]
            |--LITERAL_VOID : [void]
        |--IDENT : [foo]
        |--LPAREN : [(]
        |--PARAMETERS : [PARAMETERS]
            |--PARAMETER_DEF : [PARAMETER_DEF]
                |--MODIFIERS : [MODIFIERS]
                |--TYPE : [TYPE]
                    |--IDENT : [String]
                |--IDENT : [x]
        |--RPAREN : [)]
        |--SLIST : [{]
            |--EXPR : [EXPR]
                |--METHOD_CALL : [(]
                    |--IDENT : [foo]
                    |--ELIST : [ELIST]
                        |--EXPR : [EXPR]
                            |--IDENT : [x]
                    |--RPAREN : [)]
            |--SEMI : [;]
            |--RCURLY : [}]
    |--RCURLY : [}]

@romani
Copy link
Member Author

romani commented Jan 24, 2016

We need line number and column number, please put them in "[]".

@romani
Copy link
Member Author

romani commented Jan 24, 2016

What if we draw a tree as tool http://lintut.com/use-tree-command-in-linux/ do this ? That tool is famous so we could reuse its experience in drawing a tree.

@Vladlis
Copy link
Contributor

Vladlis commented Jan 24, 2016

OK
Please, pay attention on how line breaks are displayed.
Source:

class A {
    public static final String CONSTANT = "This
           is
           constant";
}

Result:

CLASS_DEF -> CLASS_DEF [1:0]
|--MODIFIERS -> MODIFIERS [1:0]
|--LITERAL_CLASS -> class [1:0]
|--IDENT -> A [1:6]
`--OBJBLOCK -> OBJBLOCK [1:8]
    |--LCURLY -> { [1:8]
    |--VARIABLE_DEF -> VARIABLE_DEF [2:4]
    |   |--MODIFIERS -> MODIFIERS [2:4]
    |   |   |--LITERAL_PUBLIC -> public [2:4]
    |   |   |--LITERAL_STATIC -> static [2:11]
    |   |   `--FINAL -> final [2:18]
    |   |--TYPE -> TYPE [2:24]
    |   |   `--IDENT -> String [2:24]
    |   |--IDENT -> CONSTANT [2:31]
    |   |--ASSIGN -> = [2:40]
    |   |   `--EXPR -> EXPR [2:42]
    |   |       `--STRING_LITERAL -> "This\n           is\n           constant" [2:42]
    |   `--SEMI -> ; [2:82]
    `--RCURLY -> } [3:0]

@romani
Copy link
Member Author

romani commented Jan 24, 2016

looks good, but I still do not see a reason to use simple symbols "`" instead pseudo graphics "└" (extended ascii symbols) http://www.asciitable.com/

~/.../checkstyle/checks [master|✔] $ tree
├── AbstractDeclarationCollector.java
├── AbstractFormatCheck.java
├── AbstractOptionCheck.java
├── AbstractTypeAwareCheck.java
├── annotation
│   ├── AnnotationLocationCheck.java
│   ├── AnnotationUseStyleCheck.java
│   ├── MissingDeprecatedCheck.java
│   ├── MissingOverrideCheck.java
│   ├── PackageAnnotationCheck.java
│   ├── package-info.java
│   └── SuppressWarningsCheck.java
├── ArrayTypeStyleCheck.java
├── AvoidEscapedUnicodeCharactersCheck.java
├── blocks
│   ├── AvoidNestedBlocksCheck.java
│   ├── BlockOption.java
│   ├── EmptyBlockCheck.java

@Vladlis
Copy link
Contributor

Vladlis commented Jan 29, 2016

When I try to print such a symbol right from IntelliJ IDEA under Windows I get в”” .
Even though it works OK, when I use jar, it makes development platform- or locale- or smth else- dependent.

Vladlis added a commit to Vladlis/checkstyle that referenced this issue Jan 29, 2016
@romani
Copy link
Member Author

romani commented Jan 29, 2016

@Vladlis , if that is a problems with output in IntelijIdea+windows only and it does not brake UTs execution in such IDE - we could ignore that problem. I doubt that smb will run this from IDE.

@Vladlis
Copy link
Contributor

Vladlis commented Jan 29, 2016

when I say "from IDE" I mean while running tests too

@romani
Copy link
Member Author

romani commented Jan 29, 2016

@Vladlis , are UTs failing when extended ascii symbols are used (in code and in UT test method) ?

@Vladlis
Copy link
Contributor

Vladlis commented Jan 31, 2016

Vladlis added a commit to Vladlis/checkstyle that referenced this issue Jan 31, 2016
@Vladlis
Copy link
Contributor

Vladlis commented Jan 31, 2016

Well, looks like pseudo graphics is not the reason of the failure:
https://ci.appveyor.com/project/Checkstyle/checkstyle/build/3470/job/a1evc16n02prai9c#L706

Vladlis added a commit to Vladlis/checkstyle that referenced this issue Feb 3, 2016
Vladlis added a commit to Vladlis/checkstyle that referenced this issue Feb 3, 2016
@Vladlis
Copy link
Contributor

Vladlis commented Feb 3, 2016

I've solved the problem locally by fixing line endings, but it still did not pass Appveyor until I replaced non-ASCII symbols.

@romani
Copy link
Member Author

romani commented Feb 3, 2016

do you mean "ASCII extended" by "non-ASCII" ?

Different behaviour likely relate to different locales and charsets. Some pain is shared at http://stackoverflow.com/questions/388490/unicode-characters-in-windows-command-line-how , there more other links to problem.
Please confirm that after changing a charset it works. If it works we leave it as comment and stay with simple ASCII codes.

Vladlis added a commit to Vladlis/checkstyle that referenced this issue Feb 3, 2016
Vladlis added a commit to Vladlis/checkstyle that referenced this issue Feb 3, 2016
Vladlis added a commit to Vladlis/checkstyle that referenced this issue Feb 3, 2016
@Vladlis
Copy link
Contributor

Vladlis commented Feb 3, 2016

Changing charset does not work for Appveyor:
https://ci.appveyor.com/project/Checkstyle/checkstyle/build/3491/job/si1rx5hpciltraof#L50
https://ci.appveyor.com/project/Checkstyle/checkstyle/build/3491/job/si1rx5hpciltraof#L703

And test suddenly failed on my local machine, so usage of such symbols seems to be unstable for Windows:
capture

I'll use an ASCII symbol and put a comment in code.

Vladlis added a commit to Vladlis/checkstyle that referenced this issue Feb 3, 2016
Vladlis added a commit to Vladlis/checkstyle that referenced this issue Feb 4, 2016
Vladlis added a commit to Vladlis/checkstyle that referenced this issue Feb 4, 2016
@romani
Copy link
Member Author

romani commented Feb 4, 2016

Due to unstable behaviour of different instances of Windows, we will not use ascii extended symbols.
Code should be be compilable and run out of the box on all OS.

but some Windows do show symbols correctly:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\rivanov>java -version
java version "1.8.0_71"
Java(TM) SE Runtime Environment (build 1.8.0_71-b15)
Java HotSpot(TM) Client VM (build 25.71-b15, mixed mode, sharing)

C:\Users\rivanov>cd
C:\Users\rivanov

C:\Users\rivanov>cd C:\Users\rivanov\Downloads

C:\Users\rivanov\Downloads>java -jar checkstyle-6.16-SNAPSHOT-all.jar -t MyClass
.java
CLASS_DEF -> CLASS_DEF [1:0]
|--MODIFIERS -> MODIFIERS [1:0]
|--LITERAL_CLASS -> class [1:0]
|--IDENT -> MyClass [1:6]
└--OBJBLOCK -> OBJBLOCK [1:14]
    |--LCURLY -> { [1:14]
    |--VARIABLE_DEF -> VARIABLE_DEF [2:1]
    |   |--MODIFIERS -> MODIFIERS [2:1]
    |   |   └--LITERAL_PRIVATE -> private [2:1]
    |   |--TYPE -> TYPE [2:9]
    |   |   └--LITERAL_INT -> int [2:9]
    |   |--IDENT -> value [2:13]
    |   └--SEMI -> ; [2:18]
    └--RCURLY -> } [3:0]

C:\Users\rivanov\Downloads>chcp
Active code page: 437

C:\Users\rivanov\Downloads>

screenshot-checkstyle-java-syntax-tree-ascii-extended-symbols

@romani romani added this to the 6.16 milestone Feb 4, 2016
@romani
Copy link
Member Author

romani commented Feb 4, 2016

code is merged, issue is closed.

@romani romani closed this as completed Feb 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants