1. Go to this page and download the library: Download tarsana/syntax library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tarsana / syntax example snippets
Tarsana\Syntax\Factory as S;
// a repo is an object having a name (string) and stars (number), separated by ':'
$repo = "{name: string, stars:number}";
// a line consists of a first and last names, optional number of followers, and repos, separated by space. The repos are separated by ","
$line = "{first_name, last_name, followers: (number: 0), repos: ([{$repo}]:[]) | }";
// a document is a list of lines separated by PHP_EOL
$document = "[{$line}|".PHP_EOL."]";
// Now we make the syntax object
$documentSyntax = S::syntax()->parse($document);
// Then we can use the defined syntax to parse the document:
$developers = $documentSyntax->parse(trim(file_get_contents(__DIR__ . '/files/devs.txt')));
use Tarsana\Syntax\Factory as S;
$string = S::string(); // instance of Tarsana\Syntax\StringSyntax
$string->parse('Lorem ipsum dolor sit amet');
//=> 'Lorem ipsum dolor sit amet'
$string->parse('');
// Tarsana\Syntax\Exceptions\ParseException: Error while parsing '' as String at character 0: String should not be empty
$string->dump('Lorem ipsum dolor sit amet');
//=> 'Lorem ipsum dolor sit amet'
$string->dump('');
//=> ''
use Tarsana\Syntax\Factory as S;
$number = S::number(); // instance of Tarsana\Syntax\NumberSyntax
$number->parse('58.9'); //=> 58.9
$number->parse('Lorem12');
// Tarsana\Syntax\Exceptions\ParseException: Error while parsing 'Lorem' as Number at character 0: Not a numeric value
use Tarsana\Syntax\Factory as S;
$boolean = S::boolean(); // instance of Tarsana\Syntax\BooleanSyntax
$boolean->parse('true'); //=> true
$boolean->parse('yes'); //=> true
$boolean->parse('y'); //=> true
$boolean->parse('TrUe'); //=> true (case insensitive)
$boolean->parse('false'); //=> false
$boolean->parse('no'); //=> false
$boolean->parse('N'); //=> false
$boolean->parse('Lorem');
// Tarsana\Syntax\Exceptions\ParseException: Error while parsing 'Lorem' as Boolean at character 0: Boolean value should be one of "yes", "no", "y", "n", "true", "false"
$boolean->dump(true); //=> 'true'
$boolean->dump(false); //=> 'false'
$boolean->dump('Lorem');
// Tarsana\Syntax\Exceptions\DumpException: Error while dumping some input as Boolean: Not a boolean
use Tarsana\Syntax\Factory as S;
$strings = S::array();
$strings->parse('aa:bb,cc,"ss,089",true');
//=> ['aa:bb','cc','ss,089','true']
// Note that we can use "..." to escape the separator
$strings->dump(['aa','bb,cc','76']);
//=> 'aa,"bb,cc",76'
// Yeah, it's smart enough to auto-escape items containing the separator
$vector = S::array(S::number());
$vector->parse('1,2,3,4,5');
//=> [1, 2, 3, 4, 5]
$matrix = S::array($vector, PHP_EOL);
$matrix->parse(
'1,2,3
4,5,6,7
8,9,100');
//=> [ [1, 2, 3], [4, 5, 6, 7], [8, 9, 100] ]
use Tarsana\Syntax\Factory as S;
$optionalNumber = S::optional(S::number(), 10);
$optionalNumber->parse(15); //=> 15
$optionalNumber->success(); //=> true
$optionalNumber->parse('Yo'); //=> 10 (the default value)
$optionalNumber->success(); //=> false