Skip to content

Instantly share code, notes, and snippets.

@jasondown
Created August 3, 2021 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasondown/089b60eb504d6c299ff52556b5a9ee3c to your computer and use it in GitHub Desktop.
Save jasondown/089b60eb504d6c299ff52556b5a9ee3c to your computer and use it in GitHub Desktop.
public class Parser
{
// Will cover in next blog post likely
private readonly ObjectDependencyManager _objectDependencyManager;
// All available states
private readonly ObjectHeaderState _objectHeaderState;
private readonly ObjectPropertiesState _objectPropertiesState;
private readonly PropertiesState _propertiesState;
private readonly GlobalVariablesState _globalVariablesState;
private readonly ProceduresState _proceduresState;
private readonly CommentBlockState _commentBlockState;
// tracks current state
private ParserState _currentState;
// Will cover in next blog post
private NavObjectNode _currentObject;
public Parser(ObjectDependencyManager objectDependencyManager)
{
_objectDependencyManager = objectDependencyManager;
_objectHeaderState = new ObjectHeaderState(this);
_objectPropertiesState = new ObjectPropertiesState(this);
_propertiesState = new PropertiesState(this);
_globalVariablesState = new GlobalVariablesState(this);
_propertiesState = new PropertiesState(this);
_proceduresState = new ProceduresState(this);
_commentBlockState = new CommentBlockState(this);
// start in the object header state
_currentState = ObjectHeaderState;
}
// One property to expose each state type...
// I will show first, but remaining are elided
public ObjectHeaderState ObjectHeaderState
{
get { return _objectHeaderState; }
}
// Will cover in next blog post likely
public ObjectDependencyManager ObjectDependencyManager
{
get { return _objectDependencyManager; }
}
// Will cover in next blog post
public NavObjectNode CurrentObject
{
get { return _currentObject; }
set
{
_currentObject = value;
_objectDependencyManager.AddNavObjectToCompile(value);
}
}
// the main driver...parse a file
// and the state machine cogs begin to turn
public void ParseFile(string file)
{
SetState(ObjectHeaderState);
using (var rdr = new StreamReader(file))
{
while (!rdr.EndOfStream)
{
var readLine = rdr.ReadLine();
if (readLine != null) _currentState.ReadLine(readLine.Trim());
}
}
}
// Called from within the states to change to another state
public void SetState(ParserState newState)
{
_currentState = newState;
_currentState.Reset();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment