1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use super::*;

mod build;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum Action {
    Shift(usize),
    Reduce(usize),
    Accept,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct ActionEdge {
    terminal: usize,
    action: Action,
}

impl ActionEdge {
    fn new(terminal: usize, action: Action) -> ActionEdge {
        ActionEdge {
            terminal,
            action,
        }
    }
}


#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct GotoEdge {
    production: usize,
    state: usize,
}

impl GotoEdge {
    fn new(production: usize, state: usize) -> GotoEdge {
        GotoEdge {
            production,
            state,
        }
    }
}


#[derive(Debug)]
struct State {
    index: usize,
    actions: OrdSet<ActionEdge>,
    gotos: OrdSet<GotoEdge>,
}

impl State {
    fn new(index: usize) -> State {
        State {
            index,
            actions: OrdSet::new(),
            gotos: OrdSet::new(),
        }
    }
}

#[derive(Debug)]
pub struct ClrParser {
    grammar: GrammarRef,
    states: Vec<State>,
    stack: Vec<usize>,
    channel: usize,
}

impl ClrParser {
    pub fn build(grammar: &GrammarRef, channel: usize) -> Result<ClrParser, Error> {
        build::build(ClrParser::new(grammar, channel)).map_err(|_| Error::Unspecified(line!()))
    }

    fn new(grammar: &GrammarRef, channel: usize) -> ClrParser {
        ClrParser {
            grammar: grammar.clone(),
            states: Vec::new(),
            stack: Vec::new(),
            channel,
        }
    }

    fn current_state(&self) -> &State {
        &self.states[*self.stack.last().unwrap()]
    }

    fn find_action_edge(&self, terminal: usize) -> Option<ActionEdge> {
        self.current_state().actions.iter().cloned().find(|a| a.terminal == terminal)
    }

    fn find_goto_edge(&self, production: usize) -> Option<GotoEdge> {
        self.current_state().gotos.iter().cloned().find(|g| g.production == production)
    }
}

impl Parser for ClrParser {
    fn reset(&mut self) {
        self.stack.clear();
        self.stack.push(0);
    }

    //FIXME (jc) error handling
    fn parse(&mut self, token: &Token) -> Result<Step, ParserError> {
        if let Some(a) = self.find_action_edge(token.lexeme()) {
            match a.action {
                Action::Shift(s) => {
                    self.stack.push(s);
                    return Ok(Step::Shift);
                }
                Action::Reduce(r) => {
                    let g = self.grammar.borrow();
                    let rule = g.rule(r);
                    let n = rule.symbols().len();

                    let t = self.stack.len() - n;
                    self.stack.truncate(t);

                    if let Some(g) = self.find_goto_edge(rule.production()) {
                        self.stack.push(g.state);
                    } else {
                        unreachable!();
                    }
                    return Ok(Step::Reduce(r))
                }
                Action::Accept => {
                    self.stack.pop();
                    return Ok(Step::Accept);
                }
            }
        } else {
            println!("{:?}", token);
            unreachable!();
        }
    }

    fn channel(&self) -> usize {
        self.channel
    }
}