11
22import Token from './models/Token' ;
33import { TokenType } from './definitions/TokenType' ;
4- import { Keyword } from './definitions/Keyword' ;
4+
5+ const NO_TOKEN = new Token ( TokenType . NONE , '' , - 1 , 0 ) ;
56
67export default class Coder
78{
@@ -27,62 +28,46 @@ export default class Coder
2728 generate ( ) : string
2829 {
2930 let code = '' ;
30- let previous : Token = new Token ( TokenType . NOTHING , '' , 0 , 0 ) ;
31-
32- for ( const current of this . #tokens)
31+
32+ for ( let index = 0 ; index < this . #tokens. length ; index ++ )
3333 {
34- const prefix = this . #needsSpacingBefore( current , previous ) ? ' ' : '' ;
35- const postfix = this . #needsSpacingAfter( current ) ? ' ' : '' ;
34+ const previous = this . #tokens[ index - 1 ] ?? NO_TOKEN ;
35+ const current = this . #tokens[ index ] ;
36+ const next = this . #tokens[ index + 1 ] ?? NO_TOKEN ;
37+
38+ const prefix = this . #needsSpacingBefore( previous , current ) ? ' ' : '' ;
39+ const postfix = this . #needsSpacingAfter( current , next ) ? ' ' : '' ;
3640
3741 code += `${ prefix } ${ current . value } ${ postfix } ` ;
38-
39- previous = current ;
4042 }
4143
4244 return code ;
4345 }
4446
45- #needsSpacingBefore( current : Token , previous : Token ) : boolean
47+ #needsSpacingBefore( previous : Token , current : Token ) : boolean
4648 {
47- if ( current . isType ( TokenType . KEYWORD ) && this . #isInfixKeyword ( current ) )
49+ if ( current . isType ( TokenType . KEYWORD ) && this . #isTextType ( previous ) )
4850 {
4951 return true ;
5052 }
51- else if ( previous . isType ( TokenType . OPERATOR ) && current . isType ( TokenType . OPERATOR ) )
53+ else if ( current . isType ( TokenType . OPERATOR ) && previous . isType ( TokenType . OPERATOR ) )
5254 {
5355 return true ;
5456 }
5557
5658 return false ;
5759 }
5860
59- #needsSpacingAfter( current : Token ) : boolean
60- {
61- return current . isType ( TokenType . KEYWORD ) && this . #isPrefixKeyword( current ) ;
62- }
63-
64- #isPrefixKeyword( token : Token ) : boolean
61+ #needsSpacingAfter( current : Token , next : Token ) : boolean
6562 {
66- return this . #isInfixKeyword( token )
67- || token . hasValue ( Keyword . VAR )
68- || token . hasValue ( Keyword . LET )
69- || token . hasValue ( Keyword . CONST )
70- || token . hasValue ( Keyword . FUNCTION )
71- || token . hasValue ( Keyword . CLASS )
72- || token . hasValue ( Keyword . USING )
73- || token . hasValue ( Keyword . RETURN )
74- || token . hasValue ( Keyword . ASYNC )
75- || token . hasValue ( Keyword . AWAIT )
76- || token . hasValue ( Keyword . YIELD )
77- || token . hasValue ( Keyword . NEW )
78- || token . hasValue ( Keyword . THROW ) ;
63+ return current . isType ( TokenType . KEYWORD ) && ( next . isType ( TokenType . KEYWORD ) || this . #isTextType( next ) ) ;
7964 }
8065
81- #isInfixKeyword ( token : Token ) : boolean
66+ #isTextType ( token : Token )
8267 {
83- return token . hasValue ( Keyword . OF )
84- || token . hasValue ( Keyword . IN )
85- || token . hasValue ( Keyword . AS )
86- || token . hasValue ( Keyword . FROM ) ;
68+ return token . isType ( TokenType . IDENTIFIER )
69+ || token . isType ( TokenType . BOOLEAN )
70+ || token . isType ( TokenType . NOTHING )
71+ || token . isType ( TokenType . NUMBER ) ;
8772 }
8873}
0 commit comments