@ -1,10 +1,9 @@
/ *
/ *
* Glsl . java
* Glsl . java
*
*
* Created on 24 . 09 . 2007 , 00 : 46 : 53
* Created on 24 . 09 . 2007 , 00 : 46 : 53
*
*
* /
* /
package net.java.nboglpack.glsleditor.glsl ;
package net.java.nboglpack.glsleditor.glsl ;
import java.util.HashMap ;
import java.util.HashMap ;
@ -27,79 +26,86 @@ import org.netbeans.api.lexer.TokenUtilities;
* @author Michael Bien
* @author Michael Bien
* /
* /
public final class Glsl {
public final class Glsl {
private final static String KEYWORD_FONT_COLOR = "<font color=808080>" ;
private final static String KEYWORD_FONT_COLOR = "<font color=808080>" ;
public final static Map < String , GLSLElementDescriptor > declaredFunctions = new HashMap < String , GLSLElementDescriptor > ( ) ;
public final static Map < String , GLSLElementDescriptor > declaredFunctions = new HashMap < String , GLSLElementDescriptor > ( ) ;
private Glsl ( ) {
private Glsl ( ) { }
}
/ * *
/ * *
* Assembles a human readable String containing the declaraton of a function .
* Assembles a human readable String containing the declaraton of a function .
* Asumes that the current token of the SyntaxContext represents the function name .
* Asumes that the current token of the SyntaxContext represents the function name .
* /
* /
public static final String createFunctionDeclarationString ( SyntaxContext context ) {
public static final String createFunctionDeclarationString ( SyntaxContext context ) {
AbstractDocument document = ( AbstractDocument ) context . getDocument ( ) ;
AbstractDocument document = ( AbstractDocument ) context . getDocument ( ) ;
StringBuilder sb = new StringBuilder ( ) ;
StringBuilder sb = new StringBuilder ( ) ;
try {
try {
document . readLock ( ) ;
document . readLock ( ) ;
TokenSequence sequence = TokenHierarchy . get ( context . getDocument ( ) ) . tokenSequence ( ) ;
TokenSequence sequence = TokenHierarchy . get ( context . getDocument ( ) ) . tokenSequence ( ) ;
sequence . move ( context . getOffset ( ) ) ;
sequence . move ( context . getOffset ( ) ) ;
sequence . moveNext ( ) ;
sequence . moveNext ( ) ;
sb . append ( "<html>" ) ;
sb . append ( "<html>" ) ;
int moved = 0 ;
int moved = 0 ;
while ( sequence . movePrevious ( ) & & isIgnoredToken ( sequence . token ( ) ) )
while ( sequence . movePrevious ( ) & & isIgnoredToken ( sequence . token ( ) ) ) {
moved + + ;
moved + + ;
}
String type = sequence . token ( ) . toString ( ) ;
String type = sequence . token ( ) . toString ( ) ;
while ( moved - - > = 0 )
while ( moved - - > = 0 ) {
sequence . moveNext ( ) ;
sequence . moveNext ( ) ;
}
// append function name
// append function name
sb . append ( sequence . token ( ) . text ( ) ) ;
sb . append ( sequence . token ( ) . text ( ) ) ;
while ( ! TokenUtilities . equals ( sequence . token ( ) . text ( ) , "(" ) )
while ( ! TokenUtilities . equals ( sequence . token ( ) . text ( ) , "(" ) ) {
sequence . moveNext ( ) ;
sequence . moveNext ( ) ;
}
sb . append ( "(" ) ;
sb . append ( "(" ) ;
Token token ;
Token token ;
boolean first = true ;
boolean first = true ;
while ( sequence . moveNext ( ) & & ! TokenUtilities . equals ( sequence . token ( ) . text ( ) , ")" ) ) {
while ( sequence . moveNext ( ) & & ! TokenUtilities . equals ( sequence . token ( ) . text ( ) , ")" ) ) {
token = sequence . token ( ) ;
token = sequence . token ( ) ;
if ( ! isIgnoredToken ( token ) ) {
if ( ! isIgnoredToken ( token ) ) {
if ( first ) {
if ( first ) {
sb . append ( KEYWORD_FONT_COLOR ) ;
sb . append ( KEYWORD_FONT_COLOR ) ;
} else if ( token . id ( ) ! = GlslTokenId . COMMA & & token . id ( ) ! = GlslTokenId . BRACKET & & token . id ( ) ! = GlslTokenId . INTEGER_LITERAL ) {
} else if ( token . id ( ) ! = GlslTokenId . COMMA & & token . id ( ) ! = GlslTokenId . BRACKET & & token . id ( ) ! = GlslTokenId . INTEGER_LITERAL ) {
sb . append ( " " ) ;
sb . append ( " " ) ;
}
}
if ( ! TokenUtilities . equals ( token . text ( ) , "void" ) ) {
if ( ! TokenUtilities . equals ( token . text ( ) , "void" ) ) {
moved = 0 ;
moved = 0 ;
while ( sequence . moveNext ( ) & & isIgnoredToken ( sequence . token ( ) ) )
while ( sequence . moveNext ( ) & & isIgnoredToken ( sequence . token ( ) ) ) {
moved + + ;
moved + + ;
}
if ( sequence . token ( ) . id ( ) = = GlslTokenId . COMMA | | TokenUtilities . equals ( sequence . token ( ) . text ( ) , ")" ) )
if ( sequence . token ( ) . id ( ) = = GlslTokenId . COMMA | | TokenUtilities . equals ( sequence . token ( ) . text ( ) , ")" ) ) {
sb . append ( "</font>" ) ;
sb . append ( "</font>" ) ;
}
while ( moved - - > = 0 )
while ( moved - - > = 0 ) {
sequence . movePrevious ( ) ;
sequence . movePrevious ( ) ;
}
sb . append ( token . text ( ) ) ;
sb . append ( token . text ( ) ) ;
if ( token . id ( ) = = GlslTokenId . COMMA )
if ( token . id ( ) = = GlslTokenId . COMMA ) {
sb . append ( KEYWORD_FONT_COLOR ) ;
sb . append ( KEYWORD_FONT_COLOR ) ;
}
}
}
first = false ;
first = false ;
@ -109,39 +115,39 @@ public final class Glsl {
sb . append ( "</font>)" ) ;
sb . append ( "</font>)" ) ;
if ( ! "void" . equals ( type ) ) {
if ( ! "void" . equals ( type ) ) {
sb . append ( " : " ) ;
sb . append ( " : " ) ;
sb . append ( KEYWORD_FONT_COLOR ) ;
sb . append ( KEYWORD_FONT_COLOR ) ;
sb . append ( type ) ;
sb . append ( type ) ;
sb . append ( "</font>" ) ;
sb . append ( "</font>" ) ;
}
}
sb . append ( "</html>" ) ;
sb . append ( "</html>" ) ;
} finally {
} finally {
document . readUnlock ( ) ;
document . readUnlock ( ) ;
}
}
return sb . toString ( ) ;
return sb . toString ( ) ;
}
}
/ * *
/ * *
* Assambles a human readable String containing the declaraton of a field and the field name itself .
* Assambles a human readable String containing the declaraton of a field and the field name itself .
* Asumes that the current token of the SyntaxContext represents the field name .
* Asumes that the current token of the SyntaxContext represents the field name .
* /
* /
public static final String createFieldDeclarationString ( SyntaxContext context ) {
public static final String createFieldDeclarationString ( SyntaxContext context ) {
AbstractDocument document = ( AbstractDocument ) context . getDocument ( ) ;
AbstractDocument document = ( AbstractDocument ) context . getDocument ( ) ;
StringBuilder sb = new StringBuilder ( ) ;
StringBuilder sb = new StringBuilder ( ) ;
try {
try {
document . readLock ( ) ;
document . readLock ( ) ;
TokenSequence sequence = TokenHierarchy . get ( context . getDocument ( ) ) . tokenSequence ( ) ;
TokenSequence sequence = TokenHierarchy . get ( context . getDocument ( ) ) . tokenSequence ( ) ;
sequence . move ( context . getOffset ( ) ) ;
sequence . move ( context . getOffset ( ) ) ;
sequence . moveNext ( ) ;
sequence . moveNext ( ) ;
sb . append ( "<html>" ) ;
sb . append ( "<html>" ) ;
sb . append ( sequence . token ( ) . text ( ) ) ;
sb . append ( sequence . token ( ) . text ( ) ) ;
sb . append ( KEYWORD_FONT_COLOR ) ;
sb . append ( KEYWORD_FONT_COLOR ) ;
@ -152,53 +158,55 @@ public final class Glsl {
// read forward
// read forward
int moved = 0 ;
int moved = 0 ;
Token token ;
Token token ;
while ( sequence . moveNext ( )
while ( sequence . moveNext ( )
& & sequence . token ( ) . id ( ) ! = GlslTokenId . SEMICOLON
& & sequence . token ( ) . id ( ) ! = GlslTokenId . SEMICOLON
& & sequence . token ( ) . id ( ) ! = GlslTokenId . COMMA
& & sequence . token ( ) . id ( ) ! = GlslTokenId . COMMA
& & sequence . token ( ) . id ( ) ! = GlslTokenId . EQ ) {
& & sequence . token ( ) . id ( ) ! = GlslTokenId . EQ ) {
token = sequence . token ( ) ;
token = sequence . token ( ) ;
if ( ! isIgnoredToken ( token ) )
if ( ! isIgnoredToken ( token ) ) {
sb . append ( token ) ;
sb . append ( token ) ;
}
moved + + ;
moved + + ;
}
}
while ( moved - - > = 0 )
while ( moved - - > = 0 ) {
sequence . movePrevious ( ) ;
sequence . movePrevious ( ) ;
}
// read backwards throw the declaration
// read backwards throw the declaration
boolean skipToken = false ;
boolean skipToken = false ;
while ( sequence . movePrevious ( )
while ( sequence . movePrevious ( )
& & sequence . token ( ) . id ( ) ! = GlslTokenId . SEMICOLON
& & sequence . token ( ) . id ( ) ! = GlslTokenId . SEMICOLON
& & sequence . token ( ) . id ( ) ! = GlslTokenId . END_OF_LINE ) {
& & sequence . token ( ) . id ( ) ! = GlslTokenId . END_OF_LINE ) {
token = sequence . token ( ) ;
token = sequence . token ( ) ;
if ( ! isIgnoredToken ( token ) ) {
if ( ! isIgnoredToken ( token ) ) {
// we have a struct declaration; skip everything between { }
// we have a struct declaration; skip everything between { }
if ( token . id ( ) = = GlslTokenId . BRACE & & TokenUtilities . equals ( token . text ( ) , "}" ) ) {
if ( token . id ( ) = = GlslTokenId . BRACE & & TokenUtilities . equals ( token . text ( ) , "}" ) ) {
movePreviousUntil ( sequence , GlslTokenId . BRACE , "}" , "{" ) ;
movePreviousUntil ( sequence , GlslTokenId . BRACE , "}" , "{" ) ;
continue ;
continue ;
}
}
// skip token in case of an comma seperated identifier list
// skip token in case of an comma seperated identifier list
if ( skipToken ) {
if ( skipToken ) {
if ( token . id ( ) = = GlslTokenId . BRACKET
if ( token . id ( ) = = GlslTokenId . BRACKET
& & TokenUtilities . equals ( token . text ( ) , "]" ) ) {
& & TokenUtilities . equals ( token . text ( ) , "]" ) ) {
movePreviousUntil ( sequence , GlslTokenId . BRACKET , "]" , "[" ) ;
movePreviousUntil ( sequence , GlslTokenId . BRACKET , "]" , "[" ) ;
skipToken = false ;
skipToken = false ;
} else {
} else {
skipToken = false ;
skipToken = false ;
}
}
continue ;
continue ;
}
}
if ( token . id ( ) = = GlslTokenId . COMMA ) {
if ( token . id ( ) = = GlslTokenId . COMMA ) {
skipToken = true ;
skipToken = true ;
continue ;
continue ;
}
}
if ( ! TokenUtilities . equals ( token . text ( ) , "struct" ) ) {
if ( ! TokenUtilities . equals ( token . text ( ) , "struct" ) ) {
sb . insert ( insertIndex , token . text ( ) ) ;
sb . insert ( insertIndex , token . text ( ) ) ;
sb . insert ( insertIndex , " " ) ;
sb . insert ( insertIndex , " " ) ;
}
}
@ -209,50 +217,54 @@ public final class Glsl {
sb . append ( "</font></html>" ) ;
sb . append ( "</font></html>" ) ;
} finally {
} finally {
document . readUnlock ( ) ;
document . readUnlock ( ) ;
}
}
return sb . toString ( ) ;
return sb . toString ( ) ;
}
}
public static final String createPreprocessorString ( SyntaxContext context ) {
public static final String createPreprocessorString ( SyntaxContext context ) {
ASTNode node = ( ASTNode ) context . getASTPath ( ) . getLeaf ( ) ;
ASTNode node = ( ASTNode ) context . getASTPath ( ) . getLeaf ( ) ;
List < ASTItem > children = node . getChildren ( ) ;
List < ASTItem > children = node . getChildren ( ) ;
String str = null ;
String str = null ;
for ( ASTItem item : children )
for ( ASTItem item : children ) {
if ( isTokenType ( item , GlslTokenId . PREPROCESSOR . name ( ) ) )
if ( isTokenType ( item , GlslTokenId . PREPROCESSOR . name ( ) ) ) {
str = ( ( ASTToken ) item ) . getIdentifier ( ) ;
str = ( ( ASTToken ) item ) . getIdentifier ( ) ;
}
}
for ( int i = 0 ; i < str . length ( ) ; i + + ) {
for ( int i = 0 ; i < str . length ( ) ; i + + ) {
char c = str . charAt ( i ) ;
char c = str . charAt ( i ) ;
if ( c ! = '#' & & ! Character . isWhitespace ( c ) )
if ( c ! = '#' & & ! Character . isWhitespace ( c ) ) {
for ( int j = str . length ( ) - 1 ; j > i ; j - - )
for ( int j = str . length ( ) - 1 ; j > i ; j - - ) {
if ( ! Character . isWhitespace ( str . charAt ( j ) ) )
if ( ! Character . isWhitespace ( str . charAt ( j ) ) ) {
return str . substring ( i , j + 1 ) ;
return str . substring ( i , j + 1 ) ;
}
}
}
}
}
return str ;
return str ;
}
}
/ * *
/ * *
* c alled from withen GLSL_ * . nbs each time the document has been modified .
* C alled from withen GLSL_ * . nbs each time the document has been modified .
* /
* /
public static void process ( SyntaxContext context ) {
public static void process ( SyntaxContext context ) {
AbstractDocument document = ( AbstractDocument ) context . getDocument ( ) ;
AbstractDocument document = ( AbstractDocument ) context . getDocument ( ) ;
try {
try {
document . readLock ( ) ;
document . readLock ( ) ;
// remember all declared funktions for auto completion
// remember all declared functions for auto- completion
synchronized ( declaredFunctions ) {
synchronized ( declaredFunctions ) {
declaredFunctions . clear ( ) ;
declaredFunctions . clear ( ) ;
}
}
@ -262,41 +274,43 @@ public final class Glsl {
for ( ASTItem declarationItem : declaration . getChildren ( ) ) {
for ( ASTItem declarationItem : declaration . getChildren ( ) ) {
if ( isNode ( declarationItem , "function" ) ) {
if ( isNode ( declarationItem , "function" ) ) {
List < ASTItem > functionItems = declarationItem . getChildren ( ) ;
List < ASTItem > functionItems = declarationItem . getChildren ( ) ;
if ( functionItems . size ( ) < 3 )
if ( functionItems . size ( ) < 3 ) {
break ;
break ;
}
ASTItem nameToken = functionItems . get ( 0 ) ;
ASTItem nameToken = functionItems . get ( 0 ) ;
if ( isTokenType ( nameToken , GlslTokenId . FUNCTION . name ( ) ) ) {
if ( isTokenType ( nameToken , GlslTokenId . FUNCTION . name ( ) ) ) {
// determine return type
// determine return type
StringBuilder returnType = new StringBuilder ( ) ;
StringBuilder returnType = new StringBuilder ( ) ;
for ( ASTItem typeItem : declaration . getChildren ( ) ) {
for ( ASTItem typeItem : declaration . getChildren ( ) ) {
if ( isNode ( typeItem , "function" ) )
if ( isNode ( typeItem , "function" ) ) {
break ;
break ;
}
if ( typeItem instanceof ASTNode ) {
if ( typeItem instanceof ASTNode ) {
returnType . append ( ( ( ASTNode ) typeItem ) . getAsText ( ) . trim ( ) ) ;
returnType . append ( ( ( ASTNode ) typeItem ) . getAsText ( ) . trim ( ) ) ;
} else if ( typeItem instanceof ASTToken ) {
} else if ( typeItem instanceof ASTToken ) {
final ASTToken t = ( ASTToken ) typeItem ;
final ASTToken t = ( ASTToken ) typeItem ;
returnType . append ( t . getIdentifier ( ) . trim ( ) ) ;
returnType . append ( t . getIdentifier ( ) . trim ( ) ) ;
}
}
}
}
// determine name and parameter list
// determine name and parameter list
StringBuilder name = new StringBuilder ( ) ;
StringBuilder name = new StringBuilder ( ) ;
name . append ( "(" ) ;
name . append ( "(" ) ;
ASTItem parameterList = functionItems . get ( 2 ) ;
ASTItem parameterList = functionItems . get ( 2 ) ;
if ( isNode ( parameterList , "parameter_declaration_list" ) )
if ( isNode ( parameterList , "parameter_declaration_list" ) ) {
name . append ( ( ( ASTNode ) parameterList ) . getAsText ( ) ) ;
name . append ( ( ( ASTNode ) parameterList ) . getAsText ( ) ) ;
}
name . append ( ")" ) ;
name . append ( ")" ) ;
GLSLElementDescriptor elementDesc = new GLSLElementDescriptor (
GLSLElementDescriptor elementDesc = new GLSLElementDescriptor (
@ -307,55 +321,53 @@ public final class Glsl {
returnType . toString ( ) ) ;
returnType . toString ( ) ) ;
name . insert ( 0 , ( ( ASTToken ) nameToken ) . getIdentifier ( ) ) ;
name . insert ( 0 , ( ( ASTToken ) nameToken ) . getIdentifier ( ) ) ;
synchronized ( declaredFunctions ) {
synchronized ( declaredFunctions ) {
declaredFunctions . put ( name . toString ( ) , elementDesc ) ;
declaredFunctions . put ( name . toString ( ) , elementDesc ) ;
}
}
// System.out.println("|"+returnType.toString()+"|"+name.toString()+"|");
// System.out.println("|"+returnType.toString()+"|"+name.toString()+"|");
}
}
break ;
break ;
}
}
}
}
}
}
} finally {
} finally {
document . readUnlock ( ) ;
document . readUnlock ( ) ;
}
}
}
}
private static final void movePreviousUntil ( TokenSequence sequence , GlslTokenId id , String countToken , String stopToken ) {
private static void movePreviousUntil ( TokenSequence sequence , GlslTokenId id , String countToken , String stopToken ) {
int counter = 1 ;
int counter = 1 ;
while ( sequence . movePrevious ( ) & & counter > 0 ) {
while ( sequence . movePrevious ( ) & & counter > 0 ) {
if ( sequence . token ( ) . id ( ) = = id ) {
if ( sequence . token ( ) . id ( ) = = id ) {
if ( TokenUtilities . equals ( sequence . token ( ) . text ( ) , stopToken ) ) {
if ( TokenUtilities . equals ( sequence . token ( ) . text ( ) , stopToken ) ) {
counter - - ;
counter - - ;
} else if ( TokenUtilities . equals ( sequence . token ( ) . text ( ) , countToken ) ) {
} else if ( TokenUtilities . equals ( sequence . token ( ) . text ( ) , countToken ) ) {
counter + + ;
counter + + ;
}
}
}
}
}
}
}
}
private static final boolean isIgnoredToken ( Token token ) {
private static boolean isIgnoredToken ( Token token ) {
return token . id ( ) = = GlslTokenId . WHITESPACE
return token . id ( ) = = GlslTokenId . WHITESPACE
| | token . id ( ) = = GlslTokenId . COMMENT
| | token . id ( ) = = GlslTokenId . COMMENT
| | token . id ( ) = = GlslTokenId . PREPROCESSOR ;
| | token . id ( ) = = GlslTokenId . PREPROCESSOR ;
}
}
private static final boolean isNode ( ASTItem item , String nodeToken ) {
private static boolean isNode ( ASTItem item , String nodeToken ) {
return item ! = null & & item instanceof ASTNode & & ( ( ASTNode ) item ) . getNT ( ) . equals ( nodeToken ) ;
return item ! = null & & item instanceof ASTNode & & ( ( ASTNode ) item ) . getNT ( ) . equals ( nodeToken ) ;
}
}
private static final boolean isToken ( ASTItem item , String id ) {
private static boolean isToken ( ASTItem item , String id ) {
return item ! = null & & item instanceof ASTToken & & ( ( ASTToken ) item ) . getIdentifier ( ) . equals ( id ) ;
return item ! = null & & item instanceof ASTToken & & ( ( ASTToken ) item ) . getIdentifier ( ) . equals ( id ) ;
}
}
private static final boolean isTokenType ( ASTItem item , String type ) {
private static boolean isTokenType ( ASTItem item , String type ) {
return item ! = null & & item instanceof ASTToken & & ( ( ASTToken ) item ) . getTypeName ( ) . equals ( type ) ;
return item ! = null & & item instanceof ASTToken & & ( ( ASTToken ) item ) . getTypeName ( ) . equals ( type ) ;
}
}
}
}