Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to delimiter handling. #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 122 additions & 26 deletions src/PdfSharp/Pdf.IO/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,20 +247,67 @@ public Symbol ScanName()
while (true)
{
char ch = AppendAndScanNextChar();
if (IsWhiteSpace(ch) || IsDelimiter(ch) || ch == Chars.EOF)
return _symbol = Symbol.Name;

if (ch == '#')
{
ScanNextChar(true);
char[] hex = new char[2];
hex[0] = _currChar;
hex[1] = _nextChar;
ScanNextChar(true);
// TODO Check syntax
ch = (char)(ushort)int.Parse(new string(hex), NumberStyles.AllowHexSpecifier);
_currChar = ch;
}
if (ch == '#')
{
ScanNextChar(true);
char[] hex = new char[2];
hex[0] = _currChar;
hex[1] = _nextChar;
ScanNextChar(true);
// TODO Check syntax
ch = (char)(ushort)int.Parse(new string(hex), NumberStyles.AllowHexSpecifier);
_currChar = ch;
continue;
}

if (IsNameOrCommentDelimiter(ch) || ch == Chars.EOF)
{
return _symbol = Symbol.Name;
}

if (IsWhiteSpace(ch))
{
//TODO: Check that the white space is valid.
return _symbol = Symbol.Name;
}

//Handle invalid delimiters
switch (ch)
{
case '(':
//TODO: Handle invalid delimiters
return _symbol = Symbol.Name;
case ')':
//TODO: Handle invalid delimiters
return _symbol = Symbol.Name;
case '<':
//TODO: Handle invalid delimiters
return _symbol = Symbol.Name;
case '>':
//TODO: Handle invalid delimiters
return _symbol = Symbol.Name;
case '[':
//TODO: Not Complete
if (IsWhiteSpace(_nextChar) || IsDelimiter(_nextChar) || char.IsNumber(_nextChar) || _nextChar == '-' || PeekArrayKeyword())
{
return _symbol = Symbol.Name;
}
break;
case ']':
//TODO: Not Complete
if (IsWhiteSpace(_nextChar) || IsDelimiter(_nextChar) || _nextChar == Chars.EOF)
{
return _symbol = Symbol.Name;
}
break;
case '{':
//TODO: Handle invalid delimiters
return _symbol = Symbol.Name;
case '}':
//TODO: Handle invalid delimiters
return _symbol = Symbol.Name;
}
}
}

Expand Down Expand Up @@ -631,20 +678,22 @@ internal char ScanNextChar(bool handleCRLF)
// Treat single CR as LF.
_currChar = Chars.LF;
}
}
//Console.WriteLine();
}
}
//Console.Write(_currChar);
return _currChar;
}

///// <summary>
///// Resets the current token to the empty string.
///// </summary>
//void ClearToken()
//{
// _token.Length = 0;
//}
///// <summary>
///// Resets the current token to the empty string.
///// </summary>
//void ClearToken()
//{
// _token.Length = 0;
//}

bool PeekReference()
bool PeekReference()
{
// A Reference has the form "nnn mmm R". The implementation of the parser used a
// reduce/shift algorithm in the first place. But this case is the only one we need to
Expand Down Expand Up @@ -692,6 +741,39 @@ bool PeekReference()
return false;
}

bool PeekArrayKeyword()
{
StringBuilder token = _token;
int position = Position;
ScanNextChar(true);

//Pretty sure I want to skip any non white space
char ch = MoveToNonWhiteSpace();

//reset the _token
_token = new StringBuilder();

while (!IsWhiteSpace(ch) && !IsDelimiter(ch))
{
ch = AppendAndScanNextChar();
}

bool b_is_keyword = false;
switch (_token.ToString())
{
case "null":
case "true":
case "false":
b_is_keyword = true;
break;
}

Position = position;
_token = token;

return b_is_keyword;
}

/// <summary>
/// Appends current character to the token and reads next one.
/// </summary>
Expand Down Expand Up @@ -879,10 +961,24 @@ internal static bool IsDelimiter(char ch)
return false;
}

/// <summary>
/// Gets the length of the PDF output.
/// </summary>
public int PdfLength
/// <summary>
/// Indicates whether the specified character is a PDF delimiter character.
/// </summary>
internal static bool IsNameOrCommentDelimiter(char ch)
{
switch (ch)
{
case '/':
case '%':
return true;
}
return false;
}

/// <summary>
/// Gets the length of the PDF output.
/// </summary>
public int PdfLength
{
get { return _pdfLength; }
}
Expand Down
4 changes: 3 additions & 1 deletion src/PdfSharp/Pdf.IO/PdfWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ public void Write(PdfName value)
case '(':
case ')':
case '#':
break;
case '[':
case ']':
break;

default:
pdf.Append(name[idx]);
Expand Down