Skip to main content
rules.cpp

1. Input Alphabet

Let the input lines be: Σ={L0,L1,…,Ln−1}\Sigma = \{ L_0, L_1, \dots, L_{n-1} \} Each line LiL_i is a tuple: Li=(texti,emptyi,s_counti)L_i = (text_i, empty_i, s\_count_i) where:
  • emptyi∈{0,1}empty_i \in \{0,1\} indicates if the line is empty
  • s_countis\_count_i is the leading space count
  • textitext_i is the raw text of the line
Let blk_indentblk\_indent be the block indentation of the paragraph, and end_lineend\_line be the specified or actual last line, and T\mathcal{T} the set of terminator rules: T={τj:(state,i)↦{0,1}}\mathcal{T} = \{ \tau_j : (state, i) \mapsto \{0,1\} \} Each τj\tau_j is a predicate corresponding to a block-level terminator.

2. State Machine Definition

We model RuleParagraph as a deterministic state machine: Mparagraph=(Q,Σ,δ,q0,F)M_{paragraph} = (Q, \Sigma, \delta, q_0, F)

States

Q={S0,S1,S2,S3}Q = \{ S_0, S_1, S_2, S_3 \}
  • S0S_0: initial line evaluation
  • S1S_1: scanning subsequent lines until termination
  • S2S_2: extract paragraph content and build tokens
  • S3S_3: final state

Initial State

q0=S0q_0 = S_0

Final State

F={S3}F = \{ S_3 \}

State Variables

  • ii: current line index
  • next_linenext\_line: scanning index
  • parent_typeparent\_type: current block context
  • terminate∈{0,1}terminate \in \{0,1\}: flag indicating terminator hit

3. Transition Rules

3.1 Start State (S0S_0)

At S0S_0, reject if the line is empty: If: emptystart_line=1empty_{start\_line} = 1 Then: δ1:δ(S0,start_line)=(S3)\delta_1: \delta(S_0, start\_line) = (S_3) Otherwise: δ2:δ(S0,start_line)=(S1,start_line+1)\delta_2: \delta(S_0, start\_line) = (S_1, start\_line + 1)

3.2 Scan State (S1S_1)

For i=next_linei = next\_line, continue scanning while all conditions hold: i<end_line∧emptyi=0∧s_counti−blk_indent≤3∧s_counti≥0∧∀τj∈T, τj(state,i)=0\begin{aligned} i &< end\_line &\land \\ empty_i &= 0 &\land \\ s\_count_i - blk\_indent &\le 3 &\land \\ s\_count_i &\ge 0 &\land \\ \forall \tau_j \in \mathcal{T},\ \tau_j(state,i) &= 0 \end{aligned} Then: δ3:δ(S1,i)=(S1,i+1)\delta_3: \delta(S_1, i) = (S_1, i+1) δ6: δ(S1,i)=(S1,i+1)iff s_counti<0\delta_6:\ \delta(S_1, i) = (S_1, i+1) \quad\text{iff } s\_count_i < 0 Otherwise if : i≥end_line∨emptyi=1∨s_counti−blk_indent>3∨s_counti<0∨∃τj∈T, τj(state,i)=1\begin{aligned} i &\ge end\_line &\vee \\ empty_i &= 1 &\vee \\ s\_count_i - blk\_indent &> 3 &\vee \\ s\_count_i &< 0 &\vee \\ \exists \tau_j \in \mathcal{T},\ \tau_j(state,i) &= 1 \end{aligned} δ4:δ(S1,i)=(S2,i)\delta_4: \delta(S_1, i) = (S_2, i )

3.3 Extract State (S2S_2)

At S2S_2, the machine:
  1. S2S_2 the paragraph content:
  1. Builds the tokens:
  • Open token: paragraph_open
  • Inline token: inline with content
  • Close token: paragraph_close
Then transition to final state: δ5:δ(S2,i)=(S3,i)\delta_5: \delta(S_2, i) = (S_3, i)

4. Tabular Format


5. Image