Skip to content

Commit

Permalink
Refactor shortcut properties to allow annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw429s committed Apr 26, 2024
1 parent 05af39f commit 62874af
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package ortus.boxlang.compiler.ast.statement;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -29,6 +30,7 @@
public class BoxProperty extends BoxNode {

private List<BoxAnnotation> annotations;
private List<BoxAnnotation> postAnnotations;
private List<BoxDocumentationAnnotation> documentation;

/**
Expand All @@ -42,16 +44,35 @@ public class BoxProperty extends BoxNode {
* @see Position
* @see BoxStatement
*/
public BoxProperty( List<BoxAnnotation> annotations, List<BoxDocumentationAnnotation> documentation, Position position, String sourceText ) {
public BoxProperty( List<BoxAnnotation> annotations, List<BoxAnnotation> postAnnotations, List<BoxDocumentationAnnotation> documentation, Position position,
String sourceText ) {
super( position, sourceText );
setAnnotations( annotations );
setPostAnnotations( postAnnotations );
setDocumentation( documentation );
}

public List<BoxAnnotation> getAnnotations() {
return annotations;
}

public List<BoxAnnotation> getPostAnnotations() {
return postAnnotations;
}

public List<BoxAnnotation> getAllAnnotations() {
List<BoxAnnotation> allAnnotations = new ArrayList<>();
allAnnotations.addAll( annotations );
allAnnotations.addAll( postAnnotations );
return allAnnotations;
}

public void setPostAnnotations( List<BoxAnnotation> postAnnotations ) {
replaceChildren( this.postAnnotations, postAnnotations );
this.postAnnotations = postAnnotations;
this.postAnnotations.forEach( arg -> arg.setParent( this ) );
}

public List<BoxDocumentationAnnotation> getDocumentation() {
return documentation;
}
Expand All @@ -72,7 +93,7 @@ public void setDocumentation( List<BoxDocumentationAnnotation> documentation ) {
public Map<String, Object> toMap() {
Map<String, Object> map = super.toMap();

map.put( "annotations", annotations.stream().map( BoxAnnotation::toMap ).collect( java.util.stream.Collectors.toList() ) );
map.put( "annotations", getAllAnnotations().stream().map( BoxAnnotation::toMap ).collect( java.util.stream.Collectors.toList() ) );
map.put( "documentation", documentation.stream().map( BoxDocumentationAnnotation::toMap ).collect( java.util.stream.Collectors.toList() ) );
return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import ortus.boxlang.compiler.ast.BoxClass;
import ortus.boxlang.compiler.ast.BoxDocumentation;
import ortus.boxlang.compiler.ast.BoxExpression;
import ortus.boxlang.compiler.ast.BoxInterface;
import ortus.boxlang.compiler.ast.BoxScript;
import ortus.boxlang.compiler.ast.BoxTemplate;
import ortus.boxlang.compiler.ast.expression.BoxArgument;
Expand Down Expand Up @@ -233,6 +234,44 @@ public void visit( BoxClass node ) {
print( "}" );
}

public void visit( BoxInterface node ) {
for ( var importNode : node.getImports() ) {
importNode.accept( this );
newLine();
}
// TODO: Replace this with actual comment AST Nodes
if ( !node.getDocumentation().isEmpty() ) {
println( "/**" );
for ( var doc : node.getDocumentation() ) {
print( " * " );
doc.accept( this );
newLine();
}
println( "*/" );
}
for ( var anno : node.getAnnotations() ) {
anno.accept( this );
newLine();
}
increaseIndent();
print( "interface" );
// enter template mode so our inline annotatins are in the form name="value"
currentSourceType.push( BoxSourceType.BOXTEMPLATE );
for ( var anno : node.getPostAnnotations() ) {
anno.accept( this );
newLine();
}
currentSourceType.pop();
print( " {" );
newLine();
for ( var statement : node.getBody() ) {
statement.accept( this );
newLine();
}
decreaseIndent();
print( "}" );
}

public void visit( BoxDocumentation node ) {
// TODO: Not sure this node is in use yet
println( "documentation??" );
Expand Down Expand Up @@ -788,6 +827,7 @@ public void visit( BoxForIndex node ) {
}

public void visit( BoxFunctionDeclaration node ) {
Boolean defaultInterfaceMethod = node.getFirstNodeOfType( BoxInterface.class ) != null;
newLine();
if ( isTemplate() ) {
print( "<bx:function" );
Expand All @@ -813,6 +853,9 @@ public void visit( BoxFunctionDeclaration node ) {
decreaseIndent();
print( "</bx:function>" );
} else {
if ( defaultInterfaceMethod ) {
print( "default " );
}
if ( node.getAccessModifier() != null ) {
print( node.getAccessModifier().toString().toLowerCase() );
print( " " );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,14 @@ public BoxNode visit( BoxProperty node ) {
node.getAnnotations().set( i, ( BoxAnnotation ) newAnnotation );
}
}
for ( int i = 0; i < node.getPostAnnotations().size(); i++ ) {
BoxAnnotation annotation = node.getPostAnnotations().get( i );
BoxNode newAnnotation = annotation.accept( this );
if ( newAnnotation != annotation ) {
node.replaceChildren( newAnnotation, annotation );
node.getPostAnnotations().set( i, ( BoxAnnotation ) newAnnotation );
}
}
for ( int i = 0; i < node.getDocumentation().size(); i++ ) {
BoxDocumentationAnnotation documentation = node.getDocumentation().get( i );
BoxNode newDocumentation = documentation.accept( this );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,16 @@ private List<Expression> transformProperties( List<BoxProperty> properties ) {
List<Expression> getterLookup = new ArrayList<Expression>();
List<Expression> setterLookup = new ArrayList<Expression>();
properties.forEach( prop -> {
Expression documentationStruct = transformDocumentation( prop.getDocumentation() );
Expression documentationStruct = transformDocumentation( prop.getDocumentation() );
/*
* normalize annotations to allow for
* property String userName;
*/
List<BoxAnnotation> finalAnnotations = new ArrayList<BoxAnnotation>();
var annotations = prop.getAnnotations();
List<BoxAnnotation> finalAnnotations = new ArrayList<BoxAnnotation>();
// Start wiith all inline annotatinos
var annotations = prop.getPostAnnotations();
// Add in any pre annotations that have a value, which allows type, name, or default to be set before
annotations.addAll( prop.getAnnotations().stream().filter( it -> it.getValue() != null ).toList() );
int namePosition = annotations.stream().map( BoxAnnotation::getKey ).map( BoxFQN::getValue ).map( String::toLowerCase )
.collect( java.util.stream.Collectors.toList() ).indexOf( "name" );
int typePosition = annotations.stream().map( BoxAnnotation::getKey ).map( BoxFQN::getValue ).map( String::toLowerCase )
Expand Down Expand Up @@ -517,6 +520,8 @@ private List<Expression> transformProperties( List<BoxProperty> properties ) {
}
// add remaining annotations
finalAnnotations.addAll( annotations );
// Now that name, type, and default are finalized, add in any remaining non-valued keys
finalAnnotations.addAll( prop.getAnnotations().stream().filter( it -> it.getValue() == null ).toList() );

Expression annotationStruct = transformAnnotations( finalAnnotations );
/* Process default value */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2205,13 +2205,14 @@ private BoxAnnotation toAst( File file, BoxScriptGrammar.PostannotationContext n
*/
private BoxProperty toAst( File file, BoxScriptGrammar.PropertyContext node ) {
List<BoxAnnotation> annotations = new ArrayList<>();
List<BoxAnnotation> postAnnotations = new ArrayList<>();
List<BoxDocumentationAnnotation> documentation = new ArrayList<>();

for ( BoxScriptGrammar.PreannotationContext annotation : node.preannotation() ) {
annotations.add( toAst( file, annotation ) );
}
for ( BoxScriptGrammar.PostannotationContext annotation : node.postannotation() ) {
annotations.add( toAst( file, annotation ) );
postAnnotations.add( toAst( file, annotation ) );
}

BoxDocumentation doc = getDocIndex( node );
Expand All @@ -2221,7 +2222,7 @@ private BoxProperty toAst( File file, BoxScriptGrammar.PropertyContext node ) {
}
}

return new BoxProperty( annotations, documentation, getPosition( node ), getSourceText( node ) );
return new BoxProperty( annotations, postAnnotations, documentation, getPosition( node ), getSourceText( node ) );
}

public BoxExpression parseBoxExpression( String code, Position position ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2149,7 +2149,7 @@ private BoxProperty toAst( File file, CFScriptGrammar.PropertyContext node ) {
}
}

return new BoxProperty( annotations, documentation, getPosition( node ), getSourceText( node ) );
return new BoxProperty( new ArrayList<BoxAnnotation>(), annotations, documentation, getPosition( node ), getSourceText( node ) );
}

public BoxExpression parseCFExpression( String code, Position position ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ private BoxProperty toAst( File file, PropertyContext node ) {
annotations.add( toAst( file, attr ) );
}

return new BoxProperty( annotations, documentation, getPosition( node ), getSourceText( node ) );
return new BoxProperty( new ArrayList<BoxAnnotation>(), annotations, documentation, getPosition( node ), getSourceText( node ) );
}

private List<BoxImport> toAst( File file, List<BoxImportContext> imports ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ public Loop() {

/*
* step
*
* item
* array
* characters *
* characters
* times
*/
};
Expand Down
12 changes: 11 additions & 1 deletion src/test/java/TestCases/phase3/ClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public void testProperties() {
var boxMeta = ( ClassMeta ) cfc.getBoxMeta();
var meta = boxMeta.meta;

assertThat( meta.getAsArray( Key.of( "properties" ) ).size() ).isEqualTo( 2 );
assertThat( meta.getAsArray( Key.of( "properties" ) ).size() ).isEqualTo( 3 );

var prop1 = ( IStruct ) meta.getAsArray( Key.of( "properties" ) ).get( 0 );
assertThat( prop1.get( "name" ) ).isEqualTo( "myProperty" );
Expand Down Expand Up @@ -512,6 +512,16 @@ public void testProperties() {
assertThat( preAnno.get( 0 ) ).isEqualTo( "myValue" );
assertThat( preAnno.get( 1 ) ).isEqualTo( "anothervalue" );

var prop3 = ( IStruct ) meta.getAsArray( Key.of( "properties" ) ).get( 2 );
assertThat( prop3.get( "name" ) ).isEqualTo( "theName" );
assertThat( prop3.get( "defaultValue" ) ).isEqualTo( null );
assertThat( prop3.get( "type" ) ).isEqualTo( "any" );

var prop3Annotations = prop3.getAsStruct( Key.of( "annotations" ) );
assertThat( prop3Annotations.size() ).isEqualTo( 4 );
assertThat( prop3Annotations.containsKey( Key.of( "ID" ) ) ).isTrue();
assertThat( prop3Annotations.get( Key.of( "ID" ) ) ).isEqualTo( "" );

var prop2Docs = prop2.getAsStruct( Key.of( "documentation" ) );
assertThat( prop2Docs.size() ).isEqualTo( 3 );
assertThat( prop2Docs.getAsString( Key.of( "brad" ) ).trim() ).isEqualTo( "wood" );
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/TestCases/phase3/PropertyTest.bx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class accessors=true {
@preanno "myValue" "anothervalue"
property string anotherprop;

@ID
property theName;

function init() {
getMyProperty();
}
Expand Down

0 comments on commit 62874af

Please sign in to comment.