99import mx .kenzie .foundation .Type ;
1010import mx .kenzie .foundation .WriteInstruction ;
1111import mx .kenzie .foundation .language .PostCompileClass ;
12+ import mx .kenzie .jupiter .stream .Stream ;
1213import org .byteskript .skript .api .Library ;
1314import org .byteskript .skript .api .SyntaxElement ;
1415import org .byteskript .skript .api .syntax .InnerModifyExpression ;
2425import java .nio .charset .StandardCharsets ;
2526import java .util .ArrayList ;
2627import java .util .List ;
28+ import java .util .concurrent .atomic .AtomicBoolean ;
2729import java .util .function .Consumer ;
2830import java .util .regex .Matcher ;
2931
3032public class SimpleSkriptCompiler extends SkriptCompiler implements SkriptParser {
3133 private static volatile int anonymous = 0 ;
3234 protected final List <Library > libraries = new ArrayList <>();
3335 protected final java .util .regex .Pattern whitespace = java .util .regex .Pattern .compile ("(?<=^)[\\ t ]+(?=\\ S)" );
36+ private final String regex = "\\ s+$" ;
3437
3538 public SimpleSkriptCompiler (final Library ... libraries ) {
3639 this .libraries .addAll (List .of (libraries ));
@@ -80,21 +83,20 @@ private String unstream(InputStream stream) {
8083 protected List <String > removeComments (final String string ) {
8184 final List <String > original = string .lines ().toList (); // stream of sadness :(
8285 final List <String > lines = new ArrayList <>();
83- final String regex = "\\ s+$" ;
84- boolean inComment = false ;
86+ boolean comment = false ;
8587 for (final String old : original ) {
8688 String line = old ;
87- if (inComment ) {
89+ if (comment ) {
8890 if (line .contains ("*/" )) {
8991 line = line .substring (line .indexOf ("*/" ) + 2 ); // keep last part of line
90- inComment = false ;
92+ comment = false ;
9193 } else {
9294 line = "" ; // inside a commented block
9395 }
9496 } else {
9597 if (line .contains ("//" )) line = line .substring (0 , line .indexOf ("//" )); // keep first part of line
9698 if (line .contains ("/*" )) {
97- inComment = true ;
99+ comment = true ;
98100 line = line .substring (0 , line .indexOf ("/*" )); // first part of line not in comment
99101 }
100102 }
@@ -104,19 +106,35 @@ protected List<String> removeComments(final String string) {
104106 return lines ;
105107 }
106108
109+ protected String stripLine (final String old , AtomicBoolean comment ) {
110+ String line = old ;
111+ if (comment .get ()) {
112+ if (line .contains ("*/" )) {
113+ line = line .substring (line .indexOf ("*/" ) + 2 ); // keep last part of line
114+ comment .set (false );
115+ } else line = "" ; // inside a commented block
116+ } else {
117+ if (line .contains ("//" )) line = line .substring (0 , line .indexOf ("//" )); // keep first part of line
118+ if (line .contains ("/*" )) {
119+ comment .set (true );
120+ line = line .substring (0 , line .indexOf ("/*" )); // first part of line not in comment
121+ }
122+ }
123+ return line .replaceAll (regex , "" ); // trim trailing whitespace
124+
125+ }
126+
107127 @ Override
108128 public Library [] getLibraries () {
109129 return libraries .toArray (new Library [0 ]);
110130 }
111131
112132 protected void compileLine (final String line , final FileContext context ) {
113- final ElementTree tree = parseLine (line , context );
133+ final ElementTree tree = this . parseLine (line , context );
114134 if (tree == null ) return ;
115135 tree .preCompile (context );
116136 tree .compile (context );
117- for (Consumer <Context > consumer : context .endOfLine ) {
118- consumer .accept (context );
119- }
137+ for (final Consumer <Context > consumer : context .endOfLine ) consumer .accept (context );
120138 context .endOfLine .clear ();
121139 context .currentEffect = null ;
122140 context .sectionHeader = false ;
@@ -177,6 +195,7 @@ public ElementTree parseLine(final String line, final FileContext context) {
177195 context .setIndentUnit (unit );
178196 }
179197 final int actual = this .trueIndent (line , context .indentUnit ());
198+ context .lineIndent = actual ;
180199 if (actual < expected ) {
181200 for (int i = 0 ; i < (expected - actual ); i ++) {
182201 context .destroySection ();
@@ -280,36 +299,45 @@ public boolean removeLibrary(Library library) {
280299 }
281300
282301 @ Override
283- public PostCompileClass [] compile (InputStream stream , Type name ) {
284- return compile (unstream (stream ), name );
302+ public PostCompileClass [] compile (InputStream stream , Type path ) {
303+ final FileContext context = new FileContext (path );
304+ context .libraries .addAll (libraries );
305+ for (final Library library : libraries ) {
306+ for (final Type type : library .getTypes ()) context .registerType (type );
307+ }
308+ final AtomicBoolean comment = new AtomicBoolean (false );
309+ for (final String line : Stream .controller (stream ).lines ()) {
310+ context .lineNumber ++;
311+ context .line = null ;
312+ final String stripped = this .stripLine (line , comment );
313+ if (stripped .isBlank ()) continue ;
314+ this .compileLine (context , stripped );
315+ }
316+ context .destroyUnits ();
317+ context .destroySections ();
318+ return context .compile ();
285319 }
286320
321+ @ Override
287322 public PostCompileClass [] compile (InputStream source , String path ) {
288323 if (path == null ) return this .compile (source );
289- return compile (unstream ( source ) , new Type (path ));
324+ return compile (source , new Type (path ));
290325 }
291326
327+ @ Override
292328 public PostCompileClass [] compile (String source , Type path ) {
293329 final FileContext context = new FileContext (path );
294330 context .libraries .addAll (libraries );
295331 for (final Library library : libraries ) {
296332 for (final Type type : library .getTypes ()) context .registerType (type );
297333 }
298- final List < String > lines = this . removeComments ( source );
299- for (final String line : lines ) {
334+ final AtomicBoolean comment = new AtomicBoolean ( false );
335+ for (final String line : source . lines (). toList () ) {
300336 context .lineNumber ++;
301337 context .line = null ;
302- if (line .isBlank ()) continue ;
303- if (context .getMethod () != null ) {
304- context .getMethod ().writeCode (WriteInstruction .lineNumber (context .lineNumber ));
305- }
306- try {
307- this .compileLine (line , context );
308- } catch (ScriptParseError | ScriptCompileError ex ) {
309- throw ex ;
310- } catch (Throwable ex ) {
311- throw new ScriptCompileError (context .lineNumber , "Unknown error during compilation:" , ex );
312- }
338+ final String stripped = this .stripLine (line , comment );
339+ if (stripped .isBlank ()) continue ;
340+ this .compileLine (context , stripped );
313341 }
314342 context .destroyUnits ();
315343 context .destroySections ();
@@ -324,6 +352,19 @@ public SimpleSkriptCompiler clone() {
324352 return compiler ;
325353 }
326354
355+ private void compileLine (FileContext context , String stripped ) {
356+ if (context .getMethod () != null ) {
357+ context .getMethod ().writeCode (WriteInstruction .lineNumber (context .lineNumber ));
358+ }
359+ try {
360+ this .compileLine (stripped , context );
361+ } catch (ScriptParseError | ScriptCompileError ex ) {
362+ throw ex ;
363+ } catch (Throwable ex ) {
364+ throw new ScriptCompileError (context .lineNumber , "Unknown error during compilation:" , ex );
365+ }
366+ }
367+
327368 int trueIndent (final String line , final String unit ) {
328369 if (unit == null ) return 0 ;
329370 int indent = 0 , offset = 0 ;
0 commit comments