Skip to content

qstokkink/EasyParseMachine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


EasyParseMachine (EPM)


Introduction

Over the years I have created many custom file formats which were read using a java.util.Scanner. Now, since I did not want to create a formal grammar just for a simple file format and also wanted more flexibility than a simple scanner could give me: I created EasyParseMachine.

You can think of EPM as a parse tree with local states.

See JSONReader for a complete example on how EPM can be used.

State transitions

There exist 7 state transitions, as specified in the following table and explained in their corresponding subsections.

TransitionNext stateConsume input?
AcceptreturnNo
ClosurereturnYes
ConsumeselfYes
Fail--
Gotoas specifiedNo
Guessas specifiedNo
Splitas specifiedYes

Accept

Use pattern: Consume X until Y
Input consumed: no
Tree transition: go to parent node
State transition: go to state corresponding to parent node
Example:

public IStateChange feed(int c) {
	if (Character.isDigit(c)) {
		result += (char) c;
		return new Consume();
	} else {
		return new Accept(result);
	}
}

Closure

Use pattern: End input with an X
Input consumed: yes
Tree transition: go to parent node
State transition: go to state corresponding to parent node
Example:

public IStateChange feed(int c) {
	if (c == ')')
		return new Closure();
	else
		return new Fail();
}

Consume

Use pattern: Greedy match all input of type X
Input consumed: yes
Tree transition: stay in the same node
State transition: stay in the same state
Example: see Accept


Fail

Use pattern: If not conform to language, error out
Input consumed: -
Tree transition: -
State transition: exit execution
Example: see Closure


Goto

Use pattern: Start matching with state X
Input consumed: no
Tree transition: new child node
State transition: go to specified state
Example:

public IStateChange feed(int c) {
	if (c == '(')
		return new Consume();
	else
		return new Goto("ClosingParenthesis");
}

Guess

Use pattern: Start matching with state X or Y
Input consumed: no
Tree transition: new child node, whichever state change does not fail gets to stay
State transition: go to specified states in parallel
Example:

public IStateChange feed(int c) {
	if (c == '(')
		return new Consume();
	else
		return new Guess("ClosingParenthesis", "Digits", "Letters");
}

Split

Use pattern: Upon reading X: start matching with state Y or Z
Input consumed: yes
Tree transition: new child node, whichever state change does not fail gets to stay
State transition: go to specified states in parallel
Example:

public IStateChange feed(int c) {
	if (c == '(')
		return new Split("ClosingParenthesis", "Digits", "Letters");
	else
		return new Fail();
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages