private void AST_HandleImport(SymbolStack <FrontendSymbol> symbols, SourceData src, ES_AstImportStatement import) { using var nmNameString = import.NamespaceName.ToPooledChars(); var namespaceData = GetNamespace(src, import.NamespaceName.NodeBounds, nmNameString); if (namespaceData is null) { return; } if (import.ImportedNames is null || import.ImportedNames.Length == 0) { foreach (var type in namespaceData.Types) { if (!symbols.AddSymbol(type.Address->Name.TypeName, FrontendSymbol.NewVariable(type))) { var err = ES_FrontendErrors.GenDuplicateSymbolDef( type.Address->Name.TypeNameString, src, import.NamespaceName.NodeBounds ); errorList.Add(err); } } foreach (var funcKVP in namespaceData.Functions) { if (!symbols.AddSymbol(funcKVP.Key, FrontendSymbol.NewFunction(funcKVP.Value))) { var err = ES_FrontendErrors.GenDuplicateSymbolDef( funcKVP.Value.Address->Name.TypeNameString, src, import.NamespaceName.NodeBounds ); errorList.Add(err); } } }
private void CreateFunctions_Function( ref TranslationUnitData transUnit, ES_NamespaceData.Builder namespaceBuilder, SymbolStack <FrontendSymbol> symbols, SourceData unitSrc, ES_TypeInfo *parentType, ES_AstFunctionDefinition funcDef ) { Debug.Assert(Environment is not null); Debug.Assert(EnvironmentBuilder is not null); var sourceUnit = transUnit.Name; var idPool = Environment.IdPool; // Get the namespace and function names. var funcName = Environment.IdPool.GetIdentifier(funcDef.Name.Text.Span); // Get the fully-qualified name. ES_FullyQualifiedName fullyQualifiedName; if (parentType == null) { var namespaceName = namespaceBuilder.NamespaceData.NamespaceName; fullyQualifiedName = new ES_FullyQualifiedName(namespaceName, funcName); } else { using var namespaceBytes = UnmanagedArray <byte> .GetArray(parentType->Name.NamespaceName.Length + 2 + parentType->Name.TypeName.Length); var span = namespaceBytes.Span; parentType->Name.NamespaceName.Span.CopyTo(span); span = span [parentType->Name.NamespaceName.Length..];
private void ImportNamespaceSymbols(SymbolStack <FrontendSymbol> symbols, ES_NamespaceData namespaceData) { foreach (var type in namespaceData.Types) { symbols.AddSymbol(type.Address->Name.TypeName, FrontendSymbol.NewType(type)); } foreach (var funcKVP in namespaceData.Functions) { symbols.AddSymbol(funcKVP.Key, FrontendSymbol.NewFunction(funcKVP.Value)); } }
private ES_AstTypeDeclaration_TypeReference GenerateASTTypeRef( ArrayPointer <byte> transUnitName, SymbolStack <FrontendSymbol> symbols, SourceData src, ES_AstTypeDeclaration typeDecl ) { Debug.Assert(typeDecl is not null); if (typeDecl is ES_AstTypeDeclaration_TypeReference) { return((typeDecl as ES_AstTypeDeclaration_TypeReference) !); } var type = ResolveTypeDeclaration(transUnitName, symbols, src, typeDecl); if (type == null) { type = Environment !.TypeUnknownValue; } return(new ES_AstTypeDeclaration_TypeReference(typeDecl, type)); }
public unsafe void Initialize(ES_AbstractSyntaxTree ast) { Ast = ast; Symbols = new SymbolStack <FrontendSymbol> (new FrontendSymbol(FrontendSymbolType.None, null, 0)); }
public bool Validate(List <Token> tokens, int idx = -1) { Token currentToken = null; CurrentState = CurrentState.GoToNextState(TokenEnums.TokenType.Dollar); // move from the initialstate going forward try { for (int i = idx; i < tokens.Count;) { if (CurrentState.StateAction == StateAction.Accept) { break; } if (CurrentState.StateAction == StateAction.Scan) { // do nothing i++; currentToken = i >= 0 ? tokens[i] : null; } else if (CurrentState.StateAction == StateAction.Read && currentToken.TokenType == TokenEnums.TokenType.Tag) { if (TagStack.Peek() == currentToken.Symbol) { TagStack.Pop(); } } else if (CurrentState.StateAction == StateAction.Read && (currentToken.TokenType == TokenEnums.TokenType.HeaderXmlOpeningSymbol || currentToken.TokenType == TokenEnums.TokenType.HeaderXmlClosingSymbol || currentToken.TokenType == TokenEnums.TokenType.ClosingSymbol || currentToken.TokenType == TokenEnums.TokenType.OpeningSymbol || currentToken.TokenType == TokenEnums.TokenType.OpeningWithBackslashSymbol || currentToken.TokenType == TokenEnums.TokenType.SelfClosingSymbol)) { if (currentToken.TokenType == TokenEnums.TokenType.SelfClosingSymbol || currentToken.TokenType == TokenEnums.TokenType.HeaderXmlClosingSymbol) { TagStack.Pop(); } var peek = SymbolStack.Peek(); if ((currentToken.Partners != null && currentToken.Partners.Any(item => item == peek)) || peek == currentToken.Partner) { SymbolStack.Pop(); } // TO DO: handle partner tags } else if (CurrentState.StateAction == StateAction.Write && (currentToken.TokenType == TokenEnums.TokenType.Tag || currentToken.TokenType == TokenEnums.TokenType.HeaderXmlTag)) { TagStack.Push(currentToken.Symbol); } else if (CurrentState.StateAction == StateAction.Write && (currentToken.TokenType == TokenEnums.TokenType.HeaderXmlClosingSymbol || currentToken.TokenType == TokenEnums.TokenType.HeaderXmlOpeningSymbol || currentToken.TokenType == TokenEnums.TokenType.ClosingSymbol || currentToken.TokenType == TokenEnums.TokenType.OpeningSymbol || currentToken.TokenType == TokenEnums.TokenType.OpeningWithBackslashSymbol || currentToken.TokenType == TokenEnums.TokenType.SelfClosingSymbol)) { SymbolStack.Push(currentToken.Symbol); } var nextState = CurrentState.GoToNextState(currentToken.TokenType); // move from the initialstate going forward if (nextState == null) { //// non-deterministic //// guess next state based on next token var nextToken = (i + 1) >= tokens.Count ? null : tokens[i + 1]; if (nextToken != null) { var states = CurrentState.GetPossibleNextStates(currentToken.TokenType); nextState = states.Where(state => state.StateAction != StateAction.Accept).FirstOrDefault(); } else { // end state var states = CurrentState.GetPossibleNextStates(currentToken.TokenType); nextState = states.Where(state => state.StateAction == StateAction.Accept).FirstOrDefault(); } } CurrentState = nextState; } } catch (Exception ex) { throw; } // scan //foreach (var item in tokens) //{ // if (item.TokenType == TokenEnums.TokenType.Tag) // { // if (tags.Count != 0 && tags.Peek() == item.Symbol) // { // tags.Pop(); // } // else // { // tags.Push(item.Symbol); // } // } // else if (item.TokenType == TokenEnums.TokenType.OpeningSymbol || item.TokenType == TokenEnums.TokenType.OpeningWithBackslashSymbol) // { // symbols.Push(item); // } // else if (symbols.Count != 0 && item.TokenType == TokenEnums.TokenType.ClosingSymbol) // { // if (symbols.Peek().TokenType == TokenEnums.TokenType.OpeningSymbol || symbols.Peek().TokenType == TokenEnums.TokenType.OpeningWithBackslashSymbol) // { // symbols.Pop(); // } // } // else if (item.TokenType == TokenEnums.TokenType.SelfClosingSymbol) // { // tags.Pop(); // } //} if (TagStack.Count == 0 && SymbolStack.Count == 0 && CurrentState == FinalState) { return(true); } return(false); }
public FTCResponse Create([FromBody] FTCRequest reqItem) { Tag item = new Tag() { Name = reqItem.Name, Definition = reqItem.Definition, }; SymbolStack sb = new SymbolStack(); var response = new FTCResponse() { Code = FTCResponse.ERROR //Error }; if (item == null) { response.Content = FTCResponse.INVALID_REQUEST; return(response); } else if (item.Name == null) { response.Content = Tag.INVALID_NAME; return(response); } else if (item.Name.Length == 0) { response.Content = Tag.INVALID_NAME; return(response); } else if (item.Definition == null) { response.Content = Tag.INVALID_DEFINITION; return(response); } else if (item.Definition.Length == 0) { response.Content = Tag.INVALID_DEFINITION; return(response); } // var itens = from u in _context.Tags // where u.Name.Equals(item.Name) || u.Definition.Equals(item.Definition) // select u; // if(itens.Any()) //Error if have repeated tags. if (_context.Tags.Any(u => u.Name.Equals(item.Name) || u.Definition.Equals(item.Definition))) // if(_context.Tags.Any(u=>u.Equals(item))) { response.Content = Tag.TAG_EXISTS; response.Code = FTCResponse.ERROR; return(response); } bool isEscape = false; bool valid = true; //Processing syntax foreach (char x in item.Definition) { if (isEscape) //last char was '\' { if (Tag.ESCAPE.Contains(x)) //x is a valid escape char. { sb.Add(new Symbol($"\\{x}")); //Add it to test semantic. isEscape = false; //last char now isn't a '\' continue; //next iteration. } else { valid = false; //invalid syntax, abort. break; } } if (x == Tag.ESCAPE_CHAR) //x is '\', iterating searching escape-chars. { isEscape = true; continue; } else { //x is a single symbol, search for operators, or add it as symbol. if (x == '+') { sb.Add(Operator.UNION); } else if (x == '.') { sb.Add(Operator.CONCAT); } else if (x == '*') { sb.Add(Operator.KLEENE); } else { sb.Add(new Symbol($"{x}")); } } } //no syntax errors... if (valid) { valid = sb.Verify; } //Expression have syntax or semantic errors. if (!valid) { response.Content = Tag.INVALID_DEFINITION; return(response); } //Group not defined... picking default. if (reqItem.Group == null || reqItem.Group.Length == 0) { item.Group = _context.Groups.OrderBy(i => i.GroupId).FirstOrDefault(); response.Content = Group.GROUP_DEFAULT + " " + String.Format(Tag.TAG_DEFINED, item.Name); response.Code = FTCResponse.WARNING; } //Group doesn't exists. else if (!_context.Groups.Any(g => reqItem.Group.Equals(g.Name))) { item.Group = new Group() { Name = reqItem.Group }; _context.Groups.Add(item.Group); response.Code = FTCResponse.INFO; response.Content = String.Format(Group.GROUP_NEW, item.Group) + String.Format(Tag.TAG_DEFINED, item.Name); } //Group Exists. else { item.Group = _context.Groups.FirstOrDefault(u => u.Name.Equals(reqItem.Group)); response.Code = FTCResponse.INFO; response.Content = String.Format(Tag.TAG_DEFINED, item.Name); } _context.Tags.Add(item); _context.SaveChanges(); response.Id = item.TagId; return(response); }
public void InitParser() { _symbolTableStack = new SymbolStack(); _isReady = true; }
public SymCheckVisitor(SymbolStack symStack) { _symStack = symStack; }
protected Rule() { _builder = new RuleBuilder(); _symbolStack = new SymbolStack(); _definition = new Lazy <IRuleDefinition>(BuildDefinition); }
private ES_TypeInfo *ResolveTypeDeclaration( ArrayPointer <byte> transUnitName, SymbolStack <FrontendSymbol> symbols, SourceData src, ES_AstTypeDeclaration typeDecl ) { Debug.Assert(EnvironmentBuilder is not null); switch (typeDecl) { case ES_AstTypeDeclaration_TypeName typeName: { var type = GetType(symbols, src, typeName); switch (type->AccessModifier) { case ES_AccessModifier.Public: break; case ES_AccessModifier.Internal: if (!type->SourceUnit.Equals(transUnitName)) { using var symbolName = PooledArray <char> .GetArray(typeName.GetStringLength()); typeName.ToString(symbolName); var err = ES_FrontendErrors.GenInaccessibleProtectionLevel( symbolName.Span.GetPooledString(), src, typeName.NodeBounds ); errorList.Add(err); } break; default: throw new NotImplementedException("Access modifier not implemented yet."); } return(type); } case ES_AstTypeDeclaration_TypeReference typeRef: return(typeRef.Reference); case ES_AstTypeDeclaration_Array arrayDecl: { var elemType = ResolveTypeDeclaration(transUnitName, symbols, src, arrayDecl.ElementType); return(EnvironmentBuilder.CreateArrayType(elemType, arrayDecl.Dimensions)); } case ES_AstTypeDeclaration_Basic basicDecl: { var innerType = ResolveTypeDeclaration(transUnitName, symbols, src, basicDecl.Inner !); switch (basicDecl.Type) { case ES_AstTypeDeclaration_Basic.DeclType.Const: if (innerType->IsConstant()) { errorList.Add(ES_FrontendErrors.GenTypeAlreadyConst( false, innerType->TypeTag == ES_TypeTag.Immutable, src, basicDecl.NodeBounds )); return(innerType); } return(EnvironmentBuilder.CreateConstType(innerType)); case ES_AstTypeDeclaration_Basic.DeclType.Immutable: if (innerType->IsConstant()) { errorList.Add(ES_FrontendErrors.GenTypeAlreadyConst( false, innerType->TypeTag == ES_TypeTag.Immutable, src, basicDecl.NodeBounds )); return(innerType); } return(EnvironmentBuilder.CreateImmutableType(innerType)); case ES_AstTypeDeclaration_Basic.DeclType.Nullable: return(EnvironmentBuilder.CreateNullableType(innerType)); case ES_AstTypeDeclaration_Basic.DeclType.Reference: return(EnvironmentBuilder.CreateReferenceType(innerType)); default: throw new NotImplementedException("Basic declaration type not implemented."); } } default: throw new NotImplementedException("Declaration type not implemented."); } }
private ES_TypeInfo *GetType(SymbolStack <FrontendSymbol> symbols, SourceData src, ES_AstTypeDeclaration_TypeName typeName) { var idPool = Environment !.IdPool; Debug.Assert(typeName.TypeName.Parts.Length > 0); if (typeName.Namespace is not null) { using var namespaceChars = typeName.Namespace.ToPooledChars(); using var nameChars = typeName.TypeName.ToPooledChars(); var namespaceId = idPool.GetIdentifier(namespaceChars); var nameId = idPool.GetIdentifier(nameChars); var type = Environment !.GetFullyQualifiedType(namespaceId, nameId); if (type == null) { var err = ES_FrontendErrors.GenCantFindSymbol(typeName.ToString(), src, typeName.NodeBounds); errorList.Add(err); return(Environment.TypeUnknownValue); } return(type); } else { var typeParts = typeName.TypeName.Parts; var typeId = idPool.GetIdentifier(typeParts [0].Text.Span); var symbol = symbols.GetSymbol(typeId); if (symbol.Tag == FrontendSymbolType.None) { var err = ES_FrontendErrors.GenCantFindSymbol(typeParts [0].Text.Span.GetPooledString(), typeParts [0]); errorList.Add(err); return(Environment.TypeUnknownValue); } else if (symbol.Tag == FrontendSymbolType.Variable) { var err = ES_FrontendErrors.GenVarUsedAsType(typeParts [0].Text.Span.GetPooledString(), typeParts [0]); errorList.Add(err); return(Environment.TypeUnknownValue); } else if (symbol.Tag == FrontendSymbolType.Function) { var err = ES_FrontendErrors.GenFuncUsedAsType(typeParts [0].Text.Span.GetPooledString(), typeParts [0]); errorList.Add(err); return(Environment.TypeUnknownValue); } else if (symbol.Tag != FrontendSymbolType.Type) { throw new NotImplementedException("Not implemented?"); } if (typeParts.Length > 1) { throw new NotImplementedException("[TODO] Nested types not implemented yet."); } return(symbol.MatchType()); } }