31
31
import javax .swing .text .JTextComponent ;
32
32
import javax .swing .text .StyledDocument ;
33
33
import org .antlr .v4 .runtime .Lexer ;
34
+ import org .antlr .v4 .runtime .Token ;
35
+ import org .antlr .v4 .runtime .Vocabulary ;
36
+ import org .antlr .v4 .runtime .VocabularyImpl ;
34
37
import org .netbeans .api .annotations .common .NonNull ;
35
38
import org .netbeans .api .editor .EditorRegistry ;
36
39
import org .netbeans .api .settings .ConvertAsProperties ;
@@ -601,29 +604,46 @@ public void propertyChange(PropertyChangeEvent evt) {
601
604
}
602
605
603
606
Document document = component .getDocument ();
604
- TokenDescriptor [] tokenDescriptorArray = (TokenDescriptor [] )document .getProperty (LexerDebuggerEditorKit .PROP_TOKEN_NAMES );
607
+ Vocabulary vocabulary = (Vocabulary )document .getProperty (LexerDebuggerEditorKit .PROP_VOCABULARY );
605
608
String [] modeNamesArray = (String [])document .getProperty (LexerDebuggerEditorKit .PROP_MODE_NAMES );
606
- List <TokenDescriptor > tokenDescriptors = tokenDescriptorArray != null ? Arrays .asList (tokenDescriptorArray ) : Collections .<TokenDescriptor >emptyList ();
607
609
List <String > modeNames = modeNamesArray != null ? Arrays .asList (modeNamesArray ) : Collections .<String >emptyList ();
608
- if (tokenDescriptors . isEmpty () ) {
610
+ if (vocabulary == null ) {
609
611
LexerInterpreterData lexerInterpreterData = (LexerInterpreterData )document .getProperty (LexerDebuggerEditorKit .PROP_LEXER_INTERP_DATA );
610
612
if (lexerInterpreterData != null ) {
611
- tokenDescriptors = lexerInterpreterData .tokenNames ;
613
+ vocabulary = lexerInterpreterData .vocabulary ;
612
614
modeNames = lexerInterpreterData .modeNames ;
613
615
}
614
616
}
615
617
616
- final List < TokenDescriptor > finalTokenDescriptors = tokenDescriptors ;
618
+ final Vocabulary finalVocabulary = vocabulary != null ? vocabulary : VocabularyImpl . EMPTY_VOCABULARY ;
617
619
final List <String > finalModeNames = modeNames ;
618
620
619
621
currentComponent = component ;
620
622
621
623
tblTokenTypes .setModel (new AbstractTableModel () {
622
- private final List <TokenDescriptor > elements = finalTokenDescriptors ;
624
+ private final List <String > literalNames = new ArrayList <>();
625
+ private final List <String > symbolicNames = new ArrayList <>();
626
+ private final List <Integer > values = new ArrayList <>();
627
+
628
+ {
629
+ // TODO: Find a better way to communicate this value
630
+ int maxTokenType = 1024 ;
631
+ for (int i = 0 ; i <= maxTokenType ; i ++) {
632
+ String literalName = finalVocabulary .getLiteralName (i );
633
+ String symbolicName = finalVocabulary .getSymbolicName (i );
634
+ if (literalName == null && symbolicName == null ) {
635
+ continue ;
636
+ }
637
+
638
+ literalNames .add (literalName != null ? literalName : "" );
639
+ symbolicNames .add (symbolicName != null ? symbolicName : literalName );
640
+ values .add (i );
641
+ }
642
+ }
623
643
624
644
@ Override
625
645
public int getRowCount () {
626
- return elements .size ();
646
+ return literalNames .size ();
627
647
}
628
648
629
649
@ Override
@@ -664,11 +684,11 @@ public Class<?> getColumnClass(int columnIndex) {
664
684
public Object getValueAt (int rowIndex , int columnIndex ) {
665
685
switch (columnIndex ) {
666
686
case 0 :
667
- return elements .get (rowIndex ). name ;
687
+ return symbolicNames .get (rowIndex );
668
688
case 1 :
669
- return elements .get (rowIndex ). literal ;
689
+ return literalNames .get (rowIndex );
670
690
case 2 :
671
- return elements .get (rowIndex ). value ;
691
+ return values .get (rowIndex );
672
692
default :
673
693
throw new IllegalArgumentException ();
674
694
}
@@ -690,15 +710,16 @@ public TraceToken getElementAt(int index) {
690
710
691
711
});
692
712
693
- String [] tokenNamesArray = new String [tokenDescriptors .size ()];
694
- for (int i = 0 ; i < tokenDescriptors .size (); i ++) {
695
- tokenNamesArray [i ] = tokenDescriptors .get (i ).literal ;
696
- if (tokenNamesArray [i ] == null || tokenNamesArray [i ].isEmpty ()) {
697
- tokenNamesArray [i ] = tokenDescriptors .get (i ).name ;
713
+ List <String > tokenNames = new ArrayList <>();
714
+ for (int i = Token .EOF ; i < 1024 ; i ++) {
715
+ if (finalVocabulary .getLiteralName (i ) == null && finalVocabulary .getSymbolicName (i ) == null ) {
716
+ continue ;
698
717
}
718
+
719
+ tokenNames .add (finalVocabulary .getDisplayName (i ));
699
720
}
700
721
701
- lstTokens .setCellRenderer (new TraceTokenListCellRenderer (Arrays . asList ( tokenNamesArray ) ));
722
+ lstTokens .setCellRenderer (new TraceTokenListCellRenderer (tokenNames ));
702
723
703
724
lstChannels .setModel (new AbstractListModel <Object >() {
704
725
private final Object [] elements = { defaultChannelText , hiddenChannelText };
@@ -824,10 +845,4 @@ public Component getListCellRendererComponent(JList<?> list, Object value, int i
824
845
}
825
846
826
847
}
827
-
828
- public static class TokenDescriptor {
829
- public String name = "" ;
830
- public String literal = "" ;
831
- public int value ;
832
- }
833
848
}
0 commit comments