コード例 #1
0
        private bool _disposedValue; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposedValue)
            {
                if (disposing)
                {
                    _writer.Dispose();
                    _stream.Dispose();
                    _location = null;
                }
                _disposedValue = true;
            }
        }
コード例 #2
0
 public FileWriter(string fileName)
 {
     _location = new EntryLocation()
     {
         FileName = Path.GetFullPath(fileName)
     };
     if (File.Exists(fileName))
     {
         var oldFileName = $"{fileName}.old";
         if (File.Exists(oldFileName))
         {
             File.Delete(oldFileName);
         }
         File.Move(fileName, oldFileName);
     }
     _stream = new FileStream(fileName, FileMode.CreateNew);
     _writer = new StreamWriter(_stream);
 }
コード例 #3
0
        /// <summary>
        /// Create a file reader
        /// </summary>
        /// <param name="fileName">file name</param>
        /// <param name="tabWidth">tab width, using for replace tab with whitespace</param>
        public FileReader(string fileName, int tabWidth = 4)
        {
            if (tabWidth > 0)
            {
                var sb = new StringBuilder();
                for (var i = 0; i < tabWidth; i++)
                {
                    sb.Append(" ");
                }
                _tabHolder = sb.ToString();
                TabWidth   = tabWidth;
            }

            _location = new EntryLocation()
            {
                FileName = Path.GetFullPath(fileName)
            };
            LastLine = null;

            _stream = new FileStream(fileName, FileMode.Open);
            _reader = new StreamReader(_stream);
        }
コード例 #4
0
        public static Expression ConvertToExpression(string src, HashSet <MenuEntry> entries,
                                                     EntryLocation location, out List <MenuEntry> dependsOn)
        {
            // using "[number]" to replace all symbols,
            // using "{number}" to replace nest atomic expression

            dependsOn = null;
            if (string.IsNullOrEmpty(src))
            {
                dependsOn = new List <MenuEntry>();
                return(null);
            }
            var symbols = new List <MenuEntry>();
            // three constant expression
            var exprs = new List <Expression>
            {
                ExprN, //id = 0
                ExprM, //id = 1
                ExprY, //id = 2
            };

            try
            {
                // add all string's as constant symbol, and mark as "[id]" in source
                var srcTemp = FindStringRegex.Replace(src, match =>
                {
                    if (match.Groups["markL"].Value != match.Groups["markR"].Value)
                    {
                        throw new Exception($"Quotes mark are not in pairs. source = {src}.");
                    }

                    var str = match.Groups["string"].Value;
                    var id  = GetIndexOfEntry(symbols, str, true);
                    if (id >= 0)
                    {
                        return($"[{id}]");
                    }

                    var entry = new MenuEntry()
                    {
                        EntryType = MenuEntryType.Config,
                        Value     = str,
                        IsConst   = true, // constant for strings
                    };
                    entry.Attributes.Add(new MenuAttribute()
                    {
                        AttributeType  = MenuAttributeType.ValueType,
                        ExpressionType = MenuAttributeType.String
                    });
                    symbols.Add(entry);

                    return($"[{symbols.Count - 1}]");
                });
                // storage constant symbol count
                var constSymbolCopunt = symbols.Count;

                // add all no-constant symbols, and mark as "[id]" in source
                srcTemp = FindSymbolRegex.Replace(srcTemp, match =>
                {
                    var name = match.Groups["name"].Value;
                    // replace "n" "m" "y" with constant expression "{0}" "{1}" "{2}"
                    if (IsTristateRegex.IsMatch(name))
                    {
                        Enum.TryParse(name, true, out TristateValue type);
                        return($"{{{(int)type}}}");
                    }
                    // if we have add the same symbol, just insert the "[id]"
                    var id = GetIndexOfEntry(symbols, name);
                    if (id >= 0)
                    {
                        return($"[{id}]");
                    }

                    var entry = entries.FirstOrDefault(menuEntry => menuEntry.Name == name);
                    if (entry == null)
                    {
                        throw new Exception($"Entry do not exist, entry name = {name}");
                    }
                    symbols.Add(entry);
                    return($"[{symbols.Count - 1}]");
                });

                // string to expression
                // NestExpressionRegex will find atomic expression with "()" at the outside.
                while (NestExpressionRegex.IsMatch(srcTemp))
                {
                    // replace "([n]\{n} op [m]\{m})" to "{x}"
                    srcTemp = NestExpressionRegex.Replace(srcTemp, matchNest =>
                    {
                        var exprStr = matchNest.Groups["expr"].Value;
                        return(CreateFlatExpression(exprStr, exprs, symbols));
                    });
                }
                // replace "[n]\{n} op [m]\{m}" to "{x}"
                srcTemp = CreateFlatExpression(srcTemp, exprs, symbols);

                // generate final expression, here the string should only have one "[number]" or one "{number}".
                Expression result     = null;
                var        matchIndex = FindIndexRegex.Match(srcTemp);

                if (!string.IsNullOrEmpty(matchIndex.Groups["expr"].Value))
                {
                    result = exprs[int.Parse(matchIndex.Groups["expr"].Value)];
                }
                else if (!string.IsNullOrEmpty(matchIndex.Groups["symbol"].Value))
                {
                    // create expression here when string only has one symbol
                    result = new Expression()
                    {
                        Type  = ExpressionType.None,
                        Right = new ExpressionData(
                            symbols[int.Parse(matchIndex.Groups["symbol"].Value)])
                    }
                }
                ;

                // depends on are the no constant symbols
                dependsOn = symbols.GetRange(constSymbolCopunt,
                                             symbols.Count - constSymbolCopunt);
                symbols.Clear();
                exprs.Clear();

                return(result);
            }
            catch (Exception ex)
            {
                // if depends on symbol do not exist, set expression as null, and depends on list as empty.
                Console.WriteLine($"{ex.Message}. {location}", Brushes.Red);
                dependsOn = new List <MenuEntry>();
                return(null);
            }
        }
コード例 #5
0
 public ParseException(string message,
                       Exception innerException, EntryLocation location)
     : base(message, innerException)
 {
     Location = location;
 }
コード例 #6
0
 public ParseException(EntryLocation location)
 {
     Location = location;
 }