private static KirinNode ParseLine(FiMReport report, string content, int start, int length) { string subContent = content.Substring(start, length - 1); // Remove punctuation if (KirinPostScript.TryParse(subContent, start, length, out KirinNode node)) { return(node); } if (KirinProgramStart.TryParse(subContent, start, length, out node)) { return(node); } if (KirinProgramEnd.TryParse(subContent, start, length, out node)) { return(node); } #if DEBUG if (KirinClassStart.TryParse(subContent, start, length, out node)) { return(node); } if (KirinClassEnd.TryParse(subContent, start, length, out node)) { return(node); } if (KirinClassConstructorStart.TryParse(subContent, start, length, out node)) { return(node); } if (KirinClassConstructorEnd.TryParse(subContent, start, length, out node)) { return(node); } #endif if (KirinFunctionStart.TryParse(subContent, start, length, out node)) { return(node); } if (KirinFunctionEnd.TryParse(subContent, start, length, out node)) { return(node); } ; if (KirinForInLoop.TryParse(subContent, start, length, out node)) { return(node); } if (KirinForToLoop.TryParse(subContent, start, length, out node)) { return(node); } if (KirinWhileLoop.TryParse(subContent, start, length, out node)) { return(node); } if (KirinSwitchStart.TryParse(subContent, start, length, out node)) { return(node); } if (KirinSwitchCase.TryParse(subContent, start, length, out node)) { return(node); } if (KirinSwitchCaseDefault.TryParse(subContent, start, length, out node)) { return(node); } if (KirinLoopEnd.TryParse(subContent, start, length, out node)) { return(node); } if (KirinIfStatementStart.TryParse(subContent, start, length, out node)) { return(node); } if (KirinElseIfStatement.TryParse(subContent, start, length, out node)) { return(node); } if (KirinIfStatementEnd.TryParse(subContent, start, length, out node)) { return(node); } if (KirinPrint.TryParse(subContent, start, length, out node)) { return(node); } if (KirinInput.TryParse(subContent, start, length, out node)) { return(node); } if (KirinFunctionCall.TryParse(subContent, report, start, length, out node)) { return(node); } if (KirinVariableDeclaration.TryParse(subContent, start, length, out node)) { return(node); } if (KirinListModify.TryParse(subContent, start, length, out node)) { return(node); } if (KirinVariableModify.TryParse(subContent, start, length, out node)) { return(node); } if (KirinUnary.TryParse(subContent, start, length, out node)) { return(node); } if (KirinReturn.TryParse(subContent, start, length, out node)) { return(node); } if (KirinDebugger.TryParse(subContent, start, length, out node)) { return(node); } #if DEBUG if (System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Break(); } else { Console.WriteLine("Unhandled node: " + subContent); } #endif return(new KirinNode(start, length)); }
internal static KirinStatement ParseStatement(KirinNode[] nodes, string content) { var statement = new KirinStatement(-1, -1); for (int i = 0; i < nodes.Length; i++) { var node = nodes[i]; switch (node.NodeType) { case "KirinProgramStart": case "KirinProgramEnd": case "KirinFunctionStart": case "KirinFunctionEnd": case "KirinElseIfStatement": case "KirinIfStatementEnd": case "KirinSwitchCase": case "KirinSwitchCaseDefault": case "KirinLoopEnd": { var line = FiMHelper.GetIndexPair(content, node.Start).Line; throw new Exception($"Illegal statement node at line {line}"); } case "KirinIfStatementStart": { var startingNode = node as KirinIfStatementStart; var statementNodes = KirinLoop.GetStatementNodes( startingNode, nodes, i, content, out i ); var endingNode = nodes[i] as KirinIfStatementEnd; statement.PushNode( KirinIfStatement.ParseNodes(startingNode, statementNodes, endingNode, content) ); } break; case "KirinForInLoop": { var startingNode = node as KirinForInLoop; var statementNodes = KirinLoop.GetStatementNodes( startingNode, nodes, i, content, out i ); var endingNode = nodes[i] as KirinLoopEnd; statement.PushNode( KirinForInLoop.ParseNodes(startingNode, statementNodes, endingNode, content) ); } break; case "KirinForToLoop": { var startingNode = node as KirinForToLoop; var statementNodes = KirinLoop.GetStatementNodes( startingNode, nodes, i, content, out i ); var endingNode = nodes[i] as KirinLoopEnd; statement.PushNode( KirinForToLoop.ParseNodes(startingNode, statementNodes, endingNode, content) ); } break; case "KirinWhileLoop": { var startingNode = node as KirinWhileLoop; var statementNodes = KirinLoop.GetStatementNodes( startingNode, nodes, i, content, out i ); var endingNode = nodes[i] as KirinLoopEnd; statement.PushNode( KirinWhileLoop.ParseNodes(startingNode, statementNodes, endingNode, content) ); } break; case "KirinSwitchStart": { var startingNode = node as KirinSwitchStart; var statementNodes = KirinLoop.GetStatementNodes( startingNode, nodes, i, content, out i ); var endingNode = nodes[i] as KirinLoopEnd; statement.PushNode( KirinSwitch.ParseNodes(startingNode, statementNodes, endingNode, content) ); } break; case "KirinPostScript": { // Ignore } break; default: { statement.PushNode(node); } break; } } var firstNode = statement.Body.First() as KirinNode; var lastNode = statement.Body.Last() as KirinNode; statement.Start = firstNode.Start; statement.Length = (lastNode.Start + lastNode.Length) - firstNode.Start; return(statement); }