예제 #1
0
        public static sema.Type ResolveStructure(Context ctx, diagn.Reporter reporter, syn.Node.TypeStructure node)
        {
            if (node == null)
            {
                return(null);
            }

            var nodeName = node.name as syn.Node.Identifier;
            var name     = nodeName.token.excerpt;

            var namespaceNode = ctx.names.Find(name);

            if (namespaceNode == null)
            {
                reporter.Error("unknown `" + name + "`", new diagn.Caret(nodeName.span));
                return(new sema.Type.Error());
            }

            var namespaceItem = namespaceNode.item as sema.Namespace.Item.Def;
            var defStructure  = ctx[namespaceItem.def] as sema.Def.Structure;

            if (defStructure == null)
            {
                reporter.Error("`" + name + "` is not a type", new diagn.Caret(nodeName.span));
                return(new sema.Type.Error());
            }

            return(new sema.Type.Structure {
                def = namespaceItem.def
            });
        }
예제 #2
0
        public static void Tokenize(Context ctx, diagn.Reporter reporter, H <mod.Unit> hUnit)
        {
            var unit  = ctx[hUnit];
            var src   = unit.ReadSource(ctx);
            var index = 0;

            unit.tokens = new List <syn.Token>();

            while (index < src.Length)
            {
                var match =
                    TryMatchFixed(src, index) ??
                    TryMatchFilter(syn.TokenKind.Whitespace, src, index, IsWhitespace, IsWhitespace) ??
                    TryMatchFilter(syn.TokenKind.Identifier, src, index, IsIdentifierPrefix, IsIdentifier) ??
                    TryMatchFilter(syn.TokenKind.Number, src, index, IsNumberPrefix, IsNumber) ??
                    new Match(src[index].ToString(), syn.TokenKind.Error);

                var span = new diagn.Span(hUnit, index, index + match.excerpt.Length);

                if (match.kind == syn.TokenKind.Error)
                {
                    reporter.Error("unexpected character", new diagn.Caret(span));
                }

                unit.tokens.Add(new syn.Token
                {
                    span    = span,
                    kind    = match.kind,
                    excerpt = match.excerpt
                });

                index += match.excerpt.Length;
            }
        }
예제 #3
0
        private static void ReportDuplicate(Context ctx, diagn.Reporter reporter, diagn.Span newSpan, sema.Namespace.Node originalNode)
        {
            var originalSpan = (diagn.Span)null;

            var originalItemDef = originalNode.item as sema.Namespace.Item.Def;

            if (originalItemDef != null)
            {
                originalSpan = ctx[originalItemDef.def].spanDefName;
            }

            if (originalSpan != null)
            {
                reporter.Error(
                    "duplicate definition of `" + ctx.names.PrintableFullKeyOf(originalNode) + "`",
                    new diagn.Caret(newSpan), new diagn.Caret(originalSpan, false));
            }
            else
            {
                reporter.Error(
                    "duplicate definition of `" + ctx.names.PrintableFullKeyOf(originalNode) + "`",
                    new diagn.Caret(newSpan));
            }
        }
예제 #4
0
        private static void TryCollectStructureDef(Context ctx, diagn.Reporter reporter, mod.Unit unit, syn.Node.StructureDef node)
        {
            if (node == null)
            {
                return;
            }

            var hDef = unit.semanticMap.def[node];

            if (hDef == null)
            {
                reporter.InternalError("def not found for node", new diagn.Caret(node.span));
            }

            else
            {
                var structureDef = ctx[hDef] as sema.Def.Structure;

                foreach (var child in node.fields)
                {
                    var nodeField      = child as syn.Node.StructureDefField;
                    var nodeIdentifier = nodeField.identifier as syn.Node.Identifier;

                    var fieldName      = nodeIdentifier.token.excerpt;
                    var duplicateField = structureDef.fields.Find(f => f.name == fieldName);
                    if (duplicateField != null)
                    {
                        reporter.Error("duplicate field `" + fieldName + "`",
                                       new diagn.Caret(duplicateField.spanDefName, false),
                                       new diagn.Caret(nodeIdentifier.span));
                    }

                    var fieldType = TypeResolver.Resolve(ctx, reporter, nodeField.type);

                    structureDef.fields.Add(new sema.Def.Structure.Field
                    {
                        spanDef     = nodeField.span,
                        spanDefName = nodeIdentifier.span,
                        name        = fieldName,
                        type        = fieldType
                    });
                }
            }
        }