2525
2626package jdk .graal .compiler .graphio .parsing ;
2727
28- import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_BLOCK ;
29- import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_CLASS ;
30- import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_DUPLICATE ;
31- import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_HAS_PREDECESSOR ;
32- import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_ID ;
33- import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_IDX ;
34- import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_NAME ;
35- import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_SHORT_NAME ;
36- import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyValues .CLASS_ENDNODE ;
28+ import jdk .graal .compiler .graphio .parsing .BinaryReader .EnumValue ;
29+ import jdk .graal .compiler .graphio .parsing .BinaryReader .Method ;
30+ import jdk .graal .compiler .graphio .parsing .model .Folder ;
31+ import jdk .graal .compiler .graphio .parsing .model .FolderElement ;
32+ import jdk .graal .compiler .graphio .parsing .model .GraphClassifier ;
33+ import jdk .graal .compiler .graphio .parsing .model .GraphDocument ;
34+ import jdk .graal .compiler .graphio .parsing .model .Group ;
35+ import jdk .graal .compiler .graphio .parsing .model .InputBlock ;
36+ import jdk .graal .compiler .graphio .parsing .model .InputEdge ;
37+ import jdk .graal .compiler .graphio .parsing .model .InputGraph ;
38+ import jdk .graal .compiler .graphio .parsing .model .InputMethod ;
39+ import jdk .graal .compiler .graphio .parsing .model .InputNode ;
40+ import jdk .graal .compiler .graphio .parsing .model .Properties ;
3741
3842import java .util .ArrayDeque ;
3943import java .util .ArrayList ;
4751import java .util .Objects ;
4852import java .util .Set ;
4953import java .util .logging .Logger ;
50- import java .util .regex .Matcher ;
51- import java .util .regex .Pattern ;
5254
53- import jdk .graal .compiler .graphio .parsing .BinaryReader .EnumValue ;
54- import jdk .graal .compiler .graphio .parsing .BinaryReader .Method ;
55- import jdk .graal .compiler .graphio .parsing .model .Folder ;
56- import jdk .graal .compiler .graphio .parsing .model .FolderElement ;
57- import jdk .graal .compiler .graphio .parsing .model .GraphClassifier ;
58- import jdk .graal .compiler .graphio .parsing .model .GraphDocument ;
59- import jdk .graal .compiler .graphio .parsing .model .Group ;
60- import jdk .graal .compiler .graphio .parsing .model .InputBlock ;
61- import jdk .graal .compiler .graphio .parsing .model .InputEdge ;
62- import jdk .graal .compiler .graphio .parsing .model .InputGraph ;
63- import jdk .graal .compiler .graphio .parsing .model .InputMethod ;
64- import jdk .graal .compiler .graphio .parsing .model .InputNode ;
65- import jdk .graal .compiler .graphio .parsing .model .Properties ;
55+ import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_BLOCK ;
56+ import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_CLASS ;
57+ import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_DUPLICATE ;
58+ import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_HAS_PREDECESSOR ;
59+ import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_ID ;
60+ import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_IDX ;
61+ import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_NAME ;
62+ import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyNames .PROPNAME_SHORT_NAME ;
63+ import static jdk .graal .compiler .graphio .parsing .model .KnownPropertyValues .CLASS_ENDNODE ;
6664
6765/**
6866 * Builds a model based on SAX-like events. The expected sequence of events is:
@@ -684,7 +682,7 @@ protected final void reportProgress() {
684682 @ Override
685683 public void setNodeName (NodeClass nodeClass ) {
686684 assert currentNode != null ;
687- getProperties ().setProperty (PROPNAME_NAME , createName (nodeClass , nodeEdges , nodeClass . nameTemplate ));
685+ getProperties ().setProperty (PROPNAME_NAME , createName (nodeClass , nodeEdges ));
688686 getProperties ().setProperty (PROPNAME_CLASS , nodeClass .className );
689687 switch (nodeClass .className ) {
690688 case "BeginNode" :
@@ -696,35 +694,38 @@ public void setNodeName(NodeClass nodeClass) {
696694 }
697695 }
698696
699- static final Pattern TEMPLATE_PATTERN = Pattern .compile ("\\ {([pi])#([a-zA-Z0-9$_]+)(/([lms]))?}" );
700-
701- private String createName (NodeClass nodeClass , List <EdgeInfo > edges , String template ) {
702- if (template .isEmpty ()) {
697+ private String createName (NodeClass nodeClass , List <EdgeInfo > edges ) {
698+ if (nodeClass .nameTemplate .isEmpty ()) {
703699 return nodeClass .toShortString ();
704700 }
705- Matcher m = TEMPLATE_PATTERN . matcher ( template );
706- StringBuilder sb = new StringBuilder ();
701+
702+ StringBuilder sb = new StringBuilder (nodeClass . nameTemplate . length () );
707703 Properties p = getProperties ();
708- while (m .find ()) {
709- String name = m .group (2 );
710- String type = m .group (1 );
711- String result ;
704+ List <TemplateParser .TemplatePart > templateParts = nodeClass .getTemplateParts ();
705+ for (TemplateParser .TemplatePart template : templateParts ) {
706+ if (!template .isReplacement ) {
707+ sb .append (template .value );
708+ continue ;
709+ }
710+ String name = template .name ;
711+ String type = template .type ;
712712 switch (type ) {
713713 case "i" :
714- StringBuilder inputString = new StringBuilder () ;
714+ boolean first = true ;
715715 for (EdgeInfo edge : edges ) {
716716 if (edge .label .startsWith (name ) && (name .length () == edge .label .length () || edge .label .charAt (name .length ()) == '[' )) {
717- if (inputString . length () > 0 ) {
718- inputString .append (", " );
717+ if (! first ) {
718+ sb .append (", " );
719719 }
720- inputString .append (edge .from );
720+ first = false ;
721+ sb .append (edge .from );
721722 }
722723 }
723- result = inputString .toString ();
724724 break ;
725725 case "p" :
726726 Object prop = p .get (name );
727- String length = m .group (4 );
727+ String length = template .length ;
728+ String result ;
728729 if (prop == null ) {
729730 result = "?" ;
730731 } else if (length != null && prop instanceof LengthToString lengthProp ) {
@@ -735,36 +736,21 @@ private String createName(NodeClass nodeClass, List<EdgeInfo> edges, String temp
735736 case "m" :
736737 result = lengthProp .toString (Length .M );
737738 break ;
738- default :
739739 case "l" :
740+ default :
740741 result = lengthProp .toString (Length .L );
741742 break ;
742743 }
743744 } else {
744745 result = prop .toString ();
745746 }
747+ sb .append (result );
746748 break ;
747749 default :
748- result = "#?#" ;
750+ sb . append ( "#?#" ) ;
749751 break ;
750752 }
751-
752- // Escape '\' and '$' to not interfere with the regular expression.
753- StringBuilder newResult = new StringBuilder ();
754- for (int i = 0 ; i < result .length (); ++i ) {
755- char c = result .charAt (i );
756- if (c == '\\' ) {
757- newResult .append ("\\ \\ " );
758- } else if (c == '$' ) {
759- newResult .append ("\\ $" );
760- } else {
761- newResult .append (c );
762- }
763- }
764- result = newResult .toString ();
765- m .appendReplacement (sb , result );
766753 }
767- m .appendTail (sb );
768754 return sb .toString ();
769755 }
770756
0 commit comments