lwb_parser/parser/peg/parse_pair.rs
1use crate::sources::span::Span;
2use itertools::Itertools;
3use std::fmt::{Display, Formatter};
4
5/// A parse pair is a way of representing an AST, without actually using any datatypes that depend on the language definition.
6/// This represents a parse pair for a sort. It stores which constructor was chosen.
7#[derive(Debug, Clone)]
8pub struct ParsePairSort<'src> {
9 pub sort: &'src str,
10 pub constructor_name: &'src str,
11 pub constructor_value: ParsePairExpression<'src>,
12}
13
14impl ParsePairSort<'_> {
15 /// What span does this parse pair occupy?
16 pub fn span(&self) -> Span {
17 self.constructor_value.span()
18 }
19}
20
21impl<'src> Display for ParsePairSort<'src> {
22 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23 // write!(f, "{}.{}({})", self.sort, self.constructor_name, self.constructor_value)
24 write!(f, "{}({})", self.constructor_name, self.constructor_value)
25 }
26}
27
28/// A parse pair is a way of representing an AST, without actually using any datatypes that depend on the language definition.
29/// This represents a parse pair for a constructor. Each constructor generates one of the variants of this enum.
30#[derive(Debug, Clone)]
31pub enum ParsePairExpression<'src> {
32 /// This is generated when another sort is mentioned in the definition of this sort.
33 /// That sort is parsed and the result is stored here.
34 Sort(Span, Box<ParsePairSort<'src>>),
35 /// This is generated when a list of constructors is executed. This can be generated by Sequence or Repeat.
36 List(Span, Vec<ParsePairExpression<'src>>),
37 /// This is generated when a Choice was made. The first argument is which choice was made, the second the parsed constructor.
38 Choice(Span, usize, Box<ParsePairExpression<'src>>),
39 /// This is generated when no useful information needed to be recorded here, but still a placeholder is needed to keep track of the span.
40 /// Generated by Positive and Negative, as the actual values that were parsed in Positive and Negative are irrelevant.
41 /// Generated by CharacterClass and Literal, as they don't generate values.
42 Empty(Span),
43 Error(Span),
44}
45
46impl<'src> ParsePairExpression<'src> {
47 /// What span does this parse pair occupy?
48 pub fn span(&self) -> Span {
49 match self {
50 ParsePairExpression::Sort(span, _) => span,
51 ParsePairExpression::List(span, _) => span,
52 ParsePairExpression::Choice(span, _, _) => span,
53 ParsePairExpression::Empty(span) => span,
54 ParsePairExpression::Error(span) => span,
55 }
56 .clone()
57 }
58}
59
60impl<'src> Display for ParsePairExpression<'src> {
61 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
62 match self {
63 ParsePairExpression::Sort(_, sort) => {
64 write!(f, "{}", sort)
65 }
66 ParsePairExpression::List(_, exprs) => {
67 write!(f, "[{}]", exprs.iter().map(|e| e.to_string()).join(", "))
68 }
69 ParsePairExpression::Choice(_, num, child) => {
70 write!(f, "{}:{}", num, child)
71 }
72 ParsePairExpression::Empty(_) => {
73 write!(f, "_")
74 }
75 ParsePairExpression::Error(_) => {
76 write!(f, "#")
77 }
78 }
79 }
80}