//TODO int/no decimal option? private NumberToken scanNumericLiteral() { var text = ""; var hasDecimal = false; char c; while (_tokenizer.TryRead(out c)) { if (c == '.') { if (hasDecimal) { throw new FormatException("Numeric literal cannot contain two decimals."); } hasDecimal = true; text += c; } else if (char.IsDigit(c)) { text += c; } else if (char.IsWhiteSpace(c) || c == '\0') { break; } else { break; //throw new FormatException("Invalid character in numeric literal."); } } return(new NumberToken(text)); }
private IdentiferToken scanIdentifer() { var text = ""; var isFirst = true; char c; while (_tokenizer.TryRead(out c)) { if (isFirst) { isFirst = false; if (char.IsDigit(c)) { throw new FormatException( $"The character {c.SQuote()} is not valid. An identifier must start with a letter."); } if (char.IsLetter(c)) { text += c; } else { throw new FormatException( $"The character {c.SQuote()} is not valid. An identifier must start with a letter."); } } else { if (char.IsLetterOrDigit(c) || c == '-') { text += c; } else { break; } } } if (string.IsNullOrWhiteSpace(text)) { throw new FormatException( $"The text \"{text}\" is not a valid identifier because it is null or whitespace."); } _tokenizer.Step(-1); return(new IdentiferToken(text)); }
public TextualStyle Parse() { var textualStyle = new TextualStyle { //ForegroundDescriptor = new LiteralMaterialDescriptor(Palette.Blue.P600) }; var selectorExpressionParts = _expression.Split(' '); foreach (var selectorExpressionPart in selectorExpressionParts) { _tokenizer = new TokenizerOld(selectorExpressionPart); _tokenizer.SkipWhiteSpace(); var beginFrame = _tokenizer.GetFrame(); char c; if (!_tokenizer.TryRead(out c)) { throw new FormatException("Could not read from tokenizer."); } var cl = char.ToLower(c); if (cl == 't') { var frame = _tokenizer.GetFrame(); if (_tokenizer.HasMore()) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame) + $"Unexpected character(s) after \'t\' fontweight flag"); } textualStyle.SetValue(TextualStyle.FontWeightProperty, FontWeights.Thin); } else if (cl == 'l') { var frame = _tokenizer.GetFrame(); if (_tokenizer.HasMore()) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame) + $"Unexpected character(s) after \'l\' fontweight flag"); } textualStyle.SetValue(TextualStyle.FontWeightProperty, FontWeights.Light); } else if (cl == 'm') { var frame = _tokenizer.GetFrame(); if (_tokenizer.HasMore()) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame) + $"Unexpected character(s) after \'m\' fontweight flag"); } textualStyle.SetValue(TextualStyle.FontWeightProperty, FontWeights.Medium); } else if (cl == 'b') { var frame = _tokenizer.GetFrame(); if (_tokenizer.HasMore()) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame) + $"Unexpected character(s) after \'b\' fontweight flag"); } textualStyle.SetValue(TextualStyle.FontWeightProperty, FontWeights.Bold); } else if (cl == 'h') { var frame = _tokenizer.GetFrame(); if (_tokenizer.HasMore()) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame) + $"Unexpected character(s) after \'h\' fontweight flag"); } textualStyle.SetValue(TextualStyle.FontWeightProperty, FontWeights.Heavy); } else if (cl == 'o') { var frame = _tokenizer.GetFrame(); if (_tokenizer.HasMore()) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame) + $"Unexpected character(s) after \'o\' fontstyle flag"); } textualStyle.SetValue(TextualStyle.FontStyleProperty, FontStyles.Oblique); } else if (cl == 'i') { var frame = _tokenizer.GetFrame(); if (_tokenizer.HasMore()) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame) + $"Unexpected character(s) after \'i\' fontstyle flag"); } textualStyle.SetValue(TextualStyle.FontStyleProperty, FontStyles.Italic); } else if (c == '-' || cl == 'p' || cl == 'a') { //TODO use new method .GetInsance for all code behind uses of Singleton xamlconverters var descriptorConverter = new MaterialDescriptorConverter(); _tokenizer.Step(-1); var frame = _tokenizer.GetFrame(); var strValue = scanUntil('#', false); var value = descriptorConverter.ConvertFrom(strValue) as AbstractMaterialDescriptor; textualStyle.SetValue(TextualStyle.ForegroundDescriptorProperty, value); } // SET TEXTROTATION PROPERTY else if (cl == 'r') { TextRotation textRotation; var frame = _tokenizer.GetFrame(); char c3; if (!_tokenizer.TryRead(out c3)) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame) + $"Expected rotation direction after \'r\' character. (\'r\' or \'l\')."); } var c3l = char.ToLower(c3); if (c3l == 'r') { _tokenizer.SkipWhiteSpace(); var frame2 = _tokenizer.GetFrame(); if (_tokenizer.HasMore()) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame2) + $"Unexpected character(s) after rotation direction."); } textRotation = TextRotation.Right; } else if (c3l == 'l') { _tokenizer.SkipWhiteSpace(); var frame2 = _tokenizer.GetFrame(); if (_tokenizer.HasMore()) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame2) + $"Unexpected character(s) after rotation direction."); } textRotation = TextRotation.Left; } else if (c3l == 'r') { _tokenizer.SkipWhiteSpace(); var frame2 = _tokenizer.GetFrame(); if (_tokenizer.HasMore()) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame2) + $"Unexpected character(s) after rotation direction."); } textRotation = TextRotation.None; } else { throw new FormatException( _tokenizer.GetExceptionRangeText(frame) + $"Unexpected rotation direction. Must be either \'l\', \'n\', or \'r\'."); } textualStyle.SetValue(TextualStyle.TextRotationProperty, textRotation); } else if (char.IsDigit(c) || c == '.') { _tokenizer.Step(-1); var numericLiteral = scanNumericLiteral(); _tokenizer.Step(-1); _tokenizer.SkipWhiteSpace(); var frame3 = _tokenizer.GetFrame(); char c2; if (!_tokenizer.TryRead(out c2)) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame3) + $"Expected font size unit type (\'em\' or \'%\') after relative font size numeric literal"); } var c2l = char.ToLower(c2); if (c2l == '%') { _tokenizer.SkipWhiteSpace(); var frame2 = _tokenizer.GetFrame(); if (_tokenizer.HasMore()) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame2) + $"Unexpected character(s) after font size unit type"); } var numericValue = double.Parse(numericLiteral.LiteralValue) / 100; textualStyle.SetValue(TextualStyle.RelativeFontSizeProperty, numericValue); } else if (c2l == 'e') { char c3; if (!_tokenizer.TryRead(out c3)) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame3) + $"Expected font size unit type (\'em\' or \'%\') after relative font size numeric literal"); } var c3l = char.ToLower(c3); if (c3l == 'm') { _tokenizer.SkipWhiteSpace(); var frame2 = _tokenizer.GetFrame(); if (_tokenizer.HasMore()) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame2) + $"Unexpected character(s) after font size unit type"); } var numericValue = double.Parse(numericLiteral.LiteralValue); textualStyle.SetValue(TextualStyle.RelativeFontSizeProperty, numericValue); } else { throw new FormatException( _tokenizer.GetExceptionRangeText(frame3) + $"Unexpected font size unit type. Must be either \'em\' or \'%\'"); } } else { throw new FormatException( _tokenizer.GetExceptionRangeText(frame3) + $"Unexpected unit type. Must be either \'em\' or \'%\'"); } _tokenizer.SkipWhiteSpace(); var frame = _tokenizer.GetFrame(); if (_tokenizer.Step(1) && _tokenizer.HasMore()) { throw new FormatException( _tokenizer.GetExceptionRangeText(frame) + $"Unexpected character(s)."); } } else { throw new FormatException( _tokenizer.GetExceptionRangeText(beginFrame) + $"Invalid character(s) in textualstyle expression."); } } return(textualStyle); }