| 30 | } |
| 31 | |
| 32 | func (p *Parser) Parse(input io.Reader) ([]Instruction, error) { |
| 33 | scanner := bufio.NewScanner(input) |
| 34 | instructions := []Instruction{} |
| 35 | |
| 36 | for scanner.Scan() { |
| 37 | line := scanner.Text() |
| 38 | line = strings.TrimSpace(line) |
| 39 | |
| 40 | if line == "" { |
| 41 | continue |
| 42 | } |
| 43 | |
| 44 | if IS_DEFLABEL.MatchString(line) { |
| 45 | instructions = append(instructions, processLabel(line)) |
| 46 | } else if IS_DEFSYMBOL.MatchString(line) { |
| 47 | if ins, err := parseDefSymbol(line); err == nil { |
| 48 | instructions = append(instructions, ins) |
| 49 | } else { |
| 50 | return nil, err |
| 51 | } |
| 52 | } else if INSTRUCTION.MatchString(line) { |
| 53 | if ins, err := parseInstruction(line); err == nil { |
| 54 | instructions = append(instructions, ins) |
| 55 | } else { |
| 56 | return nil, err |
| 57 | } |
| 58 | } else { |
| 59 | return nil, fmt.Errorf("unsupported/unparseable line: %s", line) |
| 60 | } |
| 61 | |
| 62 | } |
| 63 | if err := scanner.Err(); err != nil { |
| 64 | return nil, err |
| 65 | } |
| 66 | return instructions, nil |
| 67 | } |
| 68 | |
| 69 | func parseDefSymbol(line string) (Instruction, error) { |
| 70 | tokens := IS_DEFSYMBOL.FindStringSubmatch(line) |