示例#1
0
        public object Resolve(Tokenizer tok, object scope)
        {
            if (index == -1 && length == -1)
            {
                return(meta);
            }
            if (meta == null)
            {
                throw new NullReferenceException();
            }
            if (meta is string)
            {
                return(ToString((string)meta));
            }
            TokenSubstitution ss = meta as TokenSubstitution; if (ss != null)
            {
                return(ss.value);
            }
            Delim d = meta as Delim; if (d != null)

            {
                return(d.text);
            }

            Context.Entry pce = meta as Context.Entry; if (pce != null)
            {
                return(pce.Resolve(tok, scope));
            }
            throw new DecoderFallbackException();
        }
示例#2
0
        public string ToDebugString()
        {
            SyntaxTree syntax = meta as SyntaxTree;

            if (syntax == null)
            {
                return(Resolve(null, null).ToString());
            }
            Delim d = syntax.sourceMeta as Delim;

            if (d != null)
            {
                return(d.ToString());
            }
            if (IsValid)
            {
                return(ToString(syntax.TextRaw));
            }
            string output = syntax.rules.name;

            if (syntax.IsTextLiteral)
            {
                output += "(" + syntax.GetText() + ")";
            }
            return(output);
        }
示例#3
0
 public int IndexOf(Delim delimiter)
 {
     for (int i = 0; i < tokens.Count; ++i)
     {
         Delim d = tokens[i].GetAsDelimiter();
         if (d == delimiter)
         {
             return(i);
         }
     }
     return(-1);
 }
示例#4
0
        protected Type FindInternalType()
        {
            if (Current.tokenIndex >= Current.tokens.Count)
            {
                return(null);
            }
            if (!SkipComments())
            {
                AddError("failed skipping comment for initial type"); return(null);
            }
            Token token = Current.Token;
            Delim d     = token.GetAsDelimiter();

            if (d != null)
            {
                if (d.text == "=" || d.text == ":")
                {
                    SkipComments(true);
                    memberType = typeof(string);
                    if (!TryGetValue())
                    {
                        return(null);
                    }
                    memberType = null;
                    SkipComments(true);
                    string typeName = memberValue.ToString();
                    //Show.Log("looking for member type "+typeName);
                    try {
                        Type t = SetResultType(typeName);
                        //Show.Log("internal type " + typeName + " (" + typeName + ")");
                        if (t == null)
                        {
                            AddError("unknown type " + typeName);
                        }
                        return(t);
                    } catch (Exception e) {
                        Show.Error(typeName + " failed to evaluate to a type:" + e);
                        return(null);
                    }
                }
                else
                {
                    AddError("unexpected beginning token " + d.text);
                }
            }
            return(null);
        }
示例#5
0
        protected void Tokenize(Context a_context = null, int index = 0)
        {
            List <Context.Entry> contextStack = new List <Context.Entry>();

            if (a_context == null)
            {
                a_context = CodeRules.Default;
            }
            else
            {
                contextStack.Add(a_context.GetEntry(tokens, -1, null));
            }
            int     tokenBegin     = -1;
            Context currentContext = a_context;

            while (index < str.Length)
            {
                char  c     = str[index];
                Delim delim = currentContext.GetDelimiterAt(str, index);
                if (delim != null)
                {
                    FinishToken(index, ref tokenBegin);
                    HandleDelimiter(delim, ref index, contextStack, ref currentContext, a_context);
                }
                else if (Array.IndexOf(currentContext.whitespace, c) < 0)
                {
                    if (tokenBegin < 0)
                    {
                        tokenBegin = index;
                    }                                                               // handle non-whitespace
                }
                else
                {
                    FinishToken(index, ref tokenBegin);                     // handle whitespace
                }
                if (rows != null && c == '\n')
                {
                    rows.Add(index);
                }
                ++index;
            }
            FinishToken(index, ref tokenBegin);             // add the last token that was still being processed
            FinalTokenCleanup();
            DebugPrint(-1);
            ApplyOperators();
        }
示例#6
0
        protected bool SkipStructuredDelimiters(Delim delim)
        {
            if (delim == null)
            {
                return(false);
            }
            switch (delim.text)
            {
            // skip these delimiters as though they were whitespace.
            case "=":
            case ":":
            case ",": break;

            default:
                AddError("unexpected delimiter \"" + delim.text + "\"");
                return(false);
            }
            memberValue = state;
            return(true);
        }
示例#7
0
 public override string ToString()
 {
     Context.Entry pce = meta as Context.Entry;
     if (pce != null)
     {
         Delim d = pce.sourceMeta as Delim;
         if (d != null)
         {
             return(d.ToString());
         }
         if (IsValid)
         {
             return(ToString(pce.TextRaw));
         }
         string output = pce.context.name;
         if (pce.IsText())
         {
             output += "(" + pce.GetText() + ")";
         }
         return(output);
     }
     return(Resolve(null, null).ToString());
 }
示例#8
0
        private void HandleDelimiter(Delim delim, ref int index, List <Context.Entry> contextStack,
                                     ref Context currentContext, Context defaultContext)
        {
            Token delimToken = new Token(delim, index, delim.text.Length);

            if (delim.parseRule != null)
            {
                ParseResult pr = delim.parseRule.Invoke(str, index);
                if (pr.IsError && errors != null)
                {
                    pr.error.OffsetBy(delimToken.index, rows);
                    errors.Add(pr.error);
                }
                if (pr.replacementValue != null)
                {
                    delimToken.length = pr.lengthParsed;
                    delimToken.meta   = new TokenSubstitution(str, pr.replacementValue);
                }
                index += pr.lengthParsed - 1;
            }
            else
            {
                index += delim.text.Length - 1;
            }
            DelimCtx dcx = delim as DelimCtx;

            Context.Entry endedContext = null;
            if (dcx != null)
            {
                if (contextStack.Count > 0 && dcx.Context == currentContext && dcx.isEnd)
                {
                    endedContext            = contextStack[contextStack.Count - 1];
                    endedContext.endDelim   = dcx;
                    delimToken.meta         = endedContext;
                    endedContext.tokenCount = (tokens.Count - endedContext.tokenStart) + 1;
                    contextStack.RemoveAt(contextStack.Count - 1);
                    if (contextStack.Count > 0)
                    {
                        currentContext = contextStack[contextStack.Count - 1].context;
                    }
                    else
                    {
                        currentContext = defaultContext;
                    }
                }
                if (endedContext == null && dcx.isStart)
                {
                    Context.Entry parentCntx = (contextStack.Count > 0) ? contextStack[contextStack.Count - 1] : null;
                    Context.Entry newContext = dcx.Context.GetEntry(tokens, tokens.Count, str, parentCntx);
                    newContext.beginDelim = dcx;
                    currentContext        = dcx.Context;
                    delimToken.meta       = newContext;
                    contextStack.Add(newContext);
                }
            }
            tokens.Add(delimToken);
            if (endedContext != null)
            {
                ExtractContextAsSubTokenList(endedContext);
            }
        }
示例#9
0
        private void HandleDelimiter(Delim delim, ref int index, List <SyntaxTree> syntaxStack,
                                     ref ParseRuleSet currentContext, ParseRuleSet defaultContext)
        {
            Token delimToken = new Token(delim, index, delim.text.Length);

            if (delim.parseRule != null)
            {
                ParseResult pr = delim.parseRule.Invoke(str, index);
                if (pr.IsError && errors != null)
                {
                    pr.error.OffsetBy(delimToken.index, rows);
                    errors.Add(pr.error);
                }
                if (pr.replacementValue != null)
                {
                    delimToken.length = pr.lengthParsed;
                    delimToken.meta   = new TokenSubstitution(str, pr.replacementValue);
                }
                index += pr.lengthParsed - 1;
            }
            else
            {
                index += delim.text.Length - 1;
            }
            DelimCtx   dcx         = delim as DelimCtx;
            SyntaxTree endedSyntax = null;

            if (dcx != null)
            {
                if (syntaxStack.Count > 0 && dcx.Context == currentContext && dcx.isEnd)
                {
                    endedSyntax            = syntaxStack[syntaxStack.Count - 1];
                    endedSyntax.endDelim   = dcx;
                    delimToken.meta        = endedSyntax;
                    endedSyntax.tokenCount = (tokens.Count - endedSyntax.tokenStart) + 1;
                    syntaxStack.RemoveAt(syntaxStack.Count - 1);
                    if (syntaxStack.Count > 0)
                    {
                        currentContext = syntaxStack[syntaxStack.Count - 1].rules;
                    }
                    else
                    {
                        currentContext = defaultContext;
                    }
                }
                if (endedSyntax == null && dcx.isStart)
                {
                    SyntaxTree parentCntx = (syntaxStack.Count > 0) ? syntaxStack[syntaxStack.Count - 1] : null;
                    SyntaxTree newContext = dcx.Context.GetEntry(tokens, tokens.Count, str, parentCntx);
                    newContext.beginDelim = dcx;
                    currentContext        = dcx.Context;
                    delimToken.meta       = newContext;
                    syntaxStack.Add(newContext);
                }
            }
            tokens.Add(delimToken);
            tokenStrings.Add(delim.text);
            if (endedSyntax != null)
            {
                ExtractContextAsSubTokenList(endedSyntax);
            }
        }
示例#10
0
        private void WhatsThis(ParseRuleSet currentContext, int index, int tokenBegin, ParseRuleSet defaultContext, out Delim delim, out bool isWhiteSpace)
        {
            char c = str[index];

            isWhiteSpace = (currentContext.Whitespace != null)
                                ? currentContext.IsWhitespace(c) : (defaultContext.Whitespace != null)
                                ? defaultContext.IsWhitespace(c)
                                : CodeRules.Default.IsWhitespace(c);
            if (isWhiteSpace)
            {
                delim = null; return;
            }
            delim = (currentContext.Delimiters != null)
                                ? currentContext.GetDelimiterAt(str, index, tokenBegin)
                                : defaultContext.GetDelimiterAt(str, index, tokenBegin);
        }