Skip to content

A syntax analyzer written in Rust with handwritten logic for validating JSON with trailing commas and comments without using Lex or YACC

Notifications You must be signed in to change notification settings

felixfaisal/syntax-analyzer-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Syntax-Analyzer-rs

A Rust binary crate that is used to validate JSON files with trailing commas,comments and multiline strings.

In order to solve this challenge, I'm using basics of Compiler Design concepts. So the program is divided into two parts - Tokenizer (Lexical Analyzer) - Parser (Syntax Analyzer)

Tokenizer

This scans the file and identifies tokens, So currently as of writing this. We are looking for the following tokens

pub enum TokenTypes {
    LeftBrace = 0, \\ {
    RightBrace,\\ }
    LeftBracket, \\ [
    RightBracket, \\ ]
    Colon, \\ :
    Comma, \\ ,
    String, 
    MultiLineString,
    Comment,
}

After getting all the tokens, We send the tokens to the parser

Parser

A syntax analyzer basically constructs an Abstract Syntax Tree using Context Free Grammar. The difference here is that we are using CFG but not creating an AST as that goes beyond the scope of the problem statement. So we can check for productions according to the rules of ANON and parse the entire token list.

How to run

cargo run <JSON File Path>

Here's an example

felix@felix-TUF-Gaming-FX505DY-FX505DY ~/d/anonvalidator (main)> cargo run testcases/test_4.anon
   Compiling anonvalidator v0.1.0 (/home/felix/deepsource/anonvalidator)
    Finished dev [unoptimized + debuginfo] target(s) in 0.38s
     Running `target/debug/anonvalidator testcases/test_4.anon`
Valid file ✅

Test Suite

There are couple of JSON files already which are being used for testing purposes and have been integrated with GitHub Actions, If you would like to run it locally then use the following command

cargo test 

Output

felix@felix-TUF-Gaming-FX505DY-FX505DY ~/d/anonvalidator (main)> cargo test
   Compiling anonvalidator v0.1.0 (/home/felix/deepsource/anonvalidator)
    Finished test [unoptimized + debuginfo] target(s) in 0.47s
     Running unittests (target/debug/deps/anonvalidator-5e245d5b622a2128)

running 5 tests
test tests::test_example_2 - should panic ... ok
test tests::test_example_1 - should panic ... ok
test tests::test_example_3 ... ok
test tests::test_example_4 ... ok
test tests::test_example ... ok

test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

About

A syntax analyzer written in Rust with handwritten logic for validating JSON with trailing commas and comments without using Lex or YACC

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages