Exemplo n.º 1
0
        public bool IsCurrentRule(IParserImmutableContext context)
        {
            var name = this.ReadRuleName(context.CurrentStream);

            return(DefaultParserContext.GlobalContext.UserRules.FirstOrDefault(x => x.Name == name) != default(UserRule));
            //return context.UserRules.FirstOrDefault(x => x.Name == name) != default(UserRule);
        }
Exemplo n.º 2
0
        public bool IsCurrentRule(IParserImmutableContext context)
        {
            var stream        = context.CurrentStream;
            var startPosition = stream.Position;
            var reader        = new StreamReader(stream);

            var startDeclaration = reader.Read();
            var symbol           = (char)reader.Read();
            var endDeclaration   = reader.Read();

            reader.DiscardBufferedData();
            stream.Position = startPosition;


            if (startDeclaration != StartDeclaration)
            {
                return(false);
            }

            if (symbol == StartDeclaration && symbol == EndDeclaration)
            {
                return(false);
            }

            return(endDeclaration == EndDeclaration);
        }
        public override bool IsCurrentRule(IParserImmutableContext context)
        {
            var streamStartPos = context.CurrentStream.Position;

            if (!base.IsCurrentRule(context))
            {
                return(false);
            }

            context.CurrentStream.Position += this.TerminateSymbol.Length;

            var result = false;

            try {
                result = this.ParseRightArgument(context) != null;
            }
            catch (ArgumentOutOfRangeException) {
                return(false);
            }
            finally {
                context.CurrentStream.Position = streamStartPos;
            }

            return(result);
        }
Exemplo n.º 4
0
        public IRule Parse(IParserImmutableContext context)
        {
            var stream        = context.CurrentStream;
            var startPosition = stream.Position;
            var reader        = new StreamReader(stream);

            var startDeclaration = reader.Read();
            var symbol           = (char)reader.Read();
            var endDeclaration   = reader.Read();

            reader.DiscardBufferedData();
            stream.Position = startPosition;

            if (startDeclaration != StartDeclaration)
            {
                return(null);
            }

            if (symbol == StartDeclaration && symbol == EndDeclaration)
            {
                throw new SymbolParseEmptySymbolException();
            }

            if (endDeclaration != EndDeclaration)
            {
                throw new SymbolParserTooMuchSymbolsException(symbol, (char)endDeclaration);
            }

            stream.Position += 3;

            return(new SymbolRule(symbol));
        }
        public IList <UserRule> Parse(IParserImmutableContext context)
        {
            this._context = context;
            var stream  = context.CurrentStream;
            var poition = stream.Position;
            var rules   = new List <UserRule>();

            try {
                while (true)
                {
                    var pos = stream.Position;

                    if (stream.NextWithSkipedEmpty() == '}')
                    {
                        stream.TryToSeekToNext();
                        return(rules);
                    }

                    //stream.Position = pos;

                    var parsed = this._parser.Parse(this._context) as UserRule;
                    rules.Add(parsed);
                }
            }
            catch {
                stream.Position = poition;
                throw;
            }
        }
Exemplo n.º 6
0
        public bool IsCurrentRule(IParserImmutableContext context)
        {
            var(name, rulePattern, tokenPattern) = this.TryParse(context);
            var condition = string.IsNullOrEmpty(name) && string.IsNullOrEmpty(rulePattern) &&
                            string.IsNullOrEmpty(tokenPattern);

            return(!condition);
        }
Exemplo n.º 7
0
        public bool IsValid(IParserImmutableContext context)
        {
            var stream = new MemoryStream().FromString(this.RulePattern);
            var lexer  = LexerBuilder.DefaultAstLexer;
            var tree   = new AstTree.AstTree(lexer.Parse(stream));

            this._tree = tree;

            return(this.TokenConvertionPattern.Childs.Values.Max() <= tree.NodeCount);
        }
Exemplo n.º 8
0
        public IRule Parse(IParserImmutableContext context)
        {
            var name = this.ReadRuleName(context.CurrentStream);
            var rule = DefaultParserContext.GlobalContext.UserRules.FirstOrDefault(x => x.Name == name) ?? context.UserRules.FirstOrDefault(x => x.Name == name);

            if (rule == null)
            {
                throw new BadRuleNameException(name, context);
            }
            context.CurrentStream.Position += name.Length;
            return(rule);
        }
Exemplo n.º 9
0
        private (string name, string rulePattern, string tokenPattern) TryParse(IParserImmutableContext context)
        {
            var name = this.ExecutePart(context.CurrentStream, RuleNameEndTerminator)
                       ?.TrimEnd()
                       ?.TrimStart();
            var rulePattern = this.ExecutePart(context.CurrentStream, ConvertionOperator.First(),
                                               ConvertionOperator.Last())
                              ?.TrimEnd()
                              ?.TrimStart();
            var tokenPattern = this.ExecutePart(context.CurrentStream, RuleEndTerminator)
                               ?.TrimEnd()
                               ?.TrimStart()
                               ?.Remove(0, 1);

            return(name : name, rulePattern : rulePattern, tokenPattern : tokenPattern);
        }
Exemplo n.º 10
0
        protected (IRule leftArgument, string symbol) ProcessContext(IParserImmutableContext context)
        {
            var startStreamPosition = context.CurrentStream.Position;
            var reader            = new StreamReader(context.CurrentStream);
            var terminateSequence = "";

            for (var i = 0; i < this.TerminateSymbol.Length; i++)
            {
                terminateSequence += (char)reader.Read();
            }

            reader.DiscardBufferedData();
            context.CurrentStream.Position = startStreamPosition;

            var leftArgument = context.Peek();

            return(leftArgument : leftArgument, symbol : terminateSequence);
        }
Exemplo n.º 11
0
        private void InitializeWithContext(IParserImmutableContext context)
        {
            IAstNode currentNode = null;

            foreach (var rule in context.CurrentRuleCollection)
            {
                var temp = new AstNode(rule);
                this.NodeCount += 1;
                if (currentNode != null)
                {
                    temp.Childs.Add(currentNode);
                }

                currentNode = temp;
            }

            this.Root = currentNode;
        }
Exemplo n.º 12
0
        protected virtual IRule TryParse(IParserImmutableContext context)
        {
            var(argument, symbol) = this.ProcessContext(context);

            if (!symbol.Equals(this.TerminateSymbol))
            {
                return(null);
            }

            if (argument == null)
            {
                throw new RuleParserNotExistedLeftArgumentException(context,
                                                                    $"{this.TerminateSymbol}");
            }

            context.CurrentStream.Position += this.TerminateSymbol.Length;
            context.Pop();
            return(argument);
        }
Exemplo n.º 13
0
        public override IRule Parse(IParserImmutableContext context)
        {
            var leftArgument = this.TryParse(context);

            IRule rightArgument;

            try {
                rightArgument = this.ParseRightArgument(context);
            }
            catch (ArgumentOutOfRangeException) {
                throw new CantParseRightArgumentException(this.TerminateSymbol, context);
            }


            if (leftArgument is SymbolRule left && rightArgument is SymbolRule right)
            {
                return(new RangeRule(left, right));
            }

            throw new BadArgumentInRangeRuleException(context);
        }
Exemplo n.º 14
0
        public IRule  Parse(IParserImmutableContext context)
        {
            var(name, rulePattern, tokenPattern) = this.TryParse(context);

            if (string.IsNullOrEmpty(name))
            {
                throw new UserRuleParserBadNameException();
            }

            if (string.IsNullOrEmpty(rulePattern))
            {
                throw new UserRuleParserBadRulePatternException();
            }

            if (string.IsNullOrEmpty(tokenPattern))
            {
                throw new UserRuleParserBadTokenExpressionException();
            }

            var tokenExpression = this.ParseTokenExpression(tokenPattern);

            if (tokenExpression == null)
            {
                throw new UserRuleParserBadTokenExpressionException();
            }

            var rule = new UserRule(name: name, rulePattern: rulePattern, tokenConvertionPattern: tokenExpression);

            if (!rule.IsValid(context))
            {
                throw new UserRuleParserBadTokenExpressionException();
            }

            DefaultParserContext.GlobalContext.ParsedRules.Push(rule);

            return(rule);
        }
 public LexerBadStartGroupFeclarationException(IParserImmutableContext context) => this._context = context;
 public abstract override IRule Parse(IParserImmutableContext conext);
 protected IRule ParseRightArgument(IParserImmutableContext context) => context.LexerBuilder
 .Build(context.CurrentStream)
 .ParseNextRule(context.CurrentStream);
Exemplo n.º 18
0
        public override IRule Parse(IParserImmutableContext context)
        {
            var result = this.TryParse(context);

            return(result == null ? null : new OneOrZeroRule(result));
        }
Exemplo n.º 19
0
 public AstTree(IParserImmutableContext context)
 {
     this.NodeCount = 0;
     this.InitializeWithContext(context);
 }
 public RuleParserNotExistedLeftArgumentException(IParserImmutableContext context, string ruleSymbol)
 {
     this._context    = context;
     this._ruleSymbol = ruleSymbol;
 }
Exemplo n.º 21
0
 public CantParseRightArgumentException(string ruleSymbol, IParserImmutableContext context)
 {
     this._context    = context;
     this._ruleSymbol = ruleSymbol;
 }
Exemplo n.º 22
0
 public abstract IRule Parse(IParserImmutableContext context);
Exemplo n.º 23
0
 public virtual bool IsCurrentRule(IParserImmutableContext context)
 {
     var(argument, symbol) = this.ProcessContext(context);
     return(symbol.Equals(this.TerminateSymbol) && argument != null);
 }
Exemplo n.º 24
0
 public BadRuleNameException(string name, IParserImmutableContext context)
 {
     this._context = context;
     this._name    = name;
 }
Exemplo n.º 25
0
 public BadArgumentInRangeRuleException(IParserImmutableContext context) => this._context = context;