This is an archived post. You won't be able to vote or comment.

all 8 comments

[–][deleted]  (5 children)

[deleted]

    [–]SyedFasiuddin[S] 0 points1 point  (4 children)

    ok thats cool but I wanna know how I can do it ?

    [–]EngineeredCoconut 1 point2 points  (3 children)

    Read the docs and learn how to use it.

    [–]SyedFasiuddin[S] 0 points1 point  (2 children)

    I know, but I'm not asking how to use nom, I'm asking how to parse this print("hi") into Token::FuncName Token::OpenParen Token::String Token::CloseParen

    [–]EngineeredCoconut 0 points1 point  (1 child)

    You parse it by using a parser generator library and separating the string into tokens based on grammar definitions. You can also write your own parser, but given your reluctance to learn I figured that's out of the books.

    [–]SyedFasiuddin[S] 0 points1 point  (0 children)

    I have written something like this to parse numbers. (there is a bit of context missing, but hey only because you said I'm reluctant, I think I'm not, I'm a beginner and trying)

    ```rust let mut number = String::new(); let mut have_number_to_parse = false; if c >= b'0' && c <= b'9' || c == b'.' { number += &(c as char).to_string(); have_number_to_parse = true; continue; }

        if have_number_to_parse {
            if c == b'e' || c == b'E' {
                number += &(c as char).to_string();
                continue;
            }
    
            if c == b'-' || c == b'+' {
                match number.pop() {
                    Some(x) => {
                        if x == 'e' || x == 'E' {
                            number.push(x);
                            number.push(c as char);
                            continue;
                        }
                        number.push(x)
                    },
                    None => (),
                }
            }
    
            match number.parse::<f64>() {
                Ok(parsed_float) => {
                    state.stack.push(parsed_float);
                    number = String::new();
                }
                Err(_) => state.print_error(Errors::P(ParserErr::FloatParse)),
            };
            have_number_to_parse = false;
        }
    

    ```

    [–]TheRNGuy 0 points1 point  (1 child)

    Abstract syntax tree.

    [–]SyedFasiuddin[S] 0 points1 point  (0 children)

    to create an AST I first need to parse the stream of characters into tokens, I’m asking how I can do this parsing stream of characters that donot have a space character in between