// Parse parses a line. func (radr *RAdr) Parse(source *token.Source) error { radr.Label.Parse(source) err := radr.Opcode.Parse(source) if err != nil { return err } err = radr.Operand1.Parse(source) if err != nil { return err } err = token.NewComma().Parse(source) if err != nil { return err } err = radr.Operand2.Parse(source) if err != nil { return err } radr.Comment.Parse(source) if !IsRAdrOpcode(radr.Opcode.TextValue()) { return fmt.Errorf("This line can't accept R, adr %#v", source) } if !source.IsEmpty() { return fmt.Errorf("This line doesn't end. %#v", source) } return nil }
// Parse parses a line. func (r1r2 *R1R2) Parse(source *token.Source) error { r1r2.Label.Parse(source) err := r1r2.Opcode.Parse(source) if err != nil { return err } err = r1r2.Operand1.Parse(source) if err != nil { return err } err = token.NewComma().Parse(source) if err != nil { return err } err = r1r2.Operand2.Parse(source) if err != nil { return err } r1r2.Comment.Parse(source) if !IsR1R2Opcode(r1r2.Opcode.TextValue()) { return fmt.Errorf("This line can't accept R1, R2, %#v", source) } if !source.IsEmpty() { return fmt.Errorf("This line doesn't end. %#v", source) } return nil }
// Parse parses a line. func (inout *InOut) Parse(source *token.Source) error { inout.Label.Parse(source) err := inout.Opcode.Parse(source) if err != nil { return err } err = inout.Operand1.Parse(source) if err != nil { return err } err = token.NewComma().Parse(source) if err != nil { return err } err = inout.Operand2.Parse(source) if err != nil { return err } inout.Comment.Parse(source) if !IsInOutOpcode(inout.Opcode.TextValue()) { return fmt.Errorf("This line can't accept label, label %#v", source) } if !source.IsEmpty() { return fmt.Errorf("This line doesn't end. %#v", source) } return nil }
// Parse parses a line. func (adrx *AdrX) Parse(source *token.Source) error { adrx.Label.Parse(source) err := adrx.Opcode.Parse(source) if err != nil { return err } err = adrx.Operand1.Parse(source) if err != nil { return err } err = token.NewComma().Parse(source) if err != nil { return err } err = adrx.Operand2.Parse(source) if err != nil { return err } adrx.Comment.Parse(source) if !IsAdrXOpcode(adrx.Opcode.TextValue()) { return fmt.Errorf("This line can't accept adr, x %#v", source) } if !source.IsEmpty() { return fmt.Errorf("This line doesn't end. %#v", source) } return nil }
// Parse parses a line. func (dc *DC) Parse(source *token.Source) error { dc.Label.Parse(source) err := dc.Opcode.Parse(source) if err != nil { return err } for { constant := token.NewConstant() err = constant.Parse(source) if err != nil { if 0 < len(dc.Operands) { break } return fmt.Errorf("This line is invalid DC, %#v", source) } dc.Operands = append(dc.Operands, constant) err = token.NewComma().Parse(source) if err == nil { continue } break } dc.Comment.Parse(source) if !IsDCOpcode(dc.Opcode.TextValue()) { return fmt.Errorf("This line can't accept dc %#v", source) } if !source.IsEmpty() { return fmt.Errorf("This line doesn't end. %#v", source) } return nil }
func TestRegisterParse(t *testing.T) { testCaseList := []testCaseForTestRegisterParse{ {data: ` LD GR0, DATA1`}, {data: ` LD GR0, 65535`}, {data: ` LD GR0, #ABCD`}, {data: ` LD GR0, =12345`}, {data: ` LD GR0, =#1FE0`}, {data: ` LD GR0, ='string'`}, {data: ` LD GR0, ='''is esacped quote'`}, } for _, testCase := range testCaseList { testData := testCase.data source := token.NewSource(testData) opcode := token.NewOpcode() err := opcode.Parse(source) if err != nil { t.Error(err) } register := token.NewRegister() err = register.Parse(source) if err != nil { t.Error(err) } comma := token.NewComma() err = comma.Parse(source) if err != nil { t.Error(err) } address := token.NewAddress() err = address.Parse(source) if err != nil { t.Error(err) } } }