Skip to content

Commit

Permalink
Moved Parser class to separate file, outside of the `RuleBasedParse…
Browse files Browse the repository at this point in the history
…r`. Moved `cruise.umple.parser` code to `UmpleParser/` directory. Updated build-scripts. Added readme.
  • Loading branch information
bitwizeshift committed Apr 4, 2016
1 parent d64c24d commit 41526d6
Show file tree
Hide file tree
Showing 29 changed files with 1,344 additions and 1,215 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -36,12 +36,12 @@ script:
- ant deps-resolve-all - ant deps-resolve-all


# Do a modified version of `first-build` # Do a modified version of `first-build`
- $ANT_FIRST clean init codegen rtcpp template.setVersion resetUmpleSelf - $ANT_FIRST clean init codegen umpleParser rtcpp template.setVersion resetUmpleSelf
- $ANT_FIRST compile compileValidator compileUmplificator - $ANT_FIRST compile compileValidator compileUmplificator
- $ANT_FIRST package template.resetVersion - $ANT_FIRST package template.resetVersion


# Do a modified version of `build` # Do a modified version of `build`
- $ANT_BUILD clean init codegen rtcpp template.setVersion resetUmpleSelf - $ANT_BUILD clean init codegen umpleParser rtcpp template.setVersion resetUmpleSelf
- $ANT_BUILD compile compileValidator compileUmplificator - $ANT_BUILD compile compileValidator compileUmplificator
- $ANT_BUILD package - $ANT_BUILD package
- $ANT_BUILD template.test template.resetVersion - $ANT_BUILD template.test template.resetVersion
Expand Down
33 changes: 33 additions & 0 deletions UmpleParser/README.md
@@ -0,0 +1,33 @@
UmpleParser
===========

Description
-----------

The `RuleBasedParser` is a fast and sophisticated parser that is at the core of Umple.

It reads in grammar files written in a modified/extended [EBNF](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form) syntax and produces rules that can be used to parse files written in the corresponding language.

Compilation
-----------

Currently, this can only be compiled as part of the main Umple build. This will be changed in the future to be an entirely independently compilable project with a separate ant build target.

The umple source is available in `src/`, and -- once compiled -- the java source is available in `src-gen-umple/`

How To Use
----------

Using the RuleBasedParser is simple:

1. Import the package for the RuleBasedParser
In Umple this is `depend cruise.umple.parser.analysis.RuleBasedParser`. In Java, just `import cruise.umple.parser.analysis.RuleBasedParser`

2. Construct a parser with `RuleBasedParser rbp = new RuleBasedParser()`
3. Read in any necessary grammar files for the parser with `rbp.addGrammarFile("<filepath>");`
The grammar file filepath is relative to the root java package.
4. *[Optional]* Assign a handler for linking files. To do this, call `rbp.setLikedFileHandler(...)` and pass a class that implements `LinkedFileHandler`
5. *[Optional]* Assign a handler for generating analyzers. To do this, call `rbp.setAnalyzerGenerator(...)` and pass a class that implements `AnalyzerGeneratorHandler`
6. *[Optional]* Create parser actions for specific tokens with `rbp.addParserAction(...)`, passing it the string action name and a class that implements `ParserAction`
7. Create a `File` object of the file to parse (e.g. `File file = new File("somefile");`
8. Parse the file with `rbp.parse( file )`. To retrieve the tokens from the parse, use `rbp.getRootToken()`
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions UmpleParser/src/Master.ump
@@ -0,0 +1,12 @@
generate Java "../src-gen-umple";

strictness allow 1006;
strictness allow 1007;
strictness allow 1008;
strictness allow 46;
strictness allow 36;

use ParseUtilities.ump;
use ParsingRules.ump;
use TextParser.ump;
use GrammarParsing.ump;
126 changes: 126 additions & 0 deletions UmpleParser/src/ParseUtilities.ump
@@ -0,0 +1,126 @@
/*

Copyright: All contributers to the Umple Project

This file is made available subject to the open source license found at:
http://umple.org/license

Parser for the grammar that can be used to define any grammar
UmpleParser.ump is the key example

*/
namespace cruise.umple.parser;

/*
* Represents the results of parsing, such as if it was successful,
* whether it had warnings, etc.
*/
class ParseResult
{
// Specifies whether or not the parsing was successful.
Boolean wasSuccess;

// Specifies whether or not the parsing has warnings.
Boolean hasWarnings = false;

internal Boolean _acceptsErrors = true;

// Position is left for now, as not to break things
* -> 0..1 Position;

// The error messages related to parsing.
* -> * ErrorMessage;
}

/*
* Represents an error message, typically used with parsing.
*/
class ErrorMessage
{
// Indicates the type of error.
* -> 1 ErrorType;

* -> 1 Position;

String[] parameters;

depend java.io.File;
}

/*
* Represents an error type, which contains an error code, the severity of the error, etc.
* Error data is read from the en.error file
*/
class ErrorType
{
// The error code.
Integer errorCode;

// The severity of the error.
Integer severity;

String errorFormat;
String errorUrl;

key {errorCode}
}

/*
* Lists all the errors in the system
*/
class ErrorTypeSingleton
{
singleton;
1 -> * ErrorType;

depend java.io.*;
depend java.lang.IllegalStateException;
}

/*
* Fundamental to all parsing, used in conjunction with rules and their definitions.
* As parsing is taking place these will indicate what is currently being looked for
* (such as a class definition or attribute) and helps to keep
* track of the positions that the input parser parsed things at.
*/
class Token
{
// The name of the token.
name;

// The value of the token.
value;
0..1 parentToken -- * Token subTokens;
* -> 0..1 Position;
* -> 0..1 Position endPosition;

before getName { if (name == null) { return ""; } }
before getValue { if (value == null) { return ""; } }

before setPosition {String tFilename = position == null ? aNewPosition.getFilename() : position.getFilename();}
after setPosition {position.setFilename(tFilename);}
}

/*
* Used to indicate the coordinates of a position when parsing. This is done by keeping track of the
* filename, the line number and the corresponding offset on that line number.
*/
class Position
{
// The filename of the position.
String filename;

// The line number of the position.
Integer lineNumber;

// The character offset of the position.
Integer characterOffset;

// The offset of the position.
Integer offset;

key { filename, lineNumber, characterOffset, offset }
}


use ParseUtilities_Code.ump;

0 comments on commit 41526d6

Please sign in to comment.