# 85 bytes Brainfuck

    >,[>-[-----<->]<++[>++++[-<++++>]+<[[-],[[-]>-<]]]>[<<+[-->]>[<],>-]<]-[-----<+>]<--.

[Online version][1]

    Loops over the input.
    On 1, ends.
    On "!", toggles a boolean.
    On "0", toggles if there is no trailing bit, then ends.

    Memory labels  | BOOL | INPUT | FLAG |

    >,                  setup 
    [                     # loop on INPUT
      >-[-----<->]<++     subtract 49 == "1"

      [                     # not "1"
        >++++[-<++++>]      add 16 since 49 take 16 == "!"
        
        +                   set FLAG
        <                   move to INPUT
        [                     # "0"
          [+],                clear and new INPUT
          [                     # trailing !
            [-]>-<              clear INPUT and FLAG
          ]
        ]
      ]
      
      >                   move to FLAG
      [                     # "!" or tail
        <<+[-->]>[<]        not BOOL
        ,                   new INPUT
        >-                  clear FLAG
      ]
      <                   move to INPUT
    ]

    -[-----<+>]<--.     add 49 and return

I think some bytes might be saved by changing to a (not 1)>(not 0) structure to avoid the +16, but have so far found the extra flag bytes needed added too much.


*Thanks to the lovely people in meta for help posting.*
  [1]: https://copy.sh/brainfuck/?c=TWVtb3J5IGxhYmVscyAgfCBCT09MIHwgSU5QVVQgfCBGTEFHIHwKCj4sICAgICAgICAgICAgICAgICAgc2V0dXAgClsgICAgICAgICAgICAgICAgICAgICAjIGxvb3Agb24gSU5QVVQKICA-LVstLS0tLTwtPl08KysgICAgIHN1YnRyYWN0IDQ5ID09ICIxIgoKICBbICAgICAgICAgICAgICAgICAgICAgIyBub3QgIjEiCiAgICA-KysrK1stPCsrKys-XSAgICAgIGFkZCAxNiBzaW5jZSA0OSB0YWtlIDE2ID09ICIhIgogICAgCiAgICArICAgICAgICAgICAgICAgICAgIHNldCBGTEFHCiAgICA8ICAgICAgICAgICAgICAgICAgIG1vdmUgdG8gSU5QVVQKICAgIFsgICAgICAgICAgICAgICAgICAgICAjICIwIgogICAgICBbK10sICAgICAgICAgICAgICAgIGNsZWFyIGFuZCBuZXcgSU5QVVQKICAgICAgWyAgICAgICAgICAgICAgICAgICAgICMgdHJhaWxpbmcgIQogICAgICAgIFstXT4tPCAgICAgICAgICAgICAgY2xlYXIgSU5QVVQgYW5kIEZMQUcKICAgICAgXQogICAgXQogIF0KICAKICA-ICAgICAgICAgICAgICAgICAgIG1vdmUgdG8gRkxBRwogIFsgICAgICAgICAgICAgICAgICAgICAjICIhIiBvciB0YWlsCiAgICA8PCtbLS0-XT5bPF0gICAgICAgIG5vdCBCT09MCiAgICAsICAgICAgICAgICAgICAgICAgIG5ldyBJTlBVVAogICAgPi0gICAgICAgICAgICAgICAgICBjbGVhciBGTEFHCiAgXQogIDwgICAgICAgICAgICAgICAgICAgbW92ZSB0byBJTlBVVApdCgotWy0tLS0tPCs-XTwtLS4gICAgIGFkZCA0OSBhbmQgcmV0dXJu