2009-01-19 23:19:33 +00:00
|
|
|
|
|
|
|
#region ================== Copyright (c) 2007 Pascal vd Heiden
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
|
|
|
|
* This program is released under GNU General Public License
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Namespaces
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Text;
|
|
|
|
using CodeImp.DoomBuilder.IO;
|
|
|
|
using CodeImp.DoomBuilder.Data;
|
|
|
|
using System.IO;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using CodeImp.DoomBuilder.Compilers;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.Decorate
|
|
|
|
{
|
|
|
|
public sealed class DecorateParser
|
|
|
|
{
|
2009-01-21 16:18:30 +00:00
|
|
|
#region ================== Delegates
|
2009-01-21 23:09:25 +00:00
|
|
|
|
|
|
|
public delegate void IncludeDelegate(DecorateParser parser, string includefile);
|
2009-01-21 16:18:30 +00:00
|
|
|
|
|
|
|
public IncludeDelegate OnInclude;
|
|
|
|
|
|
|
|
#endregion
|
2009-01-20 16:18:25 +00:00
|
|
|
|
2009-01-21 16:18:30 +00:00
|
|
|
#region ================== Constants
|
2009-01-22 15:10:41 +00:00
|
|
|
|
2009-01-20 16:18:25 +00:00
|
|
|
// Parsing
|
|
|
|
private const string WHITESPACE = "\n \t\r";
|
2009-01-22 15:10:41 +00:00
|
|
|
private const string SPECIALTOKEN = ":{}+-\n;";
|
2009-01-20 16:18:25 +00:00
|
|
|
|
2009-01-19 23:19:33 +00:00
|
|
|
#endregion
|
2009-01-20 16:18:25 +00:00
|
|
|
|
2009-01-19 23:19:33 +00:00
|
|
|
#region ================== Variables
|
2009-01-20 16:18:25 +00:00
|
|
|
|
2009-01-22 15:10:41 +00:00
|
|
|
// These are actors we want to keep
|
|
|
|
private Dictionary<string, ActorStructure> actors;
|
|
|
|
|
|
|
|
// These are all parsed actors, also those from other games
|
|
|
|
private Dictionary<string, ActorStructure> archivedactors;
|
2009-01-20 16:18:25 +00:00
|
|
|
|
|
|
|
// Input data stream
|
|
|
|
private Stream datastream;
|
2009-01-21 23:09:25 +00:00
|
|
|
private BinaryReader datareader;
|
|
|
|
private string sourcename;
|
2009-01-20 16:18:25 +00:00
|
|
|
|
|
|
|
// Error report
|
|
|
|
private int errorline;
|
|
|
|
private string errordesc;
|
2009-01-21 23:09:25 +00:00
|
|
|
private string errorsource;
|
2009-01-20 16:18:25 +00:00
|
|
|
|
2009-01-19 23:19:33 +00:00
|
|
|
#endregion
|
2009-01-20 16:18:25 +00:00
|
|
|
|
2009-01-19 23:19:33 +00:00
|
|
|
#region ================== Properties
|
2009-01-20 16:18:25 +00:00
|
|
|
|
|
|
|
internal Stream DataStream { get { return datastream; } }
|
2009-01-21 23:09:25 +00:00
|
|
|
internal BinaryReader DataReader { get { return datareader; } }
|
2009-01-22 15:10:41 +00:00
|
|
|
public ICollection<ActorStructure> Actors { get { return actors.Values; } }
|
2009-01-20 16:18:25 +00:00
|
|
|
public int ErrorLine { get { return errorline; } }
|
|
|
|
public string ErrorDescription { get { return errordesc; } }
|
2009-01-21 23:09:25 +00:00
|
|
|
public string ErrorSource { get { return errorsource; } }
|
2009-01-20 16:18:25 +00:00
|
|
|
public bool HasError { get { return (errordesc != null); } }
|
|
|
|
|
2009-01-19 23:19:33 +00:00
|
|
|
#endregion
|
2009-01-20 16:18:25 +00:00
|
|
|
|
2009-01-19 23:19:33 +00:00
|
|
|
#region ================== Constructor / Disposer
|
2009-01-20 16:18:25 +00:00
|
|
|
|
2009-01-19 23:19:33 +00:00
|
|
|
// Constructor
|
2009-01-20 16:18:25 +00:00
|
|
|
public DecorateParser()
|
2009-01-19 23:19:33 +00:00
|
|
|
{
|
2009-01-20 16:18:25 +00:00
|
|
|
// Initialize
|
2009-01-22 15:10:41 +00:00
|
|
|
actors = new Dictionary<string, ActorStructure>();
|
|
|
|
archivedactors = new Dictionary<string, ActorStructure>();
|
2009-01-20 16:18:25 +00:00
|
|
|
errordesc = null;
|
2009-01-19 23:19:33 +00:00
|
|
|
}
|
2009-01-20 16:18:25 +00:00
|
|
|
|
2009-01-19 23:19:33 +00:00
|
|
|
#endregion
|
2009-01-22 15:10:41 +00:00
|
|
|
|
|
|
|
#region ================== Parsing
|
|
|
|
|
2009-01-20 16:18:25 +00:00
|
|
|
// This parses the given decorate stream
|
|
|
|
// Returns false on errors
|
2009-01-21 23:09:25 +00:00
|
|
|
public bool Parse(Stream stream, string sourcefilename)
|
2009-01-20 16:18:25 +00:00
|
|
|
{
|
2009-01-21 16:18:30 +00:00
|
|
|
Stream localstream = stream;
|
2009-01-21 23:09:25 +00:00
|
|
|
string localsourcename = sourcefilename;
|
|
|
|
BinaryReader localreader = new BinaryReader(localstream, Encoding.ASCII);
|
2009-01-21 16:18:30 +00:00
|
|
|
datastream = localstream;
|
|
|
|
datareader = localreader;
|
2009-01-21 23:09:25 +00:00
|
|
|
sourcename = localsourcename;
|
2009-01-20 16:18:25 +00:00
|
|
|
datastream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
// Continue until at the end of the stream
|
|
|
|
while(SkipWhitespace(true))
|
|
|
|
{
|
|
|
|
// Read a token
|
|
|
|
string objdeclaration = ReadToken();
|
|
|
|
if(objdeclaration != null)
|
|
|
|
{
|
|
|
|
objdeclaration = objdeclaration.ToLowerInvariant();
|
|
|
|
if(objdeclaration == "actor")
|
|
|
|
{
|
|
|
|
// Read actor structure
|
|
|
|
ActorStructure actor = new ActorStructure(this);
|
|
|
|
if(this.HasError) break;
|
2009-01-22 15:10:41 +00:00
|
|
|
|
|
|
|
// Add the actor
|
|
|
|
archivedactors[actor.ClassName.ToLowerInvariant()] = actor;
|
|
|
|
if(actor.CheckActorSupported())
|
|
|
|
actors[actor.ClassName.ToLowerInvariant()] = actor;
|
|
|
|
|
|
|
|
// Replace an actor?
|
|
|
|
if(actor.ReplacesClass != null)
|
|
|
|
{
|
|
|
|
if(GetArchivedActorByName(actor.ReplacesClass) != null)
|
|
|
|
archivedactors[actor.ReplacesClass.ToLowerInvariant()] = actor;
|
|
|
|
else
|
|
|
|
General.WriteLogLine("WARNING: Unable to find the DECORATE class '" + actor.ReplacesClass + "' to replace, while parsing '" + actor.ClassName + "'");
|
|
|
|
|
|
|
|
if(actor.CheckActorSupported())
|
|
|
|
{
|
|
|
|
if(GetActorByName(actor.ReplacesClass) != null)
|
|
|
|
actors[actor.ReplacesClass.ToLowerInvariant()] = actor;
|
|
|
|
}
|
|
|
|
}
|
2009-01-20 16:18:25 +00:00
|
|
|
}
|
2009-01-21 16:18:30 +00:00
|
|
|
else if(objdeclaration == "#include")
|
|
|
|
{
|
|
|
|
// Include a file
|
|
|
|
SkipWhitespace(true);
|
|
|
|
string filename = ReadToken();
|
|
|
|
if(!string.IsNullOrEmpty(filename))
|
|
|
|
{
|
2009-01-21 23:09:25 +00:00
|
|
|
// Strip the quotes
|
|
|
|
filename = filename.Replace("\"", "");
|
|
|
|
|
|
|
|
// Callback to parse this file now
|
|
|
|
if(OnInclude != null) OnInclude(this, filename);
|
|
|
|
|
|
|
|
// Set our buffers back to continue parsing
|
2009-01-21 16:18:30 +00:00
|
|
|
datastream = localstream;
|
|
|
|
datareader = localreader;
|
2009-01-21 23:09:25 +00:00
|
|
|
sourcename = localsourcename;
|
2009-01-21 16:18:30 +00:00
|
|
|
if(HasError) break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ReportError("Expected file name to include");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-01-22 15:10:41 +00:00
|
|
|
else if((objdeclaration == "const") || (objdeclaration == "native"))
|
|
|
|
{
|
|
|
|
// We don't need this, ignore up to the first next ;
|
|
|
|
while(SkipWhitespace(true))
|
|
|
|
{
|
|
|
|
string t = ReadToken();
|
|
|
|
if((t == ";") || (t == null)) break;
|
|
|
|
}
|
|
|
|
}
|
2009-01-20 16:18:25 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Unknown structure!
|
2009-01-22 15:10:41 +00:00
|
|
|
// Best we can do now is just find the first { and then
|
|
|
|
// follow the scopes until the matching } is found
|
2009-01-21 23:09:25 +00:00
|
|
|
string token2;
|
|
|
|
do
|
|
|
|
{
|
2009-01-22 15:10:41 +00:00
|
|
|
if(!SkipWhitespace(true)) break;
|
2009-01-21 23:09:25 +00:00
|
|
|
token2 = ReadToken();
|
2009-01-22 15:10:41 +00:00
|
|
|
if(token2 == null) break;
|
2009-01-21 23:09:25 +00:00
|
|
|
}
|
|
|
|
while(token2 != "{");
|
|
|
|
int scopelevel = 1;
|
|
|
|
do
|
|
|
|
{
|
2009-01-22 15:10:41 +00:00
|
|
|
if(!SkipWhitespace(true)) break;
|
2009-01-21 23:09:25 +00:00
|
|
|
token2 = ReadToken();
|
2009-01-22 15:10:41 +00:00
|
|
|
if(token2 == null) break;
|
2009-01-21 23:09:25 +00:00
|
|
|
if(token2 == "{") scopelevel++;
|
|
|
|
if(token2 == "}") scopelevel--;
|
|
|
|
}
|
|
|
|
while(scopelevel > 0);
|
2009-01-20 16:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true when no errors occurred
|
|
|
|
return (errordesc == null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This returns true if the given character is whitespace
|
|
|
|
internal bool IsWhitespace(char c)
|
|
|
|
{
|
|
|
|
return (WHITESPACE.IndexOf(c) > -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This returns true if the given character is a special token
|
|
|
|
internal bool IsSpecialToken(char c)
|
|
|
|
{
|
|
|
|
return (SPECIALTOKEN.IndexOf(c) > -1);
|
|
|
|
}
|
2009-01-19 23:19:33 +00:00
|
|
|
|
2009-01-20 16:18:25 +00:00
|
|
|
// This returns true if the given character is a special token
|
|
|
|
internal bool IsSpecialToken(string s)
|
|
|
|
{
|
|
|
|
if(s.Length > 0)
|
|
|
|
return (SPECIALTOKEN.IndexOf(s[0]) > -1);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This skips whitespace on the stream, placing the read
|
|
|
|
// position right before the first non-whitespace character
|
|
|
|
// Returns false when the end of the stream is reached
|
|
|
|
internal bool SkipWhitespace(bool skipnewline)
|
|
|
|
{
|
2009-01-21 23:09:25 +00:00
|
|
|
int offset = skipnewline ? 0 : 1;
|
|
|
|
char c;
|
2009-01-20 16:18:25 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2009-01-21 23:09:25 +00:00
|
|
|
if(datastream.Position == datastream.Length) return false;
|
|
|
|
c = (char)datareader.ReadByte();
|
|
|
|
|
|
|
|
// Check if this is comment
|
|
|
|
if(c == '/')
|
|
|
|
{
|
2009-02-09 23:07:01 +00:00
|
|
|
if(datastream.Position == datastream.Length) return false;
|
2009-01-21 23:09:25 +00:00
|
|
|
char c2 = (char)datareader.ReadByte();
|
|
|
|
if(c2 == '/')
|
|
|
|
{
|
2009-02-09 22:59:43 +00:00
|
|
|
// Check if not a special comment with a token
|
2009-02-09 23:07:01 +00:00
|
|
|
if(datastream.Position == datastream.Length) return false;
|
2009-02-09 22:59:43 +00:00
|
|
|
char c3 = (char)datareader.ReadByte();
|
|
|
|
if(c3 != '$')
|
|
|
|
{
|
|
|
|
// Skip entire line
|
2009-02-09 23:07:01 +00:00
|
|
|
char c4 = ' ';
|
|
|
|
while((c4 != '\n') && (datastream.Position < datastream.Length)) { c4 = (char)datareader.ReadByte(); }
|
2009-02-09 22:59:43 +00:00
|
|
|
c = ' ';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Not a comment
|
|
|
|
c = c3;
|
|
|
|
}
|
2009-01-21 23:09:25 +00:00
|
|
|
}
|
|
|
|
else if(c2 == '*')
|
|
|
|
{
|
|
|
|
// Skip until */
|
|
|
|
char c4, c3 = '\0';
|
|
|
|
do
|
|
|
|
{
|
|
|
|
c4 = c3;
|
|
|
|
c3 = (char)datareader.ReadByte();
|
|
|
|
}
|
|
|
|
while((c4 != '*') || (c3 != '/'));
|
|
|
|
c = ' ';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Not a comment, rewind from reading c2
|
|
|
|
datastream.Seek(-1, SeekOrigin.Current);
|
|
|
|
}
|
|
|
|
}
|
2009-01-20 16:18:25 +00:00
|
|
|
}
|
2009-01-21 23:09:25 +00:00
|
|
|
while(WHITESPACE.IndexOf(c, offset) > -1);
|
2009-01-20 16:18:25 +00:00
|
|
|
|
|
|
|
// Go one character back so we can read this non-whitespace character again
|
|
|
|
datastream.Seek(-1, SeekOrigin.Current);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This reads a token (all sequential non-whitespace characters or a single character)
|
|
|
|
// Returns null when the end of the stream has been reached
|
|
|
|
internal string ReadToken()
|
|
|
|
{
|
|
|
|
string token = "";
|
2009-01-21 16:18:30 +00:00
|
|
|
bool quotedstring = false;
|
2009-02-11 10:32:06 +00:00
|
|
|
|
2009-01-20 16:18:25 +00:00
|
|
|
// Return null when the end of the stream has been reached
|
2009-01-21 23:09:25 +00:00
|
|
|
if(datastream.Position == datastream.Length) return null;
|
2009-01-20 16:18:25 +00:00
|
|
|
|
|
|
|
// Start reading
|
2009-01-21 23:09:25 +00:00
|
|
|
char c = (char)datareader.ReadByte();
|
|
|
|
while(!IsWhitespace(c) || quotedstring || IsSpecialToken(c))
|
2009-01-20 16:18:25 +00:00
|
|
|
{
|
|
|
|
// Special token?
|
2009-01-21 23:09:25 +00:00
|
|
|
if(!quotedstring && IsSpecialToken(c))
|
2009-01-20 16:18:25 +00:00
|
|
|
{
|
|
|
|
// Not reading a token yet?
|
|
|
|
if(token.Length == 0)
|
|
|
|
{
|
|
|
|
// This is our whole token
|
2009-01-21 23:09:25 +00:00
|
|
|
token += c;
|
2009-01-20 16:18:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This is a new token and shouldn't be read now
|
|
|
|
// Go one character back so we can read this token again
|
|
|
|
datastream.Seek(-1, SeekOrigin.Current);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-11 10:32:06 +00:00
|
|
|
// Quote?
|
|
|
|
if(c == '"')
|
|
|
|
{
|
|
|
|
// Quote to end the string?
|
|
|
|
if(quotedstring) quotedstring = false;
|
|
|
|
|
|
|
|
// First character is a quote?
|
|
|
|
if(token.Length == 0) quotedstring = true;
|
|
|
|
|
|
|
|
token += c;
|
|
|
|
}
|
|
|
|
// Potential comment?
|
|
|
|
else if(c == '/')
|
|
|
|
{
|
|
|
|
// Check the next byte
|
|
|
|
if(datastream.Position == datastream.Length) return token;
|
|
|
|
char c2 = (char)datareader.ReadByte();
|
|
|
|
if((c2 == '/') || (c2 == '*'))
|
|
|
|
{
|
|
|
|
// This is a comment start, so the token ends here
|
|
|
|
// Go two characters back so we can read this comment again
|
|
|
|
datastream.Seek(-2, SeekOrigin.Current);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Not a comment
|
|
|
|
// Go one character back so we can read this char again
|
|
|
|
datastream.Seek(-1, SeekOrigin.Current);
|
|
|
|
token += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
token += c;
|
|
|
|
}
|
2009-01-20 16:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next character
|
2009-01-21 23:09:25 +00:00
|
|
|
if(datastream.Position < datastream.Length)
|
|
|
|
c = (char)datareader.Read();
|
|
|
|
else
|
|
|
|
break;
|
2009-01-20 16:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return token;
|
|
|
|
}
|
2009-02-09 22:59:43 +00:00
|
|
|
|
|
|
|
// This reads the rest of the line
|
|
|
|
// Returns null when the end of the stream has been reached
|
|
|
|
internal string ReadLine()
|
|
|
|
{
|
|
|
|
string token = "";
|
|
|
|
|
|
|
|
// Return null when the end of the stream has been reached
|
|
|
|
if(datastream.Position == datastream.Length) return null;
|
|
|
|
|
|
|
|
// Start reading
|
|
|
|
char c = (char)datareader.ReadByte();
|
|
|
|
while(c != '\n')
|
|
|
|
{
|
|
|
|
token += c;
|
|
|
|
|
|
|
|
// Next character
|
|
|
|
if(datastream.Position < datastream.Length)
|
|
|
|
c = (char)datareader.Read();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return token.Trim();
|
|
|
|
}
|
2009-01-20 16:18:25 +00:00
|
|
|
|
|
|
|
// This reports an error
|
|
|
|
internal void ReportError(string message)
|
|
|
|
{
|
|
|
|
long position = datastream.Position;
|
2009-01-21 23:09:25 +00:00
|
|
|
long readpos = 0;
|
2009-01-20 16:18:25 +00:00
|
|
|
int linenumber = 1;
|
|
|
|
|
|
|
|
// Find the line on which we found this error
|
|
|
|
datastream.Seek(0, SeekOrigin.Begin);
|
2009-01-21 23:09:25 +00:00
|
|
|
StreamReader textreader = new StreamReader(datastream, Encoding.ASCII);
|
|
|
|
while(readpos < position)
|
2009-01-20 16:18:25 +00:00
|
|
|
{
|
2009-01-21 23:09:25 +00:00
|
|
|
string line = textreader.ReadLine();
|
2009-01-20 16:18:25 +00:00
|
|
|
if(line == null) break;
|
2009-01-21 23:09:25 +00:00
|
|
|
readpos += line.Length + 2;
|
2009-01-20 16:18:25 +00:00
|
|
|
linenumber++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return to original position
|
|
|
|
datastream.Seek(position, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
// Set error information
|
|
|
|
errordesc = message;
|
|
|
|
errorline = linenumber;
|
2009-01-21 23:09:25 +00:00
|
|
|
errorsource = sourcename;
|
2009-01-20 16:18:25 +00:00
|
|
|
}
|
|
|
|
|
2009-01-19 23:19:33 +00:00
|
|
|
#endregion
|
2009-01-22 15:10:41 +00:00
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
|
|
// This returns an actor by name
|
|
|
|
// Returns null when actor cannot be found
|
|
|
|
public ActorStructure GetActorByName(string name)
|
|
|
|
{
|
|
|
|
name = name.ToLowerInvariant();
|
|
|
|
if(actors.ContainsKey(name))
|
|
|
|
return actors[name];
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This returns an actor by name
|
|
|
|
// Returns null when actor cannot be found
|
|
|
|
internal ActorStructure GetArchivedActorByName(string name)
|
|
|
|
{
|
|
|
|
name = name.ToLowerInvariant();
|
|
|
|
if(archivedactors.ContainsKey(name))
|
|
|
|
return archivedactors[name];
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2009-01-19 23:19:33 +00:00
|
|
|
}
|
|
|
|
}
|