12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Text;
- using ExCSS.Model;
- using ExCSS.Model.TextBlocks;
- // ReSharper disable once CheckNamespace
- #pragma warning disable
- namespace ExCSS
- {
- sealed class Lexer
- {
- private readonly StringBuilder _buffer;
- private readonly StylesheetReader _stylesheetReader;
- private bool _ignoreWhitespace;
- private bool _ignoreComments;
- internal Action<ParserError, string> ErrorHandler { get; set; }
- internal Lexer(StylesheetReader source)
- {
- _buffer = new StringBuilder();
- _stylesheetReader = source;
- ErrorHandler = (err, msg) => { };
- }
- private Block DataBlock(char current)
- {
- switch (current)
- {
- case Specification.LineFeed:
- case Specification.CarriageReturn:
- case Specification.Tab:
- case Specification.Space:
- do
- {
- current = _stylesheetReader.Next;
- }
- while (current.IsSpaceCharacter());
- if (_ignoreWhitespace)
- {
- return DataBlock(current);
- }
- _stylesheetReader.Back();
- return SpecialCharacter.Whitespace;
- case Specification.DoubleQuote:
- return DoubleQuoteString(_stylesheetReader.Next);
- case Specification.Hash:
- return HashStart(_stylesheetReader.Next);
- case Specification.DollarSign:
- current = _stylesheetReader.Next;
- return current == Specification.EqualSign
- ? MatchBlock.Suffix
- : Block.Delim(_stylesheetReader.Previous);
- case Specification.SingleQuote:
- return SingleQuoteString(_stylesheetReader.Next);
- case Specification.ParenOpen:
- return BracketBlock.OpenRound;
- case Specification.ParenClose:
- return BracketBlock.CloseRound;
- case Specification.Asterisk:
- current = _stylesheetReader.Next;
- return current == Specification.EqualSign
- ? MatchBlock.Substring
- : Block.Delim(_stylesheetReader.Previous);
- case Specification.PlusSign:
- {
- var nextFirst = _stylesheetReader.Next;
- if (nextFirst == Specification.EndOfFile)
- {
- _stylesheetReader.Back();
- }
- else
- {
- var nextSEcond = _stylesheetReader.Next;
- _stylesheetReader.Back(2);
- if (nextFirst.IsDigit() || (nextFirst == Specification.Period && nextSEcond.IsDigit()))
- {
- return NumberStart(current);
- }
- }
- return Block.Delim(current);
- }
- case Specification.Comma:
- return SpecialCharacter.Comma;
- case Specification.Period:
- {
- var c = _stylesheetReader.Next;
- return c.IsDigit()
- ? NumberStart(_stylesheetReader.Previous)
- : Block.Delim(_stylesheetReader.Previous);
- }
- case Specification.MinusSign:
- {
- var nextFirst = _stylesheetReader.Next;
- if (nextFirst == Specification.EndOfFile)
- {
- _stylesheetReader.Back();
- }
- else
- {
- var nextSecond = _stylesheetReader.Next;
- _stylesheetReader.Back(2);
- if (nextFirst.IsDigit() || (nextFirst == Specification.Period && nextSecond.IsDigit()))
- {
- return NumberStart(current);
- }
- if (nextFirst.IsNameStart())
- {
- return IdentStart(current);
- }
- if (nextFirst == Specification.ReverseSolidus && !nextSecond.IsLineBreak() && nextSecond != Specification.EndOfFile)
- {
- return IdentStart(current);
- }
- if (nextFirst != Specification.MinusSign || nextSecond != Specification.GreaterThan)
- {
- return Block.Delim(current);
- }
- _stylesheetReader.Advance(2);
- return _ignoreComments
- ? DataBlock(_stylesheetReader.Next)
- : CommentBlock.Close;
- }
- return Block.Delim(current);
- }
- case Specification.Solidus:
- current = _stylesheetReader.Next;
- return current == Specification.Asterisk
- ? Comment(_stylesheetReader.Next)
- : Block.Delim(_stylesheetReader.Previous);
- case Specification.ReverseSolidus:
- current = _stylesheetReader.Next;
- if (current.IsLineBreak() || current == Specification.EndOfFile)
- {
- ErrorHandler(current == Specification.EndOfFile
- ? ParserError.EndOfFile
- : ParserError.UnexpectedLineBreak,
- ErrorMessages.LineBreakEof);
- return Block.Delim(_stylesheetReader.Previous);
- }
- return IdentStart(_stylesheetReader.Previous);
- case Specification.Colon:
- return SpecialCharacter.Colon;
- case Specification.Simicolon:
- return SpecialCharacter.Semicolon;
- case Specification.LessThan:
- current = _stylesheetReader.Next;
- if (current == Specification.Em)
- {
- current = _stylesheetReader.Next;
- if (current == Specification.MinusSign)
- {
- current = _stylesheetReader.Next;
- if (current == Specification.MinusSign)
- {
- return _ignoreComments
- ? DataBlock(_stylesheetReader.Next)
- : CommentBlock.Open;
- }
- current = _stylesheetReader.Previous;
- }
- current = _stylesheetReader.Previous;
- }
- return Block.Delim(_stylesheetReader.Previous);
- case Specification.At:
- return AtKeywordStart(_stylesheetReader.Next);
- case Specification.SquareBracketOpen:
- return BracketBlock.OpenSquare;
- case Specification.SquareBracketClose:
- return BracketBlock.CloseSquare;
- case Specification.Accent:
- current = _stylesheetReader.Next;
- return current == Specification.EqualSign
- ? MatchBlock.Prefix
- : Block.Delim(_stylesheetReader.Previous);
- case Specification.CurlyBraceOpen:
- return BracketBlock.OpenCurly;
- case Specification.CurlyBraceClose:
- return BracketBlock.CloseCurly;
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- return NumberStart(current);
- case 'U':
- case 'u':
- current = _stylesheetReader.Next;
- if (current == Specification.PlusSign)
- {
- current = _stylesheetReader.Next;
- if (current.IsHex() || current == Specification.QuestionMark)
- return UnicodeRange(current);
- current = _stylesheetReader.Previous;
- }
- return IdentStart(_stylesheetReader.Previous);
- case Specification.Pipe:
- current = _stylesheetReader.Next;
- if (current == Specification.EqualSign)
- {
- return MatchBlock.Dash;
- }
- if (current == Specification.Pipe)
- {
- return Block.Column;
- }
- return Block.Delim(_stylesheetReader.Previous);
- case Specification.Tilde:
- current = _stylesheetReader.Next;
- if (current == Specification.EqualSign)
- {
- return MatchBlock.Include;
- }
- return Block.Delim(_stylesheetReader.Previous);
- case Specification.EndOfFile:
- return null;
- case Specification.Em:
- current = _stylesheetReader.Next;
- return current == Specification.EqualSign
- ? MatchBlock.Not
- : Block.Delim(_stylesheetReader.Previous);
- default:
- return current.IsNameStart()
- ? IdentStart(current)
- : Block.Delim(current);
- }
- }
- private Block DoubleQuoteString(char current)
- {
- while (true)
- {
- switch (current)
- {
- case Specification.DoubleQuote:
- case Specification.EndOfFile:
- return StringBlock.Plain(FlushBuffer());
- case Specification.FormFeed:
- case Specification.LineFeed:
- ErrorHandler(ParserError.UnexpectedLineBreak, ErrorMessages.DoubleQuotedString);
- _stylesheetReader.Back();
- return StringBlock.Plain(FlushBuffer(), true);
- case Specification.ReverseSolidus:
- current = _stylesheetReader.Next;
- if (current.IsLineBreak())
- {
- _buffer.AppendLine();
- }
- else if (current != Specification.EndOfFile)
- {
- _buffer.Append(ConsumeEscape(current));
- }
- else
- {
- ErrorHandler(ParserError.EndOfFile, ErrorMessages.DoubleQuotedStringEof);
- _stylesheetReader.Back();
- return StringBlock.Plain(FlushBuffer(), true);
- }
- break;
- default:
- _buffer.Append(current);
- break;
- }
- current = _stylesheetReader.Next;
- }
- }
- private Block SingleQuoteString(char current)
- {
- while (true)
- {
- switch (current)
- {
- case Specification.SingleQuote:
- case Specification.EndOfFile:
- return StringBlock.Plain(FlushBuffer());
- case Specification.FormFeed:
- case Specification.LineFeed:
- ErrorHandler(ParserError.UnexpectedLineBreak, ErrorMessages.SingleQuotedString);
- _stylesheetReader.Back();
- return (StringBlock.Plain(FlushBuffer(), true));
- case Specification.ReverseSolidus:
- current = _stylesheetReader.Next;
- if (current.IsLineBreak())
- {
- _buffer.AppendLine();
- }
- else if (current != Specification.EndOfFile)
- {
- _buffer.Append(ConsumeEscape(current));
- }
- else
- {
- ErrorHandler(ParserError.EndOfFile, ErrorMessages.SingleQuotedStringEof);
- _stylesheetReader.Back();
- return (StringBlock.Plain(FlushBuffer(), true));
- }
- break;
- default:
- _buffer.Append(current);
- break;
- }
- current = _stylesheetReader.Next;
- }
- }
- private Block HashStart(char current)
- {
- if (current.IsNameStart())
- {
- _buffer.Append(current);
- return HashRest(_stylesheetReader.Next);
- }
- if (IsValidEscape(current))
- {
- current = _stylesheetReader.Next;
- _buffer.Append(ConsumeEscape(current));
- return HashRest(_stylesheetReader.Next);
- }
- if (current != Specification.ReverseSolidus)
- {
- _stylesheetReader.Back();
- return Block.Delim(Specification.Hash);
- }
- ErrorHandler(ParserError.InvalidCharacter, ErrorMessages.InvalidCharacterAfterHash);
- return Block.Delim(Specification.Hash);
- }
- private Block HashRest(char current)
- {
- while (true)
- {
- if (current.IsName())
- {
- _buffer.Append(current);
- }
- else if (IsValidEscape(current))
- {
- current = _stylesheetReader.Next;
- _buffer.Append(ConsumeEscape(current));
- }
- else if (current == Specification.ReverseSolidus)
- {
- ErrorHandler(ParserError.InvalidCharacter, ErrorMessages.InvalidCharacterAfterHash);
- _stylesheetReader.Back();
- return SymbolBlock.Hash(FlushBuffer());
- }
- else
- {
- _stylesheetReader.Back();
- return SymbolBlock.Hash(FlushBuffer());
- }
- current = _stylesheetReader.Next;
- }
- }
- private Block Comment(char current)
- {
- while (true)
- {
- switch (current)
- {
- case Specification.Asterisk:
- current = _stylesheetReader.Next;
- if (current == Specification.Solidus)
- {
- return DataBlock(_stylesheetReader.Next);
- }
- break;
- case Specification.Solidus:
- {
- if (_stylesheetReader.Previous == Specification.Asterisk)
- {
- return DataBlock(_stylesheetReader.Next);
- }
- current = _stylesheetReader.Next;
- break;
- }
- case Specification.EndOfFile:
- ErrorHandler(ParserError.EndOfFile, ErrorMessages.ExpectedCommentEnd);
- return DataBlock(current);
- }
- current = _stylesheetReader.Next;
- }
- }
- private Block AtKeywordStart(char current)
- {
- if (current == Specification.MinusSign)
- {
- current = _stylesheetReader.Next;
- if (current.IsNameStart() || IsValidEscape(current))
- {
- _buffer.Append(Specification.MinusSign);
- return AtKeywordRest(current);
- }
- _stylesheetReader.Back(2);
- return Block.Delim(Specification.At);
- }
- if (current.IsNameStart())
- {
- _buffer.Append(current);
- return AtKeywordRest(_stylesheetReader.Next);
- }
- if (IsValidEscape(current))
- {
- current = _stylesheetReader.Next;
- _buffer.Append(ConsumeEscape(current));
- return AtKeywordRest(_stylesheetReader.Next);
- }
- _stylesheetReader.Back();
- return Block.Delim(Specification.At);
- }
- private Block AtKeywordRest(char current)
- {
- while (true)
- {
- if (current.IsName())
- {
- _buffer.Append(current);
- }
- else if (IsValidEscape(current))
- {
- current = _stylesheetReader.Next;
- _buffer.Append(ConsumeEscape(current));
- }
- else
- {
- _stylesheetReader.Back();
- return SymbolBlock.At(FlushBuffer());
- }
- current = _stylesheetReader.Next;
- }
- }
- private Block IdentStart(char current)
- {
- if (current == Specification.MinusSign)
- {
- current = _stylesheetReader.Next;
- if (current.IsNameStart() || IsValidEscape(current))
- {
- _buffer.Append(Specification.MinusSign);
- return IdentRest(current);
- }
- _stylesheetReader.Back();
- return Block.Delim(Specification.MinusSign);
- }
- if (current.IsNameStart())
- {
- _buffer.Append(current);
- return IdentRest(_stylesheetReader.Next);
- }
- if (current != Specification.ReverseSolidus)
- {
- return DataBlock(current);
- }
- if (!IsValidEscape(current))
- {
- return DataBlock(current);
- }
- current = _stylesheetReader.Next;
- _buffer.Append(ConsumeEscape(current));
- return IdentRest(_stylesheetReader.Next);
- }
- private Block IdentRest(char current)
- {
- while (true)
- {
- if (current.IsName())
- {
- _buffer.Append(current);
- }
- else if (IsValidEscape(current))
- {
- current = _stylesheetReader.Next;
- _buffer.Append(ConsumeEscape(current));
- }
- else if (current == Specification.ParenOpen)
- {
- switch (_buffer.ToString().ToLower())
- {
- case "url":
- _buffer.Length = 0;
- return UrlStart(_stylesheetReader.Next);//, GrammarSegment.Url);
- case "domain":
- _buffer.Length = 0;
- return UrlStart(_stylesheetReader.Next);//, GrammarSegment.Domain);
- case "url-prefix":
- _buffer.Length = 0;
- return UrlStart(_stylesheetReader.Next);//, GrammarSegment.UrlPrefix);
- default:
- return SymbolBlock.Function(FlushBuffer());
- }
- }
- else
- {
- _stylesheetReader.Back();
- return SymbolBlock.Ident(FlushBuffer());
- }
- current = _stylesheetReader.Next;
- }
- }
- private Block NumberStart(char current)
- {
- while (true)
- {
- switch (current)
- {
- case Specification.MinusSign:
- case Specification.PlusSign:
- _buffer.Append(current);
- current = _stylesheetReader.Next;
- if (current == Specification.Period)
- {
- _buffer.Append(current);
- _buffer.Append(_stylesheetReader.Next);
- return NumberFraction(_stylesheetReader.Next);
- }
- _buffer.Append(current);
- return NumberRest(_stylesheetReader.Next);
- case Specification.Period:
- _buffer.Append(current);
- _buffer.Append(_stylesheetReader.Next);
- return NumberFraction(_stylesheetReader.Next);
- default:
- if (current.IsDigit())
- {
- _buffer.Append(current);
- return NumberRest(_stylesheetReader.Next);
- }
- break;
- }
- current = _stylesheetReader.Next;
- }
- }
- private Block NumberRest(char current)
- {
- while (true)
- {
- if (current.IsDigit())
- {
- _buffer.Append(current);
- }
- else if (current.IsNameStart())
- {
- var number = FlushBuffer();
- _buffer.Append(current);
- return Dimension(_stylesheetReader.Next, number);
- }
- else if (IsValidEscape(current))
- {
- current = _stylesheetReader.Next;
- var number = FlushBuffer();
- _buffer.Append(ConsumeEscape(current));
- return Dimension(_stylesheetReader.Next, number);
- }
- else
- {
- break;
- }
- current = _stylesheetReader.Next;
- }
- switch (current)
- {
- case Specification.Period:
- current = _stylesheetReader.Next;
- if (current.IsDigit())
- {
- _buffer.Append(Specification.Period).Append(current);
- return NumberFraction(_stylesheetReader.Next);
- }
- _stylesheetReader.Back();
- return Block.Number(FlushBuffer());
- case '%':
- return UnitBlock.Percentage(FlushBuffer());
- case 'e':
- case 'E':
- return NumberExponential(current);
- case Specification.MinusSign:
- return NumberDash(current);
- default:
- _stylesheetReader.Back();
- return Block.Number(FlushBuffer());
- }
- }
- private Block NumberFraction(char current)
- {
- while (true)
- {
- if (current.IsDigit())
- {
- _buffer.Append(current);
- }
- else if ("eE%-".IndexOf(current) >= 0)
- {
- break;
- }
- else if (current.IsNameStart())
- {
- var number = FlushBuffer();
- _buffer.Append(current);
- return Dimension(_stylesheetReader.Next, number);
- }
- else if (IsValidEscape(current))
- {
- current = _stylesheetReader.Next;
- var number = FlushBuffer();
- _buffer.Append(ConsumeEscape(current));
- return Dimension(_stylesheetReader.Next, number);
- }
- else
- {
- break;
- }
- current = _stylesheetReader.Next;
- }
- switch (current)
- {
- case 'e':
- case 'E':
- return NumberExponential(current);
- case '%':
- return UnitBlock.Percentage(FlushBuffer());
- case Specification.MinusSign:
- return NumberDash(current);
- default:
- _stylesheetReader.Back();
- return Block.Number(FlushBuffer());
- }
- }
- private Block Dimension(char current, string number)
- {
- while (true)
- {
- if (current.IsName())
- {
- _buffer.Append(current);
- }
- else if (IsValidEscape(current))
- {
- current = _stylesheetReader.Next;
- _buffer.Append(ConsumeEscape(current));
- }
- else
- {
- _stylesheetReader.Back();
- return UnitBlock.Dimension(number, FlushBuffer());
- }
- current = _stylesheetReader.Next;
- }
- }
- private Block SciNotation(char current)
- {
- while (true)
- {
- if (current.IsDigit())
- {
- _buffer.Append(current);
- }
- else
- {
- _stylesheetReader.Back();
- return Block.Number(FlushBuffer());
- }
- current = _stylesheetReader.Next;
- }
- }
- private Block UrlStart(char current)
- {
- while (current.IsSpaceCharacter())
- {
- current = _stylesheetReader.Next;
- }
- switch (current)
- {
- case Specification.EndOfFile:
- ErrorHandler(ParserError.EndOfFile, ErrorMessages.InvalidUrlEnd);
- return StringBlock.Url(string.Empty, true);
- case Specification.DoubleQuote:
- return DoubleQuotedUrl(_stylesheetReader.Next);
- case Specification.SingleQuote:
- return SingleQuoteUrl(_stylesheetReader.Next);
- case ')':
- return StringBlock.Url(string.Empty);
- default:
- return UnquotedUrl(current);
- }
- }
- private Block DoubleQuotedUrl(char current)
- {
- while (true)
- {
- if (current.IsLineBreak())
- {
- ErrorHandler(ParserError.UnexpectedLineBreak, ErrorMessages.InvalidUrlEnd);
- return BadUrl(_stylesheetReader.Next);
- }
- if (Specification.EndOfFile == current)
- {
- return StringBlock.Url(FlushBuffer());
- }
- if (current == Specification.DoubleQuote)
- {
- return UrlEnd(_stylesheetReader.Next);
- }
- if (current == Specification.ReverseSolidus)
- {
- current = _stylesheetReader.Next;
- if (current == Specification.EndOfFile)
- {
- _stylesheetReader.Back(2);
- ErrorHandler(ParserError.EndOfFile, ErrorMessages.InvalidUrlEnd);
- return StringBlock.Url(FlushBuffer(), true);
- }
- if (current.IsLineBreak())
- {
- _buffer.AppendLine();
- }
- else
- {
- _buffer.Append(ConsumeEscape(current));
- }
- }
- else
- {
- _buffer.Append(current);
- }
- current = _stylesheetReader.Next;
- }
- }
- private Block SingleQuoteUrl(char current)
- {
- while (true)
- {
- if (current.IsLineBreak())
- {
- ErrorHandler(ParserError.UnexpectedLineBreak, ErrorMessages.SingleQuotedString);
- return BadUrl(_stylesheetReader.Next);
- }
- if (Specification.EndOfFile == current)
- {
- return StringBlock.Url(FlushBuffer());
- }
- if (current == Specification.SingleQuote)
- {
- return UrlEnd(_stylesheetReader.Next);
- }
- if (current == Specification.ReverseSolidus)
- {
- current = _stylesheetReader.Next;
- if (current == Specification.EndOfFile)
- {
- _stylesheetReader.Back(2);
- ErrorHandler(ParserError.EndOfFile, ErrorMessages.SingleQuotedString);
- return StringBlock.Url(FlushBuffer(), true);
- }
- if (current.IsLineBreak())
- {
- _buffer.AppendLine();
- }
- else
- {
- _buffer.Append(ConsumeEscape(current));
- }
- }
- else
- {
- _buffer.Append(current);
- }
- current = _stylesheetReader.Next;
- }
- }
- private Block UnquotedUrl(char current)
- {
- while (true)
- {
- if (current.IsSpaceCharacter())
- {
- return UrlEnd(_stylesheetReader.Next);
- }
- if (current == Specification.ParenClose || current == Specification.EndOfFile)
- {
- return StringBlock.Url(FlushBuffer());
- }
- if (current == Specification.DoubleQuote || current == Specification.SingleQuote ||
- current == Specification.ParenOpen || current.IsNonPrintable())
- {
- ErrorHandler(ParserError.InvalidCharacter, ErrorMessages.InvalidUrlQuote);
- return BadUrl(_stylesheetReader.Next);
- }
- if (current == Specification.ReverseSolidus)
- {
- if (IsValidEscape(current))
- {
- current = _stylesheetReader.Next;
- _buffer.Append(ConsumeEscape(current));
- }
- else
- {
- ErrorHandler(ParserError.InvalidCharacter, ErrorMessages.InvalidUrlCharacter);
- return BadUrl(_stylesheetReader.Next);
- }
- }
- else
- {
- _buffer.Append(current);
- }
- current = _stylesheetReader.Next;
- }
- }
- private Block UrlEnd(char current)
- {
- while (true)
- {
- if (current == Specification.ParenClose)
- {
- return StringBlock.Url(FlushBuffer());
- }
- if (!current.IsSpaceCharacter())
- {
- ErrorHandler(ParserError.InvalidCharacter, ErrorMessages.InvalidUrlCharacter);
- return BadUrl(current);
- }
- current = _stylesheetReader.Next;
- }
- }
- private Block BadUrl(char current)
- {
- while (true)
- {
- if (current == Specification.EndOfFile)
- {
- ErrorHandler(ParserError.EndOfFile, ErrorMessages.InvalidUrlEnd);
- return StringBlock.Url(FlushBuffer(), true);
- }
- if (current == Specification.ParenClose)
- {
- return StringBlock.Url(FlushBuffer(), true);
- }
- if (IsValidEscape(current))
- {
- current = _stylesheetReader.Next;
- _buffer.Append(ConsumeEscape(current));
- }
- current = _stylesheetReader.Next;
- }
- }
- private Block UnicodeRange(char current)
- {
- for (var i = 0; i < 6; i++)
- {
- if (!current.IsHex())
- {
- break;
- }
- _buffer.Append(current);
- current = _stylesheetReader.Next;
- }
- if (_buffer.Length != 6)
- {
- for (var i = 0; i < 6 - _buffer.Length; i++)
- {
- if (current != Specification.QuestionMark)
- {
- current = _stylesheetReader.Previous;
- break;
- }
- _buffer.Append(current);
- current = _stylesheetReader.Next;
- }
- var range = FlushBuffer();
- var start = range.Replace(Specification.QuestionMark, '0');
- var end = range.Replace(Specification.QuestionMark, 'F');
- return Block.Range(start, end);
- }
- if (current == Specification.MinusSign)
- {
- current = _stylesheetReader.Next;
- if (current.IsHex())
- {
- var start = _buffer.ToString();
- _buffer.Length = 0;
- for (var i = 0; i < 6; i++)
- {
- if (!current.IsHex())
- {
- current = _stylesheetReader.Previous;
- break;
- }
- _buffer.Append(current);
- current = _stylesheetReader.Next;
- }
- var end = FlushBuffer();
- return Block.Range(start, end);
- }
- _stylesheetReader.Back(2);
- return Block.Range(FlushBuffer(), null);
- }
- _stylesheetReader.Back();
- return Block.Range(FlushBuffer(), null);
- }
- private string FlushBuffer()
- {
- var value = _buffer.ToString();
- _buffer.Length = 0;
- return value;
- }
- private Block NumberExponential(char current)
- {
- current = _stylesheetReader.Next;
- if (current.IsDigit())
- {
- _buffer.Append('e').Append(current);
- return SciNotation(_stylesheetReader.Next);
- }
- if (current == Specification.PlusSign || current == Specification.MinusSign)
- {
- var op = current;
- current = _stylesheetReader.Next;
- if (current.IsDigit())
- {
- _buffer.Append('e').Append(op).Append(current);
- return SciNotation(_stylesheetReader.Next);
- }
- _stylesheetReader.Back();
- }
- current = _stylesheetReader.Previous;
- var number = FlushBuffer();
- _buffer.Append(current);
- return Dimension(_stylesheetReader.Next, number);
- }
- private Block NumberDash(char current)
- {
- current = _stylesheetReader.Next;
- if (current.IsNameStart())
- {
- var number = FlushBuffer();
- _buffer.Append(Specification.MinusSign).Append(current);
- return Dimension(_stylesheetReader.Next, number);
- }
- if (IsValidEscape(current))
- {
- current = _stylesheetReader.Next;
- var number = FlushBuffer();
- _buffer.Append(Specification.MinusSign).Append(ConsumeEscape(current));
- return Dimension(_stylesheetReader.Next, number);
- }
- _stylesheetReader.Back(2);
- return Block.Number(FlushBuffer());
- }
- private string ConsumeEscape(char current)
- {
- if (!current.IsHex())
- {
- return current.ToString(CultureInfo.InvariantCulture);
- }
- var escape = new List<Char>();
- for (var i = 0; i < 6; i++)
- {
- escape.Add(current);
- current = _stylesheetReader.Next;
- if (!current.IsHex())
- {
- break;
- }
- }
- current = _stylesheetReader.Previous;
- var code = int.Parse(new string(escape.ToArray()), NumberStyles.HexNumber);
- return Char.ConvertFromUtf32(code);
- }
- private bool IsValidEscape(char current)
- {
- if (current != Specification.ReverseSolidus)
- {
- return false;
- }
- current = _stylesheetReader.Next;
- _stylesheetReader.Back();
- if (current == Specification.EndOfFile)
- {
- return false;
- }
- return !current.IsLineBreak();
- }
- internal bool IgnoreWhitespace
- {
- get { return _ignoreWhitespace; }
- set { _ignoreWhitespace = value; }
- }
- internal bool IgnoreComments
- {
- get { return _ignoreComments; }
- set { _ignoreComments = value; }
- }
- internal StylesheetReader Stream
- {
- get { return _stylesheetReader; }
- }
- internal IEnumerable<Block> Tokens
- {
- get
- {
- while (true)
- {
- var token = DataBlock(_stylesheetReader.Current);
- if (token == null)
- {
- yield break;
- }
- _stylesheetReader.Advance();
- yield return token;
- }
- }
- }
- }
- }
- #pragma warning restore
|