Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have something similar but written in java. It parses a file with the same basic structure with a little different syntax (no '{' and '}' only indentation like in python). It is a very simple script language.</p> <p>Basically it works like this: It uses a stack to keep track of the inner most block of instructions (or in your case data) and appends every new instruction to the block on the top. If it parses an instruction which expects a new block it is pushed to the stack. If a block ends it pops one element from the stack.</p> <p>I do not want to post the entire source because it is big and it is available on google code (lizzard-entertainment, revision 405). There is a few things you need to know.</p> <ul> <li>Instruction is an abstract class and it has a block_expected method to indicate wether the concrete instruction needs a block (like loops, etc) In your case this is unnecessary you only need to check for '{'.</li> <li>Block extends Instruction. It contains a list of instructions and has an add method to add more.</li> <li>indent_level return how many spaces are preceding the instruction text. This is also unneccessary with '{}' singns.</li> </ul> <p>placeholder</p> <pre><code>BufferedReader input = null; try { input = new BufferedReader(new FileReader(inputFileName)); // Stack of instruction blocks Stack&lt;Block&gt; stack = new Stack&lt;Block&gt;(); // Push the root block stack.push(this.topLevelBlock); String line = null; Instruction prev = new Noop(); while ((line = input.readLine()) != null) { // Difference between the indentation of the previous and this line // You do not need this you will be using {} to specify block boundaries int level = indent_level(line) - stack.size(); // Parse the line (returns an instruction object) Instruction inst = Instruction.parse(line.trim().split(" +")); // If the previous instruction expects a block (for example repeat) if (prev.block_expected()) { if (level != 1) { // TODO handle error continue; } // Push the previous instruction and add the current instruction stack.push((Block)(prev)); stack.peek().add(inst); } else { if (level &gt; 0) { // TODO handle error continue; } else if (level &lt; 0) { // Pop the stack at the end of blocks for (int i = 0; i &lt; -level; ++i) stack.pop(); } stack.peek().add(inst); } prev = inst; } } finally { if (input != null) input.close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload