Class PropertyAccess

java.lang.Object
nz.org.riskscape.rl.ast.PropertyAccess
All Implemented Interfaces:
AST, Expression

public final class PropertyAccess extends Object

Property access represents an indexing of the scope or another expression, e.g. foo, foo.bar or foo().bar are all property access expressions.

PropertyAccess can also be a 'select-all' style property, which can be used within a StructDeclaration to select all of the output members or another expression in to a struct, e.g {foo.*} or {foo().*}.

  • Constructor Details

    • PropertyAccess

      public PropertyAccess(Optional<Expression> receiver, List<Token> identifiers)

      Creates a new PropertyAccess instance.

      Parameters:
      receiver - If here, this is the thing that is being accessed, e.g. in foo(bar).baz then foo(bar) is the receiver. Without this, the property is looked up against the scope, which is usually a struct.
      identifiers -
  • Method Details

    • of

      public static PropertyAccess of(String... identifiers)
    • of

      public static PropertyAccess of(List<String> identifiers)
    • and

      public PropertyAccess and(String additionalPath)
    • appendSource

      protected void appendSource(StringBuilder appendTo)

      Implementers extend this to append their source to the given string builder. Done via string builder to encourage a more efficient implementation than is possible with concatenating multiple toString calls that typically contruct a string builder anyway.

    • getAccessString

      public String getAccessString()
    • getFirstIdentifier

      public Token getFirstIdentifier()
      Returns:
      the first token from this PropertyAccess objects list of identifiers, e.g. return foo from foo.bar.baz
    • getLastIdentifier

      public Token getLastIdentifier()
      Returns:
      the last token from this PropertyAccess objects list of identifiers, e.g. return baz from foo.bar.baz. Returns the same as getFirstIdentifier() if only one identifier exists.
    • isSingleIdentifier

      public boolean isSingleIdentifier()
      Returns:
      true if this is the most basic of property access expressions, e.g. just identifies a property, e.g foo but not foo.bar and not bar().foo
    • accept

      public <T, R> R accept(ExpressionVisitor<T,R> visitor, T data)
    • getBoundary

      public Optional<Pair<Token,Token>> getBoundary()
      Returns:
      a pair of tokens that represent the start and end of this expression, to be used when giving users information about where errors have occurred. If the expression has been rebuilt then the boundary may be empty or it may be from the original AST. Intended for use in problems to trace errors back to their source.
    • isTrailingSelectAll

      public boolean isTrailingSelectAll()
      Returns:
      true if this expression has a trailing select-all, e.g. foo.bar.*
    • isReceiverSelectAll

      public boolean isReceiverSelectAll()
      Returns:
      true if the expression is in the form EXPRESSION INDEX ASTERISK, e.g foo().*`
    • isLiteral

      public boolean isLiteral()
      Description copied from interface: Expression

      Tests whether this expression is a literal expression - a literal expression is a constant expression that is unaffected by the scope in which it was realized/evaluated and contains no function-defined behaviour. Therefore a literal expression is one that contains no property access (unless the receiver is a literal) and no function calls.

      A literal expression will always realize to being a constant (e.g. RealizedExpression.isConstant() will return true), however there are constant expressions that are not literal.

      One edge case to be aware of is a limitation of the AST in determining literal expressions in contrived cases like this one - {foo: 'bar', baz: qux}.foo. The struct is not literal, but the property being accessed is. But because the AST is relatively 'dumb', the expression itself is not literal. It can, however, be realized as a constant - realization is 'smarter' and can pick out the that the property being accessed is constant.

    • equals

      public boolean equals(Object o)
    • canEqual

      protected boolean canEqual(Object other)
    • hashCode

      public int hashCode()
    • getReceiver

      public Optional<Expression> getReceiver()

      If here, this is the thing that is being accessed, e.g. in foo(bar).baz then foo(bar) is the receiver. Without this, the property is looked up against the scope, which is usually a struct.

    • getIdentifiers

      public List<Token> getIdentifiers()
    • appendSource

      protected static void appendSource(AST ast, StringBuilder appendTo)

      Helper method for appending the source of another ast element to a string builder, efficiently if possible.

    • toString

      public final String toString()

      Returns a simplified view of the ast in a source-like fashion, but with a lot of details left out for conciseness

      Overrides:
      toString in class Object
    • appendString

      protected void appendString(StringBuilder appendTo)

      Append a simplified string representation to the builder. Default implementation uses appendSource. Subclasses can override to provide a more concise representation.

    • toSource

      public final String toSource()
      Specified by:
      toSource in interface AST
      Returns:
      a normalized version of the AST that can be parsed to be exactly the same AST as this