Giter VIP home page Giter VIP logo

stringes's Introduction

Stringes

Build status

##What is a stringe?!

The Stringe is a wrapper for the .NET String object that tracks line, column, offset, and other metadata for substrings.

Stringes can be created from normal strings, either explicitly or implicitly.

Stringe stringeA = new Stringe("Hello\nWorld!");
Stringe stringeB = "Hello\nWorld!";

###Support for native System.String methods

The Stringe class supports many of the same fabulous methods that regular strings have. I plan on eventually getting them all implemented. Unlike the String type, however, methods like Stringe.Split return an IEnumerable<Stringe> instead of an array. The result of this is that these methods use lazy evaluation, which can improve performance in cases where the user does not need all of the returned data.

IEnumerable<Stringe> lines = stringe.Split('\n');
IEnumerable<Stringe> words = stringe.Split(' ');

###Finding the parent string from a substringe

Each substringe can be traced back to the string it originally came from.

Stringe parent = "The quick brown fox jumps over the lazy dog";
Stringe substr = parent.Substringe(16, 3); // "fox"
Console.WriteLine(substr.ParentString); // "The quick brown fox jumps over the lazy dog"

###Location tracking

Substringes keep track of the line, column, and index on which they appear. This information can be easily accessed through properties. This is especially useful when writing lexers, so that errors in compiled code can be traced back to the exact place where the associated tokens were read.

var lines = new Stringe("Hello\nWorld!").Split('\n');
foreach(var substringe in lines)
{
    Console.WriteLine("Line {0}: {1}", substringe.Line, substringe);
}
Line 1: Hello
Line 2: World!

###Ranges

In some instances, such as when working with tokens, retrieving a range of text between two elements in the parent string can yield extremely useful data. This is made possible in Stringes through two methods: Stringe.Between and Stringe.Range.

The Stringe.Range method returns a substringe whose endpoints comprise of the two Stringe objects passed to it:

var parent = new Stringe("The quick brown fox jumps over the lazy dog");
var a = parent.Substringe(0, 3); // "The"
var b = parent.Substringe(16, 3); // "fox"
Console.WriteLine(Stringe.Range(a, b)); // "The quick brown fox"

The Stringe.Between method returns a substringe comprised of all the text between the two Stringe objects passed to it:

var parent = new Stringe("Here are (some words) in parentheses.");
var a = parent.Substringe(9, 1); // "("
var b = parent.Substringe(20, 1); // ")"
Console.WriteLine(Stringe.Between(a, b)); // "some words"

##Lexers

The Stringes library contains all the tools you need to write a lexer. The lexer-specific classes are:

  • Lexer<T>: The main lexer class, which generates tokens according to user-specified rules.
  • Token<T>: The token class, which wraps the Stringe class, includes information identifying the token type using a user-specified type. An enum is recommended for the type parameter.

For an example of how to make a lexer, see the LexerExample source!

##NuGet To install Stringes without cloning the repository, you can use the NuGet package. Slam this into your package manager console and slap that Enter key to install it:

PM> Install-Package Stringes

##Compiling Stringes uses C# 6 syntax. To compile the source, you will need Visual Studio 2015 CTP 6.

stringes's People

Contributors

theberkin avatar

Watchers

Marcin avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.