Coder Social home page Coder Social logo

parser's Introduction

Ara Parser

Actions Status Crates.io Docs

A fault-tolerant, recursive-descent parser for Ara Programming Language ๐ŸŒฒ

Note: This project is a hard-fork of php-rust-tools/parser

Special thanks to the original authors for their work.


Usage

Add ara_parser to your Cargo.toml, and you're good to go!

[dependencies]
ara_parser = "0.6.6"

Example

use ara_parser::parser;
use ara_reporting::builder::CharSet;
use ara_reporting::builder::ColorChoice;
use ara_reporting::builder::ReportBuilder;
use ara_reporting::error::Error;
use ara_source::loader::load_directories;

fn main() -> Result<(), Error> {
   let source_map = load_directories("/path/to/project", vec!["src/"]).unwrap();

   match parser::parse_map(&source_map) {
      Ok(tree_map) => tree_map.trees.iter().for_each(|tree| {
         println!("{:#?}", tree.definitions);
      }),
      Err(report) => {
         ReportBuilder::new(&source_map)
                 .with_charset(CharSet::Unicode)
                 .with_colors(ColorChoice::Always)
                 .print(report.as_ref())?;
      }
   }

   Ok(())
}

Documentation

See the documentation for more information.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Credits

parser's People

Contributors

azjezz avatar kennedytedesco avatar ryangjchandler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

kennedytedesco

parser's Issues

add ranged string type

type NonEmptyString = string<1, _>; // should have at least 1 byte
type password = string<8, 2048>; // should have at least 8 bytes, and maximum of 2048 bytes
type char = string<1, 1>; // should have exactly 1 byte
type char = string<1>; // same as `string<1, 1>`

add new `Token` ( `Keyword` / `Symbol` ) nodes

currently in the tree, we refer to all tokens using usize, however, this should be changed, so it's a Token node.

enum Token {
   Keyword(Token),
   Symbol(Token),
}

the reason for this is that we allow mixed casing for keywords, just like PHP, so the following is valid:

ClAsS Foo ExTeNdS Bar {}

and we should send out a warning in the linter about using incorrect casing for the keywords, this is only possible if the keyword value is kept on the node.


Token would implement Node, and return correct start + end position, however, it would return None for comments, and vec![] for children.

remove `fn` type, and support spreading types.

currently, to refer to a Closure, you can use fn type, such as:

function foo(
  (fn(int): string) $fun
): void {}

this is okay, but there's a problem: what about Closure?

Closure is the actual type behind fn, all of these are instances of Closure:

$a = fn(int $a): int => $a;
$c = foo(...);
$b = $foo(...);
$f = $foo->bar(...);
$b = $bar::foo(...);

so let's just use Closure instead of introducing fn type!

but! there's another problem, Closure is not generic, the __invoke method of a closure has a signature along the lines of:

public function __invoke(mixed ...$args): mixed { .. }

tho, not really, because it is kinda magically created for every instance of a Closure, to match the parameter count, type, ..etc.

so how can we make Closure generic?

well, like this!

final class Closure<I as (), O> {
  public function __invoke(...I $arguments): O {}
}

note: ...I is not supported by Ara currently, so we need to add support for it, but only in definition sources, Ara code shouldn't be written like that.

so you can type the same code above as:

function foo(
  Closure<(int), string> $fun
): void {}

$fun here is an instance of Closure where I = (int) and O = string, i.e:

final class Closure {
  public function __invoke(int $arguments): string {}
}

allow all modifiers everywhere

we currently group modifiers differently depending on their use case, which makes it so that this is an error:

final public static class Bar {}

while it is a compile error, we shouldn't error for this in the parser.

this will be handled by the analyzer, which allows us to analyze more issues at once.

add reverse spread operator

Hey hi !

Following our discussion on Mastodon, I'm opening this issue to discuss a new feature.

The idea would be to have a spread operator, but in reverse direction.

Just like we have the spread operator which spread from the left to the right:

$a = range(0, 3);
var_dump(...$a); // Print 0, 1, 2, 3

We could have:

$a = range(0, 3);
var_dump($a...); // Print 3, 2, 1, 0

This reverse operator would spread an iterable from the right to the left.

Let me know what you think about this idea, this is something that would be very cool to have !

Thanks!

add literal float, literal int, and literal string types.

e.g:

type A = 1.2 | 3.4 | 'foo' | 80 | 8080;

function foo(): A {}

In PHP, this will be:

/**
 * @psalm-return float|'foo'|80|8080
 * @phpstan-return float|'foo'|80|8080
 * @ara-return 1.2|3.4|'foo'|80|8080
 * @return float|string|int
 */
function foo(): float|string|int {}

add ranged integer type

e.g:

type uint8 = int<0, 255>;
type int8 = int<-128, 127>;
type positive = int<0, _>;
type negative = int<_, 0>;

implement `Display` for nodes.

Display should be implemented manually for all nodes.

blocks should always be replaced with { /* ... */ }

final class Foo extends Bar implements Baz { /* ... */ }

abstract class Bar { /* ... */ }

function foo(string $bar): int { /* ... */ }

concurrently { /* ... */ }

if $foo { /* ... */ }

match $bar { /* ... */ }

async $bar;

await $baz;

foo::<T>($something);

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.