@@ -431,52 +431,67 @@ public enum TokenType
431431 public List < Class > Classes = [ ] ;
432432 public List < Container > Containers = [ ] ;
433433 public List < Method > Methods = [ ] ;
434- public LineWithTokens [ ] Tokens = Array . Empty < LineWithTokens > ( ) ;
434+ public LineWithTokens [ ] LinesWithTokens = Array . Empty < LineWithTokens > ( ) ;
435435 public Parser ( ) { }
436436 public Parser ( string code )
437437 {
438438 Code = code ;
439439 }
440- public LineWithTokens [ ] Tokenize ( ) => Tokens = Tokenize ( Code ) ;
441- public LineWithTokens [ ] Tokenize ( string code ) => Tokens = TokenArray ( code ) . Where ( x => x . Line . Value . ToString ( ) != "" ) . ToArray ( ) ;
440+ public LineWithTokens [ ] Parse ( ) => LinesWithTokens = Parse ( Code ) ;
441+ public LineWithTokens [ ] Parse ( string code ) => LinesWithTokens = TokenArray ( code ) . Where ( x => x . Line . Value . ToString ( ) != "" ) . ToArray ( ) ;
442442 private LineWithTokens [ ] TokenArray ( string code , bool insideClass = false )
443443 {
444+ // Set the Code property to the parameter if it isn't null
444445 Code ??= code ;
445- List < LineWithTokens > withTokens = new List < LineWithTokens > ( ) ;
446- Line [ ] Lines = SplitLine ( code ) ;
446+ // The LineWithTokens list that gets returned
447+ List < LineWithTokens > lineWithTokens = new List < LineWithTokens > ( ) ;
448+ // Splits the code into lines
449+ Line [ ] Lines = SplitLine ( Code ) ;
447450
451+ // loops through each line
448452 for ( int i = 0 ; i < Lines . Length ; i ++ )
449453 {
450- List < Token > tokens ;
451- Line line = Lines [ i ] ;
452- int continues = 0 , arrow , ar = 0 ;
453- string [ ] stringParts = { } ;
454+ // Some empty variables before looping through tokens
455+ List < Token > tokens ; // tokens in the line
456+ Line line = Lines [ i ] ; // current line
457+ string [ ] stringParts = [ ] ; // Needed for the 'SplitParts' method. each token as a string value
458+ int continues = 0 , // used with the '->' token to continue the line to the next line
459+ // These are used to check if the line contains the '->' and there is still code after it. 2 code lines in 1 line
460+ arrow_output_index , // The output of the index after the '->'
461+ arrow_input_index = 0 ; // The input index for after the '->'
454462 do
455463 {
456- tokens = new List < Token > ( ) ;
457- arrow = 0 ;
458- stringParts = SplitParts ( ref Lines , i , ar , ref stringParts , out _ , out _ , insideClass , true ) . Select ( x=> x . ToString ( ) ) . ToArray ( ) ;
459- object [ ] parts = SplitParts ( ref Lines , i , ar , ref stringParts , out continues , out arrow , insideClass ) ;
464+ tokens = new List < Token > ( ) ; // sets tokens to empty
465+ arrow_output_index = 0 ; // sets the arrow output to zero
466+ // gets the parts split by spaces. Tokens as strings
467+ stringParts = SplitParts ( ref Lines , i , arrow_input_index , ref stringParts , out _ , out _ , insideClass , true ) . Select ( x => x . ToString ( ) ) . ToArray ( ) ;
468+ // gets the token's values
469+ object [ ] parts = SplitParts ( ref Lines , i , arrow_input_index , ref stringParts , out continues , out arrow_output_index , insideClass ) ;
470+ // loops through each part and creates the token from it
460471 for ( int j = 0 ; j < parts . Length ; j ++ )
461472 {
462- Token token = SingleToken ( parts , j , stringParts . Length > j ? stringParts [ j ] : "" , out bool stops ) ;
463- if ( token . Type != TokenType . None && token . Type != TokenType . Comment ) tokens . Add ( token ) ;
464- if ( stops ) continue ;
473+ // Creates the appropriate token for the part
474+ Token token = SingleToken ( parts , j , stringParts . Length > j ? stringParts [ j ] : "" ) ;
475+
476+ // if the token is a comment or invalid, don't append the token to 'tokens'
477+ if ( token . Type != TokenType . None || token . Type != TokenType . Comment ) continue ;
478+ tokens . Add ( token ) ;
465479 }
466- ar = arrow ;
480+ arrow_input_index = arrow_output_index ; // sets the arrow input to the arrow output
467481
468- withTokens . Add ( new ( tokens . ToArray ( ) , line ) ) ;
482+ // adds the line with the 'tokens' to 'lineWithTokens' list
483+ lineWithTokens . Add ( new ( tokens . ToArray ( ) , line ) ) ;
469484 }
470- while ( arrow != 0 ) ;
471- i += continues ;
472- line . CodeLine += 1 ;
485+ while ( arrow_output_index != 0 ) ; // checks if there isn't anymore arrows spliting lines
486+
487+ i += continues ; // skips any lines that are apart of the line before it with the '->' token
488+ line . CodeLine += 1 ; // increments the line number by 1 so the first line is not 0
473489 }
474490
475- return withTokens . ToArray ( ) ;
491+ return lineWithTokens . ToArray ( ) ;
476492 }
477- internal Token SingleToken ( object [ ] parts , int partIndex , string stringPart , out bool stops )
493+ internal Token SingleToken ( object [ ] parts , int partIndex , string stringPart )
478494 {
479- stops = false ;
480495 TokenType tokenType = TokenType . None ;
481496 if ( parts [ partIndex ] is string )
482497 {
@@ -487,7 +502,7 @@ internal Token SingleToken(object[] parts, int partIndex, string stringPart, out
487502 case "!" : case "not" : tokenType = TokenType . Not ; break ;
488503 case "&" : case "&&" : case "and" : tokenType = TokenType . And ; break ;
489504 case "|" : case "||" : case "or" : tokenType = TokenType . Or ; break ;
490- case "//" : tokenType = TokenType . Comment ; stops = true ; break ;
505+ case "//" : tokenType = TokenType . Comment ; break ;
491506 case "=>" : tokenType = TokenType . Arrow ; break ;
492507 case ":" : tokenType = TokenType . Colon ; break ;
493508 case "{" : tokenType = TokenType . OpenCurlyBracket ; break ;
@@ -559,9 +574,12 @@ internal Token SingleToken(object[] parts, int partIndex, string stringPart, out
559574 }
560575 public object [ ] SplitParts ( ref Line [ ] lines , int lineIndex , int partStart , ref string [ ] stringParts , out int continues , out int arrow , bool insideClass = false , bool tostring = false )
561576 {
577+ // Current Line
562578 string line = lines [ lineIndex ] . Value ;
579+ // Get each token from the line. 'parts' is split by the token delimeters. 'partSpaces' is split by spaces
563580 object [ ] parts = SplitWithDelimiters ( line , Delimeters ) . Where ( x => x != "" && x != " " ) . Select ( x => ( object ) x ) . Skip ( partStart ) . ToArray ( ) ;
564581 string [ ] partsSpaces = line . Split ( " " ) . Where ( x => x != "" && x != " " ) . ToArray ( ) ;
582+ // Sets the out parameters to default
565583 continues = 0 ; arrow = 0 ;
566584 for ( int i = 0 ; i < parts . Length ; i ++ )
567585 {
@@ -1292,7 +1310,7 @@ private Statement SetStatement(ref Line[] lines, ref string[] strParts, int line
12921310 brackets = true ;
12931311 end = parts . Length - j ;
12941312 }
1295- argTokens = argTokens . Append ( SingleToken ( parts , j , parts [ partIndex ] . ToString ( ) , out _ ) ) . ToArray ( ) ;
1313+ argTokens = argTokens . Append ( SingleToken ( parts , j , parts [ partIndex ] . ToString ( ) ) ) . ToArray ( ) ;
12961314 }
12971315 argTokens = argTokens . Take ( argTokens . Length - end ) . ToArray ( ) ;
12981316 if ( argTokens . FirstOrDefault ( new Token ( TokenType . None , "" , "" ) ) . Value . ToString ( ) == "runexec" )
@@ -1303,7 +1321,7 @@ private Statement SetStatement(ref Line[] lines, ref string[] strParts, int line
13031321 object [ ] objects = SplitParts ( ref l , 0 , 0 , ref strParts , out _ , out _ ) ;
13041322 Token [ ] t = [ ] ;
13051323 for ( int i = 0 ; i < objects . Length ; i ++ )
1306- t = [ .. t , SingleToken ( objects , i , parts [ partIndex ] . ToString ( ) , out _ ) ] ;
1324+ t = [ .. t , SingleToken ( objects , i , parts [ partIndex ] . ToString ( ) ) ] ;
13071325 argTokens = t ;
13081326 }
13091327 if ( Statement . ConditionalTypes . Contains ( parts [ partIndex ] ) )
@@ -1351,7 +1369,7 @@ private Statement SetStatement(ref Line[] lines, ref string[] strParts, int line
13511369 break ;
13521370 code += bracketLine . Value + Environment . NewLine ;
13531371 }
1354- lineWithTokens = Tokenize ( code ) ;
1372+ lineWithTokens = Parse ( code ) ;
13551373 lineWithTokens = lineWithTokens . Select ( x => { x . Line . CodeLine ++ ; return x ; } ) . ToArray ( ) ;
13561374 if ( lineWithTokens . Last ( ) . Line . Value . ToString ( ) == "}" )
13571375 {
@@ -1369,6 +1387,7 @@ private Statement SetStatement(ref Line[] lines, ref string[] strParts, int line
13691387 private Method SetMethod ( Line [ ] lines , ref string [ ] strParts , int index , out string returns ) => SetMethod ( ref lines , ref strParts , index , out returns ) ;
13701388 private Method SetMethod ( ref Line [ ] lines , ref string [ ] strParts , int index , out string returns )
13711389 {
1390+ // Get the current line
13721391 Line line = lines [ index ] ;
13731392 line . CodeLine += 1 ;
13741393 returns = "" ;
@@ -1442,13 +1461,12 @@ private Method SetMethod(ref Line[] lines, ref string[] strParts, int index, out
14421461 param = param . Append ( new Var ( pName , pVal , line , type : pType , required : req ) ) . ToArray ( ) ;
14431462 }
14441463 }
1445-
1464+ int start = index + 1 ;
14461465 LineWithTokens [ ] lineWithTokens = [ ] ;
1447- Line nextLine = lines [ index + 1 ] ;
1466+ Line nextLine = lines [ start ] ;
14481467 bool sameLineBracket = nextLine . Value . StartsWith ( '{' ) ;
14491468 int curleyBrackets = sameLineBracket ? 0 : 1 ;
1450- int [ ] indexes = [ ] ;
1451- for ( int i = index + 1 ; i < lines . Length ; i ++ )
1469+ for ( int i = start ; i < lines . Length ; i ++ )
14521470 {
14531471 Line bracketLine = lines [ i ] ;
14541472 Token [ ] bracketLineTokens = TokenArray ( bracketLine . Value ) [ 0 ] . Tokens ;
@@ -1457,7 +1475,7 @@ private Method SetMethod(ref Line[] lines, ref string[] strParts, int index, out
14571475 if ( Statement . Types . Contains ( bracketLineTokens [ 0 ] . Value ) )
14581476 {
14591477 Statement statement = SetStatement ( ref lines , ref strParts , i , 0 ) ;
1460- bracketLineTokens = [ SingleToken ( [ statement ] , 0 , string . Join ( " " , statement . Line . Value ) , out _ ) ] ;
1478+ bracketLineTokens = [ SingleToken ( [ statement ] , 0 , string . Join ( " " , statement . Line . Value ) ) ] ;
14611479 if ( bracketLine . Value . Contains ( "{" ) && ! bracketLine . Value . Contains ( "}" ) ) curleyBrackets -- ;
14621480 }
14631481 } catch { }
@@ -1466,12 +1484,11 @@ private Method SetMethod(ref Line[] lines, ref string[] strParts, int index, out
14661484 if ( bracketLine . Value . Contains ( '}' ) )
14671485 curleyBrackets -- ;
14681486 lineWithTokens = [ .. lineWithTokens , new LineWithTokens ( bracketLineTokens , bracketLine ) ] ;
1469- indexes = [ .. indexes , i ] ;
14701487 if ( curleyBrackets == 0 )
14711488 break ;
14721489 }
1473- for ( int i = 0 ; i < indexes . Length ; i ++ )
1474- lines = lines . Where ( ( x , y ) => y != indexes [ i ] ) . ToArray ( ) ;
1490+ for ( int i = 0 ; i < lineWithTokens . Length ; i ++ )
1491+ lines = lines . Where ( ( x , y ) => y != start ) . ToArray ( ) ;
14751492
14761493 if ( lineWithTokens . Last ( ) . Line . Value . ToString ( ) == "}" )
14771494 {
@@ -1535,13 +1552,17 @@ internal static string[] SplitWithDelimiters(string input, char[] delimiters)
15351552 }
15361553 private static Line [ ] SplitLine ( string code )
15371554 {
1555+ // Generates an empty Line array to return
15381556 Line [ ] lines = Array . Empty < Line > ( ) ;
1539- int i = 0 ;
1540- foreach ( var item in code . Split ( Environment . NewLine ) . Select ( s => s . Trim ( ) ) . ToArray ( ) )
1557+
1558+ int index = 0 ; // index of loop
1559+ string [ ] string_lines = code . Split ( '\n ' ) . Select ( s => s . Trim ( ) ) . ToArray ( ) ; // splits code by each line
1560+ foreach ( var item in string_lines )
15411561 {
1542- lines = lines . Append ( new Line ( item , i ) ) . ToArray ( ) ;
1543- i ++ ;
1544- } ;
1562+ // Append line to the array, 'lines'
1563+ lines = lines . Append ( new Line ( item , index ) ) . ToArray ( ) ;
1564+ index ++ ; // increment the index by one
1565+ }
15451566 return lines ;
15461567 }
15471568 }
0 commit comments