@ -4,7 +4,6 @@
* Created on 24 . 09 . 2007 , 00 : 46 : 53
*
* /
package net.java.nboglpack.glsleditor.glsl ;
import java.util.HashMap ;
@ -29,10 +28,10 @@ import org.netbeans.api.lexer.TokenUtilities;
public final class Glsl {
private final static String KEYWORD_FONT_COLOR = "<font color=808080>" ;
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 .
@ -55,18 +54,21 @@ public final class Glsl {
sb . append ( "<html>" ) ;
int moved = 0 ;
while ( sequence . movePrevious ( ) & & isIgnoredToken ( sequence . token ( ) ) )
while ( sequence . movePrevious ( ) & & isIgnoredToken ( sequence . token ( ) ) ) {
moved + + ;
}
String type = sequence . token ( ) . toString ( ) ;
while ( moved - - > = 0 )
while ( moved - - > = 0 ) {
sequence . moveNext ( ) ;
}
// append function name
sb . append ( sequence . token ( ) . text ( ) ) ;
while ( ! TokenUtilities . equals ( sequence . token ( ) . text ( ) , "(" ) )
while ( ! TokenUtilities . equals ( sequence . token ( ) . text ( ) , "(" ) ) {
sequence . moveNext ( ) ;
}
sb . append ( "(" ) ;
@ -87,20 +89,24 @@ public final class Glsl {
if ( ! TokenUtilities . equals ( token . text ( ) , "void" ) ) {
moved = 0 ;
while ( sequence . moveNext ( ) & & isIgnoredToken ( sequence . token ( ) ) )
while ( sequence . moveNext ( ) & & isIgnoredToken ( sequence . token ( ) ) ) {
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>" ) ;
}
while ( moved - - > = 0 )
while ( moved - - > = 0 ) {
sequence . movePrevious ( ) ;
}
sb . append ( token . text ( ) ) ;
if ( token . id ( ) = = GlslTokenId . COMMA )
if ( token . id ( ) = = GlslTokenId . COMMA ) {
sb . append ( KEYWORD_FONT_COLOR ) ;
}
}
first = false ;
}
@ -157,12 +163,14 @@ public final class Glsl {
& & sequence . token ( ) . id ( ) ! = GlslTokenId . COMMA
& & sequence . token ( ) . id ( ) ! = GlslTokenId . EQ ) {
token = sequence . token ( ) ;
if ( ! isIgnoredToken ( token ) )
if ( ! isIgnoredToken ( token ) ) {
sb . append ( token ) ;
}
moved + + ;
}
while ( moved - - > = 0 )
while ( moved - - > = 0 ) {
sequence . movePrevious ( ) ;
}
// read backwards throw the declaration
boolean skipToken = false ;
@ -215,7 +223,6 @@ public final class Glsl {
return sb . toString ( ) ;
}
public static final String createPreprocessorString ( SyntaxContext context ) {
ASTNode node = ( ASTNode ) context . getASTPath ( ) . getLeaf ( ) ;
@ -223,19 +230,24 @@ public final class Glsl {
String str = null ;
for ( ASTItem item : children )
if ( isTokenType ( item , GlslTokenId . PREPROCESSOR . name ( ) ) )
for ( ASTItem item : children ) {
if ( isTokenType ( item , GlslTokenId . PREPROCESSOR . name ( ) ) ) {
str = ( ( ASTToken ) item ) . getIdentifier ( ) ;
}
}
for ( int i = 0 ; i < str . length ( ) ; i + + ) {
char c = str . charAt ( i ) ;
if ( c ! = '#' & & ! Character . isWhitespace ( c ) )
for ( int j = str . length ( ) - 1 ; j > i ; j - - )
if ( ! Character . isWhitespace ( str . charAt ( j ) ) )
if ( c ! = '#' & & ! Character . isWhitespace ( c ) ) {
for ( int j = str . length ( ) - 1 ; j > i ; j - - ) {
if ( ! Character . isWhitespace ( str . charAt ( j ) ) ) {
return str . substring ( i , j + 1 ) ;
}
}
}
}
@ -243,7 +255,7 @@ public final class Glsl {
}
/ * *
* 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 ) {
@ -251,7 +263,7 @@ public final class Glsl {
try {
document . readLock ( ) ;
// remember all declared funktions for auto completion
// remember all declared functions for auto- completion
synchronized ( declaredFunctions ) {
declaredFunctions . clear ( ) ;
}
@ -264,11 +276,11 @@ public final class Glsl {
if ( isNode ( declarationItem , "function" ) ) {
List < ASTItem > functionItems = declarationItem . getChildren ( ) ;
if ( functionItems . size ( ) < 3 )
if ( functionItems . size ( ) < 3 ) {
break ;
}
ASTItem nameToken = functionItems . get ( 0 ) ;
@ -278,8 +290,9 @@ public final class Glsl {
StringBuilder returnType = new StringBuilder ( ) ;
for ( ASTItem typeItem : declaration . getChildren ( ) ) {
if ( isNode ( typeItem , "function" ) )
if ( isNode ( typeItem , "function" ) ) {
break ;
}
if ( typeItem instanceof ASTNode ) {
returnType . append ( ( ( ASTNode ) typeItem ) . getAsText ( ) . trim ( ) ) ;
@ -295,8 +308,9 @@ public final class Glsl {
name . append ( "(" ) ;
ASTItem parameterList = functionItems . get ( 2 ) ;
if ( isNode ( parameterList , "parameter_declaration_list" ) )
if ( isNode ( parameterList , "parameter_declaration_list" ) ) {
name . append ( ( ( ASTNode ) parameterList ) . getAsText ( ) ) ;
}
name . append ( ")" ) ;
GLSLElementDescriptor elementDesc = new GLSLElementDescriptor (
@ -326,7 +340,7 @@ public final class Glsl {
}
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 ;
while ( sequence . movePrevious ( ) & & counter > 0 ) {
if ( sequence . token ( ) . id ( ) = = id ) {
@ -339,23 +353,21 @@ public final class Glsl {
}
}
private static final boolean isIgnoredToken ( Token token ) {
private static boolean isIgnoredToken ( Token token ) {
return token . id ( ) = = GlslTokenId . WHITESPACE
| | token . id ( ) = = GlslTokenId . COMMENT
| | 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 ) ;
}
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 ) ;
}
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 ) ;
}
}