View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jexl3.parser;
18  
19  /**
20   * Identifiers, variables, ie symbols.
21   */
22  public class ASTIdentifier extends JexlNode {
23      /**
24       */
25      private static final long serialVersionUID = 1L;
26      /** The redefined variable flag. */
27      private static final int REDEFINED = 0;
28      /** The shaded variable flag. */
29      private static final int SHADED = 1;
30      /** The captured variable flag. */
31      private static final int CAPTURED = 2;
32      /** The lexical variable flag. */
33      private static final int LEXICAL = 3;
34      /** The const variable flag. */
35      private static final int CONST = 4;
36      /**
37       * Checks the value of a flag in the mask.
38       * @param ordinal the flag ordinal
39       * @param mask the flags mask
40       * @return the mask value with this flag or-ed in
41       */
42      private static boolean isSet(final int ordinal, final int mask) {
43          return (mask & 1 << ordinal) != 0;
44      }
45      /**
46       * Sets the value of a flag in a mask.
47       * @param ordinal the flag ordinal
48       * @param mask the flags mask
49       * @param value true or false
50       * @return the new flags mask value
51       */
52      private static int set(final int ordinal, final int mask, final boolean value) {
53          return value? mask | 1 << ordinal : mask & ~(1 << ordinal);
54      }
55      protected String name;
56  
57      protected int symbol = -1;
58  
59      protected int flags;
60  
61      ASTIdentifier(final int id) {
62          super(id);
63      }
64  
65      public String getName() {
66          return name;
67      }
68  
69      public String getNamespace() {
70          return null;
71      }
72  
73      public int getSymbol() {
74          return symbol;
75      }
76  
77      public boolean isCaptured() {
78          return isSet(CAPTURED, flags);
79      }
80  
81      @Override
82      public boolean isConstant() {
83          return isSet(CONST, flags);
84      }
85  
86      public boolean isLexical() {
87          return isSet(LEXICAL, flags);
88      }
89  
90      public boolean isRedefined() {
91          return isSet(REDEFINED, flags);
92      }
93  
94      public boolean isShaded() {
95          return isSet(SHADED, flags);
96      }
97  
98      @Override
99      public Object jjtAccept(final ParserVisitor visitor, final Object data) {
100         return visitor.visit(this, data);
101     }
102 
103     public void setCaptured(final boolean f) {
104         flags = set(CAPTURED, flags, f);
105     }
106 
107     public void setConstant(final boolean f) {
108         flags = set(CONST, flags, f);
109     }
110 
111     public void setLexical(final boolean f) {
112         flags = set(LEXICAL, flags, f);
113     }
114 
115     public void setRedefined(final boolean f) {
116         flags = set(REDEFINED, flags, f);
117     }
118 
119     public void setShaded(final boolean f) {
120         flags = set(SHADED, flags, f);
121     }
122 
123     void setSymbol(final int r, final String identifier) {
124         symbol = r;
125         name = identifier;
126     }
127 
128     void setSymbol(final String identifier) {
129         if (identifier.charAt(0) == '#') {
130             symbol = Integer.parseInt(identifier.substring(1));
131         }
132         name = identifier;
133     }
134 
135     @Override
136     public String toString() {
137         return name;
138     }
139 }