2025-03-13 12:01:03 +01:00

50 lines
1.3 KiB
Java

import java.util.Objects;
/**
* Jeton pour les variables
*
* sert uniquement à des variables entières, pas d'autre type pour l'instant.
*
*
* The general rules for naming variables are:
*
* Names can contain letters and digits
* Names must begin with a letter
* Names should start with a lowercase letter, and cannot contain whitespace
* Names are case-sensitive ("myVar" and "myvar" are different variables)
* Reserved words cannot be used as names
*
*
* @author Florent Madelaine
* @see ReservedWords.java
*/
public class TokenVariable extends AbstractToken {
private final static String regexp="[a-z][0-9a-zA-Z]*";
private String variableName; // String in original program
public TokenVariable (String s){
Objects.requireNonNull(s, "s must not be null");
for (ReservedWord k : ReservedWord.values()){
if (k.toString().equals(s)){
throw new IllegalArgumentException("Illegal variable name, attempt to use reserved keyword " + k.toString());
}
}
if (! s.matches(this.regexp)){
throw new IllegalArgumentException("Illegal variable name, name should start with a lower case letter followed potentially by digits or other letters lower or upper case.");
}
else {
this.variableName=s;
}
}
@Override
public String toString(){
return this.variableName;
}
}