/// <summary> /// 处理注释状态的字符。 /// </summary> /// <returns></returns> public static DivideState GetCommentState() { var slash = new LexiState(); slash.charTypeList.Add(SourceCodeCharType.Divide); return(new DivideState(slash)); }
private static void DumpLexicalAnalyzer_GetSymbol( RegulationList grammar, CodeTypeDeclaration lexiType, string grammarId, SyntaxParserMapAlgorithm algorithm) { List <LexiState> lexiStateList = grammar.GetLexiStateList(); DivideState commentState = null;// 为了处理注释,"/"符号要特殊对待。 foreach (var state in lexiStateList) { if (state.CharTypeList.Contains(SourceCodeCharType.Divide)) { commentState = new DivideState(state); continue; } CodeMemberMethod method = state.GetMethodDefinitionStatement(grammarId, algorithm); if (method != null) { lexiType.Members.Add(method); } } { if (commentState == null) { commentState = LexiState.GetCommentState(); } CodeMemberMethod method = commentState.GetMethodDefinitionStatement(grammarId, algorithm); if (method != null) { lexiType.Members.Add(method); } } }
private static void DumpLexicalAnalyzer_TryGetToken( RegulationList grammar, CodeTypeDeclaration lexiType) { var method = new CodeMemberMethod(); method.Name = "TryGetToken"; method.Attributes = MemberAttributes.Family | MemberAttributes.Override; method.ReturnType = new CodeTypeReference(typeof(bool)); method.Parameters.Add(new CodeParameterDeclarationExpression("AnalyzingContext", "context")); method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(Token), "result")); method.Parameters.Add(new CodeParameterDeclarationExpression("SourceCodeCharType", "charType")); { // bool gotToken = false; string vargotToken = "gotToken"; // :( var varDeclaration = new CodeVariableDeclarationStatement(typeof(bool), vargotToken); varDeclaration.InitExpression = new CodePrimitiveExpression(false); method.Statements.Add(varDeclaration); } { List <LexiState> lexiStateList = grammar.GetLexiStateList(); DivideState commentState = null;// 为了处理注释,"/"符号要特殊对待。 foreach (var state in lexiStateList) { if (state.CharTypeList.Contains(SourceCodeCharType.Divide)) { commentState = new DivideState(state); continue; } var condition = new CodeConditionStatement( state.GetCondition(), state.GetMethodInvokeStatement()); method.Statements.Add(condition); } { if (commentState == null) { commentState = LexiState.GetCommentState(); } var condition = new CodeConditionStatement( commentState.GetCondition(), commentState.GetMethodInvokeStatement()); method.Statements.Add(condition); } { var getSpaceState = LexiState.GetSpaceState(); var condition = new CodeConditionStatement( getSpaceState.GetCondition(), getSpaceState.GetMethodInvokeStatement()); method.Statements.Add(condition); } { var getUnknownState = LexiState.GetUnknownState(); var lastCondition = getUnknownState.GetMethodInvokeStatement(); method.Statements.AddRange(lastCondition); } } lexiType.Members.Add(method); }
/// <summary> /// 处理Unknown状态的字符。 /// </summary> /// <returns></returns> public static LexiState GetUnknownState() { var unknown = new LexiState(); unknown.charTypeList.Add(SourceCodeCharType.Unknown); unknown.getTokenList.TryInsert(new CodeGetUnknown()); return(unknown); }
private static void TryInserteGetConstString(List <LexiState> result) { bool exists = false; foreach (var state in result) { if (state.Contains(SourceCodeCharType.DoubleQuotation)) { exists = true; break; } } if (!exists) { var state = new LexiState(); state.CharTypeList.Add(SourceCodeCharType.DoubleQuotation); state.GetTokenList.TryInsert(new CodeGetConstString()); result.Add(state); } }
private static void TryInsertGetNumber(List <LexiState> result) { bool exists = false; foreach (var state in result) { if (state.Contains(SourceCodeCharType.Number)) { exists = true; break; } } if (!exists) { var state = new LexiState(); state.CharTypeList.Add(SourceCodeCharType.Number); state.GetTokenList.TryInsert(new CodeGetNumber()); result.Add(state); } }
private static void CodeGetRegularToken(List <LexiState> result, TreeNodeType node) { string content = node.Content; SourceCodeCharType charType = content[0].GetCharType(); bool exists = false; foreach (var state in result) { if (state.Contains(charType)) { state.GetTokenList.TryInsert(new CodeGetToken(node)); exists = true; break; } } if (!exists) { var state = new LexiState(); state.CharTypeList.Add(charType); state.GetTokenList.TryInsert(new CodeGetToken(node)); result.Add(state); } }
public DivideState(LexiState state) { this.charTypeList = state.CharTypeList; this.getTokenList = new CommentCodeGetTokenCollection(state.GetTokenList); }