예제 #1
0
 private string ParseDirection(SimpleCircuitLexer lexer)
 {
     if (lexer.Is(TokenType.Word))
     {
         switch (lexer.Content)
         {
         case "u":
         case "d":
         case "l":
         case "r":
             var dir = lexer.Content;
             lexer.Next();
             return(dir);
         }
     }
     if (lexer.Is(TokenType.Question))
     {
         lexer.Next();
         return("?");
     }
     throw new ParseException($"Expected a direction", lexer.Line, lexer.Position);
 }
예제 #2
0
        private List <WireDescription> ParseWire(SimpleCircuitLexer lexer)
        {
            var wires = new List <WireDescription>();

            lexer.Check(TokenType.OpenBracket, "<");
            do
            {
                var direction = ParseDirection(lexer);
                wires.Add(new WireDescription
                {
                    Direction = direction,
                    Length    = -1.0
                });
                if (lexer.Is(TokenType.Number))
                {
                    wires[wires.Count - 1].Length = double.Parse(lexer.Content, System.Globalization.CultureInfo.InvariantCulture);
                    lexer.Next();
                }
            }while (!lexer.Is(TokenType.CloseBracket, ">"));
            lexer.Next();
            return(wires);
        }
예제 #3
0
 private void ParseLines(SimpleCircuitLexer lexer, Circuit ckt)
 {
     do
     {
         ParseLine(lexer, ckt);
         if (!lexer.Is(TokenType.Newline) && !lexer.Is(TokenType.EndOfContent))
         {
             throw new ParseException($"Expected a new line", lexer.Line, lexer.Position);
         }
         while (lexer.Is(TokenType.Newline))
         {
             lexer.Next();
         }
     }while (!lexer.Is(TokenType.EndOfContent));
 }
예제 #4
0
        private IComponent ParseComponentLabel(SimpleCircuitLexer lexer, Circuit ckt)
        {
            var component = GetComponent(ParseName(lexer), ckt);

            if (lexer.Is(TokenType.OpenBracket, "("))
            {
                lexer.Next();
                var label = ParseLabel(lexer);
                if (component is ILabeled lbl)
                {
                    lbl.Label = label;
                }
                lexer.Check(TokenType.CloseBracket, ")");
            }
            return(component);
        }
예제 #5
0
        private PinDescription ParsePin(SimpleCircuitLexer lexer, Circuit ckt)
        {
            var result = new PinDescription();

            result.ComponentName = lexer.Content;
            result.Component     = ParseComponentLabel(lexer, ckt);
            if (lexer.Is(TokenType.OpenBracket, "["))
            {
                lexer.Next();
                var pin = ParseName(lexer);
                lexer.Check(TokenType.CloseBracket, "]");
                result.AfterName = pin;
                result.After     = (TranslatingPin)result.Component.Pins[pin];
            }
            return(result);
        }
예제 #6
0
        /// <summary>
        /// Parses the specified description.
        /// </summary>
        /// <param name="input">The description.</param>
        public Circuit Parse(string input)
        {
            var lexer = new SimpleCircuitLexer(input);

            _subcircuits.Clear();
            if (!lexer.Next())
            {
                return(new Circuit());
            }
            var ckt = new Circuit
            {
                Style = Style
            };

            ParseLines(lexer, ckt);
            return(ckt);
        }
예제 #7
0
        private void ParseEquation(SimpleCircuitLexer lexer, Circuit ckt)
        {
            if (!lexer.Is(TokenType.Dash))
            {
                throw new ParseException($"A dash was expected", lexer.Line, lexer.Position);
            }
            lexer.Next();

            // Add the first equation
            var a = ParseSum(lexer, ckt);

            lexer.Check(TokenType.Equals);
            var b = ParseSum(lexer, ckt);

            if (a is Function fa && b is Function fb)
            {
                ckt.Add(fa - fb, $"keep {fa} and {fb} equal");
            }
예제 #8
0
        private PinDescription ParseDoublePin(SimpleCircuitLexer lexer, Circuit ckt)
        {
            string beforePin = null;

            if (lexer.Is(TokenType.OpenBracket, "["))
            {
                lexer.Next();
                beforePin = ParseName(lexer);
                lexer.Check(TokenType.CloseBracket, "]");
            }
            var result = ParsePin(lexer, ckt);

            if (beforePin != null)
            {
                result.BeforeName = beforePin;
                result.Before     = (TranslatingPin)result.Component.Pins[beforePin];
            }
            return(result);
        }